Skip to content

Commit

Permalink
Convert some tests BasicsTests to Swift Testing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bkhouri committed Jan 15, 2025
1 parent 1d637b3 commit 7eebb04
Show file tree
Hide file tree
Showing 26 changed files with 1,708 additions and 1,385 deletions.
11 changes: 10 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,12 @@ let package = Package(

.testTarget(
name: "BasicsTests",
dependencies: ["Basics", "_InternalTestSupport", "tsan_utils"],
dependencies: [
"Basics",
"_InternalTestSupport",
"tsan_utils",
.product(name: "Numerics", package: "swift-numerics"),
],
exclude: [
"Archiver/Inputs/archive.tar.gz",
"Archiver/Inputs/archive.zip",
Expand Down Expand Up @@ -971,6 +976,8 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
.package(url: "https://github.com/apple/swift-collections.git", "1.0.1" ..< "1.2.0"),
.package(url: "https://github.com/apple/swift-certificates.git", "1.0.1" ..< "1.6.0"),
.package(url: "https://github.com/swiftlang/swift-toolchain-sqlite.git", from: "1.0.0"),
// Test Dependencies
.package(url: "https://github.com/apple/swift-numerics", exact: "1.0.2")
]
} else {
package.dependencies += [
Expand All @@ -983,5 +990,7 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
.package(path: "../swift-collections"),
.package(path: "../swift-certificates"),
.package(path: "../swift-toolchain-sqlite"),
// Test Dependencies
.package(path: "../swift-numerics"),
]
}
35 changes: 34 additions & 1 deletion Sources/_InternalTestSupport/Observability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import Basics
import func XCTest.XCTAssertTrue
import func XCTest.XCTAssertEqual
import func XCTest.XCTFail

import struct TSCBasic.StringError

import TSCTestSupport
import Testing

extension ObservabilitySystem {
public static func makeForTesting(verbose: Bool = true) -> TestingObservability {
Expand Down Expand Up @@ -139,6 +139,39 @@ public func testDiagnostics(
}
}


public func expectDiagnostics(
_ diagnostics: [Basics.Diagnostic],
problemsOnly: Bool = true,
sourceLocation: SourceLocation = #_sourceLocation,
handler: (DiagnosticsTestResult) throws -> Void
) throws {
try expectDiagnostics(
diagnostics,
minSeverity: problemsOnly ? .warning : .debug,
sourceLocation: sourceLocation,
handler: handler
)
}


public func expectDiagnostics(
_ diagnostics: [Basics.Diagnostic],
minSeverity: Basics.Diagnostic.Severity,
sourceLocation: SourceLocation = #_sourceLocation,
handler: (DiagnosticsTestResult) throws -> Void
) throws {
let diagnostics = diagnostics.filter { $0.severity >= minSeverity }
let testResult = DiagnosticsTestResult(diagnostics)

try handler(testResult)

if !testResult.uncheckedDiagnostics.isEmpty {
Issue.record("unchecked diagnostics \(testResult.uncheckedDiagnostics)", sourceLocation: sourceLocation)
}
}


public func testPartialDiagnostics(
_ diagnostics: [Basics.Diagnostic],
minSeverity: Basics.Diagnostic.Severity,
Expand Down
6 changes: 5 additions & 1 deletion Sources/_InternalTestSupport/XCTAssertHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ public func XCTAssertEqual<T:Equatable, U:Equatable> (_ lhs:(T,U), _ rhs:(T,U),
TSCTestSupport.XCTAssertEqual(lhs, rhs, file: file, line: line)
}

public func isRunninginCI(file: StaticString = #filePath, line: UInt = #line) -> Bool {
return (ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil || ProcessInfo.processInfo.environment["CI"] != nil)
}

public func XCTSkipIfCI(file: StaticString = #filePath, line: UInt = #line) throws {
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil {
if isRunninginCI() {
throw XCTSkip("Skipping because the test is being run on CI", file: file, line: line)
}
}
Expand Down
100 changes: 50 additions & 50 deletions Tests/BasicsTests/Archiver/ZipArchiverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,56 +104,56 @@ final class ZipArchiverTests: XCTestCase {
#endif

try await testWithTemporaryDirectory { tmpdir in
let archiver = ZipArchiver(fileSystem: localFileSystem)

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

let dir1 = rootDir.appending("dir1")
try localFileSystem.createDirectory(dir1)
try localFileSystem.writeFileContents(dir1.appending("file2.txt"), string: "Hello World 2!")

let dir2 = dir1.appending("dir2")
try localFileSystem.createDirectory(dir2)
try localFileSystem.writeFileContents(dir2.appending("file3.txt"), string: "Hello World 3!")
try localFileSystem.writeFileContents(dir2.appending("file4.txt"), string: "Hello World 4!")

let archivePath = tmpdir.appending(component: UUID().uuidString + ".zip")
try await archiver.compress(directory: rootDir, to: archivePath)
XCTAssertFileExists(archivePath)

let extractRootDir = tmpdir.appending(component: UUID().uuidString)
try localFileSystem.createDirectory(extractRootDir)
try await archiver.extract(from: archivePath, to: extractRootDir)
try localFileSystem.stripFirstLevel(of: extractRootDir)

XCTAssertFileExists(extractRootDir.appending("file1.txt"))
XCTAssertEqual(
try? localFileSystem.readFileContents(extractRootDir.appending("file1.txt")),
"Hello World!"
)

let extractedDir1 = extractRootDir.appending("dir1")
XCTAssertDirectoryExists(extractedDir1)
XCTAssertFileExists(extractedDir1.appending("file2.txt"))
XCTAssertEqual(
try? localFileSystem.readFileContents(extractedDir1.appending("file2.txt")),
"Hello World 2!"
)

let extractedDir2 = extractedDir1.appending("dir2")
XCTAssertDirectoryExists(extractedDir2)
XCTAssertFileExists(extractedDir2.appending("file3.txt"))
XCTAssertEqual(
try? localFileSystem.readFileContents(extractedDir2.appending("file3.txt")),
"Hello World 3!"
)
XCTAssertFileExists(extractedDir2.appending("file4.txt"))
XCTAssertEqual(
try? localFileSystem.readFileContents(extractedDir2.appending("file4.txt")),
"Hello World 4!"
)
let archiver = ZipArchiver(fileSystem: localFileSystem)

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

let dir1 = rootDir.appending("dir1")
try localFileSystem.createDirectory(dir1)
try localFileSystem.writeFileContents(dir1.appending("file2.txt"), string: "Hello World 2!")

let dir2 = dir1.appending("dir2")
try localFileSystem.createDirectory(dir2)
try localFileSystem.writeFileContents(dir2.appending("file3.txt"), string: "Hello World 3!")
try localFileSystem.writeFileContents(dir2.appending("file4.txt"), string: "Hello World 4!")

let archivePath = tmpdir.appending(component: UUID().uuidString + ".zip")
try await archiver.compress(directory: rootDir, to: archivePath)
XCTAssertFileExists(archivePath)

let extractRootDir = tmpdir.appending(component: UUID().uuidString)
try localFileSystem.createDirectory(extractRootDir)
try await archiver.extract(from: archivePath, to: extractRootDir)
try localFileSystem.stripFirstLevel(of: extractRootDir)

XCTAssertFileExists(extractRootDir.appending("file1.txt"))
XCTAssertEqual(
try? localFileSystem.readFileContents(extractRootDir.appending("file1.txt")),
"Hello World!"
)

let extractedDir1 = extractRootDir.appending("dir1")
XCTAssertDirectoryExists(extractedDir1)
XCTAssertFileExists(extractedDir1.appending("file2.txt"))
XCTAssertEqual(
try? localFileSystem.readFileContents(extractedDir1.appending("file2.txt")),
"Hello World 2!"
)

let extractedDir2 = extractedDir1.appending("dir2")
XCTAssertDirectoryExists(extractedDir2)
XCTAssertFileExists(extractedDir2.appending("file3.txt"))
XCTAssertEqual(
try? localFileSystem.readFileContents(extractedDir2.appending("file3.txt")),
"Hello World 3!"
)
XCTAssertFileExists(extractedDir2.appending("file4.txt"))
XCTAssertEqual(
try? localFileSystem.readFileContents(extractedDir2.appending("file4.txt")),
"Hello World 4!"
)
}
}
}
Loading

0 comments on commit 7eebb04

Please sign in to comment.