From e4cda47e5612aec7cb39e73b2734e649105260ae Mon Sep 17 00:00:00 2001 From: YR Chen Date: Wed, 23 Dec 2020 20:32:26 +0800 Subject: [PATCH] Minor behavioral fixes --- Sources/TSCBasic/Process.swift | 5 ++++- Sources/TSCBasic/misc.swift | 2 +- Tests/TSCBasicTests/ProcessTests.swift | 20 +++++++++----------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Sources/TSCBasic/Process.swift b/Sources/TSCBasic/Process.swift index ab79d893..aa10fd9b 100644 --- a/Sources/TSCBasic/Process.swift +++ b/Sources/TSCBasic/Process.swift @@ -334,7 +334,10 @@ public final class Process: ObjectIdentifierProtocol { searchPaths.append(AbsolutePath(String(decodingCString: buffer, as: UTF16.self))) // The 16-bit Windows system directory - searchPaths.append(AbsolutePath("\(ProcessEnv.vars["systemdrive"] ?? "C:")\\System")) + if let systemDrive = ProcessEnv.vars["systemdrive"], + let systemPath = try? AbsolutePath(validating: "\(systemDrive))\\System") { + searchPaths.append(systemPath) + } // The Windows directory GetWindowsDirectoryW(&buffer, .init(MAX_PATH + 1)) diff --git a/Sources/TSCBasic/misc.swift b/Sources/TSCBasic/misc.swift index 45efdace..3afd4c79 100644 --- a/Sources/TSCBasic/misc.swift +++ b/Sources/TSCBasic/misc.swift @@ -91,7 +91,7 @@ public func lookupExecutablePath( guard var value = value, !value.isEmpty else { return nil } - let isPath = value.contains("\\") + let isPath = value.contains("\\") || value.contains("/") if !isPath && !value.contains(".") { value.append(executableFileSuffix) } diff --git a/Tests/TSCBasicTests/ProcessTests.swift b/Tests/TSCBasicTests/ProcessTests.swift index f0d2f6a0..51f23792 100644 --- a/Tests/TSCBasicTests/ProcessTests.swift +++ b/Tests/TSCBasicTests/ProcessTests.swift @@ -103,16 +103,12 @@ class ProcessTests: XCTestCase { XCTAssertNil(Process.findExecutable("nonExistantProgram")) XCTAssertNil(Process.findExecutable("")) - // Create a bat file to test. - let tempExecutable = tmpdir.appending(component: "program.bat") - try localFileSystem.writeFileContents(tempExecutable, bytes: """ - @echo off - exit - - """) + // 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.bc") + let tempNonExecutable = tmpdir.appending(component: "program.bat") try localFileSystem.writeFileContents(tempNonExecutable, bytes: """ @echo off exit @@ -120,14 +116,16 @@ class ProcessTests: XCTestCase { """) try withCustomEnv(["PATH": tmpdir.pathString]) { - XCTAssertNotNil(Process.findExecutable("program.bat")) - XCTAssertNil(Process.findExecutable("program.bc")) + XCTAssertNotNil(Process.findExecutable("executableProgram.exe")) + XCTAssertNotNil(Process.findExecutable("executableProgram")) + // Currently, Foundation treats all readable files as executable on Windows. + // XCTAssertNil(Process.findExecutable("program.bat")) } } #endif } - #if !os(Windows) // Foundation treats all readable files as executable on Windows. + #if !os(Windows) // Foundation treats all readable files as executable on Windows func testNonExecutableLaunch() throws { try testWithTemporaryDirectory { tmpdir in // Create a local nonexecutable file to test.