| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/kr/pretty" |
| 10 | ) |
| 11 | |
| 12 | func TestReadConfigs(t *testing.T) { |
| 13 | const in = `-g '*.go' echo {} |
| 14 | |
| 15 | # Some comment here |
| 16 | -r '^a[0-9]+\.txt$' --only-dirs --substitute='[]' echo [] |
| 17 | |
| 18 | -g '*.go' -s --only-files echo hi |
| 19 | |
| 20 | -r foo -r bar -R baz -g a \ |
| 21 | -G b -G c echo "hello |
| 22 | world" |
| 23 | ` |
| 24 | |
| 25 | got, err := readConfigsFromReader(strings.NewReader(in), "test input") |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | want := []*Config{ |
| 30 | { |
| 31 | command: []string{"echo", "{}"}, |
| 32 | source: "test input, line 1", |
| 33 | globs: []string{"*.go"}, |
| 34 | subSymbol: "{}", |
| 35 | shutdownTimeout: 500 * time.Millisecond, |
| 36 | }, |
| 37 | { |
| 38 | command: []string{"echo", "[]"}, |
| 39 | source: "test input, line 4", |
| 40 | regexes: []string{`^a[0-9]+\.txt$`}, |
| 41 | subSymbol: "[]", |
| 42 | shutdownTimeout: 500 * time.Millisecond, |
| 43 | onlyDirs: true, |
| 44 | }, |
| 45 | { |
| 46 | command: []string{"echo", "hi"}, |
| 47 | source: "test input, line 6", |
| 48 | globs: []string{"*.go"}, |
| 49 | subSymbol: "{}", |
| 50 | startService: true, |
| 51 | shutdownTimeout: 500 * time.Millisecond, |
| 52 | onlyFiles: true, |
| 53 | }, |
| 54 | { |
| 55 | command: []string{"echo", "hello\nworld"}, |
| 56 | source: "test input, line 8", |
| 57 | regexes: []string{"foo", "bar"}, |
| 58 | globs: []string{"a"}, |
| 59 | inverseRegexes: []string{"baz"}, |
| 60 | inverseGlobs: []string{"b", "c"}, |
| 61 | subSymbol: "{}", |
| 62 | shutdownTimeout: 500 * time.Millisecond, |
| 63 | }, |
| 64 | } |
| 65 | if !reflect.DeepEqual(got, want) { |
| 66 | t.Errorf("readConfigsFromReader: got diffs:\n%s", |
| 67 | strings.Join(pretty.Diff(got, want), "\n")) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestReadConfigsBad(t *testing.T) { |
| 72 | for _, in := range []string{ |
| 73 | "", |
| 74 | "--abc echo hi", |
| 75 | "-g '*.go'", |
| 76 | "--substitute='' echo hi", |
| 77 | "-s echo {}", |
| 78 | "--only-files --only-dirs echo hi", |
| 79 | } { |
| 80 | r := strings.NewReader(in) |
| 81 | if configs, err := readConfigsFromReader(r, "test input"); err == nil { |
| 82 | for _, config := range configs { |
| 83 | if _, err := NewReflex(config); err == nil { |
| 84 | t.Errorf("readConfigsFromReader(%q): got nil error", in) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |