在路径 SPM swift 中获取文件
getting file in path SPM swift
我在 SPM 中有一个包,我正在处理文件系统。我现在面临的问题是当我尝试获取 json 文件之类的文件时,我的测试用例失败了。这是我的代码和测试用例。
MT测试
func testBundle() -> Bundle? {
return Bundle(for: LandingPageSerializationTests.self)
}
func parseLandingPage(fromFile filename: String?) -> LandingPage? {
let path = testBundle()?.path(forResource: filename, ofType: nil, inDirectory: kConfigFileFolder)
XCTAssertNotNil(path)
let data = NSData(contentsOfFile: path) as Data?
XCTAssertNotNil(data)
var asDict: [String: Any]? = nil
do {
if let data = data {
asDict = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]
}
} catch {
}
XCTAssertNotNil(asDict)
return LandingPageSerialization.landingPage(fromJson: asDict)
}
我的Package.swift
let package = Package(
name: "XXXX",
platforms: [.iOS(.v11)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "XXXX",
targets: ["XXXX"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "XXXX",
dependencies: [
],
resources: [
.process("Resources"),
]
),
.testTarget(
name: "XXXXTests",
dependencies: ["XXXX"],
resources: [
.process("Resources"),
]
),
]
)
我不知道为什么尽管提到了特定的名称,但我的路径总是得到 nil。
行
resources: [
.process("Resources"),
]
如果这是一个名为 Resources 的文件,那就对了。如果 Resources 是一个文件夹,您需要 .copy
,而不是 .process
。
此外,我认为您无法按照您尝试的方式从包的测试目标包中获取资源。您需要从包的主要目标包中获取它,您可以说 Bundle.module
.
这是一个演示。这是我的包裹的结构:
这是我的包裹声明:
let package = Package(
name: "XXXXX",
products: [
.library(
name: "XXXXX",
targets: ["XXXXX"]),
],
dependencies: [
],
targets: [
.target(
name: "XXXXX",
dependencies: [],
resources: [.copy("Resources")]),
.testTarget(
name: "XXXXXTests",
dependencies: ["XXXXX"]),
]
)
这是一个有效的测试方法:
func testExample() throws {
let b = Bundle.module
let url = b.url(forResource: "test", withExtension: "json", subdirectory: "Resources")
let urlreal = try XCTUnwrap(url)
let data = try Data(contentsOf: urlreal)
let result = try JSONSerialization.jsonObject(with: data, options: [])
let resultreal = try XCTUnwrap(result as? [String:Any]) // dictionary
XCTAssertEqual(resultreal["name"] as? String ?? "", "Matt")
}
我在 SPM 中有一个包,我正在处理文件系统。我现在面临的问题是当我尝试获取 json 文件之类的文件时,我的测试用例失败了。这是我的代码和测试用例。
MT测试
func testBundle() -> Bundle? {
return Bundle(for: LandingPageSerializationTests.self)
}
func parseLandingPage(fromFile filename: String?) -> LandingPage? {
let path = testBundle()?.path(forResource: filename, ofType: nil, inDirectory: kConfigFileFolder)
XCTAssertNotNil(path)
let data = NSData(contentsOfFile: path) as Data?
XCTAssertNotNil(data)
var asDict: [String: Any]? = nil
do {
if let data = data {
asDict = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]
}
} catch {
}
XCTAssertNotNil(asDict)
return LandingPageSerialization.landingPage(fromJson: asDict)
}
我的Package.swift
let package = Package(
name: "XXXX",
platforms: [.iOS(.v11)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "XXXX",
targets: ["XXXX"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "XXXX",
dependencies: [
],
resources: [
.process("Resources"),
]
),
.testTarget(
name: "XXXXTests",
dependencies: ["XXXX"],
resources: [
.process("Resources"),
]
),
]
)
我不知道为什么尽管提到了特定的名称,但我的路径总是得到 nil。
行
resources: [
.process("Resources"),
]
如果这是一个名为 Resources 的文件,那就对了。如果 Resources 是一个文件夹,您需要 .copy
,而不是 .process
。
此外,我认为您无法按照您尝试的方式从包的测试目标包中获取资源。您需要从包的主要目标包中获取它,您可以说 Bundle.module
.
这是一个演示。这是我的包裹的结构:
这是我的包裹声明:
let package = Package(
name: "XXXXX",
products: [
.library(
name: "XXXXX",
targets: ["XXXXX"]),
],
dependencies: [
],
targets: [
.target(
name: "XXXXX",
dependencies: [],
resources: [.copy("Resources")]),
.testTarget(
name: "XXXXXTests",
dependencies: ["XXXXX"]),
]
)
这是一个有效的测试方法:
func testExample() throws {
let b = Bundle.module
let url = b.url(forResource: "test", withExtension: "json", subdirectory: "Resources")
let urlreal = try XCTUnwrap(url)
let data = try Data(contentsOf: urlreal)
let result = try JSONSerialization.jsonObject(with: data, options: [])
let resultreal = try XCTUnwrap(result as? [String:Any]) // dictionary
XCTAssertEqual(resultreal["name"] as? String ?? "", "Matt")
}