Skip to content

Fix up tests for Windows #5074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 21, 2024
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ if let environmentPath = Context.environment["ZLIB_LIBRARY_PATH"] {
var libxmlLinkFlags: [LinkerSetting] = [
.linkedLibrary("libxml2s.lib", .when(platforms: [.windows]))
]
if let environmentPath = Context.environment["LIBXML2_LIBRARY_PATH"] {
if let environmentPath = Context.environment["LIBXML_LIBRARY_PATH"] {
libxmlLinkFlags.append(.unsafeFlags([
"-L\(environmentPath)"
]))
Expand Down
12 changes: 9 additions & 3 deletions Sources/Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ let validPathSeps: [Character] = ["/"]
#endif

public func NSTemporaryDirectory() -> String {
FileManager.default.temporaryDirectory.path()
FileManager.default.temporaryDirectory.withUnsafeFileSystemRepresentation {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was necessary to ensure that paths with drive letters do not have a leading slash (the file system representation standardizes that)

String(cString: $0!)
} + String(validPathSeps[0])
}

extension String {
Expand Down Expand Up @@ -614,12 +616,16 @@ public func NSSearchPathForDirectoriesInDomains(_ directory: FileManager.SearchP
}

public func NSHomeDirectory() -> String {
FileManager.default.homeDirectoryForCurrentUser.path
FileManager.default.homeDirectoryForCurrentUser.withUnsafeFileSystemRepresentation {
String(cString: $0!)
}
}

public func NSHomeDirectoryForUser(_ user: String?) -> String? {
guard let user else { return NSHomeDirectory() }
return FileManager.default.homeDirectory(forUser: user)?.path
return FileManager.default.homeDirectory(forUser: user)?.withUnsafeFileSystemRepresentation {
String(cString: $0!)
}
}

public func NSUserName() -> String {
Expand Down
26 changes: 20 additions & 6 deletions Tests/Foundation/TestBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ class BundlePlayground {
#endif
}
}

var fileNameSuffix: String? {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe Foundation's tests have been run in debug mode before, so this test was failing when Foundation was built in debug mode and this fixes it by adding the appropriate file suffix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct! We always build in release mode currently due to other limitations that I would like to see lifted.

#if os(Windows) && DEBUG
"_debug"
#else
nil
#endif
}

var flatPathExtension: String? {
#if os(Windows)
Expand Down Expand Up @@ -216,7 +224,7 @@ class BundlePlayground {

// Make a main and an auxiliary executable:
self.mainExecutableURL = bundleURL
.appendingPathComponent(bundleName)
.appendingPathComponent(bundleName + (executableType.fileNameSuffix ?? ""))

if let ext = executableType.flatPathExtension {
self.mainExecutableURL.appendPathExtension(ext)
Expand All @@ -227,7 +235,7 @@ class BundlePlayground {
}

var auxiliaryExecutableURL = bundleURL
.appendingPathComponent(auxiliaryExecutableName)
.appendingPathComponent(auxiliaryExecutableName + (executableType.fileNameSuffix ?? ""))

if let ext = executableType.flatPathExtension {
auxiliaryExecutableURL.appendPathExtension(ext)
Expand Down Expand Up @@ -270,7 +278,7 @@ class BundlePlayground {
// Make a main and an auxiliary executable:
self.mainExecutableURL = temporaryDirectory
.appendingPathComponent(executableType.fhsPrefix)
.appendingPathComponent(executableType.nonFlatFilePrefix + bundleName)
.appendingPathComponent(executableType.nonFlatFilePrefix + bundleName + (executableType.fileNameSuffix ?? ""))

if let ext = executableType.pathExtension {
self.mainExecutableURL.appendPathExtension(ext)
Expand All @@ -280,7 +288,7 @@ class BundlePlayground {
let executablesDirectory = temporaryDirectory.appendingPathComponent("libexec").appendingPathComponent("\(bundleName).executables")
try FileManager.default.createDirectory(atPath: executablesDirectory.path, withIntermediateDirectories: true, attributes: nil)
var auxiliaryExecutableURL = executablesDirectory
.appendingPathComponent(executableType.nonFlatFilePrefix + auxiliaryExecutableName)
.appendingPathComponent(executableType.nonFlatFilePrefix + auxiliaryExecutableName + (executableType.fileNameSuffix ?? ""))

if let ext = executableType.pathExtension {
auxiliaryExecutableURL.appendPathExtension(ext)
Expand Down Expand Up @@ -317,7 +325,7 @@ class BundlePlayground {

// Make a main executable:
self.mainExecutableURL = temporaryDirectory
.appendingPathComponent(executableType.nonFlatFilePrefix + bundleName)
.appendingPathComponent(executableType.nonFlatFilePrefix + bundleName + (executableType.fileNameSuffix ?? ""))

if let ext = executableType.pathExtension {
self.mainExecutableURL.appendPathExtension(ext)
Expand All @@ -330,7 +338,7 @@ class BundlePlayground {

// Make an auxiliary executable:
var auxiliaryExecutableURL = resourcesDirectory
.appendingPathComponent(executableType.nonFlatFilePrefix + auxiliaryExecutableName)
.appendingPathComponent(executableType.nonFlatFilePrefix + auxiliaryExecutableName + (executableType.fileNameSuffix ?? ""))
if let ext = executableType.pathExtension {
auxiliaryExecutableURL.appendPathExtension(ext)
}
Expand Down Expand Up @@ -516,11 +524,14 @@ class TestBundle : XCTestCase {
XCTFail("should not fail to load")
}

// This causes a dialog box to appear on Windows which will suspend the tests, so skip testing this on Windows for now
#if !os(Windows)
// Executable cannot be located
try! _withEachPlaygroundLayout { (playground) in
let bundle = Bundle(path: playground.bundlePath)
XCTAssertThrowsError(try bundle!.loadAndReturnError())
}
#endif
}

func test_bundleWithInvalidPath() {
Expand All @@ -531,12 +542,15 @@ class TestBundle : XCTestCase {
func test_bundlePreflight() {
XCTAssertNoThrow(try testBundle(executable: true).preflight())

// Windows DLL bundles are always executable
#if !os(Windows)
try! _withEachPlaygroundLayout { (playground) in
let bundle = Bundle(path: playground.bundlePath)!

// Must throw as the main executable is a dummy empty file.
XCTAssertThrowsError(try bundle.preflight())
}
#endif
}

func test_bundleFindExecutable() {
Expand Down
Loading