Browse Source

lib/osutil: Add test for IsDeleted (ref #4925) (#4936)

Simon Frei 7 years ago
parent
commit
a2f51c85c2
1 changed files with 64 additions and 0 deletions
  1. 64 0
      lib/osutil/osutil_test.go

+ 64 - 0
lib/osutil/osutil_test.go

@@ -8,7 +8,9 @@ package osutil_test
 
 import (
 	"os"
+	"path/filepath"
 	"runtime"
+	"strings"
 	"testing"
 
 	"github.com/syncthing/syncthing/lib/fs"
@@ -204,3 +206,65 @@ func TestInWritableDirWindowsRename(t *testing.T) {
 		}
 	}
 }
+
+func TestIsDeleted(t *testing.T) {
+	type tc struct {
+		path  string
+		isDel bool
+	}
+	cases := []tc{
+		{"del", true},
+		{"del.file", false},
+		{"del/del", true},
+		{"file", false},
+		{"linkToFile", false},
+		{"linkToDel", false},
+		{"linkToDir", false},
+		{"linkToDir/file", true},
+		{"file/behindFile", true},
+		{"dir", false},
+		{"dir.file", false},
+		{"dir/file", false},
+		{"dir/del", true},
+		{"dir/del/del", true},
+		{"del/del/del", true},
+	}
+
+	testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
+
+	testFs.MkdirAll("dir", 0777)
+	for _, f := range []string{"file", "del.file", "dir.file", "dir/file"} {
+		fd, err := testFs.Create(f)
+		if err != nil {
+			t.Fatal(err)
+		}
+		fd.Close()
+	}
+	if runtime.GOOS != "windows" {
+		// Can't create unreadable dir on windows
+		testFs.MkdirAll("inacc", 0777)
+		if err := testFs.Chmod("inacc", 0000); err == nil {
+			if _, err := testFs.Lstat("inacc/file"); fs.IsPermission(err) {
+				// May fail e.g. if tests are run as root -> just skip
+				cases = append(cases, tc{"inacc", false}, tc{"inacc/file", false})
+			}
+		}
+	}
+	for _, n := range []string{"Dir", "File", "Del"} {
+		if err := osutil.DebugSymlinkForTestsOnly(filepath.Join(testFs.URI(), strings.ToLower(n)), filepath.Join(testFs.URI(), "linkTo"+n)); err != nil {
+			if runtime.GOOS == "windows" {
+				t.Skip("Symlinks aren't working")
+			}
+			t.Fatal(err)
+		}
+	}
+
+	for _, c := range cases {
+		if osutil.IsDeleted(testFs, c.path) != c.isDel {
+			t.Errorf("IsDeleted(%v) != %v", c.path, c.isDel)
+		}
+	}
+
+	testFs.Chmod("inacc", 0777)
+	os.RemoveAll("testdata")
+}