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

72 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-01-29 21:22:26 +00:00
import { getInput } from '@actions/core';
import { includes } from 'lodash-es';
import UnityVersionParser from './unity-version-parser';
2020-01-29 21:22:26 +00:00
class Input {
static get testModes() {
return ['all', 'playmode', 'editmode'];
}
2020-01-29 22:42:41 +00:00
static isValidFolderName(folderName) {
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 rawUnityVersion = getInput('unityVersion') || 'auto';
const customImage = getInput('customImage') || '';
2020-01-30 07:51:15 +00:00
const rawProjectPath = getInput('projectPath') || '.';
2021-12-31 20:14:37 +00:00
const customParameters = getInput('customParameters') || '';
const testMode = (getInput('testMode') || 'all').toLowerCase();
2020-01-30 07:51:15 +00:00
const rawArtifactsPath = getInput('artifactsPath') || 'artifacts';
2020-04-01 20:24:13 +00:00
const rawUseHostNetwork = getInput('useHostNetwork') || 'false';
const sshAgent = getInput('sshAgent') || '';
2021-12-31 20:14:37 +00:00
const gitPrivateToken = getInput('gitPrivateToken') || '';
const githubToken = getInput('githubToken') || '';
const checkName = getInput('checkName') || 'Test Results';
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(rawProjectPath)) {
throw new Error(`Invalid projectPath "${rawProjectPath}"`);
}
2021-12-31 20:14:37 +00:00
if (!this.isValidFolderName(rawArtifactsPath)) {
throw new Error(`Invalid artifactsPath "${rawArtifactsPath}"`);
}
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';
const unityVersion =
rawUnityVersion === 'auto' ? UnityVersionParser.read(projectPath) : rawUnityVersion;
2020-01-29 21:22:26 +00:00
// Return sanitised input
return {
unityVersion,
customImage,
2020-01-29 21:22:26 +00:00
projectPath,
2021-12-31 20:14:37 +00:00
customParameters,
2020-01-29 21:22:26 +00:00
testMode,
artifactsPath,
2020-04-01 20:24:13 +00:00
useHostNetwork,
sshAgent,
2021-12-31 20:14:37 +00:00
gitPrivateToken,
githubToken,
checkName,
2020-01-29 21:22:26 +00:00
};
}
}
export default Input;