ci: prepare native language and keyboard test prerequisites
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
// Integration fixtures for an ephemeral GitHub-hosted Mac, never the developer's Mac.
|
||||
// Use --check locally: it only reads the enabled layouts and iriz's language key.
|
||||
import Carbon
|
||||
import Foundation
|
||||
|
||||
func require(_ condition: Bool, _ message: String) {
|
||||
guard condition else {
|
||||
FileHandle.standardError.write(Data((message + "\n").utf8))
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
let arguments = Array(CommandLine.arguments.dropFirst())
|
||||
require(arguments == ["--check"] || arguments == ["--prepare-hosted"],
|
||||
"Usage: macos_ci_environment.swift --check | --prepare-hosted")
|
||||
let prepare = arguments == ["--prepare-hosted"]
|
||||
if prepare {
|
||||
let environment = ProcessInfo.processInfo.environment
|
||||
require(environment["GITHUB_ACTIONS"] == "true"
|
||||
&& environment["RUNNER_ENVIRONMENT"] == "github-hosted"
|
||||
&& environment["RUNNER_OS"] == "macOS",
|
||||
"Fixture writes require a GitHub-hosted macOS runner.")
|
||||
}
|
||||
|
||||
func layouts(includeAllInstalled: Bool) -> [TISInputSource] {
|
||||
// Same enabled-only filter as LayoutSwitcher.installedLayouts() when false.
|
||||
let conditions = [
|
||||
kTISPropertyInputSourceCategory as String: kTISCategoryKeyboardInputSource as Any,
|
||||
kTISPropertyInputSourceIsSelectCapable as String: true as Any,
|
||||
] as CFDictionary
|
||||
guard let sources = TISCreateInputSourceList(conditions, includeAllInstalled)?
|
||||
.takeRetainedValue() as? [TISInputSource] else {
|
||||
require(false, "Cannot list macOS keyboard input sources.")
|
||||
return []
|
||||
}
|
||||
return sources
|
||||
}
|
||||
|
||||
func sourceID(_ source: TISInputSource) -> String {
|
||||
guard let value = TISGetInputSourceProperty(source, kTISPropertyInputSourceID) else { return "" }
|
||||
return Unmanaged<CFString>.fromOpaque(value).takeUnretainedValue() as String
|
||||
}
|
||||
|
||||
func currentSourceID() -> String {
|
||||
guard let source = TISCopyCurrentKeyboardInputSource()?.takeRetainedValue() else { return "" }
|
||||
return sourceID(source)
|
||||
}
|
||||
|
||||
func language(_ source: TISInputSource) -> String {
|
||||
guard let value = TISGetInputSourceProperty(source, kTISPropertyInputSourceLanguages) else { return "" }
|
||||
let languages = Unmanaged<CFArray>.fromOpaque(value).takeUnretainedValue() as? [String]
|
||||
return String(languages?.first?.lowercased().prefix(2) ?? "")
|
||||
}
|
||||
|
||||
func hasUnicodeData(_ source: TISInputSource) -> Bool {
|
||||
guard let value = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData) else { return false }
|
||||
return CFDataGetLength(Unmanaged<CFData>.fromOpaque(value).takeUnretainedValue()) > 0
|
||||
}
|
||||
|
||||
let englishIDs = ["com.apple.keylayout.US", "com.apple.keylayout.ABC"]
|
||||
let russianID = "com.apple.keylayout.Russian"
|
||||
let languageKey = "ru.smltlk.interfaceLanguage" as CFString
|
||||
let originalSourceID = currentSourceID()
|
||||
require(!originalSourceID.isEmpty, "Cannot identify the active keyboard input source.")
|
||||
|
||||
if prepare {
|
||||
let enabled = layouts(includeAllInstalled: false)
|
||||
let all = layouts(includeAllInstalled: true)
|
||||
// Reuse an enabled Apple English layout; otherwise enable one, not both.
|
||||
let english = (enabled + all).first { englishIDs.contains(sourceID($0)) }
|
||||
let russian = all.first { sourceID($0) == russianID }
|
||||
require(english != nil && russian != nil, "Apple US/ABC and Russian keyboard layouts must be installed.")
|
||||
let pair = [english!, russian!]
|
||||
require(pair.allSatisfy(hasUnicodeData), "Apple EN/RU layouts must expose nonempty Unicode layout data.")
|
||||
for source in pair where !enabled.contains(where: { sourceID($0) == sourceID(source) }) {
|
||||
let property = TISGetInputSourceProperty(source, kTISPropertyInputSourceIsEnableCapable)
|
||||
require(property.map { Unmanaged<CFBoolean>.fromOpaque($0).takeUnretainedValue() == kCFBooleanTrue } == true,
|
||||
"The Apple layout \(sourceID(source)) cannot be enabled.")
|
||||
let status = TISEnableInputSource(source)
|
||||
require(status == noErr, "Cannot enable \(sourceID(source)): OSStatus \(status).")
|
||||
}
|
||||
// kCFPreferencesAnyApplication is NSGlobalDomain. Only this iriz key changes;
|
||||
// system language preferences and all unrelated global preferences stay intact.
|
||||
CFPreferencesSetValue(languageKey, "ru" as CFString, kCFPreferencesAnyApplication,
|
||||
kCFPreferencesCurrentUser, kCFPreferencesAnyHost)
|
||||
require(CFPreferencesSynchronize(kCFPreferencesAnyApplication, kCFPreferencesCurrentUser,
|
||||
kCFPreferencesAnyHost), "Cannot save iriz's CI language preference.")
|
||||
}
|
||||
|
||||
let enabled = layouts(includeAllInstalled: false)
|
||||
// Check the first EN/RU pair, exactly as DynamicKeyMapping.configureSharedKeyMapping does.
|
||||
for (code, allowedIDs) in [("en", englishIDs), ("ru", [russianID])] {
|
||||
guard let source = enabled.first(where: { language($0) == code }),
|
||||
allowedIDs.contains(sourceID(source)), hasUnicodeData(source) else {
|
||||
require(false, "Missing enabled Apple \(code) layout with Unicode data; the integration fixture is not ready.")
|
||||
exit(1)
|
||||
}
|
||||
print("Enabled \(code) layout: \(sourceID(source)); Unicode data available.")
|
||||
}
|
||||
require(currentSourceID() == originalSourceID,
|
||||
"The active keyboard input source changed during the environment check.")
|
||||
let globalLanguage = CFPreferencesCopyValue(languageKey, kCFPreferencesAnyApplication,
|
||||
kCFPreferencesCurrentUser, kCFPreferencesAnyHost) as? String
|
||||
require(globalLanguage == "ru" && UserDefaults.standard.string(forKey: languageKey as String) == "ru",
|
||||
"iriz's NSGlobalDomain language key must be ru and readable through UserDefaults.standard.")
|
||||
print("PASS macOS integration environment: Apple EN/RU layouts, iriz language ru; active input unchanged.")
|
||||
@@ -52,6 +52,9 @@ check(build['env']['DEVELOPER_DIR'] == '/Applications/Xcode_26.6.app/Contents/De
|
||||
check(build['env']['IRIZ_NOTARY_PROFILE'] == '' && build['env']['IRIZ_DMG_HEADLESS'] == '1', 'No Apple credentials or Finder in CI')
|
||||
check(build['steps'].any? { |step| step['run'] == 'swift test --no-parallel --jobs 2' }, 'Serial tests must precede packaging')
|
||||
test_index = build['steps'].index { |step| step['run'] == 'swift test --no-parallel --jobs 2' }
|
||||
environment_index = build['steps'].index { |step| step['name'] == 'Prepare hosted macOS integration environment' }
|
||||
check(environment_index && environment_index == test_index - 1, 'Environment preparation must immediately precede tests')
|
||||
check(!build['continue-on-error'] && build['steps'].none? { |step| step['continue-on-error'] }, 'Build failures must stop subsequent steps')
|
||||
package_index = build['steps'].index { |step| step['run'] == 'bash scripts/make_release.sh' }
|
||||
check(package_index && test_index < package_index, 'Packaging must follow successful tests')
|
||||
check(build['steps'].none? { |step| step.key?('if') }, 'build_only must retain every build, test, verification and upload step')
|
||||
@@ -71,6 +74,67 @@ workflow['jobs'].each_value do |job|
|
||||
end
|
||||
puts 'PASS workflow triggers, permissions, pins and Bash syntax'
|
||||
|
||||
environment_script = build['steps'][environment_index].fetch('run')
|
||||
helper = File.read(File.join(__dir__, 'macos_ci_environment.swift'))
|
||||
native_guard = <<'SWIFT'
|
||||
if prepare {
|
||||
let environment = ProcessInfo.processInfo.environment
|
||||
require(environment["GITHUB_ACTIONS"] == "true"
|
||||
&& environment["RUNNER_ENVIRONMENT"] == "github-hosted"
|
||||
&& environment["RUNNER_OS"] == "macOS",
|
||||
"Fixture writes require a GitHub-hosted macOS runner.")
|
||||
}
|
||||
SWIFT
|
||||
check(helper.split('func layouts', 2).first.include?(native_guard), 'The native helper must guard all writes independently of Bash')
|
||||
check(helper.include?('let prepare = arguments == ["--prepare-hosted"]'), 'Only the explicit prepare mode may write')
|
||||
prepare_body = helper[/^if prepare \{\n let enabled = .*?^\}/m]
|
||||
%w[TISEnableInputSource CFPreferencesSetValue CFPreferencesSynchronize].each do |call|
|
||||
check(prepare_body && helper.scan(/\b#{call}\(/).size == 1 && prepare_body.include?("#{call}("), "#{call} must only run inside the guarded prepare mode")
|
||||
end
|
||||
check(!helper.match?(/TISSelectInputSource|AppleLanguages|AppleLocale|setPersistentDomain|CFPreferencesSetAppValue/), 'Never select a layout or replace system language preferences')
|
||||
check(helper.include?('let englishIDs = ["com.apple.keylayout.US", "com.apple.keylayout.ABC"]') &&
|
||||
helper.include?('let russianID = "com.apple.keylayout.Russian"'), 'Only the Apple EN/RU layout IDs are allowed')
|
||||
check(helper.include?('let languageKey = "ru.smltlk.interfaceLanguage" as CFString') &&
|
||||
helper.include?('CFPreferencesSetValue(languageKey, "ru" as CFString, kCFPreferencesAnyApplication,') &&
|
||||
helper.include?('UserDefaults.standard.string(forKey: languageKey as String) == "ru"'), 'The iriz-only global preference needs an effective Foundation readback')
|
||||
puts 'PASS native helper write boundary and exact fixture scope'
|
||||
|
||||
environment_commands = <<'BASH'
|
||||
xcrun() {
|
||||
case "$*" in
|
||||
'swift scripts/macos_ci_environment.swift --prepare-hosted')
|
||||
printf 'FIXTURE_PREPARE\n'; return "$MOCK_PREPARE_EXIT" ;;
|
||||
'swift scripts/macos_ci_environment.swift --check')
|
||||
printf 'FIXTURE_CHECK\n'; return "$MOCK_CHECK_EXIT" ;;
|
||||
*) printf 'Unexpected xcrun arguments\n' >&2; return 99 ;;
|
||||
esac
|
||||
}
|
||||
swift() {
|
||||
[[ "$*" == 'test --no-parallel --jobs 2' ]] || return 99
|
||||
printf 'FIXTURE_TESTS\n'
|
||||
}
|
||||
BASH
|
||||
[
|
||||
['hosted fixture then tests', {}, 0, %w[FIXTURE_PREPARE FIXTURE_CHECK FIXTURE_TESTS]],
|
||||
['fixture preparation failure stops tests', { 'MOCK_PREPARE_EXIT' => '37' }, 37, %w[FIXTURE_PREPARE]],
|
||||
['fixture readback failure stops tests', { 'MOCK_CHECK_EXIT' => '38' }, 38, %w[FIXTURE_PREPARE FIXTURE_CHECK]],
|
||||
['local runner cannot prepare', { 'GITHUB_ACTIONS' => nil }, 1, []],
|
||||
['false Actions flag cannot prepare', { 'GITHUB_ACTIONS' => 'false' }, 1, []],
|
||||
['missing runner environment cannot prepare', { 'RUNNER_ENVIRONMENT' => nil }, 1, []],
|
||||
['self-hosted runner cannot prepare', { 'RUNNER_ENVIRONMENT' => 'self-hosted' }, 1, []],
|
||||
['missing runner OS cannot prepare', { 'RUNNER_OS' => nil }, 1, []],
|
||||
['non-macOS runner cannot prepare', { 'RUNNER_OS' => 'Linux' }, 1, []]
|
||||
].each do |name, overrides, expected_status, expected_calls|
|
||||
env = { 'GITHUB_ACTIONS' => 'true', 'RUNNER_ENVIRONMENT' => 'github-hosted', 'RUNNER_OS' => 'macOS',
|
||||
'MOCK_PREPARE_EXIT' => '0', 'MOCK_CHECK_EXIT' => '0' }.merge(overrides)
|
||||
# Both commands are mocks; the real helper never prepares this machine.
|
||||
script = environment_commands + environment_script + "\n" + build['steps'][test_index].fetch('run')
|
||||
output, status = Open3.capture2e(env, 'bash', '-e', '-o', 'pipefail', '-c', script)
|
||||
check(status.exitstatus == expected_status && output.lines.map(&:strip).grep(/^FIXTURE_/) == expected_calls, "#{name}: #{output}")
|
||||
check(!expected_calls.empty? || output.include?('Fixture writes require a GitHub-hosted macOS runner.'), "#{name}: missing guard reason")
|
||||
puts "PASS #{name}"
|
||||
end
|
||||
|
||||
sha = 'a' * 40
|
||||
version_script = build['steps'].find { |step| step['id'] == 'version' }.fetch('run')
|
||||
toolchain = <<'BASH'
|
||||
|
||||
Reference in New Issue
Block a user