/
ManifestSourceGenerationTests.swift
595 lines (544 loc) · 25.6 KB
/
ManifestSourceGenerationTests.swift
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020-2021 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
import PackageGraph
import PackageLoading
import PackageModel
import SPMTestSupport
import TSCBasic
import Workspace
import XCTest
extension String {
fileprivate func nativePathString(escaped: Bool) -> String {
#if _runtime(_ObjC)
return self
#else
let fsr = self.fileSystemRepresentation
defer { fsr.deallocate() }
if escaped {
return String(cString: fsr).replacingOccurrences(of: "\\", with: "\\\\")
}
return String(cString: fsr)
#endif
}
}
class ManifestSourceGenerationTests: XCTestCase {
/// Private function that writes the contents of a package manifest to a temporary package directory and then loads it, then serializes the loaded manifest back out again and loads it once again, after which it compares that no information was lost. Return the source of the newly generated manifest.
@discardableResult
private func testManifestWritingRoundTrip(
manifestContents: String,
toolsVersion: ToolsVersion,
toolsVersionHeaderComment: String? = .none,
additionalImportModuleNames: [String] = [],
fs: FileSystem = localFileSystem
) throws -> String {
try withTemporaryDirectory { packageDir in
let observability = ObservabilitySystem.makeForTesting()
// Write the original manifest file contents, and load it.
let manifestPath = packageDir.appending(component: Manifest.filename)
try fs.writeFileContents(manifestPath, string: manifestContents)
let manifestLoader = ManifestLoader(toolchain: try UserToolchain.default)
let identityResolver = DefaultIdentityResolver()
let manifest = try tsc_await {
manifestLoader.load(
manifestPath: manifestPath,
manifestToolsVersion: toolsVersion,
packageIdentity: .plain("Root"),
packageKind: .root(packageDir),
packageLocation: packageDir.pathString,
packageVersion: nil,
identityResolver: identityResolver,
fileSystem: fs,
observabilityScope: observability.topScope,
delegateQueue: .sharedConcurrent,
callbackQueue: .sharedConcurrent,
completion: $0
)
}
XCTAssertNoDiagnostics(observability.diagnostics)
// Generate source code for the loaded manifest,
let newContents = try manifest.generateManifestFileContents(
packageDirectory: packageDir,
toolsVersionHeaderComment: toolsVersionHeaderComment,
additionalImportModuleNames: additionalImportModuleNames)
// Check that the tools version was serialized properly.
let versionSpacing = (toolsVersion >= .v5_4) ? " " : ""
XCTAssertMatch(newContents, .prefix("// swift-tools-version:\(versionSpacing)\(toolsVersion.major).\(toolsVersion.minor)"))
// Write out the generated manifest to replace the old manifest file contents, and load it again.
try fs.writeFileContents(manifestPath, string: newContents)
let newManifest = try tsc_await {
manifestLoader.load(
manifestPath: manifestPath,
manifestToolsVersion: toolsVersion,
packageIdentity: .plain("Root"),
packageKind: .root(packageDir),
packageLocation: packageDir.pathString,
packageVersion: nil,
identityResolver: identityResolver,
fileSystem: fs,
observabilityScope: observability.topScope,
delegateQueue: .sharedConcurrent,
callbackQueue: .sharedConcurrent,
completion: $0
)
}
XCTAssertNoDiagnostics(observability.diagnostics)
// Check that all the relevant properties survived.
let failureDetails = "\n--- ORIGINAL MANIFEST CONTENTS ---\n" + manifestContents + "\n--- REWRITTEN MANIFEST CONTENTS ---\n" + newContents
XCTAssertEqual(newManifest.toolsVersion, manifest.toolsVersion, failureDetails)
XCTAssertEqual(newManifest.displayName, manifest.displayName, failureDetails)
XCTAssertEqual(newManifest.defaultLocalization, manifest.defaultLocalization, failureDetails)
XCTAssertEqual(newManifest.platforms, manifest.platforms, failureDetails)
XCTAssertEqual(newManifest.pkgConfig, manifest.pkgConfig, failureDetails)
XCTAssertEqual(newManifest.providers, manifest.providers, failureDetails)
XCTAssertEqual(newManifest.products, manifest.products, failureDetails)
XCTAssertEqual(newManifest.dependencies, manifest.dependencies, failureDetails)
XCTAssertEqual(newManifest.targets, manifest.targets, failureDetails)
XCTAssertEqual(newManifest.swiftLanguageVersions, manifest.swiftLanguageVersions, failureDetails)
XCTAssertEqual(newManifest.cLanguageStandard, manifest.cLanguageStandard, failureDetails)
XCTAssertEqual(newManifest.cxxLanguageStandard, manifest.cxxLanguageStandard, failureDetails)
// Return the generated manifest so that the caller can do further testing on it.
return newContents
}
}
func testBasics() throws {
let manifestContents = """
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyPackage",
platforms: [
.macOS(.v10_14),
.iOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "MyPackage",
targets: ["MyPackage"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "MyPackage",
dependencies: []),
.testTarget(
name: "MyPackageTests",
dependencies: ["MyPackage"]),
]
)
"""
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
}
func testCustomPlatform() throws {
let manifestContents = """
// swift-tools-version:5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyPackage",
platforms: [
.custom("customOS", versionString: "1.0")
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "MyPackage",
targets: ["MyPackage"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "MyPackage",
dependencies: []),
.testTarget(
name: "MyPackageTests",
dependencies: ["MyPackage"]),
]
)
"""
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_6)
}
func testAdvancedFeatures() throws {
let manifestContents = """
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "MyPackage",
targets: ["MyPackage"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(path: "/a/b/c"),
.package(name: "abc", path: "/a/b/d"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.systemLibrary(
name: "SystemLibraryTarget",
pkgConfig: "libSystemModule",
providers: [
.brew(["SystemModule"]),
]),
.target(
name: "MyPackage",
dependencies: [
.target(name: "SystemLibraryTarget", condition: .when(platforms: [.macOS]))
],
linkerSettings: [
.unsafeFlags(["-Xlinker", "-rpath", "-Xlinker", "@executable_path/../../../lib/swift/macosx"], .when(platforms: [.iOS])),
]),
.testTarget(
name: "MyPackageTests",
dependencies: ["MyPackage"]),
],
swiftLanguageVersions: [.v5],
cLanguageStandard: .c11,
cxxLanguageStandard: .cxx11
)
"""
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
}
func testPackageDependencyVariations() throws {
let manifestContents = """
// swift-tools-version:5.4
import PackageDescription
#if os(Windows)
let absolutePath = "file:///C:/Users/user/SourceCache/path/to/MyPkg16"
#else
let absolutePath = "file:///path/to/MyPkg16"
#endif
let package = Package(
name: "MyPackage",
dependencies: [
.package(url: "https://example.com/MyPkg1", from: "1.0.0"),
.package(url: "https://example.com/MyPkg2", .revision("58e9de4e7b79e67c72a46e164158e3542e570ab6")),
.package(url: "https://example.com/MyPkg5", .exact("1.2.3")),
.package(url: "https://example.com/MyPkg6", "1.2.3"..<"2.0.0"),
.package(url: "https://example.com/MyPkg7", .branch("main")),
.package(url: "https://example.com/MyPkg8", .upToNextMinor(from: "1.3.4")),
.package(url: "ssh://[email protected]/MyPkg9", .branch("my branch with spaces")),
.package(url: "../MyPkg10", from: "0.1.0"),
.package(path: "../MyPkg11"),
.package(path: "packages/path/to/MyPkg12"),
.package(path: "~/path/to/MyPkg13"),
.package(path: "~MyPkg14"),
.package(path: "~/path/to/~/MyPkg15"),
.package(path: "~"),
.package(path: absolutePath),
]
)
"""
let newContents = try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
// Check some things about the contents of the manifest.
XCTAssertTrue(newContents.contains("url: \"\("../MyPkg10".nativePathString(escaped: true))\""), newContents)
XCTAssertTrue(newContents.contains("path: \"\("../MyPkg11".nativePathString(escaped: true))\""), newContents)
XCTAssertTrue(newContents.contains("path: \"\("packages/path/to/MyPkg12".nativePathString(escaped: true))"), newContents)
}
func testResources() throws {
let manifestContents = """
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "Resources",
defaultLocalization: "is",
targets: [
.target(
name: "SwiftyResource",
resources: [
.copy("foo.txt"),
.process("a/b/c/"),
]
),
.target(
name: "SeaResource",
resources: [
.process("foo.txt", localization: .base),
]
),
.target(
name: "SieResource",
resources: [
.copy("bar.boo"),
]
),
]
)
"""
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
}
func testBuildSettings() throws {
let manifestContents = """
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "Localized",
targets: [
.target(name: "exe",
cxxSettings: [
.headerSearchPath("ProjectName"),
.headerSearchPath("../../.."),
.define("ABC=DEF"),
.define("GHI", to: "JKL")
]
),
.target(
name: "MyTool",
dependencies: ["Utility"],
cSettings: [
.headerSearchPath("path/relative/to/my/target"),
.define("DISABLE_SOMETHING", .when(platforms: [.iOS], configuration: .release)),
],
swiftSettings: [
.define("ENABLE_SOMETHING", .when(configuration: .release)),
],
linkerSettings: [
.linkedLibrary("openssl", .when(platforms: [.linux])),
]
),
]
)
"""
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
}
func testPluginTargets() throws {
let manifestContents = """
// swift-tools-version:5.5
import PackageDescription
let package = Package(
name: "Plugins",
targets: [
.plugin(
name: "MyPlugin",
capability: .buildTool(),
dependencies: ["MyTool"]
),
.executableTarget(
name: "MyTool"
),
]
)
"""
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_5)
}
func testCustomToolsVersionHeaderComment() throws {
let manifestContents = """
// swift-tools-version:5.5
import PackageDescription
let package = Package(
name: "Plugins",
targets: [
.plugin(
name: "MyPlugin",
capability: .buildTool(),
dependencies: ["MyTool"]
),
.executableTarget(
name: "MyTool"
),
]
)
"""
let newContents = try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_5, toolsVersionHeaderComment: "a comment")
XCTAssertTrue(newContents.hasPrefix("// swift-tools-version: 5.5; a comment\n"), "contents: \(newContents)")
}
func testAdditionalModuleImports() throws {
let manifestContents = """
// swift-tools-version:5.5
import PackageDescription
import Foundation
let package = Package(
name: "MyPkg",
targets: [
.executableTarget(
name: "MyExec"
),
]
)
"""
let newContents = try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_5, additionalImportModuleNames: ["Foundation"])
XCTAssertTrue(newContents.contains("import Foundation\n"), "contents: \(newContents)")
}
func testLatestPlatformVersions() throws {
let manifestContents = """
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyPackage",
platforms: [
.macOS(.v14),
.iOS(.v17),
.tvOS(.v17),
.watchOS(.v10),
.macCatalyst(.v17),
.driverKit(.v23)
],
targets: [
]
)
"""
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_9)
}
func testTargetPlatformConditions() throws {
let manifestContents = """
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyPackage",
targets: [
.target(
name: "MyExe",
dependencies: [
.target(name: "MyLib", condition: .when(platforms: [
.macOS, .macCatalyst, .iOS, .tvOS, .watchOS, .driverKit,
.linux, .windows, .android, .wasi, .openbsd
]))
]
),
.target(
name: "MyLib"
),
]
)
"""
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_9)
}
func testCustomProductSourceGeneration() throws {
// Create a manifest containing a product for which we'd like to do custom source fragment generation.
let packageDir = AbsolutePath("/tmp/MyLibrary")
let manifest = Manifest.createManifest(
displayName: "MyLibrary",
path: packageDir.appending("Package.swift"),
packageKind: .root(AbsolutePath(path: "/tmp/MyLibrary")),
packageLocation: packageDir.pathString,
platforms: [],
toolsVersion: .v5_5,
products: [
try .init(name: "Foo", type: .library(.static), targets: ["Bar"])
]
)
// Generate the manifest contents, using a custom source generator for the product type.
let contents = manifest.generateManifestFileContents(packageDirectory: packageDir, customProductTypeSourceGenerator: { product in
// This example handles library types in a custom way, for testing purposes.
var params: [SourceCodeFragment] = []
params.append(SourceCodeFragment(key: "name", string: product.name))
if !product.targets.isEmpty {
params.append(SourceCodeFragment(key: "targets", strings: product.targets))
}
// Handle .library specially (by not emitting as multiline), otherwise asking for default behavior.
if case .library(let type) = product.type {
if type != .automatic {
params.append(SourceCodeFragment(key: "type", enum: type.rawValue))
}
return SourceCodeFragment(enum: "library", subnodes: params, multiline: false)
}
else {
return nil
}
})
// Check that we generated what we expected.
XCTAssertTrue(contents.contains(".library(name: \"Foo\", targets: [\"Bar\"], type: .static)"), "contents: \(contents)")
}
func testModuleAliasGeneration() throws {
let manifest = Manifest.createRootManifest(
displayName: "thisPkg",
path: .init(path: "/thisPkg"),
toolsVersion: .v5_7,
dependencies: [
.localSourceControl(path: .init(path: "/fooPkg"), requirement: .upToNextMajor(from: "1.0.0")),
.localSourceControl(path: .init(path: "/barPkg"), requirement: .upToNextMajor(from: "2.0.0")),
],
targets: [
try TargetDescription(name: "exe",
dependencies: ["Logging",
.product(name: "Foo",
package: "fooPkg",
moduleAliases: ["Logging": "FooLogging"]
),
.product(name: "Bar",
package: "barPkg",
moduleAliases: ["Logging": "BarLogging"]
)
]),
try TargetDescription(name: "Logging", dependencies: []),
])
let contents = try manifest.generateManifestFileContents(packageDirectory: manifest.path.parentDirectory)
let parts =
"""
dependencies: [
"Logging",
.product(name: "Foo", package: "fooPkg", moduleAliases: [
"Logging": "FooLogging"
]),
.product(name: "Bar", package: "barPkg", moduleAliases: [
"Logging": "BarLogging"
])
]
"""
let trimmedContents = contents.components(separatedBy: CharacterSet.whitespacesAndNewlines)
let trimmedParts = parts.components(separatedBy: CharacterSet.whitespacesAndNewlines)
let isContained = trimmedParts.allSatisfy(trimmedContents.contains(_:))
XCTAssertTrue(isContained)
try testManifestWritingRoundTrip(manifestContents: contents, toolsVersion: .v5_8)
}
func testUpcomingAndExperimentalFeatures() throws {
let manifestContents = """
// swift-tools-version:5.8
import PackageDescription
let package = Package(
name: "UpcomingAndExperimentalFeatures",
targets: [
.target(
name: "MyTool",
swiftSettings: [
.enableUpcomingFeature("UpcomingFeatureOne"),
.enableUpcomingFeature("UpcomingFeatureTwo"),
.enableExperimentalFeature("ExperimentalFeature")
]
),
]
)
"""
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_8)
}
func testPluginNetworkingPermissionGeneration() throws {
let manifest = Manifest.createRootManifest(
displayName: "thisPkg",
path: .init(path: "/thisPkg"),
toolsVersion: .v5_9,
dependencies: [],
targets: [
try TargetDescription(name: "MyPlugin", type: .plugin, pluginCapability: .command(intent: .custom(verb: "foo", description: "bar"), permissions: [.allowNetworkConnections(scope: .all(ports: [23, 42, 443, 8080]), reason: "internet good")]))
])
let contents = try manifest.generateManifestFileContents(packageDirectory: manifest.path.parentDirectory)
try testManifestWritingRoundTrip(manifestContents: contents, toolsVersion: .v5_9)
}
}