예제 #1
0
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)
	}
}
예제 #2
0
파일: modd_test.go 프로젝트: cortesi/modd
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)
	}
}