-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathWhichTests.swift
More file actions
190 lines (145 loc) · 6.88 KB
/
Copy pathWhichTests.swift
File metadata and controls
190 lines (145 loc) · 6.88 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import Testing
import Foundation
@testable import TS2Skeleton
@testable import BridgeJSCore
@Suite struct WhichTests {
// MARK: - Helper Functions
private static var pathSeparator: String {
#if os(Windows)
return ";"
#else
return ":"
#endif
}
// MARK: - Successful Path Resolution Tests
@Test func whichFindsExecutableInPath() throws {
try withTemporaryDirectory { tempDir, _ in
let execFile = tempDir.appendingPathComponent("testexec")
try "#!/bin/sh\necho 'test'".write(to: execFile, atomically: true, encoding: .utf8)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: execFile.path)
let environment = ["PATH": tempDir.path]
let result = try which("testexec", environment: environment)
#expect(result.path == execFile.path)
}
}
@Test func whichReturnsFirstMatchInPath() throws {
try withTemporaryDirectory { tempDir1, _ in
try withTemporaryDirectory { tempDir2, _ in
let exec1 = tempDir1.appendingPathComponent("testexec")
let exec2 = tempDir2.appendingPathComponent("testexec")
// Create executable files in both directories
try "#!/bin/sh\necho 'first'".write(to: exec1, atomically: true, encoding: .utf8)
try "#!/bin/sh\necho 'second'".write(to: exec2, atomically: true, encoding: .utf8)
// Make files executable
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: exec1.path)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: exec2.path)
let pathEnv = "\(tempDir1.path)\(Self.pathSeparator)\(tempDir2.path)"
let environment = ["PATH": pathEnv]
let result = try which("testexec", environment: environment)
// Should return the first one found
#expect(result.path == exec1.path)
}
}
}
// MARK: - Environment Variable Override Tests
@Test func whichUsesEnvironmentVariableOverride() throws {
try withTemporaryDirectory { tempDir, _ in
let customExec = tempDir.appendingPathComponent("mynode")
try "#!/bin/sh\necho 'custom node'".write(to: customExec, atomically: true, encoding: .utf8)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: customExec.path)
let environment = [
"PATH": "/nonexistent/path",
"JAVASCRIPTKIT_NODE_EXEC": customExec.path,
]
let result = try which("node", environment: environment)
#expect(result.path == customExec.path)
}
}
@Test func whichHandlesHyphenatedExecutableNames() throws {
try withTemporaryDirectory { tempDir, _ in
let customExec = tempDir.appendingPathComponent("my-exec")
try "#!/bin/sh\necho 'hyphenated'".write(to: customExec, atomically: true, encoding: .utf8)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: customExec.path)
let environment = [
"PATH": "/nonexistent/path",
"JAVASCRIPTKIT_MY_EXEC_EXEC": customExec.path,
]
let result = try which("my-exec", environment: environment)
#expect(result.path == customExec.path)
}
}
@Test func whichPrefersEnvironmentOverridePath() throws {
try withTemporaryDirectory { tempDir1, _ in
try withTemporaryDirectory { tempDir2, _ in
let pathExec = tempDir1.appendingPathComponent("testexec")
let envExec = tempDir2.appendingPathComponent("testexec")
try "#!/bin/sh\necho 'from path'".write(to: pathExec, atomically: true, encoding: .utf8)
try "#!/bin/sh\necho 'from env'".write(to: envExec, atomically: true, encoding: .utf8)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: pathExec.path)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: envExec.path)
let environment = [
"PATH": tempDir1.path,
"JAVASCRIPTKIT_TESTEXEC_EXEC": envExec.path,
]
let result = try which("testexec", environment: environment)
// Should prefer environment variable over PATH
#expect(result.path == envExec.path)
}
}
}
// MARK: - Error Handling Tests
@Test func whichThrowsWhenExecutableNotFound() throws {
let environment = ["PATH": "/nonexistent\(Self.pathSeparator)/also/nonexistent"]
#expect(throws: BridgeJSCoreError.self) {
_ = try which("nonexistent_executable_12345", environment: environment)
}
}
@Test func whichThrowsWhenEnvironmentPathIsInvalid() throws {
try withTemporaryDirectory { tempDir, _ in
let nonExecFile = tempDir.appendingPathComponent("notexecutable")
try "not executable".write(to: nonExecFile, atomically: true, encoding: .utf8)
let environment = [
"PATH": tempDir.path,
"JAVASCRIPTKIT_NOTEXECUTABLE_EXEC": nonExecFile.path,
]
#expect(throws: BridgeJSCoreError.self) {
_ = try which("notexecutable", environment: environment)
}
}
}
@Test func whichThrowsWhenPathPointsToDirectory() throws {
try withTemporaryDirectory { tempDir, _ in
let environment = [
"PATH": "/nonexistent/path",
"JAVASCRIPTKIT_TESTEXEC_EXEC": tempDir.path,
]
#expect(throws: BridgeJSCoreError.self) {
_ = try which("testexec", environment: environment)
}
}
}
// MARK: - Edge Case Tests
@Test func whichHandlesEmptyPath() throws {
let environment = ["PATH": ""]
#expect(throws: BridgeJSCoreError.self) {
_ = try which("anyexec", environment: environment)
}
}
@Test func whichHandlesMissingPathEnvironment() throws {
let environment: [String: String] = [:]
#expect(throws: BridgeJSCoreError.self) {
_ = try which("anyexec", environment: environment)
}
}
@Test func whichIgnoresNonExecutableFiles() throws {
try withTemporaryDirectory { tempDir, _ in
let nonExecFile = tempDir.appendingPathComponent("testfile")
try "content".write(to: nonExecFile, atomically: true, encoding: .utf8)
// Don't set executable permissions
let environment = ["PATH": tempDir.path]
#expect(throws: BridgeJSCoreError.self) {
_ = try which("testfile", environment: environment)
}
}
}
}