1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
@testable import PackageCollections
import SPMTestSupport
import tsan_utils
import XCTest
class PackageCollectionsStorageTests: XCTestCase {
func testHappyCase() async throws {
try await testWithTemporaryDirectory { tmpPath in
let path = tmpPath.appending("test.db")
let storage = SQLitePackageCollectionsStorage(path: path)
defer { XCTAssertNoThrow(try storage.close()) }
let mockSources = makeMockSources()
for source in mockSources {
await XCTAssertAsyncThrowsError(try await storage.get(identifier: .init(from: source)), "expected error", { error in
XCTAssert(error is NotFoundError, "Expected NotFoundError")
})
}
let mockCollections = makeMockCollections(count: 50)
for collection in mockCollections {
_ = try await storage.put(collection: collection)
}
for collection in mockCollections {
let retVal = try await storage.get(identifier: collection.identifier)
XCTAssertEqual(retVal.identifier, collection.identifier)
}
do {
let list = try await storage.list()
XCTAssertEqual(list.count, mockCollections.count)
}
do {
let count = Int.random(in: 1 ..< mockCollections.count)
let list = try await storage.list(identifiers: mockCollections.prefix(count).map { $0.identifier })
XCTAssertEqual(list.count, count)
}
do {
_ = try await storage.remove(identifier: mockCollections.first!.identifier)
let list = try await storage.list()
XCTAssertEqual(list.count, mockCollections.count - 1)
}
await XCTAssertAsyncThrowsError(try await storage.get(identifier: mockCollections.first!.identifier), "expected error", { error in
XCTAssert(error is NotFoundError, "Expected NotFoundError")
})
guard case .path(let storagePath) = storage.location else {
return XCTFail("invalid location \(storage.location)")
}
XCTAssertTrue(storage.fileSystem.exists(storagePath), "expected file to be written")
}
}
func testFileDeleted() async throws {
#if os(Windows)
try XCTSkipIf(true, "open files cannot be deleted on Windows")
#endif
try XCTSkipIf(is_tsan_enabled())
try await testWithTemporaryDirectory { tmpPath in
let path = tmpPath.appending("test.db")
let storage = SQLitePackageCollectionsStorage(path: path)
defer { XCTAssertNoThrow(try storage.close()) }
let mockCollections = makeMockCollections(count: 3)
for collection in mockCollections {
_ = try await storage.put(collection: collection)
}
for collection in mockCollections {
let retVal = try await storage.get(identifier: collection.identifier)
XCTAssertEqual(retVal.identifier, collection.identifier)
}
guard case .path(let storagePath) = storage.location else {
return XCTFail("invalid location \(storage.location)")
}
XCTAssertTrue(storage.fileSystem.exists(storagePath), "expected file to exist at \(storagePath)")
try storage.fileSystem.removeFileTree(storagePath)
storage.resetCache()
await XCTAssertAsyncThrowsError(try await storage.get(identifier: mockCollections.first!.identifier), "expected error", { error in
XCTAssert(error is NotFoundError, "Expected NotFoundError")
})
_ = try await storage.put(collection: mockCollections.first!)
let retVal = try await storage.get(identifier: mockCollections.first!.identifier)
XCTAssertEqual(retVal.identifier, mockCollections.first!.identifier)
XCTAssertTrue(storage.fileSystem.exists(storagePath), "expected file to exist at \(storagePath)")
}
}
func testFileCorrupt() async throws {
#if os(Windows)
try XCTSkipIf(true, "open files cannot be deleted on Windows")
#endif
try XCTSkipIf(is_tsan_enabled())
try await testWithTemporaryDirectory { tmpPath in
let path = tmpPath.appending("test.db")
let storage = SQLitePackageCollectionsStorage(path: path)
defer { XCTAssertNoThrow(try storage.close()) }
let mockCollections = makeMockCollections(count: 3)
for collection in mockCollections {
_ = try await storage.put(collection: collection)
}
for collection in mockCollections {
let retVal = try await storage.get(identifier: collection.identifier)
XCTAssertEqual(retVal.identifier, collection.identifier)
}
guard case .path(let storagePath) = storage.location else {
return XCTFail("invalid location \(storage.location)")
}
try storage.close()
XCTAssertTrue(storage.fileSystem.exists(storagePath), "expected file to exist at \(path)")
try storage.fileSystem.writeFileContents(storagePath, string: "blah")
let storage2 = SQLitePackageCollectionsStorage(path: path)
defer { XCTAssertNoThrow(try storage2.close()) }
await XCTAssertAsyncThrowsError(try await storage2.get(identifier: mockCollections.first!.identifier), "expected error", { error in
XCTAssert("\(error)".contains("is not a database"), "Expected file is not a database error")
})
await XCTAssertAsyncThrowsError(try await storage2.put(collection: mockCollections.first!), "expected error", { error in
XCTAssert("\(error)".contains("is not a database"), "Expected file is not a database error")
})
}
}
func testListLessThanBatch() async throws {
var configuration = SQLitePackageCollectionsStorage.Configuration()
configuration.batchSize = 10
let storage = SQLitePackageCollectionsStorage(location: .memory, configuration: configuration)
defer { XCTAssertNoThrow(try storage.close()) }
let count = configuration.batchSize / 2
let mockCollections = makeMockCollections(count: count)
for collection in mockCollections {
_ = try await storage.put(collection: collection)
}
let list = try await storage.list()
XCTAssertEqual(list.count, mockCollections.count)
}
func testListNonBatching() async throws {
var configuration = SQLitePackageCollectionsStorage.Configuration()
configuration.batchSize = 10
let storage = SQLitePackageCollectionsStorage(location: .memory, configuration: configuration)
defer { XCTAssertNoThrow(try storage.close()) }
let count = Int(Double(configuration.batchSize) * 2.5)
let mockCollections = makeMockCollections(count: count)
for collection in mockCollections {
_ = try await storage.put(collection: collection)
}
let list = try await storage.list()
XCTAssertEqual(list.count, mockCollections.count)
}
func testListBatching() async throws {
var configuration = SQLitePackageCollectionsStorage.Configuration()
configuration.batchSize = 10
let storage = SQLitePackageCollectionsStorage(location: .memory, configuration: configuration)
defer { XCTAssertNoThrow(try storage.close()) }
let count = Int(Double(configuration.batchSize) * 2.5)
let mockCollections = makeMockCollections(count: count)
for collection in mockCollections {
_ = try await storage.put(collection: collection)
}
let list = try await storage.list(identifiers: mockCollections.map { $0.identifier })
XCTAssertEqual(list.count, mockCollections.count)
}
func testPutUpdates() async throws {
let storage = SQLitePackageCollectionsStorage(location: .memory)
defer { XCTAssertNoThrow(try storage.close()) }
let mockCollections = makeMockCollections(count: 3)
for collection in mockCollections {
_ = try await storage.put(collection: collection)
}
let list = try await storage.list(identifiers: mockCollections.map { $0.identifier })
XCTAssertEqual(list.count, mockCollections.count)
_ = try await storage.put(collection: mockCollections.last!)
XCTAssertEqual(list.count, mockCollections.count)
}
func testPopulateTargetTrie() async throws {
try await testWithTemporaryDirectory { tmpPath in
let path = tmpPath.appending("test.db")
let storage = SQLitePackageCollectionsStorage(path: path)
defer { XCTAssertNoThrow(try storage.close()) }
let mockCollections = makeMockCollections(count: 3)
for collection in mockCollections {
_ = try await storage.put(collection: collection)
}
let version = mockCollections.last!.packages.last!.versions.last!
let targetName = version.defaultManifest!.targets.last!.name
do {
let searchResult = try await storage.searchTargets(query: targetName, type: .exactMatch)
XCTAssert(searchResult.items.count > 0, "should get results")
}
// Create another instance, which should read existing data and populate target trie with it.
// Since we are not calling `storage2.put`, there is no other way for target trie to get populated.
let storage2 = SQLitePackageCollectionsStorage(path: path)
defer { XCTAssertNoThrow(try storage2.close()) }
// populateTargetTrie is called in `.init`; call it again explicitly so we know when it's finished
do {
try await storage2.populateTargetTrie()
let searchResult = try await storage2.searchTargets(query: targetName, type: .exactMatch)
XCTAssert(searchResult.items.count > 0, "should get results")
} catch {
// It's possible that some platforms don't have support FTS
XCTAssertEqual(false, storage2.useSearchIndices.get(), "populateTargetTrie should fail only if FTS is not available")
}
}
}
}
extension SQLitePackageCollectionsStorage {
convenience init(location: SQLite.Location? = nil, configuration: Configuration = .init()) {
self.init(location: location, configuration: configuration, observabilityScope: ObservabilitySystem.NOOP)
}
convenience init(path: AbsolutePath) {
self.init(location: .path(path), observabilityScope: ObservabilitySystem.NOOP)
}
}
|