From: Dan Fuhry Date: Tue, 20 Oct 2026 02:51:19 +0000 (-0400) Subject: [fsnotify] generate resolve_symlink_tests tree at test time X-Git-Url: https://go.fuhry.dev/?a=commitdiff_plain;h=73fb53df4465298eac03a43569f51c15de85ed92;p=runtime.git [fsnotify] generate resolve_symlink_tests tree at test time Bazel hates the circular symlink test data, so we have to generate the test tree in `/tmp` at the time the test is executed --- diff --git a/mtls/fsnotify/all_test.go b/mtls/fsnotify/all_test.go index 1650107..fbfa5db 100644 --- a/mtls/fsnotify/all_test.go +++ b/mtls/fsnotify/all_test.go @@ -2,6 +2,8 @@ package fsnotify import ( "os" + "path" + "strings" "testing" . "gopkg.in/check.v1" @@ -10,11 +12,100 @@ import ( ) type FsnotifySuite struct { + t *testing.T + tempFiles *hashset.HashSet[string] + oldWd string +} + +type testFileI interface { + name() string + create() error +} + +type testFileRegular struct { + filename string +} + +var _ testFileI = &testFileRegular{} + +func (f *testFileRegular) name() string { + return f.filename +} + +func (f *testFileRegular) create() error { + filename := "resolve_symlink_tests/" + f.filename + if strings.Contains(filename, "/") { + if err := os.MkdirAll(path.Dir(filename), os.FileMode(0o755)); err != nil && !os.IsExist(err) { + return err + } + } + + fp, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0o644)) + fp.Close() + return err +} + +type testFileSymlink struct { + link string + dst string +} + +var _ testFileI = &testFileSymlink{} + +func (f *testFileSymlink) name() string { + return f.link } -func SetUpSuite(s *FsnotifySuite) { +func (f *testFileSymlink) create() error { + link := "resolve_symlink_tests/" + f.link + if strings.Contains(link, "/") { + if err := os.MkdirAll(path.Dir(link), os.FileMode(0o755)); err != nil && !os.IsExist(err) { + return err + } + } + + return os.Symlink(f.dst, link) +} + +func SetUpSuite(s *FsnotifySuite, t *testing.T) { + s.t = t s.tempFiles = hashset.NewHashSet[string]() + s.oldWd = getcwd() + + os.Chdir("/tmp") + + testFiles := []testFileI{ + &testFileRegular{"01/one"}, + + &testFileRegular{"02/one"}, + &testFileSymlink{"02/two", "one"}, + + &testFileRegular{"03/one"}, + &testFileSymlink{"03/two", "one"}, + &testFileSymlink{"03/three", "two"}, + + &testFileSymlink{"04/two", "one"}, + &testFileSymlink{"04/one", "two"}, + + &testFileSymlink{"05/one", "three"}, + &testFileSymlink{"05/two", "one"}, + &testFileSymlink{"05/three", "two"}, + + &testFileSymlink{"06/one", "invalid"}, + + &testFileRegular{"07/one"}, + &testFileSymlink{"07/sub/two", "../one"}, + + &testFileSymlink{"08/one", "sub/two"}, + &testFileRegular{"08/sub/two"}, + } + + for i, f := range testFiles { + if err := f.create(); err != nil { + t.Fatalf("error creating test file %d %q: %v", i, f.name(), err) + } + } } func TearDownSuite(s *FsnotifySuite) { @@ -22,13 +113,16 @@ func TearDownSuite(s *FsnotifySuite) { for _, f := range s.tempFiles.AsSlice() { os.Remove(f) } + os.Chdir(s.oldWd) } -var s = &FsnotifySuite{} -var _ = Suite(s) +var ( + s = &FsnotifySuite{} + _ = Suite(s) +) func Test(t *testing.T) { - SetUpSuite(s) + SetUpSuite(s, t) defer TearDownSuite(s) TestingT(t) diff --git a/mtls/fsnotify/resolve_symlink_tests/01/one b/mtls/fsnotify/resolve_symlink_tests/01/one deleted file mode 100644 index e69de29..0000000 diff --git a/mtls/fsnotify/resolve_symlink_tests/02/one b/mtls/fsnotify/resolve_symlink_tests/02/one deleted file mode 100644 index d00491f..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/02/one +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/mtls/fsnotify/resolve_symlink_tests/02/two b/mtls/fsnotify/resolve_symlink_tests/02/two deleted file mode 120000 index 43dd47e..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/02/two +++ /dev/null @@ -1 +0,0 @@ -one \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/03/one b/mtls/fsnotify/resolve_symlink_tests/03/one deleted file mode 100644 index e69de29..0000000 diff --git a/mtls/fsnotify/resolve_symlink_tests/03/three b/mtls/fsnotify/resolve_symlink_tests/03/three deleted file mode 120000 index 64c5e58..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/03/three +++ /dev/null @@ -1 +0,0 @@ -two \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/03/two b/mtls/fsnotify/resolve_symlink_tests/03/two deleted file mode 120000 index 43dd47e..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/03/two +++ /dev/null @@ -1 +0,0 @@ -one \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/04/one b/mtls/fsnotify/resolve_symlink_tests/04/one deleted file mode 120000 index 64c5e58..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/04/one +++ /dev/null @@ -1 +0,0 @@ -two \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/04/two b/mtls/fsnotify/resolve_symlink_tests/04/two deleted file mode 120000 index 43dd47e..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/04/two +++ /dev/null @@ -1 +0,0 @@ -one \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/05/one b/mtls/fsnotify/resolve_symlink_tests/05/one deleted file mode 120000 index 1d19714..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/05/one +++ /dev/null @@ -1 +0,0 @@ -three \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/05/three b/mtls/fsnotify/resolve_symlink_tests/05/three deleted file mode 120000 index 64c5e58..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/05/three +++ /dev/null @@ -1 +0,0 @@ -two \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/05/two b/mtls/fsnotify/resolve_symlink_tests/05/two deleted file mode 120000 index 43dd47e..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/05/two +++ /dev/null @@ -1 +0,0 @@ -one \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/06/one b/mtls/fsnotify/resolve_symlink_tests/06/one deleted file mode 120000 index e466dcb..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/06/one +++ /dev/null @@ -1 +0,0 @@ -invalid \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/07/one b/mtls/fsnotify/resolve_symlink_tests/07/one deleted file mode 100644 index e69de29..0000000 diff --git a/mtls/fsnotify/resolve_symlink_tests/07/sub/two b/mtls/fsnotify/resolve_symlink_tests/07/sub/two deleted file mode 120000 index 747114a..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/07/sub/two +++ /dev/null @@ -1 +0,0 @@ -../one \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/08/one b/mtls/fsnotify/resolve_symlink_tests/08/one deleted file mode 120000 index 11ef329..0000000 --- a/mtls/fsnotify/resolve_symlink_tests/08/one +++ /dev/null @@ -1 +0,0 @@ -sub/two \ No newline at end of file diff --git a/mtls/fsnotify/resolve_symlink_tests/08/sub/two b/mtls/fsnotify/resolve_symlink_tests/08/sub/two deleted file mode 100644 index e69de29..0000000 diff --git a/mtls/fsnotify/util_test.go b/mtls/fsnotify/util_test.go index 0b3e857..9340416 100644 --- a/mtls/fsnotify/util_test.go +++ b/mtls/fsnotify/util_test.go @@ -2,6 +2,7 @@ package fsnotify import ( "fmt" + "log" "path" . "gopkg.in/check.v1" @@ -52,6 +53,7 @@ func (s *FsnotifySuite) TestResolveSymlink(c *C) { testDir := path.Join(cwd, "resolve_symlink_tests", fmt.Sprintf("%02d", i+1)) in := path.Join(testDir, tc.in) got := resolveSymlinkRecurse(in, nil) + log.Printf("in test %q:", tc.desc) c.Assert(got, Equals, path.Join(testDir, tc.e)) // , "test case %d: %s", i+1, tc.desc) } }