ソースを参照

TestPollerEvent: fix filemode (staticcheck)

Staticcheck reported:

    SA9002: file mode '600' evaluates to 01130; did you mean '0600'? (staticcheck)

But fixing that caused the test to fail:

    === Failed
    === FAIL: pkg/filenotify TestPollerEvent (0.80s)
        poller_test.go:75: timeout waiting for event CHMOD

The problem turned out to be that the file was created with `0644`. However,
after umask, the file created actually had `0600` filemode. Running the `os.Chmod`
with `0600` therefore was a no-op, causing the test to fail (because no
CHMOD event would fire).

This patch changes the test to;

- create the file with mode `0600`
- assert that the file has the expected mode
- change the chmod to `0644`
- assert that it has the correct mode, before testing the event.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 5 年 前
コミット
e92e0d358a
1 ファイル変更15 行追加2 行削除
  1. 15 2
      pkg/filenotify/poller_test.go

+ 15 - 2
pkg/filenotify/poller_test.go

@@ -61,16 +61,18 @@ func TestPollerEvent(t *testing.T) {
 	default:
 	}
 
-	if err := ioutil.WriteFile(f.Name(), []byte("hello"), 0644); err != nil {
+	if err := ioutil.WriteFile(f.Name(), []byte("hello"), 0600); err != nil {
 		t.Fatal(err)
 	}
+	assertFileMode(t, f.Name(), 0600)
 	if err := assertEvent(w, fsnotify.Write); err != nil {
 		t.Fatal(err)
 	}
 
-	if err := os.Chmod(f.Name(), 600); err != nil {
+	if err := os.Chmod(f.Name(), 0644); err != nil {
 		t.Fatal(err)
 	}
+	assertFileMode(t, f.Name(), 0644)
 	if err := assertEvent(w, fsnotify.Chmod); err != nil {
 		t.Fatal(err)
 	}
@@ -103,6 +105,17 @@ func TestPollerClose(t *testing.T) {
 	}
 }
 
+func assertFileMode(t *testing.T, fileName string, mode uint32) {
+	t.Helper()
+	f, err := os.Stat(fileName)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if f.Mode() != os.FileMode(mode) {
+		t.Fatalf("expected file %s to have mode %#o, but got %#o", fileName, mode, f.Mode())
+	}
+}
+
 func assertEvent(w FileWatcher, eType fsnotify.Op) error {
 	var err error
 	select {