Refactor swift check out of init-action.ts

This commit is contained in:
Michael B. Gale
2026-10-09 11:01:20 +01:00
parent ed72893659
commit 788a22022b
4 changed files with 165 additions and 94 deletions

View File

@@ -56,6 +56,7 @@ import {
runDatabaseInitCluster,
} from "./init";
import { JavaEnvVars, BuiltInLanguage } from "./languages";
import { isSwiftCompatible } from "./languages/swift";
import { Logger, withGroupAsync } from "./logging";
import {
downloadOverlayBaseDatabaseFromCache,
@@ -418,14 +419,7 @@ async function run(
logger,
});
if (
config.languages.includes(BuiltInLanguage.swift) &&
process.platform !== "darwin"
) {
throw new ConfigurationError(
`Swift analysis is only supported on macOS runner images. Please migrate to a macOS runner.`,
);
}
isSwiftCompatible(actionState, config);
if (repositoryPropertiesResult.isFailure()) {
addNoLanguageDiagnostic(

View File

@@ -0,0 +1,43 @@
import test from "ava";
import { createTestConfig, initAllState } from "../testing-utils";
import { isSwiftCompatible } from "./swift";
import { BuiltInLanguage } from ".";
test("isSwiftCompatible doesn't throw for non-Swift languages", (t) => {
for (const language of Object.values(BuiltInLanguage)) {
if (language === BuiltInLanguage.swift) {
continue;
}
t.notThrows(() =>
isSwiftCompatible(
initAllState(),
createTestConfig({ languages: [language] }),
),
);
}
});
test("isSwiftCompatible doesn't throw for Swift on darwin", (t) => {
t.notThrows(() => {
isSwiftCompatible(
initAllState({ platform: "darwin" }),
createTestConfig({ languages: [BuiltInLanguage.swift] }),
);
});
});
const nonDarwinPlatforms: NodeJS.Platform[] = ["linux", "win32"];
for (const nonDarwinPlatform of nonDarwinPlatforms) {
test(`isSwiftCompatible throws for Swift on ${nonDarwinPlatform}`, (t) => {
t.throws(() => {
isSwiftCompatible(
initAllState({ platform: nonDarwinPlatform }),
createTestConfig({ languages: [BuiltInLanguage.swift] }),
);
});
});
}

28
src/languages/swift.ts Normal file
View File

@@ -0,0 +1,28 @@
import { ActionState } from "../action-common";
import { Config } from "../config-utils";
import { ConfigurationError } from "../util";
import { BuiltInLanguage } from ".";
/**
* Determines whether we can run a Swift analysis on the current runner.
*
* @param action The Action state.
* @param config The Action configuration.
*
* @throws {ConfigurationError} If Swift analysis is not possible on the current runner.
* @returns True if we can run a Swift analysis.
*/
export function isSwiftCompatible(
action: ActionState<["Base"]>,
config: Config,
) {
if (
config.languages.includes(BuiltInLanguage.swift) &&
action.platform !== "darwin"
) {
throw new ConfigurationError(
`Swift analysis is only supported on macOS runner images. Please migrate to a macOS runner.`,
);
}
}