Skip to content

Commit 7e995b7

Browse files
committed
Convert some tests BasicsTests to Swift Testing
Convert some BasicsTests from XCTest to Swift Testing to make use of parallelism and, in some cases, test parameterization. Not all Test Suites in BasicsTests have been converted as some use helpers in swift-tools-core-support, which don't have a matching swift testing helper.
1 parent cb4ba47 commit 7e995b7

26 files changed

+1714
-1382
lines changed

Package.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,12 @@ let package = Package(
788788

789789
.testTarget(
790790
name: "BasicsTests",
791-
dependencies: ["Basics", "_InternalTestSupport", "tsan_utils"],
791+
dependencies: [
792+
"Basics",
793+
"_InternalTestSupport",
794+
"tsan_utils",
795+
.product(name: "Numerics", package: "swift-numerics"),
796+
],
792797
exclude: [
793798
"Archiver/Inputs/archive.tar.gz",
794799
"Archiver/Inputs/archive.zip",
@@ -1028,6 +1033,8 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
10281033
.package(url: "https://github.com/apple/swift-collections.git", "1.0.1" ..< "1.2.0"),
10291034
.package(url: "https://github.com/apple/swift-certificates.git", "1.0.1" ..< "1.6.0"),
10301035
.package(url: "https://github.com/swiftlang/swift-toolchain-sqlite.git", from: "1.0.0"),
1036+
// Test Dependencies
1037+
.package(url: "https://github.com/apple/swift-numerics", exact: "1.0.2")
10311038
]
10321039
} else {
10331040
package.dependencies += [
@@ -1040,6 +1047,8 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
10401047
.package(path: "../swift-collections"),
10411048
.package(path: "../swift-certificates"),
10421049
.package(path: "../swift-toolchain-sqlite"),
1050+
// Test Dependencies
1051+
.package(path: "../swift-numerics"),
10431052
]
10441053
}
10451054

Sources/_InternalTestSupport/Observability.swift

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import Basics
1414
import func XCTest.XCTAssertTrue
1515
import func XCTest.XCTAssertEqual
1616
import func XCTest.XCTFail
17-
1817
import struct TSCBasic.StringError
1918

2019
import TSCTestSupport
20+
import Testing
2121

2222
extension ObservabilitySystem {
2323
public static func makeForTesting(verbose: Bool = true) -> TestingObservability {
@@ -139,6 +139,39 @@ public func testDiagnostics(
139139
}
140140
}
141141

142+
143+
public func expectDiagnostics(
144+
_ diagnostics: [Basics.Diagnostic],
145+
problemsOnly: Bool = true,
146+
sourceLocation: SourceLocation = #_sourceLocation,
147+
handler: (DiagnosticsTestResult) throws -> Void
148+
) throws {
149+
try expectDiagnostics(
150+
diagnostics,
151+
minSeverity: problemsOnly ? .warning : .debug,
152+
sourceLocation: sourceLocation,
153+
handler: handler
154+
)
155+
}
156+
157+
158+
public func expectDiagnostics(
159+
_ diagnostics: [Basics.Diagnostic],
160+
minSeverity: Basics.Diagnostic.Severity,
161+
sourceLocation: SourceLocation = #_sourceLocation,
162+
handler: (DiagnosticsTestResult) throws -> Void
163+
) throws {
164+
let diagnostics = diagnostics.filter { $0.severity >= minSeverity }
165+
let testResult = DiagnosticsTestResult(diagnostics)
166+
167+
try handler(testResult)
168+
169+
if !testResult.uncheckedDiagnostics.isEmpty {
170+
Issue.record("unchecked diagnostics \(testResult.uncheckedDiagnostics)", sourceLocation: sourceLocation)
171+
}
172+
}
173+
174+
142175
public func testPartialDiagnostics(
143176
_ diagnostics: [Basics.Diagnostic],
144177
minSeverity: Basics.Diagnostic.Severity,

Sources/_InternalTestSupport/XCTAssertHelpers.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ public func XCTAssertEqual<T:Equatable, U:Equatable> (_ lhs:(T,U), _ rhs:(T,U),
4444
TSCTestSupport.XCTAssertEqual(lhs, rhs, file: file, line: line)
4545
}
4646

47+
public func isRunninginCI(file: StaticString = #filePath, line: UInt = #line) -> Bool {
48+
return (ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil || ProcessInfo.processInfo.environment["CI"] != nil)
49+
}
50+
4751
public func XCTSkipIfCI(file: StaticString = #filePath, line: UInt = #line) throws {
48-
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil {
52+
if isRunninginCI() {
4953
throw XCTSkip("Skipping because the test is being run on CI", file: file, line: line)
5054
}
5155
}

Tests/BasicsTests/Archiver/ZipArchiverTests.swift

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -105,44 +105,44 @@ final class ZipArchiverTests: XCTestCase {
105105
#endif
106106

107107
try await testWithTemporaryDirectory { tmpdir in
108-
let archiver = ZipArchiver(fileSystem: localFileSystem)
108+
let archiver = ZipArchiver(fileSystem: localFileSystem)
109109

110-
let rootDir = tmpdir.appending(component: UUID().uuidString)
111-
try localFileSystem.createDirectory(rootDir)
112-
try localFileSystem.writeFileContents(rootDir.appending("file1.txt"), string: "Hello World!")
110+
let rootDir = tmpdir.appending(component: UUID().uuidString)
111+
try localFileSystem.createDirectory(rootDir)
112+
try localFileSystem.writeFileContents(rootDir.appending("file1.txt"), string: "Hello World!")
113113

114-
let dir1 = rootDir.appending("dir1")
115-
try localFileSystem.createDirectory(dir1)
116-
try localFileSystem.writeFileContents(dir1.appending("file2.txt"), string: "Hello World 2!")
114+
let dir1 = rootDir.appending("dir1")
115+
try localFileSystem.createDirectory(dir1)
116+
try localFileSystem.writeFileContents(dir1.appending("file2.txt"), string: "Hello World 2!")
117117

118118
let dir2 = dir1.appending("dir2")
119119
try localFileSystem.createDirectory(dir2)
120120
try localFileSystem.writeFileContents(dir2.appending("file3.txt"), string: "Hello World 3!")
121121
try localFileSystem.writeFileContents(dir2.appending("file4.txt"), string: "Hello World 4!")
122122
try localFileSystem.createSymbolicLink(dir2.appending("file5.txt"), pointingAt: dir1.appending("file2.txt"), relative: true)
123123

124-
let archivePath = tmpdir.appending(component: UUID().uuidString + ".zip")
125-
try await archiver.compress(directory: rootDir, to: archivePath)
126-
XCTAssertFileExists(archivePath)
127-
128-
let extractRootDir = tmpdir.appending(component: UUID().uuidString)
129-
try localFileSystem.createDirectory(extractRootDir)
130-
try await archiver.extract(from: archivePath, to: extractRootDir)
131-
try localFileSystem.stripFirstLevel(of: extractRootDir)
132-
133-
XCTAssertFileExists(extractRootDir.appending("file1.txt"))
134-
XCTAssertEqual(
135-
try? localFileSystem.readFileContents(extractRootDir.appending("file1.txt")),
136-
"Hello World!"
137-
)
138-
139-
let extractedDir1 = extractRootDir.appending("dir1")
140-
XCTAssertDirectoryExists(extractedDir1)
141-
XCTAssertFileExists(extractedDir1.appending("file2.txt"))
142-
XCTAssertEqual(
143-
try? localFileSystem.readFileContents(extractedDir1.appending("file2.txt")),
144-
"Hello World 2!"
145-
)
124+
let archivePath = tmpdir.appending(component: UUID().uuidString + ".zip")
125+
try await archiver.compress(directory: rootDir, to: archivePath)
126+
XCTAssertFileExists(archivePath)
127+
128+
let extractRootDir = tmpdir.appending(component: UUID().uuidString)
129+
try localFileSystem.createDirectory(extractRootDir)
130+
try await archiver.extract(from: archivePath, to: extractRootDir)
131+
try localFileSystem.stripFirstLevel(of: extractRootDir)
132+
133+
XCTAssertFileExists(extractRootDir.appending("file1.txt"))
134+
XCTAssertEqual(
135+
try? localFileSystem.readFileContents(extractRootDir.appending("file1.txt")),
136+
"Hello World!"
137+
)
138+
139+
let extractedDir1 = extractRootDir.appending("dir1")
140+
XCTAssertDirectoryExists(extractedDir1)
141+
XCTAssertFileExists(extractedDir1.appending("file2.txt"))
142+
XCTAssertEqual(
143+
try? localFileSystem.readFileContents(extractedDir1.appending("file2.txt")),
144+
"Hello World 2!"
145+
)
146146

147147
let extractedDir2 = extractedDir1.appending("dir2")
148148
XCTAssertDirectoryExists(extractedDir2)

0 commit comments

Comments
 (0)