Skip to content

Commit

Permalink
test: Fix test false-positive
Browse files Browse the repository at this point in the history
Use a hand-written events set check because fsnotify
added an unexported field `renamedFrom` to type `Event` which
leads to the expected event not being found by `require.Contains`.
  • Loading branch information
romshark committed Nov 6, 2024
1 parent 7a3d929 commit 2291d2b
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions internal/watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,57 +78,66 @@ func TestWatcher(t *testing.T) {
})

// Event 0
require.Contains(t, events, fsnotify.Event{
eventsMustContain(t, events, fsnotify.Event{
Op: fsnotify.Create,
Name: filepath.Join(base, "newfile"),
})
// Event 1
require.Contains(t, events, fsnotify.Event{
eventsMustContain(t, events, fsnotify.Event{
Op: fsnotify.Create,
Name: filepath.Join(base, "newdir"),
})
// Event 2
require.Contains(t, events, fsnotify.Event{
eventsMustContain(t, events, fsnotify.Event{
Op: fsnotify.Create,
Name: filepath.Join(base, "newdir", "subdir"),
})
// Event 3
require.Contains(t, events, fsnotify.Event{
eventsMustContain(t, events, fsnotify.Event{
Op: fsnotify.Create,
Name: filepath.Join(base, "newdir", "subdir", "subfile"),
})
// Event 4
require.Contains(t, events, fsnotify.Event{
eventsMustContain(t, events, fsnotify.Event{
Op: fsnotify.Create,
Name: filepath.Join(base, "newdir", "subdir", "subfile2"),
})
// Event 5
require.Contains(t, events, fsnotify.Event{
eventsMustContain(t, events, fsnotify.Event{
Op: fsnotify.Create,
Name: filepath.Join(base, "existing-subdir", "subfile3"),
})
// Event 6
require.Contains(t, events, fsnotify.Event{
eventsMustContain(t, events, fsnotify.Event{
Op: fsnotify.Remove,
Name: filepath.Join(base, "existing-subdir", "subfile3"),
})
// Event 7
require.Contains(t, events, fsnotify.Event{
eventsMustContain(t, events, fsnotify.Event{
Op: fsnotify.Remove,
Name: filepath.Join(base, "existing-subdir"),
})
// Event 8
require.Contains(t, events, fsnotify.Event{
eventsMustContain(t, events, fsnotify.Event{
Op: fsnotify.Rename,
Name: filepath.Join(base, "newdir"),
})
// Event 9
require.Contains(t, events, fsnotify.Event{
eventsMustContain(t, events, fsnotify.Event{
Op: fsnotify.Create,
Name: filepath.Join(base, "newname"),
})
}

func eventsMustContain(t *testing.T, set []fsnotify.Event, contains fsnotify.Event) {
for _, e := range set {
if e.Op == contains.Op && e.Name == contains.Name {
return
}
}
t.Errorf("event set %#v doesn't contain event %#v", set, contains)
}

func TestWatcherRunCancelContext(t *testing.T) {
base := t.TempDir()
w, err := watcher.New(base, func(ctx context.Context, e fsnotify.Event) error {
Expand Down

0 comments on commit 2291d2b

Please sign in to comment.