Skip to content

Commit 1d8cb43

Browse files
committed
Fixes to many tests (plus skips) to get more tests running on windows
swiftlang#8606
1 parent 879236b commit 1d8cb43

22 files changed

+267
-178
lines changed
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
#if os(macOS) || os(iOS)
2-
import Darwin
3-
#elseif canImport(Glibc)
4-
import Glibc
5-
#elseif canImport(Musl)
6-
import Musl
7-
#elseif canImport(Bionic)
8-
import Bionic
9-
#endif
101

112
public extension Collection {
123
func shuffle() -> [Iterator.Element] {
@@ -24,15 +15,13 @@ public extension MutableCollection {
2415
guard c > 1 else { return }
2516

2617
for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
27-
#if os(macOS) || os(iOS)
28-
let d = arc4random_uniform(numericCast(unshuffledCount))
29-
#else
30-
let d = numericCast(random()) % unshuffledCount
31-
#endif
32-
let i = index(firstUnshuffled, offsetBy: numericCast(d))
18+
var g = SystemRandomNumberGenerator()
19+
let d = Int.random(in: 1...unshuffledCount, using: &g)
20+
let i = index(firstUnshuffled, offsetBy: d)
3321
swapAt(firstUnshuffled, i)
3422
}
3523
}
3624
}
3725

26+
3827
public let shuffle = false

Fixtures/Miscellaneous/EchoExecutable/Sources/secho/main.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import Musl
55
#elseif canImport(Android)
66
import Android
7-
#else
7+
#elseif canImport(Darwin.C)
88
import Darwin.C
9+
#elseif canImport(ucrt)
10+
import ucrt
11+
let PATH_MAX = MAX_PATH
912
#endif
1013

11-
let cwd = getcwd(nil, Int(PATH_MAX))
14+
let cwd = getcwd(nil, Int32(PATH_MAX))
1215
defer { free(cwd) }
1316
let workingDirectory = String(validatingUTF8: cwd!)!
1417
let values = [workingDirectory] + Array(CommandLine.arguments.dropFirst())

Sources/Build/LLBuildProgressTracker.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,14 @@ private struct CommandTaskTracker {
559559
}
560560

561561
private func progressText(of command: SPMLLBuild.Command, targetName: String?) -> String {
562+
#if os(Windows)
563+
let pathSep: Character = "\\"
564+
#else
565+
let pathSep: Character = "/"
566+
#endif
562567
// Transforms descriptions like "Linking ./.build/x86_64-apple-macosx/debug/foo" into "Linking foo".
563568
if let firstSpaceIndex = command.description.firstIndex(of: " "),
564-
let lastDirectorySeparatorIndex = command.description.lastIndex(of: "/")
569+
let lastDirectorySeparatorIndex = command.description.lastIndex(of: pathSep)
565570
{
566571
let action = command.description[..<firstSpaceIndex]
567572
let fileNameStartIndex = command.description.index(after: lastDirectorySeparatorIndex)

Sources/_InternalTestSupport/misc.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,25 @@ public func getNumberOfMatches(of match: String, in value: String) -> Int {
548548
guard match.count != 0 else { return 0 }
549549
return value.ranges(of: match).count
550550
}
551+
552+
public extension String {
553+
554+
var withPlatformNewLine: String {
555+
#if os(Windows)
556+
return replacingOccurrences(of: "\n", with: "\r\n")
557+
#else
558+
return self
559+
#endif
560+
}
561+
}
562+
563+
public func executableName(_ name: String) -> String {
564+
#if os(Windows)
565+
if name.count > 4, name.suffix(from: name.index(name.endIndex, offsetBy: -4)) == ".exe" {
566+
return name
567+
}
568+
return "\(name).exe"
569+
#else
570+
return name
571+
#endif
572+
}

Tests/BasicsTests/Archiver/TarArchiverTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ final class TarArchiverTests: XCTestCase {
2525
}
2626

2727
func testSuccess() async throws {
28+
try XCTSkipOnWindows(because: "requires tar")
2829
try await testWithTemporaryDirectory { tmpdir in
2930
let archiver = TarArchiver(fileSystem: localFileSystem)
3031
let inputArchivePath = AbsolutePath(#file).parentDirectory
@@ -64,6 +65,7 @@ final class TarArchiverTests: XCTestCase {
6465
}
6566

6667
func testInvalidArchive() async throws {
68+
try XCTSkipOnWindows(because: "requires tar")
6769
try await testWithTemporaryDirectory { tmpdir in
6870
let archiver = TarArchiver(fileSystem: localFileSystem)
6971
let inputArchivePath = AbsolutePath(#file).parentDirectory
@@ -79,6 +81,7 @@ final class TarArchiverTests: XCTestCase {
7981
}
8082

8183
func testValidation() async throws {
84+
try XCTSkipOnWindows(because: "requires tar")
8285
// valid
8386
try await testWithTemporaryDirectory { _ in
8487
let archiver = TarArchiver(fileSystem: localFileSystem)
@@ -104,6 +107,7 @@ final class TarArchiverTests: XCTestCase {
104107
}
105108

106109
func testCompress() async throws {
110+
try XCTSkipOnWindows(because: "requires tar")
107111
#if os(Linux)
108112
guard SPM_posix_spawn_file_actions_addchdir_np_supported() else {
109113
throw XCTSkip("working directory not supported on this platform")

Tests/BasicsTests/Archiver/UniversalArchiverTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ final class UniversalArchiverTests: XCTestCase {
3737
}
3838

3939
func testSuccess() async throws {
40+
try XCTSkipOnWindows(because: "requires tar")
4041
try await testWithTemporaryDirectory { tmpdir in
4142
let archiver = UniversalArchiver(localFileSystem)
4243
let inputArchivePath = AbsolutePath(#file).parentDirectory
@@ -85,6 +86,7 @@ final class UniversalArchiverTests: XCTestCase {
8586
}
8687

8788
func testInvalidArchive() async throws {
89+
try XCTSkipOnWindows(because: "requires tar")
8890
try await testWithTemporaryDirectory { tmpdir in
8991
let archiver = UniversalArchiver(localFileSystem)
9092
var inputArchivePath = AbsolutePath(#file).parentDirectory
@@ -110,6 +112,7 @@ final class UniversalArchiverTests: XCTestCase {
110112
}
111113

112114
func testValidation() async throws {
115+
try XCTSkipOnWindows(because: "requires tar")
113116
// valid
114117
try await testWithTemporaryDirectory { _ in
115118
let archiver = UniversalArchiver(localFileSystem)
@@ -135,6 +138,7 @@ final class UniversalArchiverTests: XCTestCase {
135138
}
136139

137140
func testCompress() async throws {
141+
try XCTSkipOnWindows(because: "requires tar")
138142
#if os(Linux)
139143
guard SPM_posix_spawn_file_actions_addchdir_np_supported() else {
140144
throw XCTSkip("working directory not supported on this platform")

Tests/BasicsTests/SandboxTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Darwin
2020

2121
final class SandboxTest: XCTestCase {
2222
func testSandboxOnAllPlatforms() throws {
23+
try XCTSkipOnWindows(because: "requires tar")
2324
try withTemporaryDirectory { path in
2425
#if os(Windows)
2526
let command = try Sandbox.apply(command: ["tar.exe", "-h"], strictness: .default, writableDirectories: [])

Tests/CommandsTests/BuildCommandTests.swift

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class BuildCommandTestCases: CommandsBuildProviderTestCase {
233233

234234
do {
235235
let result = try await build(["--product", "exec1"], packagePath: fullPath)
236-
XCTAssertMatch(result.binContents, ["exec1"])
236+
XCTAssertMatch(result.binContents, [.equal(executableName("exec1"))])
237237
XCTAssertNoMatch(result.binContents, ["exec2.build"])
238238
}
239239

@@ -306,6 +306,8 @@ class BuildCommandTestCases: CommandsBuildProviderTestCase {
306306
}
307307

308308
func testAtMainSupport() async throws {
309+
try XCTSkipOnWindows(because: "lld-link: error: undefined symbol: __declspec(dllimport) swift_addNewDSOImage, needs investigation")
310+
309311
try await fixture(name: "Miscellaneous/AtMainSupport") { fixturePath in
310312
let fullPath = try resolveSymlinks(fixturePath)
311313

@@ -316,12 +318,12 @@ class BuildCommandTestCases: CommandsBuildProviderTestCase {
316318

317319
do {
318320
let result = try await build(["--product", "SwiftExecSingleFile"], packagePath: fullPath)
319-
XCTAssertMatch(result.binContents, ["SwiftExecSingleFile"])
321+
XCTAssertMatch(result.binContents, [.equal(executableName("SwiftExecSingleFile"))])
320322
}
321323

322324
do {
323325
let result = try await build(["--product", "SwiftExecMultiFile"], packagePath: fullPath)
324-
XCTAssertMatch(result.binContents, ["SwiftExecMultiFile"])
326+
XCTAssertMatch(result.binContents, [.equal(executableName("SwiftExecMultiFile"))])
325327
}
326328
}
327329
}
@@ -343,8 +345,8 @@ class BuildCommandTestCases: CommandsBuildProviderTestCase {
343345
do {
344346
let result = try await build(["--product", "bexec"], packagePath: aPath)
345347
XCTAssertMatch(result.binContents, ["BTarget2.build"])
346-
XCTAssertMatch(result.binContents, ["bexec"])
347-
XCTAssertNoMatch(result.binContents, ["aexec"])
348+
XCTAssertMatch(result.binContents, [.equal(executableName("bexec"))])
349+
XCTAssertNoMatch(result.binContents, [.equal(executableName("aexec"))])
348350
XCTAssertNoMatch(result.binContents, ["ATarget.build"])
349351
XCTAssertNoMatch(result.binContents, ["BLibrary.a"])
350352

@@ -380,6 +382,8 @@ class BuildCommandTestCases: CommandsBuildProviderTestCase {
380382
}
381383

382384
func testAutomaticParseableInterfacesWithLibraryEvolution() async throws {
385+
try XCTSkipOnWindows(because: "swift-package-manager hangs, needs investigation")
386+
383387
try await fixture(name: "Miscellaneous/LibraryEvolution") { fixturePath in
384388
do {
385389
let result = try await build([], packagePath: fixturePath)
@@ -541,6 +545,8 @@ class BuildCommandTestCases: CommandsBuildProviderTestCase {
541545
}
542546

543547
func testSwiftGetVersion() async throws {
548+
try XCTSkipOnWindows(because: "SWIFT_EXEC override is not working, needs investigation")
549+
544550
try await fixture(name: "Miscellaneous/Simple") { fixturePath in
545551
func findSwiftGetVersionFile() throws -> AbsolutePath {
546552
let buildArenaPath = fixturePath.appending(components: ".build", "debug")
@@ -774,15 +780,10 @@ class BuildCommandNativeTests: BuildCommandTestCases {
774780
components: ".build",
775781
UserToolchain.default.targetTriple.platformBuildPathComponent
776782
)
777-
try await XCTAssertAsyncEqual(
778-
try await self.execute(["--show-bin-path"], packagePath: fullPath).stdout,
779-
"\(targetPath.appending("debug").pathString)\n"
780-
)
781-
try await XCTAssertAsyncEqual(
782-
try await self.execute(["-c", "release", "--show-bin-path"], packagePath: fullPath)
783-
.stdout,
784-
"\(targetPath.appending("release").pathString)\n"
785-
)
783+
let debugPath = try await self.execute(["--show-bin-path"], packagePath: fullPath).stdout.trimmingCharacters(in: .whitespacesAndNewlines)
784+
XCTAssertEqual(AbsolutePath(debugPath).pathString, targetPath.appending("debug").pathString)
785+
let releasePath = try await self.execute(["-c", "release", "--show-bin-path"], packagePath: fullPath).stdout.trimmingCharacters(in: .whitespacesAndNewlines)
786+
XCTAssertEqual(AbsolutePath(releasePath).pathString, targetPath.appending("release").pathString)
786787
}
787788
}
788789
}
@@ -861,6 +862,7 @@ class BuildCommandSwiftBuildTests: BuildCommandTestCases {
861862
override func testParseableInterfaces() async throws {
862863
try XCTSkipIfWorkingDirectoryUnsupported()
863864

865+
try XCTSkipOnWindows(because: "build errors, needs investigation")
864866
try await fixture(name: "Miscellaneous/ParseableInterfaces") { fixturePath in
865867
do {
866868
let result = try await build(["--enable-parseable-module-interfaces"], packagePath: fixturePath)
@@ -885,9 +887,9 @@ class BuildCommandSwiftBuildTests: BuildCommandTestCases {
885887
UserToolchain.default.targetTriple.platformBuildPathComponent
886888
)
887889
let debugPath = try await self.execute(["--show-bin-path"], packagePath: fullPath).stdout
888-
XCTAssertMatch(debugPath, .regex(targetPath.appending(components: "Products", "Debug").pathString + "(\\-linux|\\-Windows)?\\n"))
890+
XCTAssertMatch(AbsolutePath(debugPath).pathString, .regex(targetPath.appending(components: "Products", "Debug").escapedPathString + "(\\-linux|\\-Windows)?"))
889891
let releasePath = try await self.execute(["-c", "release", "--show-bin-path"], packagePath: fullPath).stdout
890-
XCTAssertMatch(releasePath, .regex(targetPath.appending(components: "Products", "Release").pathString + "(\\-linux|\\-Windows)?\\n"))
892+
XCTAssertMatch(AbsolutePath(releasePath).pathString, .regex(targetPath.appending(components: "Products", "Release").escapedPathString + "(\\-linux|\\-Windows)?"))
891893
}
892894
}
893895

@@ -942,6 +944,7 @@ class BuildCommandSwiftBuildTests: BuildCommandTestCases {
942944
override func testBuildSystemDefaultSettings() async throws {
943945
try XCTSkipIfWorkingDirectoryUnsupported()
944946

947+
try XCTSkipOnWindows(because: "build errors, needs investigation")
945948
if ProcessInfo.processInfo.environment["SWIFTPM_NO_SWBUILD_DEPENDENCY"] != nil {
946949
throw XCTSkip("SWIFTPM_NO_SWBUILD_DEPENDENCY is set so skipping because SwiftPM doesn't have the swift-build capability built inside.")
947950
}
@@ -952,6 +955,7 @@ class BuildCommandSwiftBuildTests: BuildCommandTestCases {
952955
override func testBuildCompleteMessage() async throws {
953956
try XCTSkipIfWorkingDirectoryUnsupported()
954957

958+
try XCTSkipOnWindows(because: "Build fails on windows, needs investigation")
955959
try await super.testBuildCompleteMessage()
956960
}
957961

Tests/CommandsTests/MultiRootSupportTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ final class MultiRootSupportTests: CommandsTestCase {
4242
let result = try XcodeWorkspaceLoader(fileSystem: fs, observabilityScope: observability.topScope).load(workspace: path)
4343

4444
XCTAssertNoDiagnostics(observability.diagnostics)
45-
XCTAssertEqual(result.map{ $0.pathString }.sorted(), ["/tmp/test/dep", "/tmp/test/local"])
45+
XCTAssertEqual(result.map{ $0.pathString }.sorted(), [AbsolutePath("/tmp/test/dep").pathString, AbsolutePath("/tmp/test/local").pathString])
4646
}
4747
}

0 commit comments

Comments
 (0)