Skip to content
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

[SR-13714] 'Swift test' fails when including package resources #4489

Open
BrentMifsud opened this issue Oct 9, 2020 · 1 comment
Open

[SR-13714] 'Swift test' fails when including package resources #4489

BrentMifsud opened this issue Oct 9, 2020 · 1 comment
Labels

Comments

@BrentMifsud
Copy link

Previous ID SR-13714
Radar None
Original Reporter @BrentMifsud
Type Bug
Status Reopened
Resolution
Environment

macOS Catalina 10.15.7

Additional Detail from JIRA
Votes 3
Component/s Package Manager
Labels Bug, swift-package-resource, swift5.3
Assignee None
Priority Medium

md5: d604344178dcd72bfa14ab736f8a5205

duplicates:

  • SR-13560 swiftpm: SWIFTPM_MODULE_BUNDLE returning nil

Issue Description:

After including a package resource to my swift package, I get the following error when running swift test commands from the command line:

Fatal error: could not load resource bundle: /Applications/Xcode.app/Contents/Developer/usr/bin/BeartoothSDK_BeartoothSDKTests.bundle: file BeartoothSDKTests/resource_bundle_accessor.swift, line 7
Exited with signal code 4

Some additional information:

  • The swift package builds and tests run without issue in Xcode 12

  • I am using `Bundle.module` to access the resources.

  • The resources are only used in my test target for test cases.

Here is a copy of my package manifest:

// swift-tools-version:5.3

import PackageDescription

let package = Package(
    name: "BeartoothSDK",
    products: [
        .library(name: "BeartoothSDK", targets: ["BeartoothSDK"]),
    ],
    dependencies: [],
    targets: [
        .target(name: "BeartoothSDK", dependencies: []),
        .testTarget(
            name: "BeartoothSDKTests",
            dependencies: ["BeartoothSDK"],
            resources: [.process("Resources")]
        ),
    ]
)

Here is a copy of test class using the bundle:

//
//  JsonReader.swift
//  
//
//  Created by Brent Mifsud on 2020-09-15.
//

import Foundation
@testable import BeartoothSDK
import XCTest

internal class JsonReader {
    private let bundle = Bundle.module

    func readJsonFile(named name: String) throws -> Data {
        guard let path = bundle.url(forResource: name, withExtension: "json") else {
            throw JsonReadError.fileNotFound("File not found: \(name)")
        }
        
        return try Data(contentsOf: path, options: .mappedIfSafe)
    }
    
    internal enum JsonReadError: Error {
        case fileNotFound(String)
    }
}

class JsonReaderTest: XCTestCase {
    private let fileName = "single-BlogPost"
    private let sut = JsonReader()
    
    func testReadJsonFile() throws {
        let jsonData = try sut.readJsonFile(named: fileName)
        XCTAssertNotNil(jsonData)
    }
}
@BrentMifsud
Copy link
Author

related to: SR-13560

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants