Skip to content

Commit e5c210a

Browse files
authored
Tests: Convert more tests to Swift Testing (#8636)
Continue to test conversion to Swift Testing. - Tests/BasicsTests/HTTPClientTests.swift - Tests/BasicsTests/SQLiteBackedCacheTests.swift - Tests/BuildTests/BuildPlanTraversalTests.swift - Tests/BuildTests/LLBuildManifestBuilderTests.swift - Tests/BuildTests/SwiftCompilerOutputParserTests.swift - Tests/BuildTests/WindowsBuildPlanTests.swift - Tests/_AsyncFileSystemTests/AsyncFileSystemTests.swift
1 parent 6e60632 commit e5c210a

8 files changed

+370
-320
lines changed

Package.swift

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

825825
.testTarget(
826826
name: "BasicsTests",
827-
dependencies: ["Basics", "_InternalTestSupport", "tsan_utils"],
827+
dependencies: [
828+
"Basics",
829+
"_InternalTestSupport",
830+
"tsan_utils",
831+
.product(name: "Numerics", package: "swift-numerics"),
832+
],
828833
exclude: [
829834
"Archiver/Inputs/archive.tar.gz",
830835
"Archiver/Inputs/archive.zip",
@@ -1077,6 +1082,9 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
10771082
.package(url: "https://github.com/swiftlang/swift-toolchain-sqlite.git", from: "1.0.0"),
10781083
// For use in previewing documentation
10791084
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.1.0"),
1085+
1086+
// Test dependency
1087+
.package(url: "https://github.com/apple/swift-numerics.git", from: "1.0.3"),
10801088
]
10811089
} else {
10821090
package.dependencies += [
@@ -1089,6 +1097,7 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
10891097
.package(path: "../swift-collections"),
10901098
.package(path: "../swift-certificates"),
10911099
.package(path: "../swift-toolchain-sqlite"),
1100+
.package(path: "../swift-numerics"),
10921101
]
10931102
}
10941103

Tests/BasicsTests/HTTPClientTests.swift

Lines changed: 146 additions & 126 deletions
Large diffs are not rendered by default.

Tests/BasicsTests/SQLiteBackedCacheTests.swift

Lines changed: 89 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2021-2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
99
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
import Foundation
1213

1314
@testable import Basics
1415
import _InternalTestSupport
1516
import tsan_utils
16-
import XCTest
17+
import Testing
1718

18-
final class SQLiteBackedCacheTests: XCTestCase {
19-
func testHappyCase() throws {
19+
struct SQLiteBackedCacheTests {
20+
@Test
21+
func happyCase() throws {
2022
try testWithTemporaryDirectory { tmpPath in
2123
let path = tmpPath.appending("test.db")
2224
let cache = SQLiteBackedCache<String>(tableName: "SQLiteBackedCacheTest", path: path)
23-
defer { XCTAssertNoThrow(try cache.close()) }
25+
defer {
26+
#expect(throws: Never.self) {
27+
try cache.close()
28+
}
29+
}
2430

2531
let mockData = try makeMockData(fileSystem: localFileSystem, rootPath: tmpPath)
2632
try mockData.forEach { key, value in
@@ -29,38 +35,42 @@ final class SQLiteBackedCacheTests: XCTestCase {
2935

3036
try mockData.forEach { key, _ in
3137
let result = try cache.get(key: key)
32-
XCTAssertEqual(mockData[key], result)
38+
#expect(mockData[key] == result)
3339
}
3440

3541
let key = mockData.first!.key
3642

3743
_ = try cache.put(key: key, value: "foobar", replace: false)
38-
XCTAssertEqual(mockData[key], try cache.get(key: key))
44+
#expect(try cache.get(key: key) == mockData[key], "Actual is not as expected")
3945

4046
_ = try cache.put(key: key, value: "foobar", replace: true)
41-
XCTAssertEqual("foobar", try cache.get(key: key))
47+
#expect(try cache.get(key: key) == "foobar", "Actual is not as expected")
4248

4349
try cache.remove(key: key)
44-
XCTAssertNil(try cache.get(key: key))
50+
#expect(try cache.get(key: key) == nil, "Actual is not as expected")
4551

4652
guard case .path(let cachePath) = cache.location else {
47-
return XCTFail("invalid location \(cache.location)")
53+
Issue.record("invalid location \(cache.location)")
54+
return
4855
}
4956

50-
XCTAssertTrue(cache.fileSystem.exists(cachePath), "expected file to be written")
57+
#expect(cache.fileSystem.exists(cachePath), "expected file to be written")
5158
}
5259
}
5360

54-
func testFileDeleted() throws {
55-
#if os(Windows)
56-
try XCTSkipIf(true, "open file cannot be deleted on Windows")
57-
#endif
58-
try XCTSkipIf(is_tsan_enabled())
59-
61+
@Test(
62+
.disabled(if: (ProcessInfo.hostOperatingSystem == .windows), "open file cannot be deleted on Windows"),
63+
.disabled(if: is_tsan_enabled(), "Disabling as tsan is enabled")
64+
)
65+
func fileDeleted() throws {
6066
try testWithTemporaryDirectory { tmpPath in
6167
let path = tmpPath.appending("test.db")
6268
let cache = SQLiteBackedCache<String>(tableName: "SQLiteBackedCacheTest", path: path)
63-
defer { XCTAssertNoThrow(try cache.close()) }
69+
defer {
70+
#expect(throws: Never.self) {
71+
try cache.close()
72+
}
73+
}
6474

6575
let mockData = try makeMockData(fileSystem: localFileSystem, rootPath: tmpPath)
6676
try mockData.forEach { key, value in
@@ -69,40 +79,49 @@ final class SQLiteBackedCacheTests: XCTestCase {
6979

7080
try mockData.forEach { key, _ in
7181
let result = try cache.get(key: key)
72-
XCTAssertEqual(mockData[key], result)
82+
#expect(mockData[key] == result)
7383
}
7484

7585
guard case .path(let cachePath) = cache.location else {
76-
return XCTFail("invalid location \(cache.location)")
86+
Issue.record("invalid location \(cache.location)")
87+
return
7788
}
7889

79-
XCTAssertTrue(cache.fileSystem.exists(cachePath), "expected file to exist at \(cachePath)")
90+
#expect(cache.fileSystem.exists(cachePath), "expected file to exist at \(cachePath)")
8091
try cache.fileSystem.removeFileTree(cachePath)
8192

8293
let key = mockData.first!.key
8394

8495
do {
8596
let result = try cache.get(key: key)
86-
XCTAssertNil(result)
97+
#expect(result == nil)
8798
}
8899

89100
do {
90-
XCTAssertNoThrow(try cache.put(key: key, value: mockData[key]!))
101+
#expect(throws: Never.self) {
102+
try cache.put(key: key, value: mockData[key]!)
103+
}
91104
let result = try cache.get(key: key)
92-
XCTAssertEqual(mockData[key], result)
105+
#expect(mockData[key] == result)
93106
}
94107

95-
XCTAssertTrue(cache.fileSystem.exists(cachePath), "expected file to exist at \(cachePath)")
108+
#expect(cache.fileSystem.exists(cachePath), "expected file to exist at \(cachePath)")
96109
}
97110
}
98111

99-
func testFileCorrupt() throws {
100-
try XCTSkipIf(is_tsan_enabled())
112+
@Test(
113+
.disabled(if: is_tsan_enabled(), "Disabling as tsan is enabled")
114+
)
115+
func fileCorrupt() throws {
101116

102117
try testWithTemporaryDirectory { tmpPath in
103118
let path = tmpPath.appending("test.db")
104119
let cache = SQLiteBackedCache<String>(tableName: "SQLiteBackedCacheTest", path: path)
105-
defer { XCTAssertNoThrow(try cache.close()) }
120+
defer {
121+
#expect(throws: Never.self) {
122+
try cache.close()
123+
}
124+
}
106125

107126
let mockData = try makeMockData(fileSystem: localFileSystem, rootPath: tmpPath)
108127
try mockData.forEach { key, value in
@@ -111,36 +130,46 @@ final class SQLiteBackedCacheTests: XCTestCase {
111130

112131
try mockData.forEach { key, _ in
113132
let result = try cache.get(key: key)
114-
XCTAssertEqual(mockData[key], result)
133+
#expect(mockData[key] == result)
115134
}
116135

117136
guard case .path(let cachePath) = cache.location else {
118-
return XCTFail("invalid location \(cache.location)")
137+
Issue.record("invalid location \(cache.location)")
138+
return
119139
}
120140

121141
try cache.close()
122142

123-
XCTAssertTrue(cache.fileSystem.exists(cachePath), "expected file to exist at \(path)")
143+
#expect(cache.fileSystem.exists(cachePath), "expected file to exist at \(path)")
124144
try cache.fileSystem.writeFileContents(cachePath, string: "blah")
125145

126-
XCTAssertThrowsError(try cache.get(key: mockData.first!.key), "expected error") { error in
127-
XCTAssert("\(error)".contains("is not a database"), "Expected file is not a database error")
146+
#expect {
147+
try cache.get(key: mockData.first!.key)
148+
} throws: { error in
149+
return "\(error)".contains("is not a database")
128150
}
129151

130-
XCTAssertThrowsError(try cache.put(key: mockData.first!.key, value: mockData.first!.value), "expected error") { error in
131-
XCTAssert("\(error)".contains("is not a database"), "Expected file is not a database error")
152+
#expect {
153+
try cache.put(key: mockData.first!.key, value: mockData.first!.value)
154+
} throws: { error in
155+
return "\(error)".contains("is not a database")
132156
}
133157
}
134158
}
135159

136-
func testMaxSizeNotHandled() throws {
160+
@Test
161+
func maxSizeNotHandled() throws {
137162
try testWithTemporaryDirectory { tmpPath in
138163
let path = tmpPath.appending("test.db")
139164
var configuration = SQLiteBackedCacheConfiguration()
140165
configuration.maxSizeInBytes = 1024 * 3
141166
configuration.truncateWhenFull = false
142167
let cache = SQLiteBackedCache<String>(tableName: "SQLiteBackedCacheTest", path: path, configuration: configuration)
143-
defer { XCTAssertNoThrow(try cache.close()) }
168+
defer {
169+
#expect(throws: Never.self) {
170+
try cache.close()
171+
}
172+
}
144173

145174
func create() throws {
146175
let mockData = try makeMockData(fileSystem: localFileSystem, rootPath: tmpPath, count: 500)
@@ -149,20 +178,28 @@ final class SQLiteBackedCacheTests: XCTestCase {
149178
}
150179
}
151180

152-
XCTAssertThrowsError(try create(), "expected error") { error in
153-
XCTAssertEqual(error as? SQLite.Errors, .databaseFull, "Expected 'databaseFull' error")
181+
#expect {
182+
try create()
183+
} throws: { error in
184+
let error = try #require(error as? SQLite.Errors)
185+
return error == .databaseFull
154186
}
155187
}
156188
}
157189

158-
func testMaxSizeHandled() throws {
190+
@Test
191+
func maxSizeHandled() throws {
159192
try testWithTemporaryDirectory { tmpPath in
160193
let path = tmpPath.appending("test.db")
161194
var configuration = SQLiteBackedCacheConfiguration()
162195
configuration.maxSizeInBytes = 1024 * 3
163196
configuration.truncateWhenFull = true
164197
let cache = SQLiteBackedCache<String>(tableName: "SQLiteBackedCacheTest", path: path, configuration: configuration)
165-
defer { XCTAssertNoThrow(try cache.close()) }
198+
defer {
199+
#expect(throws: Never.self) {
200+
try cache.close()
201+
}
202+
}
166203

167204
var keys = [String]()
168205
let mockData = try makeMockData(fileSystem: localFileSystem, rootPath: tmpPath, count: 500)
@@ -173,17 +210,18 @@ final class SQLiteBackedCacheTests: XCTestCase {
173210

174211
do {
175212
let result = try cache.get(key: mockData.first!.key)
176-
XCTAssertNil(result)
213+
#expect(result == nil)
177214
}
178215

179216
do {
180217
let result = try cache.get(key: keys.last!)
181-
XCTAssertEqual(mockData[keys.last!], result)
218+
#expect(mockData[keys.last!] == result)
182219
}
183220
}
184221
}
185222

186-
func testInitialFileCreation() throws {
223+
@Test
224+
func initialFileCreation() throws {
187225
try testWithTemporaryDirectory { tmpPath in
188226
let paths = [
189227
tmpPath.appending("foo", "test.db"),
@@ -194,9 +232,13 @@ final class SQLiteBackedCacheTests: XCTestCase {
194232
for path in paths {
195233
let cache = SQLiteBackedCache<String>(tableName: "SQLiteBackedCacheTest", path: path)
196234
// Put an entry to ensure the file is created.
197-
XCTAssertNoThrow(try cache.put(key: "foo", value: "bar"))
198-
XCTAssertNoThrow(try cache.close())
199-
XCTAssertTrue(localFileSystem.exists(path), "expected file to be created at \(path)")
235+
#expect(throws: Never.self) {
236+
try cache.put(key: "foo", value: "bar")
237+
}
238+
#expect(throws: Never.self) {
239+
try cache.close()
240+
}
241+
#expect(localFileSystem.exists(path), "expected file to be created at \(path)")
200242
}
201243
}
202244
}

0 commit comments

Comments
 (0)