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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-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
//
//===----------------------------------------------------------------------===//
///
/// This file tests `Workspace.rewriteToolsVersionSpecification(toDefaultManifestIn:specifying:fileSystem:)`.
///
import Basics
import PackageModel
@testable import Workspace
import XCTest
import class TSCBasic.InMemoryFileSystem
/// Test cases for `rewriteToolsVersionSpecification(toDefaultManifestIn:specifying:fileSystem:)`
class ToolsVersionSpecificationRewriterTests: XCTestCase {
/// Tests `rewriteToolsVersionSpecification(toDefaultManifestIn:specifying:fileSystem:)`.
func testNonVersionSpecificManifests() throws {
// Empty file.
rewriteToolsVersionSpecificationToDefaultManifest(content: "") { result in
XCTAssertEqual(result, "// swift-tools-version:4.1.2\n")
}
// File with just a new line.
rewriteToolsVersionSpecificationToDefaultManifest(content: "\n") { result in
XCTAssertEqual(result, "// swift-tools-version:4.1.2\n\n")
}
// File with some contents.
rewriteToolsVersionSpecificationToDefaultManifest(content: "let package = ... \n") { result in
XCTAssertEqual(result, "// swift-tools-version:4.1.2\nlet package = ... \n")
}
// File already having a valid version specifier.
let content = """
// swift-tools-version:3.1.2
...
"""
rewriteToolsVersionSpecificationToDefaultManifest(content: content) { result in
XCTAssertEqual(result, "// swift-tools-version:4.1.2\n...")
}
// Write a version with zero in patch number.
rewriteToolsVersionSpecificationToDefaultManifest(
content: """
// swift-tools-version:3.1.2
...
""",
version: ToolsVersion(version: "2.1.0")
) { result in
XCTAssertEqual(result, "// swift-tools-version:2.1\n...")
}
// Contents with invalid tools version specification (ignoring the validity of the version specifier).
rewriteToolsVersionSpecificationToDefaultManifest(
content: """
// swift-tool-version:3.1.2
...
"""
) { result in
XCTAssertEqual(result, "// swift-tools-version:4.1.2\n// swift-tool-version:3.1.2\n...")
}
// Contents with invalid version specifier.
rewriteToolsVersionSpecificationToDefaultManifest(
content: """
// swift-tools-version:3.1.2
...
"""
) { result in
XCTAssertEqual(result, "// swift-tools-version:4.1.2\n...")
}
// Contents with invalid version specifier and some meta data.
rewriteToolsVersionSpecificationToDefaultManifest(
content: """
// swift-tools-version:3.1.2
...
"""
) { result in
// Note: Right now we lose the metadata but if we ever start using it, we should preserve it.
XCTAssertEqual(result, "// swift-tools-version:4.1.2\n...")
}
// Try to write a version with prerelease and build meta data.
let toolsVersion = ToolsVersion(version: "4.1.2-alpha.beta+sha.1234")
rewriteToolsVersionSpecificationToDefaultManifest(
content: "let package = ... \n",
version: toolsVersion
) { result in
XCTAssertEqual(result, "// swift-tools-version:4.1.2\nlet package = ... \n")
}
}
func testManifestAccessFailures() throws {
let toolsVersion = ToolsVersion.v5_3
let inMemoryFileSystem = InMemoryFileSystem()
let manifestFilePath = AbsolutePath("/pkg/Package.swift/Package.swift")
try inMemoryFileSystem.createDirectory(manifestFilePath.parentDirectory, recursive: true) // /pkg/Package.swift/
// Test `ManifestAccessError.Kind.isADirectory`
XCTAssertThrowsError(
try ToolsVersionSpecificationWriter.rewriteSpecification(
manifestDirectory: manifestFilePath.parentDirectory.parentDirectory, // /pkg/
toolsVersion: toolsVersion,
fileSystem: inMemoryFileSystem
),
"'/pkg/Package.swift' is a directory, and an error should've been thrown"
) { error in
guard let error = error as? ToolsVersionSpecificationWriter.ManifestAccessError else {
XCTFail("a ManifestAccessError should've been thrown")
return
}
XCTAssertEqual(
error.kind,
.isADirectory
)
XCTAssertEqual(
error.description,
"no accessible Swift Package Manager manifest file found at '\(manifestFilePath.parentDirectory)'; the path is a directory; a file is expected" // /pkg/Package.swift/
)
}
// Test `ManifestAccessError.Kind.noSuchFileOrDirectory`
XCTAssertThrowsError(
try ToolsVersionSpecificationWriter.rewriteSpecification(
manifestDirectory: manifestFilePath.parentDirectory, // /pkg/Package.swift/
toolsVersion: toolsVersion,
fileSystem: inMemoryFileSystem
),
"'/pkg/Package.swift' is a directory, and an error should've been thrown"
) { error in
guard let error = error as? ToolsVersionSpecificationWriter.ManifestAccessError else {
XCTFail("a ManifestAccessError should've been thrown")
return
}
XCTAssertEqual(
error.kind,
.noSuchFileOrDirectory
)
XCTAssertEqual(
error.description,
"no accessible Swift Package Manager manifest file found at '\(manifestFilePath)'; a component of the path does not exist, or the path is an empty string" // /pkg/Package.swift/Package.swift
)
}
// TODO: Test `ManifestAccessError.Kind.unknown`
}
// Private functions are not run in tests.
private func testVersionSpecificManifests() throws {
// TODO: Add the functionality and tests for version-specific manifests too.
}
func testZeroedPatchVersion() {
XCTAssertEqual(ToolsVersion(version: "4.2.1").zeroedPatch.description, "4.2.0")
XCTAssertEqual(ToolsVersion(version: "4.2.0").zeroedPatch.description, "4.2.0")
XCTAssertEqual(ToolsVersion(version: "6.0.129").zeroedPatch.description, "6.0.0")
}
/// Does the boilerplate filesystem preparations, then calls `rewriteToolsVersionSpecification(toDefaultManifestIn:specifying:fileSystem:)`, for `testNonVersionSpecificManifests()`.
/// - Parameters:
/// - stream: The stream to read from and write to the filesystem.
/// - version: The Swift tools version to specify.
/// - resultHandler: The result handler.
func rewriteToolsVersionSpecificationToDefaultManifest(
content: String,
version: ToolsVersion = ToolsVersion(version: "4.1.2"),
resultHandler: (String) -> Void
) {
do {
let inMemoryFileSystem = InMemoryFileSystem()
let manifestFilePath = AbsolutePath("/pkg/Package.swift")
try inMemoryFileSystem.createDirectory(manifestFilePath.parentDirectory, recursive: true)
try inMemoryFileSystem.writeFileContents(manifestFilePath, string: content)
try ToolsVersionSpecificationWriter.rewriteSpecification(
manifestDirectory: manifestFilePath.parentDirectory,
toolsVersion: version,
fileSystem: inMemoryFileSystem
)
resultHandler(try inMemoryFileSystem.readFileContents(manifestFilePath))
} catch {
XCTFail("Failed with error \(error)")
}
}
}
|