Files
swift-mirror/validation-test/StdlibUnittest/CommandLineExecutablePath.swift
Jonathan Grynspan 5a71cd0053 Implement the back end of CommandLine.executablePath. (#90476)
This PR implements the C++ back end of `CommandLine.executablePath` as
approved in
[SE-0513](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0513-commandline-executablepath.md).

The front end that exposes a `FilePath`-based API is blocked on the
existence of `FilePath` in the standard library, but some toolchain
components like Swift Testing need to back-deploy and can't link to the
System package directly, so they need to directly use the C++
implementation.

Darwin doesn't need this implementation as it has
`_NSGetExecutablePath()` that will be used by the back-deployable Darwin
implementation instead of this helper function.
2026-07-14 00:48:41 -07:00

49 lines
1.4 KiB
Swift

// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/this-is-my-executable-path.out
// RUN: %target-codesign %t/this-is-my-executable-path.out
// RUN: %target-run %t/this-is-my-executable-path.out
// REQUIRES: executable_test
// UNSUPPORTED: OS=macosx, OS=ios, OS=tvos, OS=watchos, OS=xros
// UNSUPPORTED: OS=wasip1, OS=emscripten
import StdlibUnittest
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(Windows)
import WinSDK
#endif
var CommandLineExecutablePath = TestSuite("CommandLineExecutablePath")
extension CommandLine {
#if os(Windows)
internal static var executablePathCString: ContiguousArray<CWideChar> {
@_silgen_name("_swift_stdlib_executablePathCString") get
}
#else
internal static var executablePathCString: ContiguousArray<CChar> {
@_silgen_name("_swift_stdlib_executablePathCString") get
}
#endif
}
CommandLineExecutablePath.test("executablePathCapturedAndCorrect") {
#if os(Windows)
CommandLine.executablePathCString.withUnsafeBufferPointer { path in
#"\this-is-my-executable-path.out"#.withCString(encodedAs: UTF16.self) { subpath in
expectNotEqual(nil, wcsstr(path.baseAddress!, subpath))
}
}
#else
CommandLine.executablePathCString.withUnsafeBufferPointer { path in
expectNotEqual(nil, strstr(path.baseAddress!, "/this-is-my-executable-path.out"))
}
#endif
}
runAllTests()