Skip to content

Tests: Convert Build Command Tests to Swift Testing #8714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,4 @@ extension Trait where Self == Testing.ConditionTrait {
#endif
}
}

/// Constructs a condition trait that causes a test to be disabled if the Foundation process spawning implementation
/// is not using `posix_spawn_file_actions_addchdir`.
public static var requireThreadSafeWorkingDirectory: Self {
disabled("Thread-safe process working directory support is unavailable.") {
// Amazon Linux 2 has glibc 2.26, and glibc 2.29 is needed for posix_spawn_file_actions_addchdir_np support
FileManager.default.contents(atPath: "/etc/system-release")
.map { String(decoding: $0, as: UTF8.self) == "Amazon Linux release 2 (Karoo)\n" } ?? false
}
}
}
3 changes: 0 additions & 3 deletions IntegrationTests/Tests/IntegrationTests/SwiftPMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ private struct SwiftPMTests {
}

@Test(
.requireThreadSafeWorkingDirectory,
arguments: [BuildSystemProvider.native]
)
func packageInitExecutable(_ buildSystemProvider: BuildSystemProvider) throws {
Expand All @@ -84,7 +83,6 @@ private struct SwiftPMTests {

@Test(
.skipHostOS(.windows),
.requireThreadSafeWorkingDirectory,
.bug(
"https://github.com/swiftlang/swift-package-manager/issues/8416",
"[Linux] swift run using --build-system swiftbuild fails to run executable"
Expand Down Expand Up @@ -125,7 +123,6 @@ private struct SwiftPMTests {
}

@Test(
.requireThreadSafeWorkingDirectory,
.bug(id: 0, "SWBINTTODO: Linux: /lib/x86_64-linux-gnu/Scrt1.o:function _start: error:"),
.bug("https://github.com/swiftlang/swift-package-manager/issues/8380", "lld-link: error: subsystem must be defined"),
.bug(id: 0, "SWBINTTODO: MacOS: Could not find or use auto-linked library 'Testing': library 'Testing' not found"),
Expand Down
1 change: 1 addition & 0 deletions Sources/Basics/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum OperatingSystem: Hashable, Sendable {
case unknown
}


extension ProcessInfo {
public static var hostOperatingSystem: OperatingSystem {
#if os(macOS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 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 struct SPMBuildCore.BuildSystemProvider
import enum PackageModel.BuildConfiguration

extension BuildSystemProvider.Kind {

public func binPathSuffixes(for config: BuildConfiguration) -> [String] {
let suffix: String

#if os(Linux)
suffix = "-linux"
#elseif os(Windows)
suffix = "-windows"
#else
suffix = ""
#endif
switch self {
case .native:
return ["\(config)".lowercased()]
case .swiftbuild:
return ["Products" , "\(config)".capitalized + suffix]
case .xcode:
return ["apple", "Products" , "\(config)".capitalized + suffix]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 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 struct SPMBuildCore.BuildSystemProvider


public var SupportedBuildSystemOnAllPlatforms: [BuildSystemProvider.Kind] = BuildSystemProvider.Kind.allCases.filter { $0 != .xcode }

public var SupportedBuildSystemOnPlatform: [BuildSystemProvider.Kind] {
#if os(macOS)
BuildSystemProvider.Kind.allCases
#else
SupportedBuildSystemOnAllPlatforms
#endif
}
47 changes: 47 additions & 0 deletions Sources/_InternalTestSupport/SkippedTestSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,50 @@ extension Trait where Self == Testing.ConditionTrait {
}
}
}

extension Trait where Self == Testing.Bug {
public static func SWBINTTODO(_ comment: Comment) -> Self {
bug(nil, id: 0, comment)
}
}
extension Tag {
public enum TestSize {}
public enum Feature {}
@Tag public static var UserWorkflow: Tag
}

extension Tag.TestSize {
@Tag public static var small: Tag
@Tag public static var medium: Tag
@Tag public static var large: Tag
}

extension Tag.Feature {
public enum Command {}
public enum PackageType {}

@Tag public static var CodeCoverage: Tag
@Tag public static var Resource: Tag
@Tag public static var SpecialCharacters: Tag
}


extension Tag.Feature.Command {
public enum Package {}
@Tag public static var Build: Tag
@Tag public static var Test: Tag
@Tag public static var Run: Tag
}

extension Tag.Feature.Command.Package {
@Tag public static var Init: Tag
}
extension Tag.Feature.PackageType {
@Tag public static var Library: Tag
@Tag public static var Executable: Tag
@Tag public static var Tool: Tag
@Tag public static var Plugin: Tag
@Tag public static var BuildToolPlugin: Tag
@Tag public static var CommandPlugin: Tag
@Tag public static var Macro: Tag
}
7 changes: 4 additions & 3 deletions Sources/_InternalTestSupport/XCTAssertHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Basics
import class Foundation.Bundle
#endif
import SPMBuildCore
import enum PackageModel.BuildConfiguration
import TSCTestSupport
import XCTest

Expand Down Expand Up @@ -55,7 +56,7 @@ public func XCTSkipIfPlatformCI(because reason: String? = nil, file: StaticStrin

public func XCTSkipIfselfHostedCI(because reason: String, file: StaticString = #filePath, line: UInt = #line) throws {
// TODO: is this actually the right variable now?
if isSelfHostedCiEnvironment {
if CiEnvironment.runningInSelfHostedPipeline {
throw XCTSkip(reason, file: file, line: line)
}
}
Expand Down Expand Up @@ -145,7 +146,7 @@ package func XCTAssertAsyncNoThrow<T>(

public func XCTAssertBuilds(
_ path: AbsolutePath,
configurations: Set<Configuration> = [.Debug, .Release],
configurations: Set<BuildConfiguration> = [.debug, .release],
extraArgs: [String] = [],
Xcc: [String] = [],
Xld: [String] = [],
Expand Down Expand Up @@ -175,7 +176,7 @@ public func XCTAssertBuilds(

public func XCTAssertSwiftTest(
_ path: AbsolutePath,
configuration: Configuration = .Debug,
configuration: BuildConfiguration = .debug,
extraArgs: [String] = [],
Xcc: [String] = [],
Xld: [String] = [],
Expand Down
35 changes: 23 additions & 12 deletions Sources/_InternalTestSupport/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ import enum TSCUtility.Git
@_exported import func TSCTestSupport.systemQuietly
@_exported import enum TSCTestSupport.StringPattern

public let isInCiEnvironment = ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil
public let isSelfHostedCiEnvironment = ProcessInfo.processInfo.environment["SWIFTCI_IS_SELF_HOSTED"] != nil
@available(*, deprecated, message: "Use CiEnvironment.runningInSmokeTestPipeline")
public let isInCiEnvironment = CiEnvironment.runningInSmokeTestPipeline

@available(*, deprecated, message: "Use CiEnvironment.isSelfHostedCiEnvironment")
public let isSelfHostedCiEnvironment = CiEnvironment.runningInSelfHostedPipeline

public struct CiEnvironmentStruct {
public let runningInSmokeTestPipeline = ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil
public let runningInSelfHostedPipeline = ProcessInfo.processInfo.environment["SWIFTCI_IS_SELF_HOSTED"] != nil
}

public let CiEnvironment = CiEnvironmentStruct()

public let isRealSigningIdentyEcLabelEnvVarSet =
ProcessInfo.processInfo.environment["REAL_SIGNING_IDENTITY_EC_LABEL"] != nil
Expand Down Expand Up @@ -281,13 +291,14 @@ public func getBuildSystemArgs(for buildSystem: BuildSystemProvider.Kind?) -> [S
@discardableResult
public func executeSwiftBuild(
_ packagePath: AbsolutePath?,
configuration: Configuration = .Debug,
configuration: BuildConfiguration = .debug,
extraArgs: [String] = [],
Xcc: [String] = [],
Xld: [String] = [],
Xswiftc: [String] = [],
env: Environment? = nil,
buildSystem: BuildSystemProvider.Kind = .native
buildSystem: BuildSystemProvider.Kind = .native,
throwIfCommandFails: Bool = true,
) async throws -> (stdout: String, stderr: String) {
let args = swiftArgs(
configuration: configuration,
Expand All @@ -297,14 +308,14 @@ public func executeSwiftBuild(
Xswiftc: Xswiftc,
buildSystem: buildSystem
)
return try await SwiftPM.Build.execute(args, packagePath: packagePath, env: env)
return try await SwiftPM.Build.execute(args, packagePath: packagePath, env: env, throwIfCommandFails: throwIfCommandFails)
}

@discardableResult
public func executeSwiftRun(
_ packagePath: AbsolutePath?,
_ executable: String?,
configuration: Configuration = .Debug,
configuration: BuildConfiguration = .debug,
extraArgs: [String] = [],
Xcc: [String] = [],
Xld: [String] = [],
Expand All @@ -329,7 +340,7 @@ public func executeSwiftRun(
@discardableResult
public func executeSwiftPackage(
_ packagePath: AbsolutePath?,
configuration: Configuration = .Debug,
configuration: BuildConfiguration = .debug,
extraArgs: [String] = [],
Xcc: [String] = [],
Xld: [String] = [],
Expand All @@ -351,7 +362,7 @@ public func executeSwiftPackage(
@discardableResult
public func executeSwiftPackageRegistry(
_ packagePath: AbsolutePath?,
configuration: Configuration = .Debug,
configuration: BuildConfiguration = .debug,
extraArgs: [String] = [],
Xcc: [String] = [],
Xld: [String] = [],
Expand All @@ -373,7 +384,7 @@ public func executeSwiftPackageRegistry(
@discardableResult
public func executeSwiftTest(
_ packagePath: AbsolutePath?,
configuration: Configuration = .Debug,
configuration: BuildConfiguration = .debug,
extraArgs: [String] = [],
Xcc: [String] = [],
Xld: [String] = [],
Expand All @@ -394,7 +405,7 @@ public func executeSwiftTest(
}

private func swiftArgs(
configuration: Configuration,
configuration: BuildConfiguration,
extraArgs: [String],
Xcc: [String],
Xld: [String],
Expand All @@ -403,9 +414,9 @@ private func swiftArgs(
) -> [String] {
var args = ["--configuration"]
switch configuration {
case .Debug:
case .debug:
args.append("debug")
case .Release:
case .release:
args.append("release")
}

Expand Down
21 changes: 14 additions & 7 deletions Tests/BasicsTests/ConcurrencyHelpersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ struct ConcurrencyHelpersTest {
struct ThreadSafeKeyValueStoreTests {
let queue = DispatchQueue(label: "ConcurrencyHelpersTest", attributes: .concurrent)

@Test
@Test(
.bug("https://github.com/swiftlang/swift-package-manager/issues/8770"),
)
func threadSafeKeyValueStore() throws {
for _ in 0 ..< 100 {
let sync = DispatchGroup()
Expand All @@ -45,14 +47,16 @@ struct ConcurrencyHelpersTest {
}
}

try #require(sync.wait(timeout: .now() + .seconds(2)) == .success)
try #require(sync.wait(timeout: .now() + .seconds(300)) == .success)
expected.forEach { key, value in
#expect(cache[key] == value)
}
}
}

@Test
@Test(
.bug("https://github.com/swiftlang/swift-package-manager/issues/8770"),
)
func threadSafeArrayStore() throws {
for _ in 0 ..< 100 {
let sync = DispatchGroup()
Expand All @@ -72,15 +76,18 @@ struct ConcurrencyHelpersTest {
}
}

try #require(sync.wait(timeout: .now() + .seconds(2)) == .success)

try #require(sync.wait(timeout: .now() + .seconds(300)) == .success)
let expectedSorted = expected.sorted()
let resultsSorted = cache.get().sorted()
#expect(expectedSorted == resultsSorted)
}
}
}
}

@Test
@Test(
.bug("https://github.com/swiftlang/swift-package-manager/issues/8770"),
)
func threadSafeBox() throws {
let queue = DispatchQueue(label: "ConcurrencyHelpersTest", attributes: .concurrent)
for _ in 0 ..< 100 {
Expand Down Expand Up @@ -108,7 +115,7 @@ struct ConcurrencyHelpersTest {
}
}

try #require(sync.wait(timeout: .now() + .seconds(2)) == .success)
try #require(sync.wait(timeout: .now() + .seconds(300)) == .success)
#expect(cache.get() == winner)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/BasicsTests/Environment/EnvironmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ struct EnvironmentTests {
/// Important: This test is inherently race-prone, if it is proven to be
/// flaky, it should run in a singled threaded environment/removed entirely.
@Test(
.disabled(if: isInCiEnvironment || isSelfHostedCiEnvironment, "This test can disrupt other tests running in parallel."),
.disabled(if: isInCiEnvironment || CiEnvironment.runningInSelfHostedPipeline, "This test can disrupt other tests running in parallel."),
)
func makeCustomPathEnv() async throws {
let customEnvironment: Environment = .current
Expand Down
Loading