Skip to content

Commit

Permalink
Update testFindExecutable for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
stevapple committed Apr 22, 2022
1 parent 0c931dd commit ec7e1bf
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions Tests/TSCBasicTests/ProcessTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down

0 comments on commit ec7e1bf

Please sign in to comment.