Files
RedLintu16andClaude Opus 5.5 babba1905c Run on Linux too: iPhone apps with xtool 2.0, Android with Gradle
One codebase; on the Mac nothing changes (rl16build --dry-run --for ios
android gives the same output before and after). On Linux:

- ios builds each app's XcodeGen .xcodeproj with `xtool dev build
  --configuration release --scheme <s> --ipa [--sign]`, run in
  ~/.cache/rl16build/xtool/<folder> with an xtool.yml pointing at the
  project, so nothing of xtool's lands in the app's repo. The build number
  goes into the generated project, since xtool takes no build settings.
- macos and watchos are not available on Linux (xtool): no watchOS SDK, and
  Mac apps xtool can't sign.
- What xtool can't build there is skipped, with the reason on the app's
  line and in the summary: an asset catalog, storyboard, xib, Metal, string
  catalog, Core Data or Core ML model, intent definition (Apple's tools,
  which Linux doesn't have), or a watch app inside. What it builds without
  is said on the line: App Intents metadata (xtool skips it on Linux),
  ExtensionKit extensions.
- The .ipa files are never uploaded: xtool signs for development under
  XTL-… bundle IDs, so they aren't the Mac's builds. --upload-only on Linux
  checks each .ipa and sends only the app's own bundle ID with Ad Hoc.
- Upload keys through secret-tool (the same service and account names),
  else $IPA_UPLOAD_KEY/$APK_UPLOAD_KEY, else asked; --key-file keeps one in
  ~/.config/rl16build/<site>.key (600) instead.
- Android: JDK from JAVA_HOME, /usr/lib/jvm/java-17/21-openjdk or
  archlinux-java; SDK from ANDROID_HOME, ~/Android/Sdk or /opt/android-sdk;
  a local.properties naming the Mac's SDK is pointed at this one.
- --extract-watch is macOS-only.

Both systems: no Keystores folder at all stops the Android builds before
anything is built (copy it from the Mac, or --new-keys); RL16BUILD_SWIFT_ROOT,
RL16BUILD_KOTLIN_ROOT and RL16BUILD_OUTPUT_ROOT move the folders; a test
target (swift test, macOS and Linux) covers the logic. README: a Linux
(xtool) section with the Arch setup.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ug8WAdn5H4r3oi1q2LdYaY
2026-11-23 19:47:26 -08:00

331 lines
18 KiB
Swift

import XCTest
@testable import RL16Build
// The logic that decides what gets built, and where, on either system: these
// run under `swift test` on the Mac and in a Linux container alike.
final class PlatformTests: XCTestCase {
func testLinuxCantBuildMacOrWatchApps() {
for platform in ["macOS", "watchOS"] {
let why = Platforms.unavailable(platform, onLinux: true)
XCTAssertNotNil(why, platform)
XCTAssertTrue(why?.hasPrefix("not available on Linux (xtool)") ?? false, platform)
}
for platform in ["iOS", "Android", "Wear OS"] {
XCTAssertNil(Platforms.unavailable(platform, onLinux: true), platform)
}
}
func testTheMacCanBuildEverything() {
for platform in Platforms.all {
XCTAssertNil(Platforms.unavailable(platform, onLinux: false), platform)
}
}
func testPlatformWords() {
XCTAssertEqual(Platforms.parse("ios mac,watch"), ["iOS", "macOS", "watchOS"])
XCTAssertEqual(Platforms.parse("all"), Set(Platforms.all))
XCTAssertNil(Platforms.parse("ios 2048"))
}
}
final class GapTests: XCTestCase {
func testAppleOnlyCompilers() {
XCTAssertEqual(XtoolGap.compiler(for: "Assets.xcassets", type: "folder.assetcatalog")?.tool, "actool")
XCTAssertEqual(XtoolGap.compiler(for: "Media.xcassets", type: nil)?.tool, "actool")
XCTAssertEqual(XtoolGap.compiler(for: "AppIcon.icon", type: nil)?.tool, "actool")
XCTAssertEqual(XtoolGap.compiler(for: "LaunchScreen.storyboard", type: "file.storyboard")?.tool, "ibtool")
XCTAssertEqual(XtoolGap.compiler(for: "Cell.xib", type: nil)?.tool, "ibtool")
XCTAssertEqual(XtoolGap.compiler(for: "Shaders.metal", type: "sourcecode.metal")?.tool, "metal")
XCTAssertEqual(XtoolGap.compiler(for: "Localizable.xcstrings", type: nil)?.tool, "xcstringstool")
XCTAssertEqual(XtoolGap.compiler(for: "Model.xcdatamodeld", type: nil)?.tool, "momc")
XCTAssertNil(XtoolGap.compiler(for: "App.swift", type: "sourcecode.swift"))
XCTAssertNil(XtoolGap.compiler(for: "Raleway-Bold.ttf", type: nil))
XCTAssertNil(XtoolGap.compiler(for: "Localizable.strings", type: "text.plist.strings"))
}
func testSkipReasonsSayWhy() {
let assets = XtoolGap.compilerGap("Assets.xcassets", type: nil)
XCTAssertEqual(assets?.effect, .fails)
XCTAssertEqual(assets?.reason, "Assets.xcassets is an asset catalog, which needs actool (not on Linux)")
XCTAssertEqual(XtoolGap.compilerGap("Main.storyboard", type: nil)?.reason, "Main.storyboard is a storyboard, which needs ibtool (not on Linux)")
XCTAssertEqual(XtoolGap.compilerGap("Blur.metal", type: nil)?.reason, "Blur.metal is a Metal shader, which needs metal (not on Linux)")
XCTAssertEqual(XtoolGap.watchApp("DemoWatch"), .fails("DemoWatch is a watch app, and xtool's Darwin SDK has no watchOS"))
XCTAssertEqual(XtoolGap.appIntents.effect, .degrades)
XCTAssertTrue(XtoolGap.appIntents.reason.contains("App Intents metadata"))
XCTAssertEqual(XtoolGap.extensionKit("Ext").effect, .degrades)
}
func testAppIntentsImport() {
XCTAssertTrue(XtoolGap.usesAppIntents("import SwiftUI\nimport AppIntents\n"))
XCTAssertTrue(XtoolGap.usesAppIntents("@preconcurrency import AppIntents"))
XCTAssertTrue(XtoolGap.usesAppIntents(" import struct AppIntents.IntentParameter"))
XCTAssertFalse(XtoolGap.usesAppIntents("// import AppIntents\nimport SwiftUI"))
XCTAssertFalse(XtoolGap.usesAppIntents("import AppIntentsKit"))
}
}
final class ProjectFileTests: XCTestCase {
let project = PBXProject(data: Data(Fixture.pbxproj.utf8), dir: "/p")!
func testReadsTargetsAndPlatforms() {
XCTAssertEqual(project.targets.map(\.name), ["Demo", "DemoWidget", "DemoWatch", "DemoMac", "Plain"])
XCTAssertEqual(project.targets.first?.productFile, "Demo.app")
XCTAssertEqual(project.platforms(of: "TG0000000000000000DEMO"), ["iOS"])
XCTAssertEqual(project.platforms(of: "TG0000000000000000WTCH"), ["watchOS"])
XCTAssertEqual(project.platforms(of: "TG0000000000000000MACA"), ["macOS"])
// SUPPORTED_PLATFORMS wins over an SDKROOT of auto.
XCTAssertEqual(project.platforms(of: "TG0000000000000000PLAI"), ["iOS"])
// The target's setting over the project's.
XCTAssertEqual(project.setting("CURRENT_PROJECT_VERSION", of: "TG0000000000000000DEMO"), "7")
XCTAssertEqual(project.setting("CURRENT_PROJECT_VERSION", of: "TG0000000000000000WIDG"), "1")
}
func testFollowsDependenciesAndGroups() {
XCTAssertEqual(
project.closure(of: "TG0000000000000000DEMO"),
["TG0000000000000000DEMO", "TG0000000000000000WIDG", "TG0000000000000000WTCH"])
XCTAssertEqual(
project.files(of: "TG0000000000000000DEMO", phase: "PBXResourcesBuildPhase"),
[PBXProject.File(path: "/p/Demo/Assets.xcassets", name: "Assets.xcassets", type: "folder.assetcatalog")])
XCTAssertEqual(project.files(of: "TG0000000000000000WIDG", phase: "PBXSourcesBuildPhase").first?.path, "/p/Demo/Widget/Widget.swift")
XCTAssertEqual(project.files(of: "TG0000000000000000PLAI", phase: "PBXSourcesBuildPhase").first?.path, "/p/Plain/Plain.swift")
XCTAssertEqual(project.localPackages, ["/p/Packages/Kit"])
}
func testGapsOfAnApp() {
let sources = ["/p/Demo/Widget/Widget.swift": "import WidgetKit\nimport AppIntents\n", "/p/Demo/App.swift": "import SwiftUI\n"]
let gaps = XtoolGap.of(app: "TG0000000000000000DEMO", in: project, read: { sources[$0] }, walk: { _ in [] })
XCTAssertEqual(
gaps,
[
.fails("Assets.xcassets is an asset catalog, which needs actool (not on Linux)"),
.fails("DemoWatch is a watch app, and xtool's Darwin SDK has no watchOS"),
XtoolGap.appIntents,
])
// Nothing in the way of the plain app.
XCTAssertEqual(XtoolGap.of(app: "TG0000000000000000PLAI", in: project, read: { _ in "import SwiftUI" }, walk: { _ in [] }), [])
// A local package's asset catalog stops every app that uses the project.
let withPackage = XtoolGap.of(
app: "TG0000000000000000PLAI", in: project, read: { _ in nil },
walk: { $0 == "/p/Packages/Kit/Sources" ? ["/p/Packages/Kit/Sources/Kit/Media.xcassets"] : [] })
XCTAssertEqual(withPackage, [.fails("Media.xcassets is an asset catalog, which needs actool (not on Linux)")])
}
func testBuildNumberGoesIntoEveryConfiguration() {
let stamped = PBXProject.stampBuildNumber(42, into: Fixture.pbxproj)
XCTAssertFalse(stamped.contains("CURRENT_PROJECT_VERSION = 7;"))
XCTAssertEqual(stamped.components(separatedBy: "CURRENT_PROJECT_VERSION = 42;").count - 1, 6)
let reread = PBXProject(data: Data(stamped.utf8), dir: "/p")
XCTAssertNotNil(reread)
for target in reread?.targets ?? [] {
XCTAssertEqual(reread?.setting("CURRENT_PROJECT_VERSION", of: target.id), "42", target.name)
}
}
func testXcconfigLines() {
let settings = PBXProject.xcconfigSettings("SDKROOT = iphoneos // the phone\nFOO[sdk=iphoneos*] = x\n#include \"Base.xcconfig\"\nBAR = a b;\n")
XCTAssertEqual(settings["SDKROOT"] as? String, "iphoneos")
XCTAssertEqual(settings["BAR"] as? String, "a b")
XCTAssertNil(settings["FOO[sdk=iphoneos*]"])
XCTAssertEqual(settings.count, 2)
}
/// The whole Linux discovery, from files on disk.
func testDiscoveryFromDisk() throws {
let dir = Fixture.folder()
defer { try? FileManager.default.removeItem(atPath: dir) }
let xcodeproj = dir + "/Demo.xcodeproj"
try FileManager.default.createDirectory(atPath: xcodeproj + "/xcshareddata/xcschemes", withIntermediateDirectories: true)
try Fixture.pbxproj.write(toFile: xcodeproj + "/project.pbxproj", atomically: true, encoding: .utf8)
try Fixture.scheme(running: "TG0000000000000000DEMO", alsoBuilding: "TG0000000000000000WTCH")
.write(toFile: xcodeproj + "/xcshareddata/xcschemes/Demo.xcscheme", atomically: true, encoding: .utf8)
try Fixture.scheme(running: "TG0000000000000000PLAI")
.write(toFile: xcodeproj + "/xcshareddata/xcschemes/Plain.xcscheme", atomically: true, encoding: .utf8)
let found = try XCTUnwrap(XtoolDiscovery.apps(label: "Demo", project: xcodeproj))
let byName = Dictionary(found.map { ("\($0.build.product)|\($0.build.platform)", $0) }) { a, _ in a }
XCTAssertEqual(Set(byName.keys), ["Demo|iOS", "DemoWatch|watchOS", "DemoMac|macOS", "Plain|iOS"])
XCTAssertEqual(byName["Demo|iOS"]?.build.scheme, "Demo")
XCTAssertTrue(byName["Demo|iOS"]?.gaps.contains { $0.effect == .fails } ?? false)
XCTAssertNil(byName["Demo|iOS"]?.problem)
XCTAssertEqual(byName["Plain|iOS"]?.build.scheme, "Plain")
XCTAssertEqual(byName["Plain|iOS"]?.gaps, [])
// No scheme runs the Mac app: xtool couldn't be pointed at it.
XCTAssertNotNil(byName["DemoMac|macOS"]?.problem)
}
}
final class SchemeTests: XCTestCase {
func testRunActionPicksTheApp() {
let apps: Set = ["APP", "WATCH"]
let both = XtoolScheme.parse(Fixture.scheme(running: "APP", alsoBuilding: "WATCH"))
XCTAssertEqual(both, XtoolScheme.Parsed(runningTargets: ["APP", "WATCH"], runnable: "APP"))
XCTAssertEqual(XtoolScheme.app(of: both, isApp: apps.contains), "APP")
// Two apps and nothing to run: xtool can't tell which.
let noRunnable = XtoolScheme.parse(Fixture.scheme(running: "APP", alsoBuilding: "WATCH", runnable: false))
XCTAssertNil(XtoolScheme.app(of: noRunnable, isApp: apps.contains))
let one = XtoolScheme.parse(Fixture.scheme(running: "APP", runnable: false))
XCTAssertEqual(XtoolScheme.app(of: one, isApp: apps.contains), "APP")
}
func testEntriesNotBuiltForRunningDontCount() {
let xml = Fixture.scheme(running: "APP", runnable: false)
.replacingOccurrences(of: #"buildForRunning="YES""#, with: #"buildForRunning="NO""#)
XCTAssertEqual(XtoolScheme.parse(xml).runningTargets, [])
}
}
final class XtoolTests: XCTestCase {
func testBuildArguments() {
XCTAssertEqual(
Xtool.buildArguments(scheme: "CrossSound-iOS", sign: true),
["xtool", "dev", "build", "--configuration", "release", "--scheme", "CrossSound-iOS", "--ipa", "--sign"])
XCTAssertEqual(
Xtool.buildArguments(scheme: "RL16 Admin", sign: false),
["xtool", "dev", "build", "--configuration", "release", "--scheme", "RL16 Admin", "--ipa"])
}
func testConfigPointsAtTheProject() {
let config = Xtool.config(project: #"/home/u/Documents/Swift/My "App"/App.xcodeproj"#)
XCTAssertTrue(config.contains("version: 1\n"))
XCTAssertTrue(config.contains(#"project: "/home/u/Documents/Swift/My \"App\"/App.xcodeproj""#))
XCTAssertTrue(config.contains("skipLSP: true\n"))
XCTAssertFalse(config.contains("scheme:"))
}
func testVersions() {
XCTAssertEqual(Xtool.version("xtool 2.0.0-beta.1")?.major, 2)
XCTAssertEqual(Xtool.version("xtool 2.0.0-beta.1")?.text, "2.0.0-beta.1")
XCTAssertEqual(Xtool.version("xtool 1.24.0\n")?.major, 1)
XCTAssertNil(Xtool.version("command not found"))
}
func testWhatXtoolWrote() {
XCTAssertEqual(Xtool.writtenIPA("[100.00%] Build completed.\nPackaging...\nWrote to /c/xtool/Demo.ipa\n"), "/c/xtool/Demo.ipa")
XCTAssertNil(Xtool.writtenIPA("Wrote to /c/xtool/Demo.app\n"))
}
func testWhenXtoolCantBuild() {
var status = Xtool.Status()
XCTAssertTrue(Xtool.problem(status)?.contains("2.0 beta") ?? false)
status.path = "/usr/local/bin/xtool"
status.version = (1, "1.24.0")
XCTAssertEqual(Xtool.problem(status), "xtool 1.24.0 (/usr/local/bin/xtool) can't build Xcode projects: rl16build needs the 2.0 beta")
status.version = (2, "2.0.0-beta.1")
XCTAssertTrue(Xtool.problem(status)?.contains("xtool setup") ?? false)
status.sdk = true
XCTAssertNil(Xtool.problem(status))
XCTAssertTrue(Xtool.lines(status).last?.contains("unsigned") ?? false)
status.loggedIn = true
XCTAssertTrue(Xtool.lines(status).last?.contains("development") ?? false)
}
}
final class IPATests: XCTestCase {
let development: [String: Any] = ["Entitlements": ["get-task-allow": true], "ProvisionedDevices": ["a"]]
let adHoc: [String: Any] = ["Entitlements": ["get-task-allow": false], "ProvisionedDevices": ["a", "b"]]
func testOnlyTheMacsKindOfBuildGoesUp() {
XCTAssertTrue(
IPACheck.uploadProblem(bundleID: "XTL-ABCDE12345.fi.RedLintu16.CrossSound", profile: development)?.contains("xtool's") ?? false)
XCTAssertTrue(IPACheck.uploadProblem(bundleID: "fi.RedLintu16.CrossSound", profile: development)?.contains("development") ?? false)
XCTAssertTrue(IPACheck.uploadProblem(bundleID: "fi.RedLintu16.CrossSound", profile: nil)?.contains("no profile") ?? false)
XCTAssertNotNil(IPACheck.uploadProblem(bundleID: nil, profile: adHoc))
XCTAssertNil(IPACheck.uploadProblem(bundleID: "fi.RedLintu16.CrossSound", profile: adHoc))
}
func testProfileKinds() {
XCTAssertEqual(IPACheck.profileKind(development), "development")
XCTAssertEqual(IPACheck.profileKind(adHoc), "Ad Hoc")
XCTAssertEqual(IPACheck.profileKind(["ProvisionsAllDevices": true]), "enterprise")
XCTAssertEqual(IPACheck.profileKind([:]), "App Store")
XCTAssertEqual(IPACheck.profileKind(nil), "no")
}
func testReadingAProfile() {
let xml = #"<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict><key>Name</key><string>XTL profile 1</string></dict></plist>"#
let envelope = Data([0x30, 0x82, 0x01]) + Data(xml.utf8) + Data([0x00, 0xA0])
XCTAssertEqual(IPAInfo.profilePlist(envelope)?["Name"] as? String, "XTL profile 1")
XCTAssertNil(IPAInfo.profilePlist(Data("no plist here".utf8)))
}
func testMainAppEntries() {
XCTAssertTrue(IPAInfo.isMainApp("Payload/CrossSound.app/Info.plist", file: "Info.plist"))
XCTAssertFalse(IPAInfo.isMainApp("Payload/CrossSound.app/PlugIns/Widget.appex/Info.plist", file: "Info.plist"))
XCTAssertFalse(IPAInfo.isMainApp("Payload/CrossSound.app/Watch/W.app/Info.plist", file: "Info.plist"))
XCTAssertEqual(IPAInfo.unzipPattern("Payload/A[1]*?.app/Info.plist"), "Payload/A[[]1][*][?].app/Info.plist")
}
}
final class KeystoreGuardTests: XCTestCase {
func testNoFolderStopsTheAndroidBuilds() {
let problem = Keystores.missingFolderProblem(folderExists: false, newKeys: false, dir: "/home/u/Documents/Kotlin/Keystores")
XCTAssertNotNil(problem)
XCTAssertTrue(problem?.contains("/home/u/Documents/Kotlin/Keystores doesn't exist") ?? false)
XCTAssertTrue(problem?.contains("--new-keys") ?? false)
XCTAssertTrue(problem?.contains("passwords.txt") ?? false)
}
func testNewKeysOrAFolderGoesOn() {
XCTAssertNil(Keystores.missingFolderProblem(folderExists: false, newKeys: true, dir: "/k"))
XCTAssertNil(Keystores.missingFolderProblem(folderExists: true, newKeys: false, dir: "/k"))
}
}
final class RootTests: XCTestCase {
func testDefaultsAreTheDocumentsFolders() {
XCTAssertEqual(
Roots.resolve(environment: [:], home: "/h/u", cwd: "/w"),
Roots(swift: "/h/u/Documents/Swift", kotlin: "/h/u/Documents/Kotlin", output: "/h/u/Documents/Swift/Build"))
}
func testOverrides() {
let roots = Roots.resolve(
environment: ["RL16BUILD_SWIFT_ROOT": "~/src/swift", "RL16BUILD_KOTLIN_ROOT": "/srv/kotlin/", "RL16BUILD_OUTPUT_ROOT": "out"],
home: "/h/u", cwd: "/w")
XCTAssertEqual(roots, Roots(swift: "/h/u/src/swift", kotlin: "/srv/kotlin", output: "/w/out"))
// The output follows a moved Swift folder unless it is moved itself.
XCTAssertEqual(Roots.resolve(environment: ["RL16BUILD_SWIFT_ROOT": "/s"], home: "/h/u", cwd: "/w").output, "/s/Build")
// An empty variable is no override.
XCTAssertEqual(Roots.resolve(environment: ["RL16BUILD_SWIFT_ROOT": " "], home: "/h/u", cwd: "/w").swift, "/h/u/Documents/Swift")
}
}
final class LinuxPieceTests: XCTestCase {
func testLocalPropertiesGetsTheSDK() {
let sdk = "/home/u/Android/Sdk"
XCTAssertEqual(LocalProperties.pointing(nil, at: sdk, exists: { _ in false }), "sdk.dir=/home/u/Android/Sdk\n")
XCTAssertEqual(
LocalProperties.pointing("sdk.dir=/Users/u/Library/Android/sdk\nredwhite.dir=../RedWhiteUI\n", at: sdk, exists: { _ in false }),
"sdk.dir=/home/u/Android/Sdk\nredwhite.dir=../RedWhiteUI\n")
XCTAssertEqual(LocalProperties.pointing("redwhite.dir=x", at: sdk, exists: { _ in false }), "redwhite.dir=x\nsdk.dir=/home/u/Android/Sdk\n")
// One that names an SDK that is there, or this one, stays.
XCTAssertNil(LocalProperties.pointing("sdk.dir=/opt/android-sdk\n", at: sdk, exists: { $0 == "/opt/android-sdk" }))
XCTAssertNil(LocalProperties.pointing("sdk.dir=\(sdk)\n", at: sdk, exists: { _ in false }))
}
func testSecretToolNeverSeesTheKey() {
XCTAssertEqual(
SecretTool.lookup(service: "ipas.redlintu16.com", account: "upload-key"),
["secret-tool", "lookup", "service", "ipas.redlintu16.com", "account", "upload-key"])
let store = SecretTool.store(service: "apks.redlintu16.com", account: "upload-key", label: "apks.redlintu16.com upload key")
XCTAssertEqual(store, ["secret-tool", "store", "--label=apks.redlintu16.com upload key", "service", "apks.redlintu16.com", "account", "upload-key"])
}
func testKeyFileIsOnlyYours() throws {
let home = Fixture.folder()
defer { try? FileManager.default.removeItem(atPath: home) }
XCTAssertNil(KeyFile.read(for: "ipas.redlintu16.com", home: home))
XCTAssertNil(KeyFile.save("s3cret", for: "ipas.redlintu16.com", home: home))
XCTAssertEqual(KeyFile.read(for: "ipas.redlintu16.com", home: home), "s3cret")
let file = KeyFile.path(for: "ipas.redlintu16.com", home: home)
XCTAssertEqual(file, home + "/.config/rl16build/ipas.redlintu16.com.key")
let fileMode = try FileManager.default.attributesOfItem(atPath: file)[.posixPermissions] as? Int
let dirMode = try FileManager.default.attributesOfItem(atPath: (file as NSString).deletingLastPathComponent)[.posixPermissions] as? Int
XCTAssertEqual(fileMode, 0o600)
XCTAssertEqual(dirMode, 0o700)
}
}