Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vfs_test.go « local « vfs « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec67d59595f0157e5af8eaf67025fc1667b3b1d6 (plain)
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
107
108
109
110
111
112
113
114
115
package local

import (
	"context"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var localVFS = &VFS{}

func tmpDir(t *testing.T) (string, func()) {
	tmpDir, err := ioutil.TempDir("", "vfs")
	require.NoError(t, err)

	// On some systems `/tmp` can be a symlink
	tmpDir, err = filepath.EvalSymlinks(tmpDir)
	require.NoError(t, err)

	return tmpDir, func() {
		os.RemoveAll(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, cleanup := tmpDir(t)
	defer cleanup()

	dirPath := filepath.Join(tmpDir, "dir")
	err := os.Mkdir(dirPath, 0755)
	require.NoError(t, err)

	filePath := filepath.Join(tmpDir, "file")
	err = ioutil.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
		expectedIsNotExist bool
	}{
		"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",
			expectedIsNotExist: true,
		},
	}

	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.expectedIsNotExist {
				require.Equal(t, test.expectedIsNotExist, os.IsNotExist(err))
				return
			}

			if test.expectedErr != nil {
				require.EqualError(t, err, test.expectedErr.Error())
				return
			}

			require.NoError(t, err)
			require.IsType(t, &Root{}, rootVFS)
			assert.Equal(t, test.expectedPath, rootVFS.(*Root).rootPath)
		})
	}
}