Skip to content

Commit 2955641

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

File tree

3 files changed

+136
-4
lines changed

3 files changed

+136
-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: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,133 @@ 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+
"SWIFT_ENABLE_TESTABILITY": "NO",
603+
])
604+
],
605+
targets: [
606+
TestStandardTarget(
607+
"UnitTestRunner",
608+
type: .swiftpmTestRunner,
609+
buildConfigurations: [
610+
TestBuildConfiguration("Debug", buildSettings: [
611+
"LD_RUNPATH_SEARCH_PATHS": "$(RPATH_ORIGIN)",
612+
]),
613+
],
614+
buildPhases: [
615+
TestSourcesBuildPhase(),
616+
TestFrameworksBuildPhase([
617+
"MyTests.so"
618+
])
619+
],
620+
dependencies: ["MyTests"]
621+
),
622+
TestStandardTarget(
623+
"MyTests",
624+
type: .unitTest,
625+
buildConfigurations: [
626+
TestBuildConfiguration("Debug", buildSettings: [
627+
"LD_RUNPATH_SEARCH_PATHS": "$(RPATH_ORIGIN)",
628+
"LD_DYLIB_INSTALL_NAME": "MyTests.so"
629+
])
630+
],
631+
buildPhases: [
632+
TestSourcesBuildPhase(["test.swift"]),
633+
TestFrameworksBuildPhase([
634+
TestBuildFile(.target("library")),
635+
])
636+
], dependencies: [
637+
"library"
638+
],
639+
productReferenceName: "MyTests.so"
640+
),
641+
TestStandardTarget(
642+
"library",
643+
type: .dynamicLibrary,
644+
buildConfigurations: [
645+
TestBuildConfiguration("Debug", buildSettings: [
646+
"LD_RUNPATH_SEARCH_PATHS": "$(RPATH_ORIGIN)",
647+
"LD_DYLIB_INSTALL_NAME": "liblibrary.so",
648+
649+
// FIXME: Find a way to make these default
650+
"EXECUTABLE_PREFIX": "lib",
651+
"EXECUTABLE_PREFIX[sdk=windows*]": "",
652+
])
653+
],
654+
buildPhases: [
655+
TestSourcesBuildPhase(["library.swift"]),
656+
],
657+
)
658+
])
659+
let core = try await getCore()
660+
let tester = try await BuildOperationTester(core, testProject, simulated: false)
661+
try localFS.createDirectory(tmpDir.join("index"))
662+
let projectDir = tester.workspace.projects[0].sourceRoot
663+
664+
try await tester.fs.writeFileContents(projectDir.join("library.swift")) { stream in
665+
stream <<< "public func foo() -> Int { 42 }\n"
666+
}
667+
668+
try await tester.fs.writeFileContents(projectDir.join("test.swift")) { stream in
669+
stream <<< """
670+
import Testing
671+
import XCTest
672+
import library
673+
@Suite struct MySuite {
674+
@Test func myTest() {
675+
#expect(foo() == 42)
676+
}
677+
}
678+
679+
final class MYXCTests: XCTestCase {
680+
func testFoo() {
681+
XCTAssertTrue(true)
682+
}
683+
}
684+
"""
685+
}
686+
687+
let destination: RunDestinationInfo = .host
688+
try await tester.checkBuild(runDestination: destination, persistent: true) { results in
689+
results.checkWarning(.prefix("Skipping XCTest discovery for 'MyTests' because it was not built for testing"))
690+
results.checkNoErrors()
691+
692+
let environment = destination.hostRuntimeEnvironment(core)
693+
694+
do {
695+
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)
696+
#expect(String(decoding: executionResult.stdout, as: UTF8.self).contains("Executed 0 tests"))
697+
}
698+
do {
699+
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)
700+
#expect(String(decoding: executionResult.stderr, as: UTF8.self).contains("Test run with 1 test "))
701+
}
702+
}
703+
}
704+
}
705+
579706
/// Check that environment variables are propagated from the user environment correctly.
580707
@Test(.requireSDKs(.host), .skipHostOS(.windows), .requireSystemPackages(apt: "yacc", yum: "byacc"))
581708
func userEnvironment() async throws {

0 commit comments

Comments
 (0)