Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 9 additions & 3 deletions Sources/Testing/Support/Additions/CommandLineAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,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
1 change: 1 addition & 0 deletions Sources/_TestingInternals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include(GitCommit)
include(TargetTriple)
add_library(_TestingInternals STATIC
Discovery.cpp
ExecutablePath.cpp
Versions.cpp
WillThrow.cpp)
target_include_directories(_TestingInternals PUBLIC
Expand Down
35 changes: 35 additions & 0 deletions Sources/_TestingInternals/ExecutablePath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#include "ExecutablePath.h"

#include <atomic>

#if defined(__OpenBSD__)
static std::atomic<const char *> earlyCWD { nullptr };

/// 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.)
__attribute__((__constructor__(101), __used__))
static void swt_captureEarlyCWD(void) {
static char buffer[PATH_MAX * 2];
if (getcwd(buffer, sizeof(buffer))) {
earlyCWD.store(buffer);
}
}

const char *swt_getEarlyCWD(void) {
return earlyCWD.load();
}
#endif
31 changes: 31 additions & 0 deletions Sources/_TestingInternals/include/ExecutablePath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if !defined(SWT_EXECUTABLE_PATH_H)
#define SWT_EXECUTABLE_PATH_H

#include "Defines.h"
#include "Includes.h"

SWT_ASSUME_NONNULL_BEGIN

#if defined(__OpenBSD__)
/// 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.)
SWT_EXTERN const char *_Nullable swt_getEarlyCWD(void);
#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