-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPackageModelTests.swift
More file actions
290 lines (251 loc) · 11.9 KB
/
PackageModelTests.swift
File metadata and controls
290 lines (251 loc) · 11.9 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2024 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
@_spi(SwiftPMInternal)
@testable import PackageModel
import _InternalTestSupport
import func TSCBasic.withTemporaryFile
import Testing
import XCTest
import struct TSCBasic.ByteString
final class PackageModelTests: XCTestCase {
func testProductTypeCodable() throws {
struct Foo: Codable, Equatable {
var type: ProductType
}
func checkCodable(_ type: ProductType) {
do {
let foo = Foo(type: type)
let data = try JSONEncoder.makeWithDefaults().encode(foo)
let decodedFoo = try JSONDecoder.makeWithDefaults().decode(Foo.self, from: data)
XCTAssertEqual(foo, decodedFoo)
} catch {
XCTFail("\(error)")
}
}
checkCodable(.library(.automatic))
checkCodable(.library(.static))
checkCodable(.library(.dynamic))
checkCodable(.executable)
checkCodable(.test)
}
func testProductFilterCodable() throws {
// Test ProductFilter.everything
try {
let data = try JSONEncoder().encode(ProductFilter.everything)
let decoded = try JSONDecoder().decode(ProductFilter.self, from: data)
XCTAssertEqual(decoded, ProductFilter.everything)
}()
// Test ProductFilter.specific(), including that the order is normalized
try {
let data = try JSONEncoder().encode(ProductFilter.specific(["Bar", "Foo"]))
let decoded = try JSONDecoder().decode(ProductFilter.self, from: data)
XCTAssertEqual(decoded, ProductFilter.specific(["Foo", "Bar"]))
}()
}
func testAndroidCompilerFlags() throws {
let triple = try Triple("x86_64-unknown-linux-android")
let fileSystem = InMemoryFileSystem()
let sdkDir = AbsolutePath("/some/path/to/an/SDK.sdk")
try fileSystem.createDirectory(sdkDir, recursive: true)
let toolchainPath = AbsolutePath("/some/path/to/a/toolchain.xctoolchain")
try fileSystem.createDirectory(toolchainPath, recursive: true)
let swiftSDK = SwiftSDK(
targetTriple: triple,
toolset: .init(toolchainBinDir: toolchainPath.appending(components: "usr", "bin"), buildFlags: .init()),
pathsConfiguration: .init(sdkRootPath: sdkDir)
)
XCTAssertEqual(
try UserToolchain.deriveSwiftCFlags(
triple: triple,
swiftSDK: swiftSDK,
environment: .current,
fileSystem: fileSystem
).map { $0.value },
[
// Needed when cross‐compiling for Android. 2020‐03‐01
"-sdk",
sdkDir.pathString,
]
)
}
// tiny PE binary from: https://archive.is/w01DO
static let tinyPEBytes: [UInt8] = [
0x4D, 0x5A, 0x00, 0x00, 0x50, 0x45, 0x00, 0x00, 0x4C, 0x01, 0x01, 0x00,
0x6A, 0x2A, 0x58, 0xC3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x00, 0x03, 0x01, 0x0B, 0x01, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x68, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02,
]
func testWindowsLibrarianSelection() throws {
#if os(Windows)
let suffix = ".exe"
#else
let suffix = ""
#endif
let triple = try Triple("x86_64-unknown-windows-msvc")
let fs = localFileSystem
try withTemporaryFile { _ in
try withTemporaryDirectory(removeTreeOnDeinit: true) { tmp in
let contents = Self.tinyPEBytes
let bin = tmp.appending("bin")
try fs.createDirectory(bin)
let lld = bin.appending("lld-link\(suffix)")
try fs.writeFileContents(lld, bytes: ByteString(contents))
let not = bin.appending("not-link\(suffix)")
try fs.writeFileContents(not, bytes: ByteString(contents))
#if !os(Windows)
try fs.chmod(.executable, path: lld, options: [])
try fs.chmod(.executable, path: not, options: [])
#endif
try XCTAssertEqual(
UserToolchain.determineLibrarian(
triple: triple, binDirectories: [bin], useXcrun: false, environment: [:], searchPaths: [],
extraSwiftFlags: ["-Xswiftc", "-use-ld=lld"],
fileSystem: fs
),
lld
)
try XCTAssertEqual(
UserToolchain.determineLibrarian(
triple: triple, binDirectories: [bin], useXcrun: false, environment: [:], searchPaths: [],
extraSwiftFlags: ["-Xswiftc", "-use-ld=not-link"],
fileSystem: fs
),
not
)
try XCTAssertThrowsError(
UserToolchain.determineLibrarian(
triple: triple, binDirectories: [bin], useXcrun: false, environment: [:], searchPaths: [],
extraSwiftFlags: [],
fileSystem: fs
)
)
}
}
}
func testDetermineSwiftCompilers() throws {
let fs = localFileSystem
try withTemporaryDirectory(removeTreeOnDeinit: true) { tmp in
// When swiftc is not in the toolchain bin directory, UserToolchain
// should find it in the system PATH search paths in the order they
// are specified.
let toolchainPath = tmp.appending("swift.xctoolchain")
let toolchainBinDir = toolchainPath.appending(components: "usr", "bin")
// Create the toolchain bin directory, but don't put swiftc in it.
try fs.createDirectory(toolchainBinDir, recursive: true)
// Create a directory with two swiftc binaries in it.
let binDirs = ["bin1", "bin2"].map { tmp.appending($0) }
#if os(Windows)
let exeSuffix = ".exe"
#else
let exeSuffix = ""
#endif
let expectedExecuable = "swiftc\(exeSuffix)" // Files that end with .exe are considered executable on Windows.
for binDir in binDirs {
try fs.createDirectory(binDir)
let binFile = binDir.appending(expectedExecuable)
try fs.writeFileContents(binFile, bytes: ByteString(Self.tinyPEBytes))
XCTAssertTrue(fs.exists(binFile), "File '\(binFile)' does not exist when it should")
#if !os(Windows)
try fs.chmod(.executable, path: binFile, options: [])
#endif
}
let compilers = try UserToolchain.determineSwiftCompilers(
binDirectories: [toolchainBinDir],
useXcrun: false,
environment: [:],
searchPaths: binDirs,
fileSystem: fs
)
// The first swiftc in the search paths should be chosen.
XCTAssertEqual(compilers.compile, binDirs.first?.appending(expectedExecuable))
}
}
func testDetermineSwiftCompilersWarnsOnInvalidSWIFT_EXEC() throws {
let fs = localFileSystem
try withTemporaryDirectory(removeTreeOnDeinit: true) { tmp in
let toolchainPath = tmp.appending("swift.xctoolchain")
let toolchainBinDir = toolchainPath.appending(components: "usr", "bin")
try fs.createDirectory(toolchainBinDir, recursive: true)
#if os(Windows)
let exeSuffix = ".exe"
#else
let exeSuffix = ""
#endif
// Create a valid swiftc in the toolchain
let validSwiftc = toolchainBinDir.appending("swiftc\(exeSuffix)")
try fs.writeFileContents(validSwiftc, bytes: ByteString(Self.tinyPEBytes))
#if !os(Windows)
try fs.chmod(.executable, path: validSwiftc, options: [])
#endif
// Test 1: SWIFT_EXEC points to non-existent file
do {
let observability = ObservabilitySystem.makeForTesting()
let nonExistentPath = tmp.appending(components: "nonexistent", "path", "to", "swiftc")
let environment: Environment = ["SWIFT_EXEC": nonExistentPath.pathString]
_ = try UserToolchain.determineSwiftCompilers(
binDirectories: [toolchainBinDir],
useXcrun: false,
environment: environment,
searchPaths: [],
fileSystem: fs,
observabilityScope: observability.topScope
)
testDiagnostics(observability.diagnostics) { result in
result.check(diagnostic: .contains("SWIFT_EXEC is set to '\(nonExistentPath.pathString)' but the file does not exist; ignoring"), severity: .warning)
}
}
// Test 2: SWIFT_EXEC points to file that exists but is not executable
do {
let observability = ObservabilitySystem.makeForTesting()
let notExecutablePath = tmp.appending("not-executable")
try fs.writeFileContents(notExecutablePath, bytes: "")
#if !os(Windows)
try fs.chmod(.userUnWritable, path: notExecutablePath, options: [])
#endif
let environment: Environment = ["SWIFT_EXEC": notExecutablePath.pathString]
_ = try UserToolchain.determineSwiftCompilers(
binDirectories: [toolchainBinDir],
useXcrun: false,
environment: environment,
searchPaths: [],
fileSystem: fs,
observabilityScope: observability.topScope
)
testDiagnostics(observability.diagnostics) { result in
result.check(diagnostic: .contains("SWIFT_EXEC is set to '\(notExecutablePath.pathString)' which exists but is not executable; ignoring"), severity: .warning)
}
}
// Test 3: SWIFT_EXEC is not an absolute path and not found in search paths
do {
let observability = ObservabilitySystem.makeForTesting()
let environment: Environment = ["SWIFT_EXEC": "nonexistent-compiler"]
_ = try UserToolchain.determineSwiftCompilers(
binDirectories: [toolchainBinDir],
useXcrun: false,
environment: environment,
searchPaths: [],
fileSystem: fs,
observabilityScope: observability.topScope
)
testDiagnostics(observability.diagnostics) { result in
result.check(diagnostic: .contains("SWIFT_EXEC is set to 'nonexistent-compiler' but no executable was found in search paths; ignoring"), severity: .warning)
}
}
}
}
}