Skip to content

Commit a8651fb

Browse files
authored
Merge pull request #2387 from gmittert/DontTrustTheProcess
Fix TestProcess on Windows
2 parents a9868fe + e62ffee commit a8651fb

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

Foundation/Process.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ open class Process: NSObject {
888888
open func terminate() {
889889
precondition(hasStarted, "task not launched")
890890
#if os(Windows)
891-
TerminateProcess(processHandle, UINT(SIGTERM))
891+
TerminateProcess(processHandle, UINT(0xC0000000 | DWORD(SIGTERM)))
892892
#else
893893
kill(processIdentifier, SIGTERM)
894894
#endif
@@ -950,6 +950,9 @@ open class Process: NSObject {
950950
#if os(Windows)
951951
open private(set) var processHandle: HANDLE = INVALID_HANDLE_VALUE
952952
open var processIdentifier: Int32 {
953+
guard processHandle != INVALID_HANDLE_VALUE else {
954+
return 0
955+
}
953956
return Int32(GetProcessId(processHandle))
954957
}
955958
open private(set) var isRunning: Bool = false

TestFoundation/TestProcess.swift

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ class TestProcess : XCTestCase {
116116
XCTFail("Could not read stdout")
117117
return
118118
}
119-
XCTAssertEqual(string.trimmingCharacters(in: CharacterSet(["\n"])), FileManager.default.currentDirectoryPath)
119+
120+
XCTAssertEqual(string.trimmingCharacters(in: CharacterSet(["\n", "\r"])), FileManager.default.currentDirectoryPath)
120121
}
121122

122123
func test_pipe_stderr() throws {
@@ -194,7 +195,7 @@ class TestProcess : XCTestCase {
194195
XCTFail("Could not read stdout")
195196
return
196197
}
197-
XCTAssertEqual(string.trimmingCharacters(in: CharacterSet(["\n"])), FileManager.default.currentDirectoryPath)
198+
XCTAssertEqual(string.trimmingCharacters(in: CharacterSet(["\r", "\n"])), FileManager.default.currentDirectoryPath)
198199
}
199200

200201
func test_passthrough_environment() {
@@ -213,7 +214,12 @@ class TestProcess : XCTestCase {
213214
do {
214215
let (output, _) = try runTask([xdgTestHelperURL().path, "--env"], environment: [:])
215216
let env = try parseEnv(output)
217+
#if os(Windows)
218+
// On Windows, Path is always passed to the sub process
219+
XCTAssertEqual(env.count, 1)
220+
#else
216221
XCTAssertEqual(env.count, 0)
222+
#endif
217223
} catch {
218224
XCTFail("Test failed: \(error)")
219225
}
@@ -223,15 +229,23 @@ class TestProcess : XCTestCase {
223229
do {
224230
let input = ["HELLO": "WORLD", "HOME": "CUPERTINO"]
225231
let (output, _) = try runTask([xdgTestHelperURL().path, "--env"], environment: input)
226-
let env = try parseEnv(output)
232+
var env = try parseEnv(output)
233+
#if os(Windows)
234+
// On Windows, Path is always passed to the sub process, remove it
235+
// before comparing.
236+
env.removeValue(forKey: "Path")
237+
#endif
227238
XCTAssertEqual(env, input)
228239
} catch {
229240
XCTFail("Test failed: \(error)")
230241
}
231242
}
232243

233-
func test_current_working_directory() {
234-
let tmpDir = "/tmp" //.standardizingPath
244+
func test_current_working_directory() throws {
245+
var tmpDir = NSTemporaryDirectory()
246+
if let lastChar = tmpDir.last, lastChar == "/" || lastChar == "\\" {
247+
tmpDir.removeLast()
248+
}
235249

236250
let fm = FileManager.default
237251
let previousWorkingDirectory = fm.currentDirectoryPath
@@ -310,7 +324,7 @@ class TestProcess : XCTestCase {
310324
let process = Process()
311325
process.executableURL = URL(fileURLWithPath: "/..", isDirectory: false)
312326
process.arguments = []
313-
process.currentDirectoryURL = URL(fileURLWithPath: "/tmp")
327+
process.currentDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory())
314328
try process.run()
315329
XCTFail("Somehow executed a directory!")
316330
process.terminate()
@@ -537,12 +551,12 @@ class TestProcess : XCTestCase {
537551
if let d = try stdoutPipe.fileHandleForReading.readToEnd() {
538552
stdoutData.append(d)
539553
}
540-
XCTAssertEqual(String(data: stdoutData, encoding: .utf8), "No files specified.\n")
554+
XCTAssertEqual(String(data: stdoutData, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines), "No files specified.")
541555
}
542556

543557

544558
static var allTests: [(String, (TestProcess) -> () throws -> Void)] {
545-
return [
559+
var tests = [
546560
("test_exit0" , test_exit0),
547561
("test_exit1" , test_exit1),
548562
("test_exit100" , test_exit100),
@@ -559,9 +573,7 @@ class TestProcess : XCTestCase {
559573
("test_custom_environment", test_custom_environment),
560574
("test_run", test_run),
561575
("test_preStartEndState", test_preStartEndState),
562-
("test_interrupt", test_interrupt),
563576
("test_terminate", test_terminate),
564-
("test_suspend_resume", test_suspend_resume),
565577
("test_redirect_stdin_using_null", test_redirect_stdin_using_null),
566578
("test_redirect_stdout_using_null", test_redirect_stdout_using_null),
567579
("test_redirect_stdin_stdout_using_null", test_redirect_stdin_stdout_using_null),
@@ -570,6 +582,15 @@ class TestProcess : XCTestCase {
570582
("test_redirect_all_using_nil", test_redirect_all_using_nil),
571583
("test_plutil", test_plutil),
572584
]
585+
586+
#if !os(Windows)
587+
// Windows doesn't have signals
588+
tests += [
589+
("test_interrupt", test_interrupt),
590+
("test_suspend_resume", test_suspend_resume),
591+
]
592+
#endif
593+
return tests
573594
}
574595
}
575596

@@ -726,7 +747,7 @@ internal func runTask(_ arguments: [String], environment: [String: String]? = ni
726747

727748
private func parseEnv(_ env: String) throws -> [String: String] {
728749
var result = [String: String]()
729-
for line in env.components(separatedBy: "\n") where line != "" {
750+
for line in env.components(separatedBy: .newlines) where line != "" {
730751
guard let range = line.range(of: "=") else {
731752
throw Error.InvalidEnvironmentVariable(line)
732753
}

TestFoundation/xdgTestHelper/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ case "--sleep":
240240
case "--signal-self":
241241
if let signalnum = arguments.next(), let signal = Int32(signalnum) {
242242
#if os(Windows)
243-
TerminateProcess(GetCurrentProcess(), UINT(signal))
243+
TerminateProcess(GetCurrentProcess(), UINT(0xC0000000 | UINT(signal)))
244244
#else
245245
kill(ProcessInfo.processInfo.processIdentifier, signal)
246246
#endif

0 commit comments

Comments
 (0)