我是Swift软件包管理器的新手,但是由于它已集成到Xcode 11中,因此现在可以尝试一下。我在新的工作区中有一个新的应用程序和SPM库。我有一个带有测试的工作库,并且已成功将该库导入到应用程序中。
我需要使用解析json文件的新测试来扩展SPM库。我了解到不支持资源目录功能。唯一可行的方案似乎是添加到库构建过程中的文件复制步骤,以便可执行文件可以发现资源文件。
我可以从命令行中找出方法,但不能通过Xcode运行构建和测试。没有“副本捆绑包资源”,即快速包的构建阶段。实际上,所有内容似乎都被Xcode隐藏。
我在SPM中查找了Makefile类型的文件,这些文件使我可以编辑默认命令行操作,从而规避Xcode;但我没看到他们。
是否有某种方式可以交互/控制Xcode 11如何构建SPM目标,以便可以将非代码文件复制到测试目标?
答案 0 :(得分:0)
让它正常工作!
struct Resource {
let name: String
let type: String
let url: URL
init(name: String, type: String, sourceFile: StaticString = #file) throws {
self.name = name
self.type = type
// The following assumes that your test source files are all in the same directory, and the resources are one directory down and over
// <Some folder>
// - Resources
// - <resource files>
// - <Some test source folder>
// - <test case files>
let testCaseURL = URL(fileURLWithPath: "\(sourceFile)", isDirectory: false)
let testsFolderURL = testCaseURL.deletingLastPathComponent()
let resourcesFolderURL = testsFolderURL.deletingLastPathComponent().appendingPathComponent("Resources", isDirectory: true)
self.url = resourcesFolderURL.appendingPathComponent("\(name).\(type)", isDirectory: false)
}
}
用法:
final class SPMTestDataTests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(SPMTestData().text, "Hello, World!")
let file = try Resource(name: "image", type: "png")
let image = UIImage(contentsOfFile: file.url.path)
print(image)
}
}
我发现了使用#file
here
答案 1 :(得分:0)
这是提供对测试资源的访问的另一种解决方法。希望对OP的问题的答案能很快到来。
使用下面的代码,创建一个扩展名,以允许调用者创建URL来测试这样的资源。
let url = URL(forResource: "payload", type: "json")
此代码要求所有资源文件都位于测试目标下方名为“ Resources”的平面目录中。
// MARK: - ./Resources/ Workaround
// URL of the directory containing non-code, test resource fi;es.
//
// It is required that a directory named "Resources" be contained immediately below the test target.
// Root
// Package.swift
// Tests
// (target)
// Resources
//
fileprivate let _resources: URL = {
func packageRoot(of file: String) -> URL? {
func isPackageRoot(_ url: URL) -> Bool {
let filename = url.appendingPathComponent("Package.swift", isDirectory: false)
return FileManager.default.fileExists(atPath: filename.path)
}
var url = URL(fileURLWithPath: file, isDirectory: false)
repeat {
url = url.deletingLastPathComponent()
if url.pathComponents.count <= 1 {
return nil
}
} while !isPackageRoot(url)
return url
}
guard let root = packageRoot(of: #file) else {
fatalError("\(#file) must be contained in a Swift Package Manager project.")
}
let fileComponents = URL(fileURLWithPath: #file, isDirectory: false).pathComponents
let rootComponenets = root.pathComponents
let trailingComponents = Array(fileComponents.dropFirst(rootComponenets.count))
let resourceComponents = rootComponenets + trailingComponents[0...1] + ["Resources"]
return URL(fileURLWithPath: resourceComponents.joined(separator: "/"), isDirectory: true)
}()
extension URL {
init(forResource name: String, type: String) {
let url = _resources.appendingPathComponent("\(name).\(type)", isDirectory: false)
self = url
}
}