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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 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
//
//===----------------------------------------------------------------------===//
@testable import Basics
@testable import PackageModel
import SPMTestSupport
import XCTest
import class TSCBasic.InMemoryFileSystem
private let usrBinTools = Dictionary(uniqueKeysWithValues: Toolset.KnownTool.allCases.map {
($0, try! AbsolutePath(validating: "/usr/bin/\($0.rawValue)"))
})
private let cCompilerOptions = ["-fopenmp"]
private let newCCompilerOptions = ["-pedantic"]
private let cxxCompilerOptions = ["-nostdinc++"]
private let compilersNoRoot = (
path: try! AbsolutePath(validating: "/tools/compilersNoRoot.json"),
json: #"""
{
"schemaVersion": "1.0",
"swiftCompiler": { "path": "\#(usrBinTools[.swiftCompiler]!)" },
"cCompiler": { "path": "\#(usrBinTools[.cCompiler]!)", "extraCLIOptions": \#(cCompilerOptions) },
"cxxCompiler": { "path": "\#(usrBinTools[.cxxCompiler]!)", "extraCLIOptions": \#(cxxCompilerOptions) },
}
"""# as SerializedJSON
)
private let noValidToolsNoRoot = (
path: try! AbsolutePath(validating: "/tools/noValidToolsNoRoot.json"),
json: #"""
{
"schemaVersion": "1.0",
"cCompiler": {}
}
"""# as SerializedJSON
)
private let unknownToolsNoRoot = (
path: try! AbsolutePath(validating: "/tools/unknownToolsNoRoot.json"),
json: #"""
{
"schemaVersion": "1.0",
"foo": {},
"bar": {}
}
"""# as SerializedJSON
)
private let otherToolsNoRoot = (
path: try! AbsolutePath(validating: "/tools/otherToolsNoRoot.json"),
json: #"""
{
"schemaVersion": "1.0",
"librarian": { "path": "\#(usrBinTools[.librarian]!)" },
"linker": { "path": "\#(usrBinTools[.linker]!)" },
"debugger": { "path": "\#(usrBinTools[.debugger]!)" }
}
"""# as SerializedJSON
)
private let someToolsWithRoot = (
path: try! AbsolutePath(validating: "/tools/someToolsWithRoot.json"),
json: #"""
{
"schemaVersion": "1.0",
"rootPath": "/custom",
"cCompiler": { "extraCLIOptions": \#(newCCompilerOptions) },
"linker": { "path": "ld" },
"librarian": { "path": "llvm-ar" },
"debugger": { "path": "\#(usrBinTools[.debugger]!)" }
}
"""# as SerializedJSON
)
private let someToolsWithRelativeRoot = (
path: try! AbsolutePath(validating: "/tools/someToolsWithRelativeRoot.json"),
json: #"""
{
"schemaVersion": "1.0",
"rootPath": "relative/custom",
"cCompiler": { "extraCLIOptions": \#(newCCompilerOptions) }
}
"""# as SerializedJSON
)
final class ToolsetTests: XCTestCase {
func testToolset() throws {
let fileSystem = InMemoryFileSystem()
try fileSystem.createDirectory(AbsolutePath(validating: "/tools"))
for testFile in [compilersNoRoot, noValidToolsNoRoot, unknownToolsNoRoot, otherToolsNoRoot, someToolsWithRoot, someToolsWithRelativeRoot] {
try fileSystem.writeFileContents(testFile.path, string: testFile.json.underlying)
}
let observability = ObservabilitySystem.makeForTesting()
let compilersToolset = try Toolset(from: compilersNoRoot.path, at: fileSystem, observability.topScope)
XCTAssertEqual(
compilersToolset.knownTools[.swiftCompiler],
Toolset.ToolProperties(path: usrBinTools[.swiftCompiler]!)
)
XCTAssertEqual(
compilersToolset.knownTools[.cCompiler],
Toolset.ToolProperties(path: usrBinTools[.cCompiler]!, extraCLIOptions: cCompilerOptions)
)
XCTAssertEqual(
compilersToolset.knownTools[.cxxCompiler],
Toolset.ToolProperties(path: usrBinTools[.cxxCompiler]!, extraCLIOptions: cxxCompilerOptions)
)
XCTAssertThrowsError(try Toolset(from: noValidToolsNoRoot.path, at: fileSystem, observability.topScope))
XCTAssertEqual(observability.errors.count, 1)
XCTAssertEqual(observability.warnings.count, 0)
let unknownToolsToolset = try Toolset(from: unknownToolsNoRoot.path, at: fileSystem, observability.topScope)
XCTAssertTrue(unknownToolsToolset.knownTools.isEmpty)
// +2 warnings for each unknown tool, no new errors
XCTAssertEqual(observability.errors.count, 1)
XCTAssertEqual(observability.warnings.count, 2)
var otherToolsToolset = try Toolset(from: otherToolsNoRoot.path, at: fileSystem, observability.topScope)
XCTAssertEqual(otherToolsToolset.knownTools.count, 3)
// no new warnings and errors were emitted
XCTAssertEqual(observability.errors.count, 1)
XCTAssertEqual(observability.warnings.count, 2)
otherToolsToolset.merge(with: compilersToolset)
XCTAssertEqual(
compilersToolset.knownTools[.swiftCompiler],
Toolset.ToolProperties(path: usrBinTools[.swiftCompiler]!)
)
XCTAssertEqual(
compilersToolset.knownTools[.cCompiler],
Toolset.ToolProperties(path: usrBinTools[.cCompiler]!, extraCLIOptions: cCompilerOptions)
)
XCTAssertEqual(
compilersToolset.knownTools[.cxxCompiler],
Toolset.ToolProperties(path: usrBinTools[.cxxCompiler]!, extraCLIOptions: cxxCompilerOptions)
)
XCTAssertEqual(
otherToolsToolset.knownTools[.librarian],
Toolset.ToolProperties(path: usrBinTools[.librarian]!)
)
XCTAssertEqual(
otherToolsToolset.knownTools[.linker],
Toolset.ToolProperties(path: usrBinTools[.linker]!)
)
XCTAssertEqual(
otherToolsToolset.knownTools[.debugger],
Toolset.ToolProperties(path: usrBinTools[.debugger]!)
)
let someToolsWithRoot = try Toolset(from: someToolsWithRoot.path, at: fileSystem, observability.topScope)
XCTAssertEqual(someToolsWithRoot.knownTools.count, 4)
// no new warnings and errors emitted
XCTAssertEqual(observability.errors.count, 1)
XCTAssertEqual(observability.warnings.count, 2)
otherToolsToolset.merge(with: someToolsWithRoot)
XCTAssertEqual(
otherToolsToolset.knownTools[.swiftCompiler],
Toolset.ToolProperties(path: usrBinTools[.swiftCompiler]!)
)
XCTAssertEqual(
otherToolsToolset.knownTools[.cCompiler],
Toolset.ToolProperties(
path: usrBinTools[.cCompiler]!,
extraCLIOptions: cCompilerOptions + newCCompilerOptions
)
)
XCTAssertEqual(
otherToolsToolset.knownTools[.cxxCompiler],
Toolset.ToolProperties(path: usrBinTools[.cxxCompiler]!, extraCLIOptions: cxxCompilerOptions)
)
XCTAssertEqual(
otherToolsToolset.knownTools[.librarian],
Toolset.ToolProperties(path: try! AbsolutePath(validating: "/custom/llvm-ar"))
)
XCTAssertEqual(
otherToolsToolset.knownTools[.linker],
Toolset.ToolProperties(path: try! AbsolutePath(validating: "/custom/ld"))
)
XCTAssertEqual(
otherToolsToolset.knownTools[.debugger],
Toolset.ToolProperties(path: usrBinTools[.debugger]!)
)
let someToolsWithRelativeRoot = try Toolset(from: someToolsWithRelativeRoot.path, at: fileSystem, observability.topScope)
XCTAssertEqual(
someToolsWithRelativeRoot,
Toolset(
knownTools: [.cCompiler: .init(extraCLIOptions: newCCompilerOptions)],
rootPaths: [try AbsolutePath(validating: "/tools/relative/custom")]
)
)
}
}
|