1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
package local
import (
"context"
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var localVFS = &VFS{}
func tmpDir(t *testing.T) string {
var err error
tmpDir := t.TempDir()
// On some systems `/tmp` can be a symlink
tmpDir, err = filepath.EvalSymlinks(tmpDir)
require.NoError(t, err)
return tmpDir
}
func TestVFSRoot(t *testing.T) {
// create structure as:
// /tmp/dir: directory
// /tmp/dir_link: symlink to `dir`
// /tmp/dir_absolute_link: symlink to `/tmp/dir`
// /tmp/file: file
// /tmp/file_link: symlink to `file`
// /tmp/file_absolute_link: symlink to `/tmp/file`
tmpDir := tmpDir(t)
dirPath := filepath.Join(tmpDir, "dir")
err := os.Mkdir(dirPath, 0755)
require.NoError(t, err)
filePath := filepath.Join(tmpDir, "file")
err = os.WriteFile(filePath, []byte{}, 0644)
require.NoError(t, err)
symlinks := map[string]string{
"dir_link": "dir",
"dir_absolute_link": dirPath,
"file_link": "file",
"file_absolute_link": filePath,
}
for dest, src := range symlinks {
err := os.Symlink(src, filepath.Join(tmpDir, dest))
require.NoError(t, err)
}
tests := map[string]struct {
path string
expectedPath string
expectedErr error
}{
"a valid directory": {
path: "dir",
expectedPath: dirPath,
},
"a symlink to directory": {
path: "dir_link",
expectedPath: dirPath,
},
"a symlink to absolute directory": {
path: "dir_absolute_link",
expectedPath: dirPath,
},
"a file": {
path: "file",
expectedErr: errNotDirectory,
},
"a symlink to file": {
path: "file_link",
expectedErr: errNotDirectory,
},
"a symlink to absolute file": {
path: "file_absolute_link",
expectedErr: errNotDirectory,
},
"a non-existing file": {
path: "not-existing",
expectedErr: fs.ErrNotExist,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
rootVFS, err := localVFS.Root(context.Background(), filepath.Join(tmpDir, test.path), "")
if test.expectedErr != nil {
require.ErrorIs(t, err, test.expectedErr)
return
}
require.NoError(t, err)
require.IsType(t, &Root{}, rootVFS)
assert.Equal(t, test.expectedPath, rootVFS.(*Root).rootPath)
})
}
}
|