util.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package test
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. )
  7. // MatchLog looks for the key/val in the input (a log file)
  8. func MatchLog(input string, startLine int, args ...interface{}) bool {
  9. size := len(args)
  10. if size < 2 || size%2 != 0 {
  11. panic("args must be even")
  12. }
  13. lines := strings.Split(input, "\n")
  14. if len(lines) < startLine {
  15. panic("log too short, lines:" + fmt.Sprintf("%v", len(lines)))
  16. }
  17. re, _ := regexp.Compile(`[[:space:]:\\]`)
  18. var lookFor string
  19. // for each line
  20. found := false
  21. for i := startLine - 1; i < len(lines); i++ {
  22. // for each pair
  23. for j := 0; j < len(args); j++ {
  24. if j%2 != 0 {
  25. continue
  26. }
  27. key := args[j]
  28. val := args[j+1]
  29. lookFor = fmt.Sprintf("%v", val)
  30. if re.MatchString(lookFor) {
  31. // quote it
  32. lookFor = fmt.Sprintf(`%s="%s"`, key, val)
  33. } else {
  34. lookFor = fmt.Sprintf(`%s=%v`, key, val)
  35. }
  36. if pos := strings.Index(lines[i], lookFor); pos != -1 {
  37. found = true
  38. } else {
  39. found = false
  40. // short circuit
  41. break
  42. }
  43. }
  44. if found {
  45. break
  46. }
  47. }
  48. return found
  49. }