Skip to content

Tests: Convert some tests BasicsTests to Swift Testing #8093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,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 @@ -1028,6 +1033,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 @@ -1040,6 +1047,8 @@ 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"),
]
}

Expand Down
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 @@ -44,8 +44,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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idiomatic way to do this with SwiftTesting would be a trait, something like this:

extension Trait where Self == Testing.ConditionTrait {
    static var skipInCI: Self {
        disabled("the test is being run on CI", ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil || ProcessInfo.processInfo.environment["CI"] != nil)
    }
}

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
58 changes: 29 additions & 29 deletions Tests/BasicsTests/Archiver/ZipArchiverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,44 +105,44 @@ final class ZipArchiverTests: XCTestCase {
#endif

try await testWithTemporaryDirectory { tmpdir in
let archiver = ZipArchiver(fileSystem: localFileSystem)
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 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 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!")
try localFileSystem.createSymbolicLink(dir2.appending("file5.txt"), pointingAt: dir1.appending("file2.txt"), relative: true)

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 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)
Expand Down
Loading