import (
"os"
+ "path"
+ "strings"
"testing"
. "gopkg.in/check.v1"
)
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) {
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)