2
2
//
3
3
// This source file is part of the Swift open source project
4
4
//
5
- // Copyright (c) 2021 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2021-2025 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See http://swift.org/LICENSE.txt for license information
9
9
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
10
//
11
11
//===----------------------------------------------------------------------===//
12
+ import Foundation
12
13
13
14
@testable import Basics
14
15
import _InternalTestSupport
15
16
import tsan_utils
16
- import XCTest
17
+ import Testing
17
18
18
- final class SQLiteBackedCacheTests : XCTestCase {
19
- func testHappyCase( ) throws {
19
+ struct SQLiteBackedCacheTests {
20
+ @Test
21
+ func happyCase( ) throws {
20
22
try testWithTemporaryDirectory { tmpPath in
21
23
let path = tmpPath. appending ( " test.db " )
22
24
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
+ }
24
30
25
31
let mockData = try makeMockData ( fileSystem: localFileSystem, rootPath: tmpPath)
26
32
try mockData. forEach { key, value in
@@ -29,38 +35,42 @@ final class SQLiteBackedCacheTests: XCTestCase {
29
35
30
36
try mockData. forEach { key, _ in
31
37
let result = try cache. get ( key: key)
32
- XCTAssertEqual ( mockData [ key] , result)
38
+ #expect ( mockData [ key] == result)
33
39
}
34
40
35
41
let key = mockData. first!. key
36
42
37
43
_ = 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 " )
39
45
40
46
_ = 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 " )
42
48
43
49
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 " )
45
51
46
52
guard case . path( let cachePath) = cache. location else {
47
- return XCTFail ( " invalid location \( cache. location) " )
53
+ Issue . record ( " invalid location \( cache. location) " )
54
+ return
48
55
}
49
56
50
- XCTAssertTrue ( cache. fileSystem. exists ( cachePath) , " expected file to be written " )
57
+ #expect ( cache. fileSystem. exists ( cachePath) , " expected file to be written " )
51
58
}
52
59
}
53
60
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 {
60
66
try testWithTemporaryDirectory { tmpPath in
61
67
let path = tmpPath. appending ( " test.db " )
62
68
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
+ }
64
74
65
75
let mockData = try makeMockData ( fileSystem: localFileSystem, rootPath: tmpPath)
66
76
try mockData. forEach { key, value in
@@ -69,40 +79,49 @@ final class SQLiteBackedCacheTests: XCTestCase {
69
79
70
80
try mockData. forEach { key, _ in
71
81
let result = try cache. get ( key: key)
72
- XCTAssertEqual ( mockData [ key] , result)
82
+ #expect ( mockData [ key] == result)
73
83
}
74
84
75
85
guard case . path( let cachePath) = cache. location else {
76
- return XCTFail ( " invalid location \( cache. location) " )
86
+ Issue . record ( " invalid location \( cache. location) " )
87
+ return
77
88
}
78
89
79
- XCTAssertTrue ( cache. fileSystem. exists ( cachePath) , " expected file to exist at \( cachePath) " )
90
+ #expect ( cache. fileSystem. exists ( cachePath) , " expected file to exist at \( cachePath) " )
80
91
try cache. fileSystem. removeFileTree ( cachePath)
81
92
82
93
let key = mockData. first!. key
83
94
84
95
do {
85
96
let result = try cache. get ( key: key)
86
- XCTAssertNil ( result)
97
+ #expect ( result == nil )
87
98
}
88
99
89
100
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
+ }
91
104
let result = try cache. get ( key: key)
92
- XCTAssertEqual ( mockData [ key] , result)
105
+ #expect ( mockData [ key] == result)
93
106
}
94
107
95
- XCTAssertTrue ( cache. fileSystem. exists ( cachePath) , " expected file to exist at \( cachePath) " )
108
+ #expect ( cache. fileSystem. exists ( cachePath) , " expected file to exist at \( cachePath) " )
96
109
}
97
110
}
98
111
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 {
101
116
102
117
try testWithTemporaryDirectory { tmpPath in
103
118
let path = tmpPath. appending ( " test.db " )
104
119
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
+ }
106
125
107
126
let mockData = try makeMockData ( fileSystem: localFileSystem, rootPath: tmpPath)
108
127
try mockData. forEach { key, value in
@@ -111,36 +130,46 @@ final class SQLiteBackedCacheTests: XCTestCase {
111
130
112
131
try mockData. forEach { key, _ in
113
132
let result = try cache. get ( key: key)
114
- XCTAssertEqual ( mockData [ key] , result)
133
+ #expect ( mockData [ key] == result)
115
134
}
116
135
117
136
guard case . path( let cachePath) = cache. location else {
118
- return XCTFail ( " invalid location \( cache. location) " )
137
+ Issue . record ( " invalid location \( cache. location) " )
138
+ return
119
139
}
120
140
121
141
try cache. close ( )
122
142
123
- XCTAssertTrue ( cache. fileSystem. exists ( cachePath) , " expected file to exist at \( path) " )
143
+ #expect ( cache. fileSystem. exists ( cachePath) , " expected file to exist at \( path) " )
124
144
try cache. fileSystem. writeFileContents ( cachePath, string: " blah " )
125
145
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 " )
128
150
}
129
151
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 " )
132
156
}
133
157
}
134
158
}
135
159
136
- func testMaxSizeNotHandled( ) throws {
160
+ @Test
161
+ func maxSizeNotHandled( ) throws {
137
162
try testWithTemporaryDirectory { tmpPath in
138
163
let path = tmpPath. appending ( " test.db " )
139
164
var configuration = SQLiteBackedCacheConfiguration ( )
140
165
configuration. maxSizeInBytes = 1024 * 3
141
166
configuration. truncateWhenFull = false
142
167
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
+ }
144
173
145
174
func create( ) throws {
146
175
let mockData = try makeMockData ( fileSystem: localFileSystem, rootPath: tmpPath, count: 500 )
@@ -149,20 +178,28 @@ final class SQLiteBackedCacheTests: XCTestCase {
149
178
}
150
179
}
151
180
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
154
186
}
155
187
}
156
188
}
157
189
158
- func testMaxSizeHandled( ) throws {
190
+ @Test
191
+ func maxSizeHandled( ) throws {
159
192
try testWithTemporaryDirectory { tmpPath in
160
193
let path = tmpPath. appending ( " test.db " )
161
194
var configuration = SQLiteBackedCacheConfiguration ( )
162
195
configuration. maxSizeInBytes = 1024 * 3
163
196
configuration. truncateWhenFull = true
164
197
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
+ }
166
203
167
204
var keys = [ String] ( )
168
205
let mockData = try makeMockData ( fileSystem: localFileSystem, rootPath: tmpPath, count: 500 )
@@ -173,17 +210,18 @@ final class SQLiteBackedCacheTests: XCTestCase {
173
210
174
211
do {
175
212
let result = try cache. get ( key: mockData. first!. key)
176
- XCTAssertNil ( result)
213
+ #expect ( result == nil )
177
214
}
178
215
179
216
do {
180
217
let result = try cache. get ( key: keys. last!)
181
- XCTAssertEqual ( mockData [ keys. last!] , result)
218
+ #expect ( mockData [ keys. last!] == result)
182
219
}
183
220
}
184
221
}
185
222
186
- func testInitialFileCreation( ) throws {
223
+ @Test
224
+ func initialFileCreation( ) throws {
187
225
try testWithTemporaryDirectory { tmpPath in
188
226
let paths = [
189
227
tmpPath. appending ( " foo " , " test.db " ) ,
@@ -194,9 +232,13 @@ final class SQLiteBackedCacheTests: XCTestCase {
194
232
for path in paths {
195
233
let cache = SQLiteBackedCache < String > ( tableName: " SQLiteBackedCacheTest " , path: path)
196
234
// 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) " )
200
242
}
201
243
}
202
244
}
0 commit comments