Add input validations with tests.

This commit is contained in:
Yang Chen
2019-11-06 18:59:27 +11:00
parent d6aab89c3b
commit 137c96a972
9 changed files with 168 additions and 54 deletions
+32
View File
@@ -0,0 +1,32 @@
import { isNumber } from "util";
export const MIN_API_LEVEL = 21;
export const VALID_TARGETS: Array<string> = ['default', 'google_apis'];
export const VALID_ABIS: Array<string> = ['x86', 'x86_64'];
export function checkApiLevel(apiLevel: string): void {
if (isNaN(Number(apiLevel)) || !Number.isInteger(Number(apiLevel))) {
throw new Error(`Unexpected API level: '${apiLevel}'.`);
}
if (Number(apiLevel) < MIN_API_LEVEL) {
throw new Error(`Minimum API level supported is ${MIN_API_LEVEL}.`);
}
}
export function checkTarget(target: string): void {
if (!VALID_TARGETS.includes(target)) {
throw new Error(`Value for input.target '${target}' is unknown. Supported options: ${VALID_TARGETS}.`);
}
}
export function checkAbi(abi: string): void {
if (!VALID_ABIS.includes(abi)) {
throw new Error(`Value for input.abi '${abi}' is unknown. Supported options: ${VALID_ABIS}.`);
}
}
export function checkHeadless(headless: string): void {
if (headless !== 'true' && headless !== 'false') {
throw new Error(`Input for input.headless should be either 'true' or 'false'.`);
}
}
+12 -20
View File
@@ -1,5 +1,7 @@
import * as core from '@actions/core';
import { installAndroidSdk } from './sdk-installer'
import { InputOptions } from "@actions/core/lib/core";
import { installAndroidSdk } from './sdk-installer';
import { checkApiLevel, checkTarget, checkAbi, checkHeadless } from './input-validator';
async function run() {
try {
@@ -8,34 +10,24 @@ async function run() {
throw new Error('This action is expected to be run within a macOS virtual machine to enable hardware acceleration.');
}
// TODO test inputs
// TODO test real inputs in test.yml
// api-level is required
// TODO use InputOptions {true}
const apiLevel = core.getInput('api-level');
// TODO check apiLevel is number and within valid range
// API level of the platform and system image
const apiLevel = core.getInput('api-level', <InputOptions>{required: true});
checkApiLevel(apiLevel);
console.log(`API level: ${apiLevel}`);
// target is optional with default
// target of the system image
const target = core.getInput('target');
if (target !== 'default' && target !== 'google_apis') {
throw new Error(`Target ${target} is unknown. Please use either 'default' or 'google_apis'.`);
}
checkTarget(target);
console.log(`target: ${target}`);
// abi is optional with default
// CPU / ABI of the system image
const abi = core.getInput('abi');
if (abi !== 'x86' && abi !== 'x86_64') {
throw new Error(`abi ${abi} is unknown (ARM-based emulators are not supported). Please use either 'x86' or 'x86_64'.`);
}
checkAbi(abi);
console.log(`cpu/abi: ${abi}`);
// headless is optional with default
// headless mode
const headless = core.getInput('headless');
if (headless !== 'true' && headless !== 'false') {
throw new Error(`Input 'headless' should be either 'true' or 'false'.`);
}
checkHeadless(headless);
console.log(`headless mode: ${headless}`);
// install SDK