package ci_test import ( "bytes" "context" "os" "path/filepath" "testing" "hakurei.app/cmd/mbf/internal/ci" ) func TestFollow(t *testing.T) { t.Parallel() pathname := filepath.Join(t.TempDir(), "f") w, err := os.Create(pathname) if err != nil { t.Fatal(err) } var buf bytes.Buffer ctx, cancel := context.WithCancel(t.Context()) var want string go func() { defer cancel() for _, s := range []string{ "\xde\xad\xbe\xef", "\xff\xff\xff\xff", "\x00\x00", } { want += s if _, _err := w.WriteString(s); _err != nil { panic(_err) } } }() retry: if err = ci.Follow(ctx, pathname, &buf); err != nil { t.Fatal(err) } <-ctx.Done() // the inotify event takes time to arrive, and there is no way to // synchronise for this cleanly if buf.Len() != len(want) { buf.Reset() goto retry } if got := buf.String(); got != want { t.Fatalf("follow: %q, want %q", got, want) } }