1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package afero
16
17 import (
18 "os"
19 "path/filepath"
20 "testing"
21 )
22
23 func TestLstatIfPossible(t *testing.T) {
24 wd, _ := os.Getwd()
25 defer func() {
26 os.Chdir(wd)
27 }()
28
29 osFs := &OsFs{}
30
31 workDir, err := TempDir(osFs, "", "afero-lstate")
32 if err != nil {
33 t.Fatal(err)
34 }
35
36 defer func() {
37 osFs.RemoveAll(workDir)
38 }()
39
40 memWorkDir := "/lstate"
41
42 memFs := NewMemMapFs()
43 overlayFs1 := &CopyOnWriteFs{base: osFs, layer: memFs}
44 overlayFs2 := &CopyOnWriteFs{base: memFs, layer: osFs}
45 overlayFsMemOnly := &CopyOnWriteFs{base: memFs, layer: NewMemMapFs()}
46 basePathFs := &BasePathFs{source: osFs, path: workDir}
47 basePathFsMem := &BasePathFs{source: memFs, path: memWorkDir}
48 roFs := &ReadOnlyFs{source: osFs}
49 roFsMem := &ReadOnlyFs{source: memFs}
50
51 pathFileMem := filepath.Join(memWorkDir, "aferom.txt")
52
53 WriteFile(osFs, filepath.Join(workDir, "afero.txt"), []byte("Hi, Afero!"), 0o777)
54 WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0o777)
55
56 os.Chdir(workDir)
57 if err := os.Symlink("afero.txt", "symafero.txt"); err != nil {
58 t.Fatal(err)
59 }
60
61 pathFile := filepath.Join(workDir, "afero.txt")
62 pathSymlink := filepath.Join(workDir, "symafero.txt")
63
64 checkLstat := func(l Lstater, name string, shouldLstat bool) os.FileInfo {
65 statFile, isLstat, err := l.LstatIfPossible(name)
66 if err != nil {
67 t.Fatalf("Lstat check failed: %s", err)
68 }
69 if isLstat != shouldLstat {
70 t.Fatalf("Lstat status was %t for %s", isLstat, name)
71 }
72 return statFile
73 }
74
75 testLstat := func(l Lstater, pathFile, pathSymlink string) {
76 shouldLstat := pathSymlink != ""
77 statRegular := checkLstat(l, pathFile, shouldLstat)
78 statSymlink := checkLstat(l, pathSymlink, shouldLstat)
79 if statRegular == nil || statSymlink == nil {
80 t.Fatal("got nil FileInfo")
81 }
82
83 symSym := statSymlink.Mode()&os.ModeSymlink == os.ModeSymlink
84 if symSym == (pathSymlink == "") {
85 t.Fatal("expected the FileInfo to describe the symlink")
86 }
87
88 _, _, err := l.LstatIfPossible("this-should-not-exist.txt")
89 if err == nil || !os.IsNotExist(err) {
90 t.Fatalf("expected file to not exist, got %s", err)
91 }
92 }
93
94 testLstat(osFs, pathFile, pathSymlink)
95 testLstat(overlayFs1, pathFile, pathSymlink)
96 testLstat(overlayFs2, pathFile, pathSymlink)
97 testLstat(basePathFs, "afero.txt", "symafero.txt")
98 testLstat(overlayFsMemOnly, pathFileMem, "")
99 testLstat(basePathFsMem, "aferom.txt", "")
100 testLstat(roFs, pathFile, pathSymlink)
101 testLstat(roFsMem, pathFileMem, "")
102 }
103
View as plain text