|
| 1 | +import Testing |
| 2 | +import Foundation |
| 3 | +@testable import TS2Skeleton |
| 4 | +@testable import BridgeJSCore |
| 5 | + |
| 6 | +@Suite struct WhichTests { |
| 7 | + |
| 8 | + // MARK: - Helper Functions |
| 9 | + |
| 10 | + private static var pathSeparator: String { |
| 11 | + #if os(Windows) |
| 12 | + return ";" |
| 13 | + #else |
| 14 | + return ":" |
| 15 | + #endif |
| 16 | + } |
| 17 | + |
| 18 | + // MARK: - Successful Path Resolution Tests |
| 19 | + |
| 20 | + @Test func whichFindsExecutableInPath() throws { |
| 21 | + try withTemporaryDirectory { tempDir, _ in |
| 22 | + let execFile = tempDir.appendingPathComponent("testexec") |
| 23 | + try "#!/bin/sh\necho 'test'".write(to: execFile, atomically: true, encoding: .utf8) |
| 24 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: execFile.path) |
| 25 | + |
| 26 | + let environment = ["PATH": tempDir.path] |
| 27 | + |
| 28 | + let result = try which("testexec", environment: environment) |
| 29 | + |
| 30 | + #expect(result.path == execFile.path) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + @Test func whichReturnsFirstMatchInPath() throws { |
| 35 | + try withTemporaryDirectory { tempDir1, _ in |
| 36 | + try withTemporaryDirectory { tempDir2, _ in |
| 37 | + let exec1 = tempDir1.appendingPathComponent("testexec") |
| 38 | + let exec2 = tempDir2.appendingPathComponent("testexec") |
| 39 | + |
| 40 | + // Create executable files in both directories |
| 41 | + try "#!/bin/sh\necho 'first'".write(to: exec1, atomically: true, encoding: .utf8) |
| 42 | + try "#!/bin/sh\necho 'second'".write(to: exec2, atomically: true, encoding: .utf8) |
| 43 | + |
| 44 | + // Make files executable |
| 45 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: exec1.path) |
| 46 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: exec2.path) |
| 47 | + |
| 48 | + let pathEnv = "\(tempDir1.path)\(Self.pathSeparator)\(tempDir2.path)" |
| 49 | + let environment = ["PATH": pathEnv] |
| 50 | + |
| 51 | + let result = try which("testexec", environment: environment) |
| 52 | + |
| 53 | + // Should return the first one found |
| 54 | + #expect(result.path == exec1.path) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // MARK: - Environment Variable Override Tests |
| 60 | + |
| 61 | + @Test func whichUsesEnvironmentVariableOverride() throws { |
| 62 | + try withTemporaryDirectory { tempDir, _ in |
| 63 | + let customExec = tempDir.appendingPathComponent("mynode") |
| 64 | + try "#!/bin/sh\necho 'custom node'".write(to: customExec, atomically: true, encoding: .utf8) |
| 65 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: customExec.path) |
| 66 | + |
| 67 | + let environment = [ |
| 68 | + "PATH": "/nonexistent/path", |
| 69 | + "JAVASCRIPTKIT_NODE_EXEC": customExec.path, |
| 70 | + ] |
| 71 | + |
| 72 | + let result = try which("node", environment: environment) |
| 73 | + |
| 74 | + #expect(result.path == customExec.path) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test func whichHandlesHyphenatedExecutableNames() throws { |
| 79 | + try withTemporaryDirectory { tempDir, _ in |
| 80 | + let customExec = tempDir.appendingPathComponent("my-exec") |
| 81 | + try "#!/bin/sh\necho 'hyphenated'".write(to: customExec, atomically: true, encoding: .utf8) |
| 82 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: customExec.path) |
| 83 | + |
| 84 | + let environment = [ |
| 85 | + "PATH": "/nonexistent/path", |
| 86 | + "JAVASCRIPTKIT_MY_EXEC_EXEC": customExec.path, |
| 87 | + ] |
| 88 | + |
| 89 | + let result = try which("my-exec", environment: environment) |
| 90 | + |
| 91 | + #expect(result.path == customExec.path) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test func whichPrefersEnvironmentOverridePath() throws { |
| 96 | + try withTemporaryDirectory { tempDir1, _ in |
| 97 | + try withTemporaryDirectory { tempDir2, _ in |
| 98 | + let pathExec = tempDir1.appendingPathComponent("testexec") |
| 99 | + let envExec = tempDir2.appendingPathComponent("testexec") |
| 100 | + |
| 101 | + try "#!/bin/sh\necho 'from path'".write(to: pathExec, atomically: true, encoding: .utf8) |
| 102 | + try "#!/bin/sh\necho 'from env'".write(to: envExec, atomically: true, encoding: .utf8) |
| 103 | + |
| 104 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: pathExec.path) |
| 105 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: envExec.path) |
| 106 | + |
| 107 | + let environment = [ |
| 108 | + "PATH": tempDir1.path, |
| 109 | + "JAVASCRIPTKIT_TESTEXEC_EXEC": envExec.path, |
| 110 | + ] |
| 111 | + |
| 112 | + let result = try which("testexec", environment: environment) |
| 113 | + |
| 114 | + // Should prefer environment variable over PATH |
| 115 | + #expect(result.path == envExec.path) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // MARK: - Error Handling Tests |
| 121 | + |
| 122 | + @Test func whichThrowsWhenExecutableNotFound() throws { |
| 123 | + let environment = ["PATH": "/nonexistent\(Self.pathSeparator)/also/nonexistent"] |
| 124 | + |
| 125 | + #expect(throws: BridgeJSCoreError.self) { |
| 126 | + _ = try which("nonexistent_executable_12345", environment: environment) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Test func whichThrowsWhenEnvironmentPathIsInvalid() throws { |
| 131 | + try withTemporaryDirectory { tempDir, _ in |
| 132 | + let nonExecFile = tempDir.appendingPathComponent("notexecutable") |
| 133 | + try "not executable".write(to: nonExecFile, atomically: true, encoding: .utf8) |
| 134 | + |
| 135 | + let environment = [ |
| 136 | + "PATH": tempDir.path, |
| 137 | + "JAVASCRIPTKIT_NOTEXECUTABLE_EXEC": nonExecFile.path, |
| 138 | + ] |
| 139 | + |
| 140 | + #expect(throws: BridgeJSCoreError.self) { |
| 141 | + _ = try which("notexecutable", environment: environment) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Test func whichThrowsWhenPathPointsToDirectory() throws { |
| 147 | + try withTemporaryDirectory { tempDir, _ in |
| 148 | + let environment = [ |
| 149 | + "PATH": "/nonexistent/path", |
| 150 | + "JAVASCRIPTKIT_TESTEXEC_EXEC": tempDir.path, |
| 151 | + ] |
| 152 | + |
| 153 | + #expect(throws: BridgeJSCoreError.self) { |
| 154 | + _ = try which("testexec", environment: environment) |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // MARK: - Edge Case Tests |
| 160 | + |
| 161 | + @Test func whichHandlesEmptyPath() throws { |
| 162 | + let environment = ["PATH": ""] |
| 163 | + |
| 164 | + #expect(throws: BridgeJSCoreError.self) { |
| 165 | + _ = try which("anyexec", environment: environment) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Test func whichHandlesMissingPathEnvironment() throws { |
| 170 | + let environment: [String: String] = [:] |
| 171 | + |
| 172 | + #expect(throws: BridgeJSCoreError.self) { |
| 173 | + _ = try which("anyexec", environment: environment) |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Test func whichIgnoresNonExecutableFiles() throws { |
| 178 | + try withTemporaryDirectory { tempDir, _ in |
| 179 | + let nonExecFile = tempDir.appendingPathComponent("testfile") |
| 180 | + try "content".write(to: nonExecFile, atomically: true, encoding: .utf8) |
| 181 | + // Don't set executable permissions |
| 182 | + |
| 183 | + let environment = ["PATH": tempDir.path] |
| 184 | + |
| 185 | + #expect(throws: BridgeJSCoreError.self) { |
| 186 | + _ = try which("testfile", environment: environment) |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments