复制 Xcode SPM 测试的资源文件 [英] Copying Resource Files For Xcode SPM Tests

查看:39
本文介绍了复制 Xcode SPM 测试的资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift Package Manager 的新手,但随着它与 Xcode 11 的集成,是时候尝试一下了.我在新工作区中有一个新应用程序和 SPM 库.我有一个带有测试的工作库,并且已成功将该库导入到应用程序中.

I am new to the Swift Package Manager but with its integration into Xcode 11 it is time to give it a try. I have a new application and SPM library within a new workspace. I have a working library with tests and have successfully imported the library into the application.

我需要使用解析 json 文件的新测试来扩展 SPM 库.我了解到不支持资源目录功能.唯一可行的方案似乎是在库构建过程中添加文件复制步骤,以便可执行文件可以发现资源文件.

I need to extend the SPM library with new tests that parse json files. I have learned that a resources directory feature is not supported. The only workable scheme seems to be a file copy step added to the library build process so that the resource files can be discovered by the executable.

我可以弄清楚如何从命令行执行此操作,但不能使用运行构建和测试的 Xcode.没有复制包资源,swift 包的构建阶段.事实上一切似乎都被Xcode隐藏了.

I could figure out how to do this from the command line but not with Xcode running the build and test. There is no Copy Bundle Resources, build phase for swift packages. In fact everything appears to be hidden by Xcode.

我在 SPM 中查看了 Makefile 类型的文件,这些文件允许我编辑默认的命令行操作从而绕过 Xcode;但我没有看到他们.

I have looked within the SPM for Makefile type files that would allow me to edit default command line actions thereby circumventing Xcode; but I am not seeing them.

是否有某种方法可以交互/控制 Xcode 11 如何构建 SPM 目标,以便我可以将非代码文件复制到测试目标?

Is there some way to interact/control how Xcode 11 builds SPM targets so that I can copy non-code files to test targets?

推荐答案

这是提供对测试资源的访问的另一种解决方法.希望 OP 的问题即将得到解答.

This is another workaround to provide access to test resources. Hopefully an answer to the OP's question will be forthcoming.

使用下面的代码,创建了一个扩展程序,允许调用者创建 URL 来测试这样的资源.

Using the code below, an extension is created to allow callers to create URL's to test resources like this.

let url = URL(forResource: "payload", type: "json")

此代码要求所有资源文件都位于测试目标下名为Resources"的平面目录中.

This code requires that all resource files be located in a flat directory named "Resources" just under the test target.

// 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
    }
}

这篇关于复制 Xcode SPM 测试的资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆