Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions Sources/Testing/Support/Additions/CommandLineAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@

private import _TestingInternals

#if os(OpenBSD)
/// At process start (before `main()` is called), capture the current working
/// directory.
///
/// -Note: This function is the only valid caller of `swt_captureEarlyCWD()`.
@_section(".init_array.101") @_used
private let _captureEarlyCWD: @convention(c) () -> Void = {
swt_captureEarlyCWD()
}
#endif

extension CommandLine {
/// The path to the current process' executable.
static var executablePath: String {
Expand Down Expand Up @@ -57,11 +68,17 @@ extension CommandLine {
}
#elseif os(OpenBSD)
// OpenBSD does not have API to get a path to the running executable. Use
// arguments[0]. We do a basic sniff test for a path-like string, but
// otherwise return argv[0] verbatim.
guard let argv0 = arguments.first, argv0.contains("/") else {
// arguments[0]. We do a basic sniff test for a path-like string, and
// prepend the early CWD if it looks like a relative path, but otherwise
// return argv[0] verbatim.
guard var argv0 = arguments.first, argv0.contains("/") else {
throw CError(rawValue: ENOEXEC)
}
if argv0.first != "/",
let earlyCWD = swt_getEarlyCWD().flatMap(String.init(validatingCString:)),
!earlyCWD.isEmpty {
argv0 = "\(earlyCWD)/\(argv0)"
}
return argv0
#elseif os(Windows)
var result: String?
Expand Down
35 changes: 35 additions & 0 deletions Sources/_TestingInternals/include/Stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,41 @@ static int swt_setfdflags(int fd, int flags) {
}
#endif

#if defined(__OpenBSD__)
/// Storage for the early working directory.
static _Atomic(const char *_Nullable) swt_earlyCWD = NULL;

/// At process start (before `main()` is called), capture the current working
/// directory.
///
/// This function is necessary on OpenBSD so that we can (as correctly as
/// possible) resolve the executable path when the first argument is a relative
/// path (which can occur when manually invoking the test executable.)
///
/// - Important: Do not call this function. It is automatically called by the
/// loader when the process starts. To get the early current working
/// directory, call ``swt_getEarlyCWD()`` instead.
static void swt_captureEarlyCWD(void) {
static char buffer[PATH_MAX * 2];
if (getcwd(buffer, sizeof(buffer))) {
// stdatomic.h is missing on OpenBSD 7.7, so use clang's builtins instead.
const char *expectingNULL = NULL;
__c11_atomic_store(&swt_earlyCWD, buffer, __ATOMIC_SEQ_CST);
}
}

/// Get the current working directory as it was set shortly after the process
/// started and before `main()` has been called.
///
/// This function is necessary on OpenBSD so that we can (as correctly as
/// possible) resolve the executable path when the first argument is a relative
/// path (which can occur when manually invoking the test executable.)
static const char *_Nullable swt_getEarlyCWD(void) {
// stdatomic.h is missing on OpenBSD 7.7, so use clang's builtins instead.
return __c11_atomic_load(&swt_earlyCWD, __ATOMIC_SEQ_CST);
}
#endif

SWT_ASSUME_NONNULL_END

#endif
8 changes: 8 additions & 0 deletions Tests/TestingTests/ExitTestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,14 @@ private import _TestingInternals
}
}
#endif

#if os(OpenBSD)
@Test("Changing the CWD doesn't break exit tests")
func changeCWD() async throws {
try #require(0 == chdir("/"))
await #expect(processExitsWith: .success) {}
}
#endif
}

// MARK: - Fixtures
Expand Down