Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
appstore-distribution ▾

xtool.git / Tests / XToolTests / RequiresXcodeTrait.swift

RequiresXcodeTrait.swift63 lines · 2.0 KiBRaw
1import Foundation
2import Testing
3import Subprocess
4
5enum TestXcode {
6 /// A path to a copy of Xcode.app.
7 ///
8 /// Tests that use this must be annotated with `Trait.requiresXcode`.
9 static func current(sourceLocation: SourceLocation = #_sourceLocation) -> URL {
10 guard let url = TestXcode._current else {
11 Issue.record(
12 "TestXcode.current was used without Trait.requiresXcode()",
13 sourceLocation: sourceLocation
14 )
15 return URL(filePath: "/dev/null")
16 }
17 return url
18 }
19
20 @TaskLocal fileprivate static var _current: URL?
21
22 fileprivate static let resolved = Task<URL?, Error> {
23 if let xcode = ProcessInfo.processInfo.environment["XTL_TEST_XCODE_APP"], !xcode.isEmpty {
24 return URL(filePath: xcode)
25 }
26 #if os(macOS)
27 let output = try await Subprocess.run(
28 .path("/usr/bin/xcode-select"),
29 arguments: ["-p"],
30 output: .string(limit: .max),
31 ).standardOutput.trimmingCharacters(in: .whitespacesAndNewlines)
32 if output.hasSuffix(".app/Contents/Developer") {
33 return URL(filePath: output).deletingLastPathComponent().deletingLastPathComponent()
34 }
35 #endif
36 let candidate = URL(filePath: "/usr/local/share/Xcode.app")
37 if FileManager.default.fileExists(atPath: candidate.path) {
38 return candidate
39 }
40 return nil
41 }
42}
43
44struct RequiresXcodeTrait: TestTrait, SuiteTrait, TestScoping {
45 func provideScope(
46 for test: Test,
47 testCase: Test.Case?,
48 performing function: () async throws -> Void
49 ) async throws {
50 guard let xcode = try? await TestXcode.resolved.value else {
51 try Test.cancel("Could not resolve Xcode.app path. Set XTL_TEST_XCODE_APP.")
52 }
53 return try await TestXcode.$_current.withValue(xcode) {
54 try await function()
55 }
56 }
57}
58
59extension Trait where Self == RequiresXcodeTrait {
60 static var requiresXcode: Self {
61 Self()
62 }
63}