Skip to content

Commit 947b33e

Browse files
committed
Fixes many tests (plus skips) to get more tests running on windows
#8606
1 parent 2b0505e commit 947b33e

30 files changed

+275
-201
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
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 = 260
12+
typealias Int = Int32
913
#endif
1014

1115
let cwd = getcwd(nil, Int(PATH_MAX))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
echo sentinel
2+
echo %*
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"debugger": { "path": "echo.bat" },
3+
"testRunner": { "path": "echo.bat" },
4+
"schemaVersion" : "1.0",
5+
"rootPath" : "."
6+
}

Fixtures/Miscellaneous/PluginGeneratedResources/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.7
1+
// swift-tools-version: 6.0
22

33
import PackageDescription
44

Fixtures/Miscellaneous/PluginGeneratedResources/Plugins/Generator/plugin.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import PackagePlugin
2+
import Foundation
23

34
#if os(Android)
45
let touchExe = "/system/bin/touch"
6+
let touchArgs: [String] = []
7+
#elseif os(Windows)
8+
let touchExe = "C:/Windows/System32/cmd.exe"
9+
let touchArgs = ["/c", "copy", "NUL"]
510
#else
611
let touchExe = "/usr/bin/touch"
12+
let touchArgs: [String] = []
713
#endif
814

915
@main
@@ -12,9 +18,9 @@ struct GeneratorPlugin: BuildToolPlugin {
1218
return [
1319
.prebuildCommand(
1420
displayName: "Generating empty file",
15-
executable: .init(touchExe),
16-
arguments: [context.pluginWorkDirectory.appending("best.txt")],
17-
outputFilesDirectory: context.pluginWorkDirectory
21+
executable: .init(fileURLWithPath: touchExe),
22+
arguments: touchArgs + [String(cString: (context.pluginWorkDirectoryURL.appending(path: "best.txt") as NSURL).fileSystemRepresentation)],
23+
outputFilesDirectory: context.pluginWorkDirectoryURL
1824
)
1925
]
2026
}

Package.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,13 @@ let package = Package(
946946
dependencies: ["XCBuildSupport", "_InternalTestSupport", "_InternalBuildTestSupport"],
947947
exclude: ["Inputs/Foo.pc"]
948948
),
949+
.testTarget(
950+
name: "FunctionalPerformanceTests",
951+
dependencies: [
952+
"swift-package-manager",
953+
"_InternalTestSupport",
954+
]
955+
),
949956
// Examples (These are built to ensure they stay up to date with the API.)
950957
.executableTarget(
951958
name: "package-info",
@@ -964,19 +971,6 @@ package.targets.append(contentsOf: [
964971
])
965972
#endif
966973

967-
// Workaround SwiftPM's attempt to link in executables which does not work on all
968-
// platforms.
969-
#if !os(Windows)
970-
package.targets.append(contentsOf: [
971-
.testTarget(
972-
name: "FunctionalPerformanceTests",
973-
dependencies: [
974-
"swift-package-manager",
975-
"_InternalTestSupport",
976-
]
977-
),
978-
])
979-
980974
// rdar://101868275 "error: cannot find 'XCTAssertEqual' in scope" can affect almost any functional test, so we flat out
981975
// disable them all until we know what is going on
982976
if ProcessInfo.processInfo.environment["SWIFTCI_DISABLE_SDK_DEPENDENT_TESTS"] == nil {
@@ -989,7 +983,6 @@ if ProcessInfo.processInfo.environment["SWIFTCI_DISABLE_SDK_DEPENDENT_TESTS"] ==
989983
"_InternalTestSupport",
990984
]
991985
),
992-
993986
.executableTarget(
994987
name: "dummy-swiftc",
995988
dependencies: [
@@ -1020,7 +1013,6 @@ if ProcessInfo.processInfo.environment["SWIFTCI_DISABLE_SDK_DEPENDENT_TESTS"] ==
10201013
),
10211014
])
10221015
}
1023-
#endif
10241016

10251017

10261018
func swiftSyntaxDependencies(_ names: [String]) -> [Target.Dependency] {

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/SwiftPMProduct.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ extension SwiftPM {
9191
packagePath: packagePath,
9292
env: env
9393
)
94-
95-
let stdout = try result.utf8Output()
96-
let stderr = try result.utf8stderrOutput()
94+
//Remove /r from stdout/stderr so that tests do not have to deal with them
95+
let stdout = try String(decoding: result.output.get().filter( { $0 != 13 }), as: Unicode.UTF8.self)
96+
let stderr = try String(decoding: result.stderrOutput.get().filter( { $0 != 13 }), as: Unicode.UTF8.self)
9797

9898
let returnValue = (stdout: stdout, stderr: stderr)
9999
if (!throwIfCommandFails) { return returnValue }

Sources/_InternalTestSupport/misc.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,20 @@ public func getNumberOfMatches(of match: String, in value: String) -> Int {
559559
guard match.count != 0 else { return 0 }
560560
return value.ranges(of: match).count
561561
}
562+
563+
public extension String {
564+
var withSwiftLineEnding: String {
565+
return replacingOccurrences(of: "\r\n", with: "\n")
566+
}
567+
}
568+
569+
public func executableName(_ name: String) -> String {
570+
#if os(Windows)
571+
if name.count > 4, name.suffix(from: name.index(name.endIndex, offsetBy: -4)) == ProcessInfo.exeSuffix {
572+
return name
573+
}
574+
return "\(name)\(ProcessInfo.exeSuffix)"
575+
#else
576+
return name
577+
#endif
578+
}

Tests/BasicsTests/AsyncProcessTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,13 @@ final class AsyncProcessTests: XCTestCase {
144144
}
145145

146146
func testFindExecutable() throws {
147-
try XCTSkipOnWindows(because: "https://github.com/swiftlang/swift-package-manager/issues/8547: Assertion failure when trying to find ls executable")
148-
149147
try testWithTemporaryDirectory { tmpdir in
150148
// This process should always work.
149+
#if os(Windows)
150+
XCTAssertTrue(AsyncProcess.findExecutable("cmd.exe") != nil)
151+
#else
151152
XCTAssertTrue(AsyncProcess.findExecutable("ls") != nil)
153+
#endif
152154

153155
XCTAssertEqual(AsyncProcess.findExecutable("nonExistantProgram"), nil)
154156
XCTAssertEqual(AsyncProcess.findExecutable(""), nil)

Tests/BasicsTests/FileSystem/VFSTests.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import Testing
1717

1818
import struct TSCBasic.ByteString
1919

20-
import _InternalTestSupport // for XCTSkipOnWindows
21-
2220
func testWithTemporaryDirectory(
2321
function: StaticString = #function,
2422
body: @escaping (AbsolutePath) async throws -> Void

Tests/CommandsTests/BuildCommandTests.swift

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class BuildCommandTestCases: CommandsBuildProviderTestCase {
8585

8686

8787
if cleanAfterward {
88-
try! await executeSwiftPackage(
88+
try await executeSwiftPackage(
8989
packagePath,
9090
extraArgs: ["clean"],
9191
buildSystem: buildSystemProvider
@@ -100,7 +100,7 @@ class BuildCommandTestCases: CommandsBuildProviderTestCase {
100100
)
101101
} catch {
102102
if cleanAfterward {
103-
try! await executeSwiftPackage(
103+
try await executeSwiftPackage(
104104
packagePath,
105105
extraArgs: ["clean"],
106106
buildSystem: buildSystemProvider
@@ -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: "https://github.com/swiftlang/swift-package-manager/issues/8658, 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

@@ -541,6 +543,8 @@ class BuildCommandTestCases: CommandsBuildProviderTestCase {
541543
}
542544

543545
func testSwiftGetVersion() async throws {
546+
try XCTSkipOnWindows(because: "https://github.com/swiftlang/swift-package-manager/issues/8659, SWIFT_EXEC override is not working")
547+
544548
try await fixture(name: "Miscellaneous/Simple") { fixturePath in
545549
func findSwiftGetVersionFile() throws -> AbsolutePath {
546550
let buildArenaPath = fixturePath.appending(components: ".build", "debug")
@@ -678,7 +682,7 @@ class BuildCommandTestCases: CommandsBuildProviderTestCase {
678682
}
679683
}
680684

681-
#if !canImport(Darwin)
685+
#if os(Linux)
682686
func testIgnoresLinuxMain() async throws {
683687
try await fixture(name: "Miscellaneous/TestDiscovery/IgnoresLinuxMain") { fixturePath in
684688
let buildResult = try await self.build(["-v", "--build-tests", "--enable-test-discovery"], packagePath: fixturePath, cleanAfterward: false)
@@ -774,15 +778,10 @@ class BuildCommandNativeTests: BuildCommandTestCases {
774778
components: ".build",
775779
UserToolchain.default.targetTriple.platformBuildPathComponent
776780
)
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-
)
781+
let debugPath = try await self.execute(["--show-bin-path"], packagePath: fullPath).stdout.trimmingCharacters(in: .whitespacesAndNewlines)
782+
XCTAssertEqual(AbsolutePath(debugPath).pathString, targetPath.appending("debug").pathString)
783+
let releasePath = try await self.execute(["-c", "release", "--show-bin-path"], packagePath: fullPath).stdout.trimmingCharacters(in: .whitespacesAndNewlines)
784+
XCTAssertEqual(AbsolutePath(releasePath).pathString, targetPath.appending("release").pathString)
786785
}
787786
}
788787
}
@@ -885,9 +884,9 @@ class BuildCommandSwiftBuildTests: BuildCommandTestCases {
885884
UserToolchain.default.targetTriple.platformBuildPathComponent
886885
)
887886
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"))
887+
XCTAssertMatch(AbsolutePath(debugPath).pathString, .regex(targetPath.appending(components: "Products", "Debug").escapedPathString + "(\\-linux|\\-Windows)?"))
889888
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"))
889+
XCTAssertMatch(AbsolutePath(releasePath).pathString, .regex(targetPath.appending(components: "Products", "Release").escapedPathString + "(\\-linux|\\-Windows)?"))
891890
}
892891
}
893892

@@ -923,7 +922,7 @@ class BuildCommandSwiftBuildTests: BuildCommandTestCases {
923922
throw XCTSkip("SWBINTTODO: Test fails because of a difference in the build layout. This needs to be updated to the expected path")
924923
}
925924

926-
#if !canImport(Darwin)
925+
#if os(Linux)
927926
override func testIgnoresLinuxMain() async throws {
928927
throw XCTSkip("SWBINTTODO: Swift build doesn't currently ignore Linux main when linking on Linux. This needs further investigation.")
929928
}

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)