Skip to content

Commit 9bb616d

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 d0d4ecb commit 9bb616d

26 files changed

+1708
-1385
lines changed

Package.swift

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

721721
.testTarget(
722722
name: "BasicsTests",
723-
dependencies: ["Basics", "_InternalTestSupport", "tsan_utils"],
723+
dependencies: [
724+
"Basics",
725+
"_InternalTestSupport",
726+
"tsan_utils",
727+
.product(name: "Numerics", package: "swift-numerics"),
728+
],
724729
exclude: [
725730
"Archiver/Inputs/archive.tar.gz",
726731
"Archiver/Inputs/archive.zip",
@@ -962,6 +967,8 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
962967
.package(url: "https://github.com/apple/swift-collections.git", "1.0.1" ..< "1.2.0"),
963968
.package(url: "https://github.com/apple/swift-certificates.git", "1.0.1" ..< "1.6.0"),
964969
.package(url: "https://github.com/swiftlang/swift-toolchain-sqlite.git", from: "1.0.0"),
970+
// Test Dependencies
971+
.package(url: "https://github.com/apple/swift-numerics", exact: "1.0.2")
965972
]
966973
} else {
967974
package.dependencies += [
@@ -974,5 +981,7 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
974981
.package(path: "../swift-collections"),
975982
.package(path: "../swift-certificates"),
976983
.package(path: "../swift-toolchain-sqlite"),
984+
// Test Dependencies
985+
.package(path: "../swift-numerics"),
977986
]
978987
}

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
@@ -43,8 +43,12 @@ public func XCTAssertEqual<T:Equatable, U:Equatable> (_ lhs:(T,U), _ rhs:(T,U),
4343
TSCTestSupport.XCTAssertEqual(lhs, rhs, file: file, line: line)
4444
}
4545

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

Tests/BasicsTests/Archiver/ZipArchiverTests.swift

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -104,56 +104,56 @@ final class ZipArchiverTests: XCTestCase {
104104
#endif
105105

106106
try await testWithTemporaryDirectory { tmpdir in
107-
let archiver = ZipArchiver(fileSystem: localFileSystem)
108-
109-
let rootDir = tmpdir.appending(component: UUID().uuidString)
110-
try localFileSystem.createDirectory(rootDir)
111-
try localFileSystem.writeFileContents(rootDir.appending("file1.txt"), string: "Hello World!")
112-
113-
let dir1 = rootDir.appending("dir1")
114-
try localFileSystem.createDirectory(dir1)
115-
try localFileSystem.writeFileContents(dir1.appending("file2.txt"), string: "Hello World 2!")
116-
117-
let dir2 = dir1.appending("dir2")
118-
try localFileSystem.createDirectory(dir2)
119-
try localFileSystem.writeFileContents(dir2.appending("file3.txt"), string: "Hello World 3!")
120-
try localFileSystem.writeFileContents(dir2.appending("file4.txt"), string: "Hello World 4!")
121-
122-
let archivePath = tmpdir.appending(component: UUID().uuidString + ".zip")
123-
try await archiver.compress(directory: rootDir, to: archivePath)
124-
XCTAssertFileExists(archivePath)
125-
126-
let extractRootDir = tmpdir.appending(component: UUID().uuidString)
127-
try localFileSystem.createDirectory(extractRootDir)
128-
try await archiver.extract(from: archivePath, to: extractRootDir)
129-
try localFileSystem.stripFirstLevel(of: extractRootDir)
130-
131-
XCTAssertFileExists(extractRootDir.appending("file1.txt"))
132-
XCTAssertEqual(
133-
try? localFileSystem.readFileContents(extractRootDir.appending("file1.txt")),
134-
"Hello World!"
135-
)
136-
137-
let extractedDir1 = extractRootDir.appending("dir1")
138-
XCTAssertDirectoryExists(extractedDir1)
139-
XCTAssertFileExists(extractedDir1.appending("file2.txt"))
140-
XCTAssertEqual(
141-
try? localFileSystem.readFileContents(extractedDir1.appending("file2.txt")),
142-
"Hello World 2!"
143-
)
144-
145-
let extractedDir2 = extractedDir1.appending("dir2")
146-
XCTAssertDirectoryExists(extractedDir2)
147-
XCTAssertFileExists(extractedDir2.appending("file3.txt"))
148-
XCTAssertEqual(
149-
try? localFileSystem.readFileContents(extractedDir2.appending("file3.txt")),
150-
"Hello World 3!"
151-
)
152-
XCTAssertFileExists(extractedDir2.appending("file4.txt"))
153-
XCTAssertEqual(
154-
try? localFileSystem.readFileContents(extractedDir2.appending("file4.txt")),
155-
"Hello World 4!"
156-
)
107+
let archiver = ZipArchiver(fileSystem: localFileSystem)
108+
109+
let rootDir = tmpdir.appending(component: UUID().uuidString)
110+
try localFileSystem.createDirectory(rootDir)
111+
try localFileSystem.writeFileContents(rootDir.appending("file1.txt"), string: "Hello World!")
112+
113+
let dir1 = rootDir.appending("dir1")
114+
try localFileSystem.createDirectory(dir1)
115+
try localFileSystem.writeFileContents(dir1.appending("file2.txt"), string: "Hello World 2!")
116+
117+
let dir2 = dir1.appending("dir2")
118+
try localFileSystem.createDirectory(dir2)
119+
try localFileSystem.writeFileContents(dir2.appending("file3.txt"), string: "Hello World 3!")
120+
try localFileSystem.writeFileContents(dir2.appending("file4.txt"), string: "Hello World 4!")
121+
122+
let archivePath = tmpdir.appending(component: UUID().uuidString + ".zip")
123+
try await archiver.compress(directory: rootDir, to: archivePath)
124+
XCTAssertFileExists(archivePath)
125+
126+
let extractRootDir = tmpdir.appending(component: UUID().uuidString)
127+
try localFileSystem.createDirectory(extractRootDir)
128+
try await archiver.extract(from: archivePath, to: extractRootDir)
129+
try localFileSystem.stripFirstLevel(of: extractRootDir)
130+
131+
XCTAssertFileExists(extractRootDir.appending("file1.txt"))
132+
XCTAssertEqual(
133+
try? localFileSystem.readFileContents(extractRootDir.appending("file1.txt")),
134+
"Hello World!"
135+
)
136+
137+
let extractedDir1 = extractRootDir.appending("dir1")
138+
XCTAssertDirectoryExists(extractedDir1)
139+
XCTAssertFileExists(extractedDir1.appending("file2.txt"))
140+
XCTAssertEqual(
141+
try? localFileSystem.readFileContents(extractedDir1.appending("file2.txt")),
142+
"Hello World 2!"
143+
)
144+
145+
let extractedDir2 = extractedDir1.appending("dir2")
146+
XCTAssertDirectoryExists(extractedDir2)
147+
XCTAssertFileExists(extractedDir2.appending("file3.txt"))
148+
XCTAssertEqual(
149+
try? localFileSystem.readFileContents(extractedDir2.appending("file3.txt")),
150+
"Hello World 3!"
151+
)
152+
XCTAssertFileExists(extractedDir2.appending("file4.txt"))
153+
XCTAssertEqual(
154+
try? localFileSystem.readFileContents(extractedDir2.appending("file4.txt")),
155+
"Hello World 4!"
156+
)
157157
}
158158
}
159159
}

0 commit comments

Comments
 (0)