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 Nov 13, 2024
1 parent 42aaad6 commit abfba54
Show file tree
Hide file tree
Showing 25 changed files with 1,613 additions and 1,295 deletions.
11 changes: 10 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,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 @@ -959,6 +964,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 @@ -971,5 +978,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"),
]
}
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 abfba54

Please sign in to comment.