func _testWatch(t *testing.T, modfunc func(), trigger string, expected []string) { defer utils.WithTempDir(t)() err := os.MkdirAll("a", 0777) if err != nil { t.Fatal(err) } err = os.MkdirAll("b", 0777) if err != nil { t.Fatal(err) } confTxt := ` ** { prep: echo ":all:" @mods } a/** { prep: echo ":a:" @mods } b/** { prep: echo ":b:" @mods } ` cnf, err := conf.Parse("test", confTxt) if err != nil { t.Fatal(err) } lt := termlog.NewLogTest() modchan := make(chan *watch.Mod, 1024) cback := func() { // There's some race condition in rjeczalik/notify. If we don't wait a // bit here, we sometimes don't receive notifications for our changes. time.Sleep(200 * time.Millisecond) start := time.Now() modfunc() for { if strings.Contains(lt.String(), trigger) { break } if time.Now().Sub(start) > timeout { fmt.Println("Timeout!") break } time.Sleep(50 * time.Millisecond) } modchan <- nil } _, err = runOnChan(modchan, cback, lt.Log, cnf, "", nil) if err != nil { t.Fatalf("runOnChan: %s", err) } ret := events(lt.String()) if !reflect.DeepEqual(ret, expected) { t.Errorf("Expected\n%#v\nGot\n%#v", expected, ret) } }
func TestVarCmd(t *testing.T) { defer utils.WithTempDir(t)() dst := path.Join("./tdir") err := os.MkdirAll(dst, 0777) if err != nil { t.Fatalf("MkdirAll: %v", err) } err = ioutil.WriteFile(path.Join(dst, "tfile"), []byte("test"), 0777) if err != nil { t.Fatalf("WriteFile: %v", err) } b := conf.Block{} b.Include = []string{"tdir/**"} vc := VarCmd{&b, nil, map[string]string{}} ret, err := vc.Render("@mods @dirmods") if err != nil { t.Fatalf("unexpected error: %s", err) } expect := `"./tdir/tfile" "./tdir"` if ret != expect { t.Errorf("Expected: %#v, got %#v", expect, ret) } vc = VarCmd{ &b, &moddwatch.Mod{Changed: []string{"foo"}}, map[string]string{}, } ret, err = vc.Render("@mods @dirmods") if err != nil { t.Fatal("unexpected error") } expected := `"./foo" "./."` if ret != expected { t.Errorf("Expected: %#v, got %#v", expected, ret) } }
func TestFind(t *testing.T) { defer utils.WithTempDir(t)() paths := []string{ "a/a.test1", "a/b.test2", "b/a.test1", "b/b.test2", "x", "x.test1", } for _, p := range paths { dst := filepath.Join(".", p) err := os.MkdirAll(filepath.Dir(dst), 0777) if err != nil { t.Fatalf("Error creating test dir: %v", err) } err = ioutil.WriteFile(dst, []byte("test"), 0777) if err != nil { t.Fatalf("Error writing test file: %v", err) } } for i, tt := range findTests { ret, err := Find(".", tt.include, tt.exclude) if err != nil { t.Fatal(err) } expected := tt.expected for i := range ret { ret[i] = filepath.ToSlash(ret[i]) } if !reflect.DeepEqual(ret, expected) { t.Errorf( "%d: %#v, %#v - Expected\n%#v\ngot:\n%#v", i, tt.include, tt.exclude, expected, ret, ) } } }
func _testWatch(t *testing.T, modfunc func(), trigger string, expected []string) { defer utils.WithTempDir(t)() err := os.MkdirAll("a", 0777) if err != nil { t.Fatal(err) } err = os.MkdirAll("b", 0777) if err != nil { t.Fatal(err) } touch(t, "a/initial") // There's some race condition in rjeczalik/notify. If we don't wait a bit // here, we sometimes receive notifications for the change above even // though we haven't started the watcher. time.Sleep(200 * time.Millisecond) confTxt := ` ** { prep +onchange: echo ":skipit:" @mods prep: echo ":all:" @mods } a/** { prep: echo ":a:" @mods } b/** { prep: echo ":b:" @mods } ` cnf, err := conf.Parse("test", confTxt) if err != nil { t.Fatal(err) } lt := termlog.NewLogTest() modchan := make(chan *moddwatch.Mod, 1024) cback := func() { start := time.Now() modfunc() for { if strings.Contains(lt.String(), trigger) { break } if time.Now().Sub(start) > timeout { fmt.Println("Timeout!") break } time.Sleep(50 * time.Millisecond) } modchan <- nil } mr := ModRunner{ Log: lt.Log, Config: cnf, } err = mr.runOnChan(modchan, cback) if err != nil { t.Fatalf("runOnChan: %s", err) } ret := events(lt.String()) if !reflect.DeepEqual(ret, expected) { t.Errorf("Expected\n%#v\nGot\n%#v", expected, ret) } }