-
Notifications
You must be signed in to change notification settings - Fork 0
/
realpath_test.go
102 lines (71 loc) · 2.11 KB
/
realpath_test.go
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
package realpath_test
import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/gandarez/go-realpath"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRealpath(t *testing.T) {
tmpDir := t.TempDir()
tmpFile, err := os.CreateTemp(tmpDir, "file")
require.NoError(t, err)
defer tmpFile.Close()
path, err := realpath.Realpath(tmpFile.Name())
require.NoError(t, err)
assert.Equal(t, tmpFile.Name(), path)
}
func TestRealpath_ZeroLenght(t *testing.T) {
_, err := realpath.Realpath("")
assert.EqualError(t, err, "invalid argument")
}
func TestRealpath_NonFile(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping test on windows platform")
}
wd, err := os.Getwd()
require.NoError(t, err)
_, err = realpath.Realpath("non-file")
assert.EqualError(t, err, fmt.Sprintf("lstat %s: no such file or directory", filepath.Join(wd, "non-file")))
}
func TestRealpath_NonFile_Windows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("skipping test on non-windows platform")
}
wd, err := os.Getwd()
require.NoError(t, err)
_, err = realpath.Realpath("non-file")
assert.EqualError(
t,
err,
fmt.Sprintf("CreateFile %s: The system cannot find the file specified.", filepath.Join(wd, "non-file")),
)
}
func TestRealpath_RelativePath(t *testing.T) {
path, err := realpath.Realpath("testdata/relative.go")
require.NoError(t, err)
wd, err := os.Getwd()
require.NoError(t, err)
assert.Equal(t, filepath.Join(wd, "testdata/relative.go"), path)
}
func TestRealpath_Symlink(t *testing.T) {
tmpDir := t.TempDir()
tmpFile, err := os.CreateTemp(tmpDir, "file")
require.NoError(t, err)
defer tmpFile.Close()
tmpFileSymlink := filepath.Join(tmpDir, "file_symlink")
err = os.Symlink(tmpFile.Name(), tmpFileSymlink)
require.NoError(t, err)
path, err := realpath.Realpath(tmpFileSymlink)
require.NoError(t, err)
assert.Equal(t, tmpFile.Name(), path)
}
func TestRealpath_TrailingSlash(t *testing.T) {
tmpDir := t.TempDir()
path, err := realpath.Realpath(tmpDir + string(os.PathSeparator))
require.NoError(t, err)
assert.Equal(t, tmpDir, path)
}