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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020 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 XCTest
import class TSCBasic.InMemoryFileSystem
final class PackageCollectionsSourcesStorageTest: XCTestCase {
func testHappyCase() async throws {
let mockFileSystem = InMemoryFileSystem()
let storage = FilePackageCollectionsSourcesStorage(fileSystem: mockFileSystem)
try await assertHappyCase(storage: storage)
let buffer = try mockFileSystem.readFileContents(storage.path)
XCTAssertNotEqual(buffer.count, 0, "expected file to be written")
print(buffer)
}
func testRealFile() async throws {
try await testWithTemporaryDirectory { tmpPath in
let fileSystem = localFileSystem
let path = tmpPath.appending("test.json")
let storage = FilePackageCollectionsSourcesStorage(fileSystem: fileSystem, path: path)
try await assertHappyCase(storage: storage)
let buffer = try fileSystem.readFileContents(storage.path)
XCTAssertNotEqual(buffer.count, 0, "expected file to be written")
print(buffer)
}
}
func assertHappyCase(storage: PackageCollectionsSourcesStorage) async throws {
let sources = makeMockSources()
for source in sources {
_ = try await storage.add(source: source, order: nil)
}
do {
let list = try await storage.list()
XCTAssertEqual(list.count, sources.count, "sources should match")
}
let remove = sources.enumerated().filter { index, _ in index % 2 == 0 }.map { $1 }
for source in remove {
_ = try await storage.remove(source: source)
}
do {
let list = try await storage.list()
XCTAssertEqual(list.count, sources.count - remove.count, "sources should match")
}
let remaining = sources.filter { !remove.contains($0) }
for source in sources {
try await XCTAssertAsyncTrue(try await storage.exists(source: source) == remaining.contains(source))
}
do {
_ = try await storage.move(source: remaining.last!, to: 0)
let list = try await storage.list()
XCTAssertEqual(list.count, remaining.count, "sources should match")
XCTAssertEqual(list.first, remaining.last, "item should match")
}
do {
_ = try await storage.move(source: remaining.last!, to: remaining.count - 1)
let list = try await storage.list()
XCTAssertEqual(list.count, remaining.count, "sources should match")
XCTAssertEqual(list.last, remaining.last, "item should match")
}
do {
let list = try await storage.list()
var source = list.first!
source.isTrusted = !(source.isTrusted ?? false)
_ = try await storage.update(source: source)
let listAfter = try await storage.list()
XCTAssertEqual(source.isTrusted, listAfter.first!.isTrusted, "isTrusted should match")
}
do {
let list = try await storage.list()
var source = list.first!
source.skipSignatureCheck = !source.skipSignatureCheck
_ = try await storage.update(source: source)
let listAfter = try await storage.list()
XCTAssertEqual(source.skipSignatureCheck, listAfter.first!.skipSignatureCheck, "skipSignatureCheck should match")
}
}
func testFileDeleted() async throws {
let mockFileSystem = InMemoryFileSystem()
let storage = FilePackageCollectionsSourcesStorage(fileSystem: mockFileSystem)
let sources = makeMockSources()
for source in sources {
_ = try await storage.add(source: source, order: nil)
}
do {
let list = try await storage.list()
XCTAssertEqual(list.count, sources.count, "collections should match")
}
try mockFileSystem.removeFileTree(storage.path)
XCTAssertFalse(mockFileSystem.exists(storage.path), "expected file to be deleted")
do {
let list = try await storage.list()
XCTAssertEqual(list.count, 0, "collections should match")
}
}
func testFileEmpty() async throws {
let mockFileSystem = InMemoryFileSystem()
let storage = FilePackageCollectionsSourcesStorage(fileSystem: mockFileSystem)
let sources = makeMockSources()
for source in sources {
_ = try await storage.add(source: source, order: nil)
}
do {
let list = try await storage.list()
XCTAssertEqual(list.count, sources.count, "collections should match")
}
try mockFileSystem.writeFileContents(storage.path, bytes: [])
let buffer = try mockFileSystem.readFileContents(storage.path)
XCTAssertEqual(buffer.count, 0, "expected file to be empty")
do {
let list = try await storage.list()
XCTAssertEqual(list.count, 0, "collections should match")
}
}
func testFileCorrupt() async throws {
let mockFileSystem = InMemoryFileSystem()
let storage = FilePackageCollectionsSourcesStorage(fileSystem: mockFileSystem)
let sources = makeMockSources()
for source in sources {
_ = try await storage.add(source: source, order: nil)
}
let list = try await storage.list()
XCTAssertEqual(list.count, sources.count, "collections should match")
try mockFileSystem.writeFileContents(storage.path, string: "{")
let buffer = try mockFileSystem.readFileContents(storage.path)
XCTAssertNotEqual(buffer.count, 0, "expected file to be written")
print(buffer)
await XCTAssertAsyncThrowsError(try await storage.list(), "expected an error", { error in
XCTAssert(error is DecodingError, "expected error to match")
})
}
}
|