Skip to content

Commit 8a51218

Browse files
authored
Merge pull request #283 from swiftwasm/release/5.3
[pull] swiftwasm-release/5.3 from release/5.3
2 parents 25cf90a + aa589c7 commit 8a51218

File tree

8 files changed

+101
-16
lines changed

8 files changed

+101
-16
lines changed

IntegrationTests/Tests/IntegrationTests/BasicTests.swift

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,80 @@ final class BasicTests: XCTestCase {
232232
}
233233
}
234234
}
235+
236+
func testSwiftTestWithResources() throws {
237+
try withTemporaryDirectory { dir in
238+
let toolDir = dir.appending(component: "swiftTestResources")
239+
try localFileSystem.createDirectory(toolDir)
240+
try localFileSystem.writeFileContents(
241+
toolDir.appending(component: "Package.swift"),
242+
bytes: ByteString(encodingAsUTF8: """
243+
// swift-tools-version:5.3
244+
import PackageDescription
245+
246+
let package = Package(
247+
name: "AwesomeResources",
248+
targets: [
249+
.target(name: "AwesomeResources", resources: [.copy("hello.txt")]),
250+
.testTarget(name: "AwesomeResourcesTest", dependencies: ["AwesomeResources"], resources: [.copy("world.txt")])
251+
]
252+
)
253+
""")
254+
)
255+
try localFileSystem.createDirectory(toolDir.appending(component: "Sources"))
256+
try localFileSystem.createDirectory(toolDir.appending(components: "Sources", "AwesomeResources"))
257+
try localFileSystem.writeFileContents(
258+
toolDir.appending(components: "Sources", "AwesomeResources", "AwesomeResource.swift"),
259+
bytes: ByteString(encodingAsUTF8: """
260+
import Foundation
261+
262+
public struct AwesomeResource {
263+
public init() {}
264+
public let hello = try! String(contentsOf: Bundle.module.url(forResource: "hello", withExtension: "txt")!)
265+
}
266+
267+
""")
268+
)
269+
270+
try localFileSystem.writeFileContents(
271+
toolDir.appending(components: "Sources", "AwesomeResources", "hello.txt"),
272+
bytes: ByteString(encodingAsUTF8: "hello")
273+
)
274+
275+
try localFileSystem.createDirectory(toolDir.appending(component: "Tests"))
276+
try localFileSystem.createDirectory(toolDir.appending(components: "Tests", "AwesomeResourcesTest"))
277+
278+
try localFileSystem.writeFileContents(
279+
toolDir.appending(components: "Tests", "AwesomeResourcesTest", "world.txt"),
280+
bytes: ByteString(encodingAsUTF8: "world")
281+
)
282+
283+
try localFileSystem.writeFileContents(
284+
toolDir.appending(components: "Tests", "AwesomeResourcesTest", "MyTests.swift"),
285+
bytes: ByteString(encodingAsUTF8: """
286+
import XCTest
287+
import Foundation
288+
import AwesomeResources
289+
290+
final class MyTests: XCTestCase {
291+
func testFoo() {
292+
XCTAssertTrue(AwesomeResource().hello == "hello")
293+
}
294+
func testBar() {
295+
let world = try! String(contentsOf: Bundle.module.url(forResource: "world", withExtension: "txt")!)
296+
XCTAssertTrue(world == "world")
297+
}
298+
}
299+
"""))
300+
301+
let testOutput = try sh(swiftTest, "--package-path", toolDir, "--filter", "MyTests.*").stderr
302+
303+
// Check the test log.
304+
XCTAssertContents(testOutput) { checker in
305+
checker.check(.contains("Test Suite 'MyTests' started"))
306+
checker.check(.contains("Test Suite 'MyTests' passed"))
307+
checker.check(.contains("Executed 2 tests, with 0 failures"))
308+
}
309+
}
310+
}
235311
}

Sources/Build/BuildPlan.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,19 +578,21 @@ public final class SwiftTargetBuildDescription {
578578
// Do nothing if we're not generating a bundle.
579579
guard let bundlePath = self.bundlePath else { return }
580580

581-
// Compute the basename of the bundle.
582-
let bundleBasename = bundlePath.basename
583-
584581
let stream = BufferedOutputByteStream()
585582
stream <<< """
586583
import class Foundation.Bundle
587584
588585
extension Foundation.Bundle {
589586
static var module: Bundle = {
590-
let bundlePath = Bundle.main.bundlePath + "/" + "\(bundleBasename)"
591-
guard let bundle = Bundle(path: bundlePath) else {
592-
fatalError("could not load resource bundle: \\(bundlePath)")
587+
let mainPath = Bundle.main.bundlePath + "/" + "\(bundlePath.basename)"
588+
let buildPath = "\(bundlePath.pathString)"
589+
590+
let preferredBundle = Bundle(path: mainPath)
591+
592+
guard let bundle = preferredBundle != nil ? preferredBundle : Bundle(path: buildPath) else {
593+
fatalError("could not load resource bundle: from \\(mainPath) or \\(buildPath)")
593594
}
595+
594596
return bundle
595597
}()
596598
}

Sources/Build/XCFrameworkInfo.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

1111
import TSCBasic
12+
import TSCUtility
1213
import PackageModel
1314
import SPMBuildCore
1415
import Foundation

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
494494
}
495495
}
496496

497+
private static var _hostTriple: Triple?
497498
private static var _packageDescriptionMinimumDeploymentTarget: String?
498499

499500
/// Parse the manifest at the given path to JSON.
@@ -558,11 +559,15 @@ public final class ManifestLoader: ManifestLoaderProtocol {
558559

559560
// Use the same minimum deployment target as the PackageDescription library (with a fallback of 10.15).
560561
#if os(macOS)
562+
if Self._hostTriple == nil {
563+
Self._hostTriple = Triple.getHostTriple(usingSwiftCompiler: resources.swiftCompiler)
564+
}
565+
let triple = Self._hostTriple!
561566
if Self._packageDescriptionMinimumDeploymentTarget == nil {
562567
Self._packageDescriptionMinimumDeploymentTarget = (try MinimumDeploymentTarget.computeMinimumDeploymentTarget(of: macOSPackageDescriptionPath))?.versionString ?? "10.15"
563568
}
564569
let version = Self._packageDescriptionMinimumDeploymentTarget!
565-
cmd += ["-target", "x86_64-apple-macosx\(version)"]
570+
cmd += ["-target", "\(triple.tripleString(forPlatformVersion: version))"]
566571
#endif
567572

568573
cmd += compilerFlags

Sources/SPMBuildCore/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ add_library(SPMBuildCore
1111
BuildSystem.swift
1212
BuiltTestProduct.swift
1313
Sanitizers.swift
14-
Toolchain.swift
15-
Triple.swift)
14+
Toolchain.swift)
1615
# NOTE(compnerd) workaround for CMake not setting up include flags yet
1716
set_target_properties(SPMBuildCore PROPERTIES
1817
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ final class BuildPlanTests: XCTestCase {
5959
config: BuildConfiguration = .debug,
6060
flags: BuildFlags = BuildFlags(),
6161
shouldLinkStaticSwiftStdlib: Bool = false,
62-
destinationTriple: Triple = hostTriple,
62+
destinationTriple: TSCUtility.Triple = hostTriple,
6363
indexStoreMode: BuildParameters.IndexStoreMode = .off
6464
) -> BuildParameters {
6565
return BuildParameters(
@@ -75,7 +75,7 @@ final class BuildPlanTests: XCTestCase {
7575
}
7676

7777
func mockBuildParameters(environment: BuildEnvironment) -> BuildParameters {
78-
let triple: Triple
78+
let triple: TSCUtility.Triple
7979
switch environment.platform {
8080
case .macOS:
8181
triple = Triple.macOS
@@ -1765,7 +1765,7 @@ final class BuildPlanTests: XCTestCase {
17651765
)
17661766
XCTAssertNoDiagnostics(diagnostics)
17671767

1768-
func createResult(for dest: Triple) throws -> BuildPlanResult {
1768+
func createResult(for dest: TSCUtility.Triple) throws -> BuildPlanResult {
17691769
return BuildPlanResult(plan: try BuildPlan(
17701770
buildParameters: mockBuildParameters(destinationTriple: dest),
17711771
graph: graph, diagnostics: diagnostics,
@@ -2207,7 +2207,8 @@ final class BuildPlanTests: XCTestCase {
22072207
])
22082208
}
22092209

2210-
func testBinaryTargets(platform: String, arch: String, destinationTriple: Triple) throws {
2210+
func testBinaryTargets(platform: String, arch: String, destinationTriple: TSCUtility.Triple)
2211+
throws {
22112212
let fs = InMemoryFileSystem(emptyFiles:
22122213
"/Pkg/Sources/exe/main.swift",
22132214
"/Pkg/Sources/Library/Library.swift",
@@ -2345,9 +2346,9 @@ final class BuildPlanTests: XCTestCase {
23452346

23462347
func testBinaryTargets() throws {
23472348
try testBinaryTargets(platform: "macos", arch: "x86_64", destinationTriple: .macOS)
2348-
let arm64Triple = try Triple("arm64-apple-macosx")
2349+
let arm64Triple = try TSCUtility.Triple("arm64-apple-macosx")
23492350
try testBinaryTargets(platform: "macos", arch: "arm64", destinationTriple: arm64Triple)
2350-
let arm64eTriple = try Triple("arm64e-apple-macosx")
2351+
let arm64eTriple = try TSCUtility.Triple("arm64e-apple-macosx")
23512352
try testBinaryTargets(platform: "macos", arch: "arm64e", destinationTriple: arm64eTriple)
23522353
}
23532354

@@ -2481,7 +2482,7 @@ fileprivate extension TargetBuildDescription {
24812482
}
24822483
}
24832484

2484-
fileprivate extension Triple {
2485+
fileprivate extension TSCUtility.Triple {
24852486
static let x86_64Linux = try! Triple("x86_64-unknown-linux-gnu")
24862487
static let arm64Linux = try! Triple("aarch64-unknown-linux-gnu")
24872488
static let arm64Android = try! Triple("aarch64-unknown-linux-android")

swift-tools-support-core/Sources/TSCUtility/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_library(TSCUtility
2929
SimplePersistence.swift
3030
StringExtensions.swift
3131
StringMangling.swift
32+
Triple.swift
3233
URL.swift
3334
Verbosity.swift
3435
Version.swift

0 commit comments

Comments
 (0)