Skip to content

Commit a398e27

Browse files
committed
Skip XCTest discovery if tests are not built with -enable-testing
1 parent f59c406 commit a398e27

File tree

3 files changed

+135
-4
lines changed

3 files changed

+135
-4
lines changed

Sources/SWBTaskConstruction/TaskProducers/TaskProducer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ public class TaskProducerContext: StaleFileRemovalContext, BuildFileResolution
867867
}
868868

869869
/// Report a note from task construction.
870-
func note(_ message: String, location: Diagnostic.Location = .unknown, component: Component = .default) {
870+
public func note(_ message: String, location: Diagnostic.Location = .unknown, component: Component = .default) {
871871
if let configuredTarget {
872872
delegate.note(.overrideTarget(configuredTarget), message, location: location, component: component)
873873
} else {
@@ -876,7 +876,7 @@ public class TaskProducerContext: StaleFileRemovalContext, BuildFileResolution
876876
}
877877

878878
/// Report a warning from task construction.
879-
func warning(_ message: String, location: Diagnostic.Location = .unknown, component: Component = .default) {
879+
public func warning(_ message: String, location: Diagnostic.Location = .unknown, component: Component = .default) {
880880
if let configuredTarget {
881881
delegate.warning(.overrideTarget(configuredTarget), message, location: location, component: component)
882882
} else {
@@ -894,7 +894,7 @@ public class TaskProducerContext: StaleFileRemovalContext, BuildFileResolution
894894
}
895895

896896
/// Report a remark from task construction.
897-
func remark(_ message: String, location: Diagnostic.Location = .unknown, component: Component = .default) {
897+
public func remark(_ message: String, location: Diagnostic.Location = .unknown, component: Component = .default) {
898898
if let configuredTarget {
899899
delegate.remark(.overrideTarget(configuredTarget), message, location: location, component: component)
900900
} else {

Sources/SWBUniversalPlatform/TestEntryPointTaskProducer.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ class TestEntryPointTaskProducer: PhasedTaskProducer, TaskProducer {
3636
guard settings.productType?.conformsTo(identifier: "com.apple.product-type.bundle.unit-test") == true else {
3737
continue
3838
}
39+
guard settings.globalScope.evaluate(BuiltinMacros.SWIFT_ENABLE_TESTABILITY) || settings.globalScope.evaluate(BuiltinMacros.OTHER_SWIFT_FLAGS).contains("-enable-testing") else {
40+
context.warning("Skipping XCTest discovery for '\(directDependency.target.name)' because it was not built for testing")
41+
continue
42+
}
3943
guard settings.globalScope.evaluate(BuiltinMacros.SWIFT_INDEX_STORE_ENABLE) else {
40-
context.error("Cannot perform test discovery for '\(directDependency.target.name)' because index while building is disabled")
44+
context.warning("Skipping XCTest discovery for '\(directDependency.target.name)' because indexing was disabled")
4145
continue
4246
}
4347
let path = settings.globalScope.evaluate(BuiltinMacros.SWIFT_INDEX_STORE_PATH)
4448
guard !path.isEmpty else {
49+
context.warning("Skipping XCTest discovery for '\(directDependency.target.name)' because the index store path could not be determined")
4550
continue
4651
}
4752
indexStoreDirectories.append(path)

Tests/SWBBuildSystemTests/BuildOperationTests.swift

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,132 @@ fileprivate struct BuildOperationTests: CoreBasedTests {
576576
}
577577
}
578578

579+
@Test(.requireSDKs(.host), .skipHostOS(.macOS), .skipHostOS(.windows, "cannot find testing library"))
580+
func unitTestWithGeneratedEntryPoint_testabilityDisabled() async throws {
581+
try await withTemporaryDirectory(removeTreeOnDeinit: false) { (tmpDir: Path) in
582+
let testProject = try await TestProject(
583+
"TestProject",
584+
sourceRoot: tmpDir,
585+
groupTree: TestGroup(
586+
"SomeFiles",
587+
children: [
588+
TestFile("library.swift"),
589+
TestFile("test.swift"),
590+
]),
591+
buildConfigurations: [
592+
TestBuildConfiguration("Debug", buildSettings: [
593+
"ARCHS": "$(ARCHS_STANDARD)",
594+
"CODE_SIGNING_ALLOWED": "NO",
595+
"PRODUCT_NAME": "$(TARGET_NAME)",
596+
"SDKROOT": "$(HOST_PLATFORM)",
597+
"SUPPORTED_PLATFORMS": "$(HOST_PLATFORM)",
598+
"SWIFT_VERSION": swiftVersion,
599+
"INDEX_DATA_STORE_DIR": "\(tmpDir.join("index").str)",
600+
"LINKER_DRIVER": "swiftc",
601+
"ENABLE_TESTABILITY": "NO",
602+
])
603+
],
604+
targets: [
605+
TestStandardTarget(
606+
"UnitTestRunner",
607+
type: .swiftpmTestRunner,
608+
buildConfigurations: [
609+
TestBuildConfiguration("Debug", buildSettings: [
610+
"LD_RUNPATH_SEARCH_PATHS": "$(RPATH_ORIGIN)",
611+
]),
612+
],
613+
buildPhases: [
614+
TestSourcesBuildPhase(),
615+
TestFrameworksBuildPhase([
616+
"MyTests.so"
617+
])
618+
],
619+
dependencies: ["MyTests"]
620+
),
621+
TestStandardTarget(
622+
"MyTests",
623+
type: .unitTest,
624+
buildConfigurations: [
625+
TestBuildConfiguration("Debug", buildSettings: [
626+
"LD_RUNPATH_SEARCH_PATHS": "$(RPATH_ORIGIN)",
627+
"LD_DYLIB_INSTALL_NAME": "MyTests.so"
628+
])
629+
],
630+
buildPhases: [
631+
TestSourcesBuildPhase(["test.swift"]),
632+
TestFrameworksBuildPhase([
633+
TestBuildFile(.target("library")),
634+
])
635+
], dependencies: [
636+
"library"
637+
],
638+
productReferenceName: "MyTests.so"
639+
),
640+
TestStandardTarget(
641+
"library",
642+
type: .dynamicLibrary,
643+
buildConfigurations: [
644+
TestBuildConfiguration("Debug", buildSettings: [
645+
"LD_RUNPATH_SEARCH_PATHS": "$(RPATH_ORIGIN)",
646+
"LD_DYLIB_INSTALL_NAME": "liblibrary.so",
647+
648+
// FIXME: Find a way to make these default
649+
"EXECUTABLE_PREFIX": "lib",
650+
"EXECUTABLE_PREFIX[sdk=windows*]": "",
651+
])
652+
],
653+
buildPhases: [
654+
TestSourcesBuildPhase(["library.swift"]),
655+
],
656+
)
657+
])
658+
let core = try await getCore()
659+
let tester = try await BuildOperationTester(core, testProject, simulated: false)
660+
try localFS.createDirectory(tmpDir.join("index"))
661+
let projectDir = tester.workspace.projects[0].sourceRoot
662+
663+
try await tester.fs.writeFileContents(projectDir.join("library.swift")) { stream in
664+
stream <<< "public func foo() -> Int { 42 }\n"
665+
}
666+
667+
try await tester.fs.writeFileContents(projectDir.join("test.swift")) { stream in
668+
stream <<< """
669+
import Testing
670+
import XCTest
671+
import library
672+
@Suite struct MySuite {
673+
@Test func myTest() {
674+
#expect(foo() == 42)
675+
}
676+
}
677+
678+
final class MYXCTests: XCTestCase {
679+
func testFoo() {
680+
XCTAssertTrue(true)
681+
}
682+
}
683+
"""
684+
}
685+
686+
let destination: RunDestinationInfo = .host
687+
try await tester.checkBuild(runDestination: destination, persistent: true) { results in
688+
results.checkWarning("Skipping XCTest discovery for 'MyTests' because it was not built for testing")
689+
results.checkNoErrors()
690+
691+
let environment = destination.hostRuntimeEnvironment(core)
692+
693+
do {
694+
let executionResult = try await Process.getOutput(url: URL(fileURLWithPath: projectDir.join("build").join("Debug\(destination.builtProductsDirSuffix)").join(core.hostOperatingSystem.imageFormat.executableName(basename: "UnitTestRunner")).str), arguments: [], environment: environment)
695+
#expect(String(decoding: executionResult.stdout, as: UTF8.self).contains("Executed 0 tests"))
696+
}
697+
do {
698+
let executionResult = try await Process.getOutput(url: URL(fileURLWithPath: projectDir.join("build").join("Debug\(destination.builtProductsDirSuffix)").join(core.hostOperatingSystem.imageFormat.executableName(basename: "UnitTestRunner")).str), arguments: ["--testing-library", "swift-testing"], environment: environment)
699+
#expect(String(decoding: executionResult.stderr, as: UTF8.self).contains("Test run with 1 test "))
700+
}
701+
}
702+
}
703+
}
704+
579705
/// Check that environment variables are propagated from the user environment correctly.
580706
@Test(.requireSDKs(.host), .skipHostOS(.windows), .requireSystemPackages(apt: "yacc", yum: "byacc"))
581707
func userEnvironment() async throws {

0 commit comments

Comments
 (0)