unity-test-runner/src/model/input.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-01-29 21:22:26 +00:00
import { getInput } from '@actions/core';
import { includes } from 'lodash-es';
class Input {
static get testModes() {
return ['all', 'playmode', 'editmode'];
}
2020-01-29 22:42:41 +00:00
static isValidFolderName(folderName) {
2020-01-30 08:04:32 +00:00
const validFolderName = new RegExp(/^(\.|\.\/)?(\.?\w+([-_]?\w+)*\/?)*$/);
2020-01-29 22:42:41 +00:00
return validFolderName.test(folderName);
}
2020-01-29 21:22:26 +00:00
static getFromUser() {
// Input variables specified in workflow using "with" prop.
const unityVersion = getInput('unityVersion') || '2019.2.11f1';
const testMode = getInput('testMode') || 'all';
2020-01-30 07:51:15 +00:00
const rawProjectPath = getInput('projectPath') || '.';
const rawArtifactsPath = getInput('artifactsPath') || 'artifacts';
2020-04-01 20:24:13 +00:00
const rawUseHostNetwork = getInput('useHostNetwork') || 'false';
2020-01-29 22:54:44 +00:00
const customParameters = getInput('customParameters') || '';
2020-01-29 21:22:26 +00:00
// Validate input
if (!includes(this.testModes, testMode)) {
throw new Error(`Invalid testMode ${testMode}`);
}
2020-01-29 22:42:41 +00:00
if (!this.isValidFolderName(rawArtifactsPath)) {
throw new Error(`Invalid artifactsPath "${rawArtifactsPath}"`);
}
if (!this.isValidFolderName(rawProjectPath)) {
throw new Error(`Invalid projectPath "${rawProjectPath}"`);
}
2020-04-01 20:24:13 +00:00
if (rawUseHostNetwork !== 'true' && rawUseHostNetwork !== 'false') {
throw new Error(`Invalid useHostNetwork "${rawUseHostNetwork}"`);
}
2020-01-29 21:22:26 +00:00
// Sanitise input
const projectPath = rawProjectPath.replace(/\/$/, '');
const artifactsPath = rawArtifactsPath.replace(/\/$/, '');
2020-04-01 20:24:13 +00:00
const useHostNetwork = rawUseHostNetwork === 'true';
2020-01-29 21:22:26 +00:00
// Return sanitised input
return {
unityVersion,
projectPath,
testMode,
artifactsPath,
2020-04-01 20:24:13 +00:00
useHostNetwork,
2020-01-29 22:54:44 +00:00
customParameters,
2020-01-29 21:22:26 +00:00
};
}
}
export default Input;