Files
NetNewsWire/Tests/NetNewsWireTests/ScriptingTests/AppleScriptTestRunner.swift
T
Brent Simmons 5c50d7b97d
CI / SwiftLint (push) Canceled after 0s
CI / macOS Tests (push) Canceled after 0s
CI / iOS Simulator Tests (push) Canceled after 0s
Make TestBundleToken an NSObject subclass so Bundle(for:) can work to find the test bundle and the scripting tests can work in CI.
2026-12-04 09:48:31 -08:00

50 lines
1.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// AppleScriptTestRunner.swift
// NetNewsWireTests
//
// Created by Olof Hellman on 2/10/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import Foundation
import Testing
/// Loads `filename`.scpt from the test bundle and runs it.
///
/// The script must return a record of the form `{test_result:true, script_result:<anything>}`.
/// Records an issue if `test_result` is false or missing.
///
/// - Returns: the value of `script_result`, if any.
@MainActor @discardableResult func runTestScript(named filename: String, sourceLocation: SourceLocation = #_sourceLocation) -> NSAppleEventDescriptor? {
let testBundle = Bundle(for: TestBundleToken.self)
guard let testScriptURL = testBundle.url(forResource: filename, withExtension: "scpt") else {
Issue.record("Failed getting script URL for \(filename)", sourceLocation: sourceLocation)
return nil
}
var errorDict: NSDictionary?
guard let testScript = NSAppleScript(contentsOf: testScriptURL, error: &errorDict) else {
Issue.record("Failed initializing NSAppleScript: \(String(describing: errorDict))", sourceLocation: sourceLocation)
return nil
}
let scriptResult = testScript.executeAndReturnError(&errorDict)
if errorDict != nil {
Issue.record("Failed executing script: \(String(describing: errorDict))", sourceLocation: sourceLocation)
return nil
}
let usrfDictionary = scriptResult.usrfDictionary()
guard let testResult = usrfDictionary["test_result"] else {
Issue.record("Test script didn’t return test_result in usrf", sourceLocation: sourceLocation)
return nil
}
let scriptResultValue = usrfDictionary["script_result"]
#expect(testResult.booleanValue, "test_result should be true. script_result was \(String(describing: scriptResultValue))", sourceLocation: sourceLocation)
return scriptResultValue
}
// NSObject subclass so that Bundle(for:) reliably finds the test bundle.
private final class TestBundleToken: NSObject {}