Skip to content

Commit 428e6fd

Browse files
committed
Tests: Augment Command Tests
Augment Commands Tests to run against the Native and Swift Build build systems. - BuildCommandTests - TestCommandTests - RunCommandTests - APIDiffTests - PackageCommandTests Also, - Add a new BuildCommandTest and RunCommandTest test case that ensures a package with target conditionals builds and runs successfully - update BuildSystemProvider.Kind to define a "useXcodeBuildSystemPath" variable instead of sprinkling the `buildSystem == .xcode` all over the place - Augment the Swift Build integration test to run `swift test` TODO: - Instead of marking test failures as "Skipped", See if we can mark them as "expected fail" so we are forced to update the test once the production code has been update to support the "feature".
1 parent 844a6b3 commit 428e6fd

File tree

21 files changed

+707
-143
lines changed

21 files changed

+707
-143
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// swift-tools-version: 6.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "ExecutableTargetWhen",
8+
products: [
9+
.executable(
10+
name: "test",
11+
targets: ["ExecutableTargetWhen"]
12+
)
13+
],
14+
targets: [
15+
// Targets are the basic building blocks of a package, defining a module or a test suite.
16+
// Targets can depend on other targets in this package and products from dependencies.
17+
.executableTarget(
18+
name: "ExecutableTargetWhen",
19+
dependencies: [
20+
.target(name:"LinuxOnly", condition: .when(platforms:[.linux])),
21+
.target(name:"MacOSOnly", condition: .when(platforms:[.macOS])),
22+
.target(name:"WindowsOnly", condition: .when(platforms:[.windows])),
23+
.target(name:"AllPlatforms")
24+
]
25+
),
26+
.target(
27+
name: "AllPlatforms"
28+
),
29+
.target(
30+
name: "LinuxOnly",
31+
dependencies: [
32+
"CLibArchive",
33+
"AllPlatforms"
34+
]
35+
),
36+
.target(
37+
name: "MacOSOnly",
38+
dependencies: [
39+
"AllPlatforms"
40+
]
41+
),
42+
.target(
43+
name: "WindowsOnly",
44+
dependencies: [
45+
"AllPlatforms"
46+
]
47+
),
48+
.systemLibrary(
49+
name: "CLibArchive",
50+
pkgConfig: "libarchive",
51+
providers: [
52+
.apt(["libarchive-dev"]),
53+
]
54+
),
55+
]
56+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public func getPlatform() throws -> String {
2+
#if os(Windows)
3+
return "Windows"
4+
#else
5+
#if os(macOS)
6+
return "macOS"
7+
#else
8+
#if os(linux)
9+
return "Linux"
10+
#else
11+
return "Unknown platform"
12+
#endif
13+
#endif
14+
#endif
15+
}
16+
17+
18+
public protocol MyProtocol {
19+
static var name: String { get }
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module CLibArchive [system] {
2+
header "shim.h"
3+
link "archive"
4+
export *
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "archive.h"
2+
#include "archive_entry.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// The Swift Programming Language
2+
// https://docs.swift.org/swift-book
3+
4+
import AllPlatforms
5+
6+
#if os(Windows)
7+
import WindowsOnly
8+
#else
9+
#if os(macOS)
10+
import MacOSOnly
11+
#else
12+
#if os(linux)
13+
import LinuxOnly
14+
#endif
15+
#endif
16+
#endif
17+
18+
let platform = try getPlatform()
19+
print("Hello, world on \(platform)! OSplatform: \(OSPlatform.name)")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import CLibArchive
2+
3+
import AllPlatforms
4+
5+
public struct OSPlatform: MyProtocol {
6+
7+
public static var name: String {
8+
return "Linux"
9+
}
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import AllPlatforms
2+
public struct OSPlatform: MyProtocol {
3+
4+
public static var name: String {
5+
return "macOS"
6+
}
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import AllPlatforms
2+
3+
public struct OSPlatform: MyProtocol {
4+
5+
public static var name: String {
6+
return "Windows"
7+
}
8+
9+
}

IntegrationTests/Tests/IntegrationTests/SwiftPMTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ final class SwiftPMTests: XCTestCase {
6060
try withTemporaryDirectory { tmpDir in
6161
let packagePath = tmpDir.appending(component: "foo")
6262
try localFileSystem.createDirectory(packagePath)
63-
try sh(swiftPackage, "--package-path", packagePath, "init", "--type", "executable")
63+
try sh(swiftPackage, "--package-path", packagePath, "init", "--type", "library")
6464
try sh(swiftBuild, "--package-path", packagePath, "--build-system", "swiftbuild")
65+
try sh(swiftTest, "--package-path", packagePath, "--build-system", "swiftbuild")
6566
}
6667
}
6768

Sources/CoreCommands/SwiftCommandState.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ public final class SwiftCommandState {
441441
self.observabilityHandler.progress,
442442
self.observabilityHandler.prompt
443443
)
444-
let isXcodeBuildSystemEnabled = self.options.build.buildSystem == .xcode
444+
let isXcodeBuildSystemEnabled = self.options.build.buildSystem.useXcodeBuildSystemPath
445445
let workspace = try Workspace(
446446
fileSystem: self.fileSystem,
447447
location: .init(
@@ -459,7 +459,7 @@ public final class SwiftCommandState {
459459
configuration: .init(
460460
skipDependenciesUpdates: options.resolver.skipDependencyUpdate,
461461
prefetchBasedOnResolvedFile: options.resolver.shouldEnableResolverPrefetching,
462-
shouldCreateMultipleTestProducts: toolWorkspaceConfiguration.wantsMultipleTestProducts || options.build.buildSystem == .xcode,
462+
shouldCreateMultipleTestProducts: toolWorkspaceConfiguration.wantsMultipleTestProducts || self.options.build.buildSystem.useXcodeBuildSystemPath,
463463
createREPLProduct: toolWorkspaceConfiguration.wantsREPLProduct,
464464
additionalFileRules: isXcodeBuildSystemEnabled ? FileRuleDescription.xcbuildFileTypes : FileRuleDescription.swiftpmFileTypes,
465465
sharedDependenciesCacheEnabled: self.options.caching.useDependenciesCache,
@@ -795,7 +795,7 @@ public final class SwiftCommandState {
795795
workers: options.build.jobs ?? UInt32(ProcessInfo.processInfo.activeProcessorCount),
796796
sanitizers: options.build.enabledSanitizers,
797797
indexStoreMode: options.build.indexStoreMode.buildParameter,
798-
isXcodeBuildSystemEnabled: options.build.buildSystem == .xcode,
798+
isXcodeBuildSystemEnabled: self.options.build.buildSystem.useXcodeBuildSystemPath,
799799
prepareForIndexing: prepareForIndexingMode,
800800
debuggingParameters: .init(
801801
debugInfoFormat: options.build.debugInfoFormat.buildParameter,

Sources/SPMBuildCore/BuildSystem/BuildSystem.swift

+13
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,16 @@ public enum BuildSystemUtilities {
182182
return try AbsolutePath(validating: env, relativeTo: workingDir)
183183
}
184184
}
185+
186+
187+
extension BuildSystemProvider.Kind {
188+
189+
public var useXcodeBuildSystemPath: Bool {
190+
switch self {
191+
case .native: return false
192+
case .swiftbuild: return true
193+
case .xcode: return true
194+
}
195+
}
196+
197+
}

Sources/SwiftBuildSupport/SwiftBuildSystem.swift

+2
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
242242

243243
do {
244244
try await withSession(service: service, name: buildParameters.pifManifest.pathString) { session, _ in
245+
self.outputStream.send("Building for \(self.buildParameters.configuration == .debug ? "debugging" : "production")...\n")
246+
245247
// Load the workspace, and set the system information to the default
246248
do {
247249
try await session.loadWorkspace(containerPath: self.buildParameters.pifManifest.pathString)

Sources/_InternalTestSupport/misc.swift

+14-8
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public func testWithTemporaryDirectory<Result>(
117117
}
118118
}
119119

120+
public enum TestError: Error {
121+
case platformNotSupported
122+
}
123+
120124
@discardableResult public func fixture<T>(
121125
name: String,
122126
createGitRepo: Bool = true,
@@ -252,7 +256,7 @@ public func getBuildSystemArgs(for buildSystem: BuildSystemProvider.Kind?) -> [S
252256

253257
@discardableResult
254258
public func executeSwiftBuild(
255-
_ packagePath: AbsolutePath,
259+
_ packagePath: AbsolutePath?,
256260
configuration: Configuration = .Debug,
257261
extraArgs: [String] = [],
258262
Xcc: [String] = [],
@@ -269,14 +273,14 @@ public func executeSwiftBuild(
269273
Xswiftc: Xswiftc,
270274
buildSystem: buildSystem
271275
)
272-
let buildArgs = getBuildSystemArgs(for: buildSystem)
276+
let buildArgs: [String] = getBuildSystemArgs(for: buildSystem)
273277
return try await SwiftPM.Build.execute(args, packagePath: packagePath, env: env)
274278
}
275279

276280
@discardableResult
277281
public func executeSwiftRun(
278-
_ packagePath: AbsolutePath,
279-
_ executable: String,
282+
_ packagePath: AbsolutePath?,
283+
_ executable: String?,
280284
configuration: Configuration = .Debug,
281285
extraArgs: [String] = [],
282286
Xcc: [String] = [],
@@ -293,13 +297,15 @@ public func executeSwiftRun(
293297
Xswiftc: Xswiftc,
294298
buildSystem: buildSystem
295299
)
296-
args.append(executable)
300+
if let executable {
301+
args.append(executable)
302+
}
297303
return try await SwiftPM.Run.execute(args, packagePath: packagePath, env: env)
298304
}
299305

300306
@discardableResult
301307
public func executeSwiftPackage(
302-
_ packagePath: AbsolutePath,
308+
_ packagePath: AbsolutePath?,
303309
configuration: Configuration = .Debug,
304310
extraArgs: [String] = [],
305311
Xcc: [String] = [],
@@ -321,7 +327,7 @@ public func executeSwiftPackage(
321327

322328
@discardableResult
323329
public func executeSwiftPackageRegistry(
324-
_ packagePath: AbsolutePath,
330+
_ packagePath: AbsolutePath?,
325331
configuration: Configuration = .Debug,
326332
extraArgs: [String] = [],
327333
Xcc: [String] = [],
@@ -343,7 +349,7 @@ public func executeSwiftPackageRegistry(
343349

344350
@discardableResult
345351
public func executeSwiftTest(
346-
_ packagePath: AbsolutePath,
352+
_ packagePath: AbsolutePath?,
347353
configuration: Configuration = .Debug,
348354
extraArgs: [String] = [],
349355
Xcc: [String] = [],

Sources/swift-bootstrap/main.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ struct SwiftBootstrapBuildTool: AsyncParsableCommand {
290290
triple: self.hostToolchain.targetTriple,
291291
flags: buildFlags,
292292
architectures: architectures,
293-
isXcodeBuildSystemEnabled: buildSystem == .xcode,
293+
isXcodeBuildSystemEnabled: buildSystem.useXcodeBuildSystemPath,
294294
driverParameters: .init(
295295
explicitTargetDependencyImportCheckingMode: explicitTargetDependencyImportCheck == .error ? .error : .none,
296296
useIntegratedSwiftDriver: useIntegratedSwiftDriver,

Tests/CommandsTests/APIDiffTests.swift

+35-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import Basics
1414
import Build
1515
import Commands
16+
import SPMBuildCore
1617

1718
@_spi(SwiftPMInternal)
1819
import DriverSupport
@@ -24,7 +25,11 @@ import _InternalTestSupport
2425
import Workspace
2526
import XCTest
2627

27-
final class APIDiffTests: CommandsTestCase {
28+
class APIDiffTestCase: CommandsBuildProviderTestCase {
29+
override func setUpWithError() throws {
30+
try XCTSkipIf(type(of: self) == APIDiffTestCase.self, "Pay no attention to the class behind the curtain.")
31+
}
32+
2833
@discardableResult
2934
private func execute(
3035
_ args: [String],
@@ -34,7 +39,12 @@ final class APIDiffTests: CommandsTestCase {
3439
var environment = env ?? [:]
3540
// don't ignore local packages when caching
3641
environment["SWIFTPM_TESTS_PACKAGECACHE"] = "1"
37-
return try await SwiftPM.Package.execute(args, packagePath: packagePath, env: environment)
42+
return try await executeSwiftPackage(
43+
packagePath,
44+
extraArgs: args,
45+
env: environment,
46+
buildSystem: buildSystemProvider
47+
)
3848
}
3949

4050
func skipIfApiDigesterUnsupportedOrUnset() throws {
@@ -449,3 +459,26 @@ final class APIDiffTests: CommandsTestCase {
449459
}
450460
}
451461
}
462+
463+
class APIDiffNativeTests: APIDiffTestCase {
464+
465+
override open var buildSystemProvider: BuildSystemProvider.Kind {
466+
return .native
467+
}
468+
469+
override func skipIfApiDigesterUnsupportedOrUnset() throws {
470+
try super.skipIfApiDigesterUnsupportedOrUnset()
471+
}
472+
473+
}
474+
475+
class APIDiffSwiftBuildTests: APIDiffTestCase {
476+
477+
override open var buildSystemProvider: BuildSystemProvider.Kind {
478+
return .swiftbuild
479+
}
480+
481+
override func skipIfApiDigesterUnsupportedOrUnset() throws {
482+
try super.skipIfApiDigesterUnsupportedOrUnset()
483+
}
484+
}

0 commit comments

Comments
 (0)