From ec7e1bff3ef2dea2df2b0ba718a3fd6d79c55ab5 Mon Sep 17 00:00:00 2001 From: YR Chen Date: Wed, 8 Dec 2021 08:22:33 +0800 Subject: [PATCH] Update `testFindExecutable` for Windows --- Tests/TSCBasicTests/ProcessTests.swift | 40 +++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/Tests/TSCBasicTests/ProcessTests.swift b/Tests/TSCBasicTests/ProcessTests.swift index 1ff03416..08beed32 100644 --- a/Tests/TSCBasicTests/ProcessTests.swift +++ b/Tests/TSCBasicTests/ProcessTests.swift @@ -108,12 +108,13 @@ class ProcessTests: XCTestCase { } func testFindExecutable() throws { + #if !os(Windows) try testWithTemporaryDirectory { tmpdir in // This process should always work. - XCTAssertTrue(Process.findExecutable("ls") != nil) + XCTAssertNotNil(Process.findExecutable("ls")) - XCTAssertEqual(Process.findExecutable("nonExistantProgram"), nil) - XCTAssertEqual(Process.findExecutable(""), nil) + XCTAssertNil(Process.findExecutable("nonExistantProgram")) + XCTAssertNil(Process.findExecutable("")) // Create a local nonexecutable file to test. let tempExecutable = tmpdir.appending(component: "nonExecutableProgram") @@ -124,9 +125,40 @@ class ProcessTests: XCTestCase { """) try withCustomEnv(["PATH": tmpdir.pathString]) { - XCTAssertEqual(Process.findExecutable("nonExecutableProgram"), nil) + XCTAssertNil(Process.findExecutable("nonExecutableProgram")) } } + #else + try testWithTemporaryDirectory { tmpdir in + // Test System32 without .exe suffix. + XCTAssertNotNil(Process.findExecutable("cmd")) + + // Test Windows with .exe suffix. + XCTAssertNotNil(Process.findExecutable("explorer.exe")) + + // Test non-existant programs. + XCTAssertNil(Process.findExecutable("nonExistantProgram")) + XCTAssertNil(Process.findExecutable("")) + + // Copy an executable file to test. + let tempExecutable = tmpdir.appending(component: "executableProgram.exe") + try localFileSystem.copy(from: Process.findExecutable("cmd")!, to: tempExecutable) + + // Create a non-executable file to test. + let tempNonExecutable = tmpdir.appending(component: "program.bat") + try localFileSystem.writeFileContents(tempNonExecutable, bytes: """ + @echo off + exit + + """) + + try withCustomEnv(["Path": tmpdir.pathString]) { + XCTAssertNotNil(Process.findExecutable("executableProgram.exe")) + XCTAssertNotNil(Process.findExecutable("executableProgram")) + XCTAssertNil(Process.findExecutable("program.bat")) + } + } + #endif } func testNonExecutableLaunch() throws {