expandpath_test.go 3.5 KB
Newer Older
1
2
3
4
5
6
package parser

import (
	"os"
	"os/user"
	"path/filepath"
7
	"runtime"
8
9
10
11
12
13
14
	"testing"
)

func TestExpandPath(t *testing.T) {
	mockCurrentUser := func() (*user.User, error) {
		return &user.User{
			Username: "testuser",
15
16
17
18
19
20
			HomeDir: func() string {
				if os.PathSeparator == '\\' {
					return filepath.FromSlash("D:/home/testuser")
				}
				return "/home/testuser"
			}(),
21
22
23
24
25
		}, nil
	}

	mockLookupUser := func(username string) (*user.User, error) {
		fakeUsers := map[string]string{
26
27
28
29
30
31
32
33
34
35
36
37
			"testuser": func() string {
				if os.PathSeparator == '\\' {
					return filepath.FromSlash("D:/home/testuser")
				}
				return "/home/testuser"
			}(),
			"anotheruser": func() string {
				if os.PathSeparator == '\\' {
					return filepath.FromSlash("D:/home/anotheruser")
				}
				return "/home/anotheruser"
			}(),
38
39
40
41
42
43
44
45
46
47
48
		}

		if homeDir, ok := fakeUsers[username]; ok {
			return &user.User{
				Username: username,
				HomeDir:  homeDir,
			}, nil
		}
		return nil, os.ErrNotExist
	}

49
50
51
	pwd, err := os.Getwd()
	if err != nil {
		t.Fatal(err)
52
53
	}

54
55
56
	t.Run("unix tests", func(t *testing.T) {
		if runtime.GOOS == "windows" {
			return
57
		}
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

		tests := []struct {
			path        string
			relativeDir string
			expected    string
			shouldErr   bool
		}{
			{"~", "", "/home/testuser", false},
			{"~/myfolder/myfile.txt", "", "/home/testuser/myfolder/myfile.txt", false},
			{"~anotheruser/docs/file.txt", "", "/home/anotheruser/docs/file.txt", false},
			{"~nonexistentuser/file.txt", "", "", true},
			{"relative/path/to/file", "", filepath.Join(pwd, "relative/path/to/file"), false},
			{"/absolute/path/to/file", "", "/absolute/path/to/file", false},
			{"/absolute/path/to/file", "someotherdir/", "/absolute/path/to/file", false},
			{".", pwd, pwd, false},
			{".", "", pwd, false},
			{"somefile", "somedir", filepath.Join(pwd, "somedir", "somefile"), false},
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
116
117
118
119
120
121
122

		for _, test := range tests {
			result, err := expandPathImpl(test.path, test.relativeDir, mockCurrentUser, mockLookupUser)
			if (err != nil) != test.shouldErr {
				t.Errorf("expandPathImpl(%q) returned error: %v, expected error: %v", test.path, err != nil, test.shouldErr)
			}

			if result != test.expected && !test.shouldErr {
				t.Errorf("expandPathImpl(%q) = %q, want %q", test.path, result, test.expected)
			}
		}
	})

	t.Run("windows tests", func(t *testing.T) {
		if runtime.GOOS != "windows" {
			return
		}

		tests := []struct {
			path        string
			relativeDir string
			expected    string
			shouldErr   bool
		}{
			{"~", "", "D:\\home\\testuser", false},
			{"~/myfolder/myfile.txt", "", "D:\\home\\testuser\\myfolder\\myfile.txt", false},
			{"~anotheruser/docs/file.txt", "", "D:\\home\\anotheruser\\docs\\file.txt", false},
			{"~nonexistentuser/file.txt", "", "", true},
			{"relative\\path\\to\\file", "", filepath.Join(pwd, "relative\\path\\to\\file"), false},
			{"D:\\absolute\\path\\to\\file", "", "D:\\absolute\\path\\to\\file", false},
			{"D:\\absolute\\path\\to\\file", "someotherdir/", "D:\\absolute\\path\\to\\file", false},
			{".", pwd, pwd, false},
			{".", "", pwd, false},
			{"somefile", "somedir", filepath.Join(pwd, "somedir", "somefile"), false},
		}

		for _, test := range tests {
			result, err := expandPathImpl(test.path, test.relativeDir, mockCurrentUser, mockLookupUser)
			if (err != nil) != test.shouldErr {
				t.Errorf("expandPathImpl(%q) returned error: %v, expected error: %v", test.path, err != nil, test.shouldErr)
			}

			if result != test.expected && !test.shouldErr {
				t.Errorf("expandPathImpl(%q) = %q, want %q", test.path, result, test.expected)
			}
		}
	})
123
}