|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +@testable import PackageLoading |
| 12 | +import PackageModel |
| 13 | +import TSCBasic |
| 14 | +import TSCTestSupport |
| 15 | +import TSCUtility |
| 16 | +import XCTest |
| 17 | + |
| 18 | +class ManifestLoaderSQLiteCacheTests: XCTestCase { |
| 19 | + func testHappyCase() throws { |
| 20 | + try testWithTemporaryDirectory { tmpPath in |
| 21 | + let path = tmpPath.appending(component: "test.db") |
| 22 | + let storage = SQLiteManifestCache(path: path) |
| 23 | + defer { XCTAssertNoThrow(try storage.close()) } |
| 24 | + |
| 25 | + |
| 26 | + let mockManifests = try makeMockManifests(fileSystem: localFileSystem, rootPath: tmpPath) |
| 27 | + try mockManifests.forEach { key, manifest in |
| 28 | + _ = try storage.put(key: key, manifest: manifest) |
| 29 | + } |
| 30 | + |
| 31 | + try mockManifests.forEach { key, manifest in |
| 32 | + let result = try storage.get(key: key) |
| 33 | + XCTAssertEqual(result?.parsedManifest, manifest.parsedManifest) |
| 34 | + } |
| 35 | + |
| 36 | + guard case .path(let storagePath) = storage.location else { |
| 37 | + return XCTFail("invalid location \(storage.location)") |
| 38 | + } |
| 39 | + |
| 40 | + XCTAssertTrue(storage.fileSystem.exists(storagePath), "expected file to be written") |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + func testFileDeleted() throws { |
| 45 | + try testWithTemporaryDirectory { tmpPath in |
| 46 | + let path = tmpPath.appending(component: "test.db") |
| 47 | + let storage = SQLiteManifestCache(path: path) |
| 48 | + defer { XCTAssertNoThrow(try storage.close()) } |
| 49 | + |
| 50 | + let mockManifests = try makeMockManifests(fileSystem: localFileSystem, rootPath: tmpPath) |
| 51 | + try mockManifests.forEach { key, manifest in |
| 52 | + _ = try storage.put(key: key, manifest: manifest) |
| 53 | + } |
| 54 | + |
| 55 | + try mockManifests.forEach { key, manifest in |
| 56 | + let result = try storage.get(key: key) |
| 57 | + XCTAssertEqual(result?.parsedManifest, manifest.parsedManifest) |
| 58 | + } |
| 59 | + |
| 60 | + guard case .path(let storagePath) = storage.location else { |
| 61 | + return XCTFail("invalid location \(storage.location)") |
| 62 | + } |
| 63 | + |
| 64 | + XCTAssertTrue(storage.fileSystem.exists(storagePath), "expected file to exist at \(storagePath)") |
| 65 | + try storage.fileSystem.removeFileTree(storagePath) |
| 66 | + |
| 67 | + do { |
| 68 | + let result = try storage.get(key: mockManifests.first!.key) |
| 69 | + XCTAssertNil(result) |
| 70 | + } |
| 71 | + |
| 72 | + do { |
| 73 | + XCTAssertNoThrow(try storage.put(key: mockManifests.first!.key, manifest: mockManifests.first!.value)) |
| 74 | + let result = try storage.get(key: mockManifests.first!.key) |
| 75 | + XCTAssertEqual(result?.parsedManifest, mockManifests.first!.value.parsedManifest) |
| 76 | + } |
| 77 | + |
| 78 | + XCTAssertTrue(storage.fileSystem.exists(storagePath), "expected file to exist at \(storagePath)") |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + func testFileCorrupt() throws { |
| 83 | + try testWithTemporaryDirectory { tmpPath in |
| 84 | + let path = tmpPath.appending(component: "test.db") |
| 85 | + let storage = SQLiteManifestCache(path: path) |
| 86 | + defer { XCTAssertNoThrow(try storage.close()) } |
| 87 | + |
| 88 | + let mockManifests = try makeMockManifests(fileSystem: localFileSystem, rootPath: tmpPath) |
| 89 | + try mockManifests.forEach { key, manifest in |
| 90 | + _ = try storage.put(key: key, manifest: manifest) |
| 91 | + } |
| 92 | + |
| 93 | + try mockManifests.forEach { key, manifest in |
| 94 | + let result = try storage.get(key: key) |
| 95 | + XCTAssertEqual(result?.parsedManifest, manifest.parsedManifest) |
| 96 | + } |
| 97 | + |
| 98 | + guard case .path(let storagePath) = storage.location else { |
| 99 | + return XCTFail("invalid location \(storage.location)") |
| 100 | + } |
| 101 | + |
| 102 | + try storage.close() |
| 103 | + |
| 104 | + XCTAssertTrue(storage.fileSystem.exists(storagePath), "expected file to exist at \(path)") |
| 105 | + try storage.fileSystem.writeFileContents(storagePath, bytes: ByteString("blah".utf8)) |
| 106 | + |
| 107 | + XCTAssertThrowsError(try storage.get(key: mockManifests.first!.key), "expected error", { error in |
| 108 | + XCTAssert("\(error)".contains("is not a database"), "Expected file is not a database error") |
| 109 | + }) |
| 110 | + |
| 111 | + XCTAssertThrowsError(try storage.put(key: mockManifests.first!.key, manifest: mockManifests.first!.value), "expected error", { error in |
| 112 | + XCTAssert("\(error)".contains("is not a database"), "Expected file is not a database error") |
| 113 | + }) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + func testMaxSizeNotHandled() throws { |
| 118 | + try testWithTemporaryDirectory { tmpPath in |
| 119 | + let path = tmpPath.appending(component: "test.db") |
| 120 | + var configuration = SQLiteManifestCache.Configuration() |
| 121 | + configuration.maxSizeInBytes = 1024 * 3 |
| 122 | + configuration.truncateWhenFull = false |
| 123 | + let storage = SQLiteManifestCache(location: .path(path), configuration: configuration) |
| 124 | + defer { XCTAssertNoThrow(try storage.close()) } |
| 125 | + |
| 126 | + func create() throws { |
| 127 | + let mockManifests = try makeMockManifests(fileSystem: localFileSystem, rootPath: tmpPath, count: 50) |
| 128 | + try mockManifests.forEach { key, manifest in |
| 129 | + _ = try storage.put(key: key, manifest: manifest) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + XCTAssertThrowsError(try create(), "expected error", { error in |
| 134 | + XCTAssertEqual(error as? SQLite.Errors, .databaseFull, "Expected 'databaseFull' error") |
| 135 | + }) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + func testMaxSizeHandled() throws { |
| 140 | + try testWithTemporaryDirectory { tmpPath in |
| 141 | + let path = tmpPath.appending(component: "test.db") |
| 142 | + var configuration = SQLiteManifestCache.Configuration() |
| 143 | + configuration.maxSizeInBytes = 1024 * 3 |
| 144 | + configuration.truncateWhenFull = true |
| 145 | + let storage = SQLiteManifestCache(location: .path(path), configuration: configuration) |
| 146 | + defer { XCTAssertNoThrow(try storage.close()) } |
| 147 | + |
| 148 | + var keys = [ManifestLoader.ManifestCacheKey]() |
| 149 | + let mockManifests = try makeMockManifests(fileSystem: localFileSystem, rootPath: tmpPath, count: 50) |
| 150 | + try mockManifests.forEach { key, manifest in |
| 151 | + _ = try storage.put(key: key, manifest: manifest) |
| 152 | + keys.append(key) |
| 153 | + } |
| 154 | + |
| 155 | + do { |
| 156 | + let result = try storage.get(key: mockManifests.first!.key) |
| 157 | + XCTAssertNil(result) |
| 158 | + } |
| 159 | + |
| 160 | + do { |
| 161 | + let result = try storage.get(key: keys.last!) |
| 162 | + XCTAssertEqual(result?.parsedManifest, mockManifests[keys.last!]?.parsedManifest) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +fileprivate func makeMockManifests(fileSystem: FileSystem, rootPath: AbsolutePath, count: Int = Int.random(in: 50 ..< 100)) throws -> [ManifestLoader.ManifestCacheKey: ManifestLoader.ManifestParseResult] { |
| 169 | + var manifests = [ManifestLoader.ManifestCacheKey: ManifestLoader.ManifestParseResult]() |
| 170 | + for index in 0 ..< count { |
| 171 | + let manifestPath = rootPath.appending(components: "\(index)", "Package.swift") |
| 172 | + try fileSystem.writeFileContents(manifestPath) { stream in |
| 173 | + stream <<< """ |
| 174 | + import PackageDescription |
| 175 | + let package = Package( |
| 176 | + name: "Trivial-\(index)", |
| 177 | + targets: [ |
| 178 | + .target( |
| 179 | + name: "foo-\(index)", |
| 180 | + dependencies: []), |
| 181 | +
|
| 182 | + ) |
| 183 | + """ |
| 184 | + } |
| 185 | + let key = try ManifestLoader.ManifestCacheKey(packageIdentity: PackageIdentity.init(path: manifestPath), |
| 186 | + manifestPath: manifestPath, |
| 187 | + toolsVersion: ToolsVersion.currentToolsVersion, |
| 188 | + env: [:], |
| 189 | + swiftpmVersion: Versioning.currentVersion.displayString, |
| 190 | + fileSystem: fileSystem) |
| 191 | + manifests[key] = ManifestLoader.ManifestParseResult(compilerOutput: "mock-output-\(index)", |
| 192 | + parsedManifest: "{ 'name': 'mock-manifest-\(index)' }") |
| 193 | + } |
| 194 | + |
| 195 | + return manifests |
| 196 | +} |
| 197 | + |
0 commit comments