181 lines
8.3 KiB
Python
181 lines
8.3 KiB
Python
|
|
"""Offline subprocess checks for archive orchestration, never Xcode acceptance."""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import os
|
||
|
|
from pathlib import Path
|
||
|
|
import plistlib
|
||
|
|
import shutil
|
||
|
|
import subprocess
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
|
||
|
|
|
||
|
|
SCRIPT = Path(__file__).resolve().parents[1] / "verify-and-archive.sh"
|
||
|
|
|
||
|
|
|
||
|
|
def find_bash() -> str | None:
|
||
|
|
if os.name == "nt":
|
||
|
|
candidate = Path("C:/Program Files/Git/bin/bash.exe")
|
||
|
|
return str(candidate) if candidate.is_file() else None
|
||
|
|
return shutil.which("bash")
|
||
|
|
|
||
|
|
|
||
|
|
BASH = find_bash()
|
||
|
|
|
||
|
|
|
||
|
|
@unittest.skipUnless(BASH, "Requires bash; Windows checks use existing Git Bash")
|
||
|
|
class ArchiveScriptTests(unittest.TestCase):
|
||
|
|
def setUp(self) -> None:
|
||
|
|
self.temporary = tempfile.TemporaryDirectory(prefix="ucvl-ios-archive-")
|
||
|
|
self.addCleanup(self.temporary.cleanup)
|
||
|
|
self.root = Path(self.temporary.name)
|
||
|
|
(self.root / "Scripts").mkdir()
|
||
|
|
(self.root / "bin").mkdir()
|
||
|
|
self.trace = self.root / "trace"
|
||
|
|
self.trace.mkdir()
|
||
|
|
shutil.copyfile(SCRIPT, self.root / "Scripts/verify-and-archive.sh")
|
||
|
|
self.project(' MARKETING_VERSION: "1.0.3"\n')
|
||
|
|
self.environment = os.environ.copy()
|
||
|
|
self.environment.update({
|
||
|
|
"PATH": str(self.root / "bin") + os.pathsep + self.environment.get("PATH", ""),
|
||
|
|
"DEVELOPMENT_TEAM": "TESTTEAM01",
|
||
|
|
"ARCHIVE_TEST_TRACE": self.trace.as_posix(),
|
||
|
|
})
|
||
|
|
for name in ("EXPORT_OPTIONS_PLIST", "SIMULATOR_DESTINATION", "ARCHIVE_TEST_FAIL_STAGE"):
|
||
|
|
self.environment.pop(name, None)
|
||
|
|
for stage, name in (("fetch", "fetch-libxray.sh"), ("generate", "generate-project.sh")):
|
||
|
|
self.executable(self.root / "Scripts" / name,
|
||
|
|
'#!/bin/sh\nset -eu\n'
|
||
|
|
f'printf "%s\\n" {stage} >> "$ARCHIVE_TEST_TRACE/events"\n'
|
||
|
|
f'[ "${{ARCHIVE_TEST_FAIL_STAGE:-}}" != {stage} ] || exit 37\n')
|
||
|
|
self.executable(self.root / "bin/xcodebuild", r'''#!/bin/sh
|
||
|
|
set -eu
|
||
|
|
action=
|
||
|
|
for argument do
|
||
|
|
case "$argument" in test|archive|-exportArchive) action=$argument;; esac
|
||
|
|
done
|
||
|
|
[ "$action" != -exportArchive ] || action=export
|
||
|
|
printf '%s\n' "$action" >> "$ARCHIVE_TEST_TRACE/events"
|
||
|
|
printf '%s\0' "$@" > "$ARCHIVE_TEST_TRACE/$action.args"
|
||
|
|
[ "${ARCHIVE_TEST_FAIL_STAGE:-}" != "$action" ] || exit 37
|
||
|
|
''')
|
||
|
|
self.executable(self.root / "bin/plutil", r'''#!/bin/sh
|
||
|
|
set -eu
|
||
|
|
printf '%s\n' validate_export >> "$ARCHIVE_TEST_TRACE/events"
|
||
|
|
printf '%s\0' "$@" > "$ARCHIVE_TEST_TRACE/plutil.args"
|
||
|
|
[ "${ARCHIVE_TEST_FAIL_STAGE:-}" != validate_export ] || exit 37
|
||
|
|
''')
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def executable(path: Path, text: str) -> None:
|
||
|
|
path.write_text(text, encoding="utf-8", newline="\n")
|
||
|
|
path.chmod(0o755)
|
||
|
|
|
||
|
|
def project(self, text: str) -> None:
|
||
|
|
(self.root / "project.yml").write_text("settings:\n base:\n" + text,
|
||
|
|
encoding="utf-8", newline="\n")
|
||
|
|
|
||
|
|
def run_script(self) -> subprocess.CompletedProcess[str]:
|
||
|
|
return subprocess.run([BASH, "--noprofile", "--norc", "./Scripts/verify-and-archive.sh"],
|
||
|
|
cwd=self.root, env=self.environment, capture_output=True,
|
||
|
|
text=True, encoding="utf-8", timeout=15, check=False)
|
||
|
|
|
||
|
|
def events(self) -> list[str]:
|
||
|
|
path = self.trace / "events"
|
||
|
|
return path.read_text(encoding="utf-8").splitlines() if path.exists() else []
|
||
|
|
|
||
|
|
def arguments(self, stage: str) -> list[str]:
|
||
|
|
raw = (self.trace / (stage + ".args")).read_bytes()
|
||
|
|
self.assertTrue(raw.endswith(b"\0"))
|
||
|
|
return [value.decode("utf-8") for value in raw[:-1].split(b"\0")]
|
||
|
|
|
||
|
|
def export_options(self) -> str:
|
||
|
|
path = self.root / "export options.plist"
|
||
|
|
path.write_bytes(plistlib.dumps({"method": "debugging", "teamID": "TESTTEAM01"}))
|
||
|
|
return path.as_posix()
|
||
|
|
|
||
|
|
def test_archive_uses_project_version_and_default_simulator(self) -> None:
|
||
|
|
result = self.run_script()
|
||
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
||
|
|
self.assertEqual(self.events(), ["fetch", "generate", "test", "archive"])
|
||
|
|
self.assertEqual(self.arguments("test"), [
|
||
|
|
"-project", "UCVLZonghengjia.xcodeproj", "-scheme", "UCVLZonghengjia",
|
||
|
|
"-destination", "platform=iOS Simulator,name=iPhone 16 Pro",
|
||
|
|
"DEVELOPMENT_TEAM=TESTTEAM01", "test"])
|
||
|
|
self.assertEqual(self.arguments("archive"), [
|
||
|
|
"-project", "UCVLZonghengjia.xcodeproj", "-scheme", "UCVLZonghengjia",
|
||
|
|
"-configuration", "Release", "-destination", "generic/platform=iOS",
|
||
|
|
"-archivePath", "build/UCVL-Zonghengjia-1.0.3.xcarchive",
|
||
|
|
"DEVELOPMENT_TEAM=TESTTEAM01", "archive"])
|
||
|
|
|
||
|
|
def test_changed_project_version_changes_archive_path(self) -> None:
|
||
|
|
self.project(' MARKETING_VERSION: "2.7.19"\n')
|
||
|
|
result = self.run_script()
|
||
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
||
|
|
arguments = self.arguments("archive")
|
||
|
|
self.assertEqual(arguments[arguments.index("-archivePath") + 1],
|
||
|
|
"build/UCVL-Zonghengjia-2.7.19.xcarchive")
|
||
|
|
|
||
|
|
def test_explicit_simulator_destination_is_one_argument(self) -> None:
|
||
|
|
self.environment["SIMULATOR_DESTINATION"] = "platform=iOS Simulator,id=test-simulator-id"
|
||
|
|
result = self.run_script()
|
||
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
||
|
|
arguments = self.arguments("test")
|
||
|
|
self.assertEqual(arguments[arguments.index("-destination") + 1],
|
||
|
|
"platform=iOS Simulator,id=test-simulator-id")
|
||
|
|
|
||
|
|
def test_export_validates_first_and_uses_same_versioned_archive(self) -> None:
|
||
|
|
options = self.export_options()
|
||
|
|
self.environment["EXPORT_OPTIONS_PLIST"] = options
|
||
|
|
result = self.run_script()
|
||
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
||
|
|
self.assertEqual(self.events(), ["validate_export", "fetch", "generate", "test", "archive", "export"])
|
||
|
|
self.assertEqual(self.arguments("plutil"), ["-lint", "--", options])
|
||
|
|
self.assertEqual(self.arguments("export"), [
|
||
|
|
"-exportArchive", "-archivePath", "build/UCVL-Zonghengjia-1.0.3.xcarchive",
|
||
|
|
"-exportPath", "build/export", "-exportOptionsPlist", options])
|
||
|
|
|
||
|
|
def test_missing_team_stops_before_external_commands(self) -> None:
|
||
|
|
self.environment.pop("DEVELOPMENT_TEAM")
|
||
|
|
result = self.run_script()
|
||
|
|
self.assertNotEqual(result.returncode, 0)
|
||
|
|
self.assertEqual(self.events(), [])
|
||
|
|
|
||
|
|
def test_missing_export_file_stops_before_fetch_or_build(self) -> None:
|
||
|
|
self.environment["EXPORT_OPTIONS_PLIST"] = (self.root / "missing.plist").as_posix()
|
||
|
|
result = self.run_script()
|
||
|
|
self.assertNotEqual(result.returncode, 0)
|
||
|
|
self.assertEqual(self.events(), [])
|
||
|
|
|
||
|
|
def test_export_lint_failure_stops_before_fetch_or_build(self) -> None:
|
||
|
|
self.environment["EXPORT_OPTIONS_PLIST"] = self.export_options()
|
||
|
|
self.environment["ARCHIVE_TEST_FAIL_STAGE"] = "validate_export"
|
||
|
|
result = self.run_script()
|
||
|
|
self.assertNotEqual(result.returncode, 0)
|
||
|
|
self.assertEqual(self.events(), ["validate_export"])
|
||
|
|
|
||
|
|
def test_invalid_or_ambiguous_project_version_stops_before_fetch(self) -> None:
|
||
|
|
for text in (' SWIFT_VERSION: "5.10"\n',
|
||
|
|
' MARKETING_VERSION: "../../escape"\n',
|
||
|
|
' MARKETING_VERSION: "1.0.3"\n MARKETING_VERSION: "1.0.4"\n'):
|
||
|
|
with self.subTest(project=text):
|
||
|
|
self.project(text)
|
||
|
|
result = self.run_script()
|
||
|
|
self.assertNotEqual(result.returncode, 0)
|
||
|
|
self.assertEqual(self.events(), [])
|
||
|
|
|
||
|
|
def test_each_failed_stage_prevents_all_later_stages(self) -> None:
|
||
|
|
stages = ["fetch", "generate", "test", "archive", "export"]
|
||
|
|
self.environment["EXPORT_OPTIONS_PLIST"] = self.export_options()
|
||
|
|
for index, stage in enumerate(stages):
|
||
|
|
with self.subTest(stage=stage):
|
||
|
|
(self.trace / "events").unlink(missing_ok=True)
|
||
|
|
self.environment["ARCHIVE_TEST_FAIL_STAGE"] = stage
|
||
|
|
result = self.run_script()
|
||
|
|
self.assertEqual(result.returncode, 37, result.stderr)
|
||
|
|
self.assertEqual(self.events(), ["validate_export"] + stages[:index + 1])
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|