Skip to content

Commit 7ee2e09

Browse files
authored
Ensure testable variants of targets depend on the primary variant's resource bundle (#9486)
This is required in order to ensure that package resource accessors are correctly generated for the testable variants Closes #9459 Depends on swiftlang/swift-build#967
1 parent accc945 commit 7ee2e09

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// swift-tools-version: 6.0
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "TestableExe",
6+
targets: [
7+
.executableTarget(
8+
name: "TestableExe",
9+
resources: [.embedInCode("foo.txt")]
10+
),
11+
.testTarget(
12+
name: "TestableExeTests",
13+
dependencies: [
14+
"TestableExe",
15+
]
16+
),
17+
]
18+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bar
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public func GetGreeting1() -> String {
2+
return String(decoding: PackageResources.foo_txt, as: UTF8.self)
3+
}
4+
5+
print("\(GetGreeting1())!")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import XCTest
2+
@testable import TestableExe
3+
4+
final class TestableExeTests: XCTestCase {
5+
func testExample() throws {
6+
XCTAssertEqual(GetGreeting1(), "bar\n")
7+
}
8+
}

Sources/SwiftBuildSupport/PackagePIFProjectBuilder+Modules.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ extension PackagePIFProjectBuilder {
317317
// `inputResourceBundleName`.
318318
shouldGenerateBundleAccessor = true
319319
shouldGenerateEmbedInCodeAccessor = true
320+
if resourceBundleName != nil {
321+
let resourceTargetID = pifTargetIdForResourceBundle(sourceModule.name)
322+
self.project[keyPath: sourceModuleTargetKeyPath].common.addDependency(
323+
on: resourceTargetID,
324+
platformFilters: [],
325+
linkProduct: false
326+
)
327+
}
320328
}
321329

322330
// Find the PIF target for the resource bundle, if any. Otherwise fall back to the module.

Tests/CommandsTests/TestCommandTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,4 +1251,29 @@ struct TestCommandTests {
12511251
}
12521252
}
12531253

1254+
@Test(
1255+
.IssueWindowsLongPath,
1256+
.tags(
1257+
.Feature.TargetType.Executable,
1258+
),
1259+
arguments: SupportedBuildSystemOnAllPlatforms, BuildConfiguration.allCases,
1260+
)
1261+
func testableExecutableWithEmbeddedResources(
1262+
buildSystem: BuildSystemProvider.Kind,
1263+
configuration: BuildConfiguration,
1264+
) async throws {
1265+
try await withKnownIssue(isIntermittent: true) {
1266+
try await fixture(name: "Miscellaneous/TestableExeWithResources") { fixturePath in
1267+
let result = try await execute(
1268+
["--vv"],
1269+
packagePath: fixturePath,
1270+
configuration: configuration,
1271+
buildSystem: buildSystem,
1272+
)
1273+
}
1274+
} when: {
1275+
.windows == ProcessInfo.hostOperatingSystem
1276+
}
1277+
}
1278+
12541279
}

0 commit comments

Comments
 (0)