Skip to content

Commit 2e98598

Browse files
authored
Fixes many tests (plus skips) to get more tests running on windows (#8609)
Now that we can have executable dependencies on test targets for windows this change updates many tests that have not been building/running on windows. closes #8606 closes #8658 closes #8547
1 parent 02fe923 commit 2e98598

35 files changed

+305
-217
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/AtMainSupport/Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ let package = Package(
99
.executable(name: "SwiftExecMultiFile", targets: ["SwiftExecMultiFile"]),
1010
],
1111
targets: [
12-
.executableTarget(name: "ClangExecSingleFile"),
12+
.executableTarget(name: "ClangExecSingleFile",
13+
linkerSettings: [
14+
.linkedLibrary("swiftCore", .when(platforms: [.windows])), // for swift_addNewDSOImage
15+
]),
1316
.executableTarget(name: "SwiftExecSingleFile"),
1417
.executableTarget(name: "SwiftExecMultiFile"),
1518
]

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
}

Fixtures/Miscellaneous/TestableExe/Package.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@ let package = Package(
55
name: "TestableExe",
66
targets: [
77
.executableTarget(
8-
name: "TestableExe1"
8+
name: "TestableExe1",
9+
linkerSettings: [
10+
.linkedLibrary("swiftCore", .when(platforms: [.windows])), // for swift_addNewDSOImage
11+
]
912
),
1013
.executableTarget(
11-
name: "TestableExe2"
14+
name: "TestableExe2",
15+
linkerSettings: [
16+
.linkedLibrary("swiftCore", .when(platforms: [.windows])), // for swift_addNewDSOImage
17+
]
18+
1219
),
1320
.executableTarget(
14-
name: "TestableExe3"
21+
name: "TestableExe3",
22+
linkerSettings: [
23+
.linkedLibrary("swiftCore", .when(platforms: [.windows])), // for swift_addNewDSOImage
24+
]
1525
),
1626
.testTarget(
1727
name: "TestableExeTests",

Fixtures/Miscellaneous/TestableExe/Tests/TestableExeTests/TestableExeTests.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import XCTest
55
import class Foundation.Bundle
66

77
final class TestableExeTests: XCTestCase {
8+
9+
#if os(Windows)
10+
let eol = "\r\n"
11+
#else
12+
let eol = "\n"
13+
#endif
14+
815
func testExample() throws {
916
// This is an example of a functional test case.
1017
// Use XCTAssert and related functions to verify your tests produce the correct
@@ -30,7 +37,7 @@ final class TestableExeTests: XCTestCase {
3037
process.waitUntilExit()
3138
var data = pipe.fileHandleForReading.readDataToEndOfFile()
3239
var output = String(data: data, encoding: .utf8)
33-
XCTAssertEqual(output, "Hello, world!\n")
40+
XCTAssertEqual(output, "Hello, world!\(eol)")
3441

3542
execPath = productsDirectory.appendingPathComponent("TestableExe2")
3643
process = Process()
@@ -41,7 +48,7 @@ final class TestableExeTests: XCTestCase {
4148
process.waitUntilExit()
4249
data = pipe.fileHandleForReading.readDataToEndOfFile()
4350
output = String(data: data, encoding: .utf8)
44-
XCTAssertEqual(output, "Hello, planet!\n")
51+
XCTAssertEqual(output, "Hello, planet!\(eol)")
4552

4653
execPath = productsDirectory.appendingPathComponent("TestableExe3")
4754
process = Process()
@@ -52,7 +59,7 @@ final class TestableExeTests: XCTestCase {
5259
process.waitUntilExit()
5360
data = pipe.fileHandleForReading.readDataToEndOfFile()
5461
output = String(data: data, encoding: .utf8)
55-
XCTAssertEqual(output, "Hello, universe!\n")
62+
XCTAssertEqual(output, "Hello, universe!\(eol)")
5663
}
5764

5865
/// Returns path to the built products directory.

Package.swift

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

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

10241016

10251017
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/BuildTests/BuildPlanTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
621621

622622
func testPackageNameFlag() async throws {
623623
try XCTSkipIfPlatformCI() // test is disabled because it isn't stable, see rdar://118239206
624+
try XCTSkipOnWindows(because: "https://github.com/swiftlang/swift-package-manager/issues/8547: 'swift test' was hanging.")
624625
let isFlagSupportedInDriver = try DriverSupport.checkToolchainDriverFlags(
625626
flags: ["package-name"],
626627
toolchain: UserToolchain.default,
@@ -7279,9 +7280,7 @@ class BuildPlanSwiftBuildTests: BuildPlanTestCase {
72797280

72807281
override func testPackageNameFlag() async throws {
72817282
try XCTSkipIfWorkingDirectoryUnsupported()
7282-
#if os(Windows)
7283-
throw XCTSkip("Skip until there is a resolution to the partial linking with Windows that results in a 'subsystem must be defined' error.")
7284-
#endif
7283+
try XCTSkipOnWindows(because: "Skip until there is a resolution to the partial linking with Windows that results in a 'subsystem must be defined' error.")
72857284
#if os(Linux)
72867285
// Linking error: "/usr/bin/ld.gold: fatal error: -pie and -static are incompatible".
72877286
// Tracked by GitHub issue: https://github.com/swiftlang/swift-package-manager/issues/8499

0 commit comments

Comments
 (0)