aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/turbopath/absolute_system_path_test.go
blob: 4ca36f9b91f938fa3f3731b180b0dc7d08dc5097 (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package turbopath

import (
	"os"
	"runtime"
	"testing"

	"gotest.tools/v3/assert"
	"gotest.tools/v3/fs"
)

func Test_Mkdir(t *testing.T) {
	type Case struct {
		name         string
		isDir        bool
		exists       bool
		mode         os.FileMode
		expectedMode os.FileMode
	}

	cases := []Case{
		{
			name:         "dir doesn't exist",
			exists:       false,
			expectedMode: os.ModeDir | 0777,
		},
		{
			name:         "path exists as file",
			exists:       true,
			isDir:        false,
			mode:         0666,
			expectedMode: os.ModeDir | 0755,
		},
		{
			name:         "dir exists with incorrect mode",
			exists:       true,
			isDir:        false,
			mode:         os.ModeDir | 0755,
			expectedMode: os.ModeDir | 0655,
		},
		{
			name:         "dir exists with correct mode",
			exists:       true,
			isDir:        false,
			mode:         os.ModeDir | 0755,
			expectedMode: os.ModeDir | 0755,
		},
	}

	for _, testCase := range cases {
		testDir := fs.NewDir(t, "system-path-mkdir-test")
		testName := testCase.name
		path := testDir.Join("foo")
		if testCase.isDir {
			err := os.Mkdir(path, testCase.mode)
			assert.NilError(t, err, "%s: Mkdir", testName)
		} else if testCase.exists {
			file, err := os.Create(path)
			assert.NilError(t, err, "%s: Create", testName)
			err = file.Chmod(testCase.mode)
			assert.NilError(t, err, "%s: Chmod", testName)
			err = file.Close()
			assert.NilError(t, err, "%s: Close", testName)
		}

		testPath := AbsoluteSystemPath(path)
		err := testPath.MkdirAllMode(testCase.expectedMode)
		assert.NilError(t, err, "%s: Mkdir", testName)

		stat, err := testPath.Lstat()
		assert.NilError(t, err, "%s: Lstat", testName)
		assert.Assert(t, stat.IsDir(), testName)

		assert.Assert(t, stat.IsDir(), testName)

		if runtime.GOOS == "windows" {
			// For windows os.Chmod will only change the writable bit so that's all we check
			assert.Equal(t, stat.Mode().Perm()&0200, testCase.expectedMode.Perm()&0200, testName)
		} else {
			assert.Equal(t, stat.Mode(), testCase.expectedMode, testName)
		}

	}
}

func TestAbsoluteSystemPath_Findup(t *testing.T) {
	tests := []struct {
		name               string
		fs                 []AnchoredSystemPath
		executionDirectory AnchoredSystemPath
		fileName           RelativeSystemPath
		want               AnchoredSystemPath
		wantErr            bool
	}{
		{
			name: "hello world",
			fs: []AnchoredSystemPath{
				AnchoredUnixPath("one/two/three/four/.file").ToSystemPath(),
				AnchoredUnixPath("one/two/three/four/.target").ToSystemPath(),
			},
			executionDirectory: AnchoredUnixPath("one/two/three/four").ToSystemPath(),
			fileName:           RelativeUnixPath(".target").ToSystemPath(),
			want:               AnchoredUnixPath("one/two/three/four/.target").ToSystemPath(),
		},
		{
			name: "parent",
			fs: []AnchoredSystemPath{
				AnchoredUnixPath("one/two/three/four/.file").ToSystemPath(),
				AnchoredUnixPath("one/two/three/.target").ToSystemPath(),
			},
			executionDirectory: AnchoredUnixPath("one/two/three/four").ToSystemPath(),
			fileName:           RelativeUnixPath(".target").ToSystemPath(),
			want:               AnchoredUnixPath("one/two/three/.target").ToSystemPath(),
		},
		{
			name: "gets the closest",
			fs: []AnchoredSystemPath{
				AnchoredUnixPath("one/two/three/four/.file").ToSystemPath(),
				AnchoredUnixPath("one/two/three/.target").ToSystemPath(),
				AnchoredUnixPath("one/two/.target").ToSystemPath(),
			},
			executionDirectory: AnchoredUnixPath("one/two/three/four").ToSystemPath(),
			fileName:           RelativeUnixPath(".target").ToSystemPath(),
			want:               AnchoredUnixPath("one/two/three/.target").ToSystemPath(),
		},
		{
			name: "nonexistent",
			fs: []AnchoredSystemPath{
				AnchoredUnixPath("one/two/three/four/.file").ToSystemPath(),
			},
			executionDirectory: AnchoredUnixPath("one/two/three/four").ToSystemPath(),
			fileName:           RelativeUnixPath(".nonexistent").ToSystemPath(),
			want:               "",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			fsRoot := AbsoluteSystemPath(t.TempDir())
			for _, file := range tt.fs {
				path := file.RestoreAnchor(fsRoot)
				assert.NilError(t, path.Dir().MkdirAll(0777))
				assert.NilError(t, path.WriteFile(nil, 0777))
			}

			got, err := tt.executionDirectory.RestoreAnchor(fsRoot).Findup(tt.fileName)
			if tt.wantErr {
				assert.ErrorIs(t, err, os.ErrNotExist)
				return
			}
			if got != "" && got != tt.want.RestoreAnchor(fsRoot) {
				t.Errorf("AbsoluteSystemPath.Findup() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestJoin(t *testing.T) {
	rawRoot, err := os.Getwd()
	if err != nil {
		t.Fatalf("cwd %v", err)
	}
	root := AbsoluteSystemPathFromUpstream(rawRoot)
	testRoot := root.Join("a", "b", "c")
	dot := testRoot.Join(".")
	if dot != testRoot {
		t.Errorf(". path got %v, want %v", dot, testRoot)
	}

	doubleDot := testRoot.Join("..")
	expectedDoubleDot := root.Join("a", "b")
	if doubleDot != expectedDoubleDot {
		t.Errorf(".. path got %v, want %v", doubleDot, expectedDoubleDot)
	}
}