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

77 lines
2.5 KiB
TypeScript
Raw Normal View History

import UnityVersionParser from './unity-version-parser';
import { getInput } from '@actions/core';
2020-01-29 21:22:26 +00:00
const Input = {
get testModes() {
2020-01-29 21:22:26 +00:00
return ['all', 'playmode', 'editmode'];
},
2020-01-29 21:22:26 +00:00
isValidFolderName(folderName) {
const validFolderName = new RegExp(/^(\.|\.\/)?(\.?[\w~]+([ _-]?[\w~]+)*\/?)*$/);
2020-01-29 22:42:41 +00:00
return validFolderName.test(folderName);
},
2020-01-29 22:42:41 +00:00
getFromUser() {
2020-01-29 21:22:26 +00:00
// Input variables specified in workflow using "with" prop.
const unityVersion = getInput('unityVersion') || 'auto';
const customImage = getInput('customImage') || '';
2020-01-30 07:51:15 +00:00
const rawProjectPath = getInput('projectPath') || '.';
2022-01-01 20:38:47 +00:00
const customParameters = getInput('customParameters') || '';
const testMode = (getInput('testMode') || 'all').toLowerCase();
const enableCodeCoverage = getInput('enableCodeCoverage') || 'false';
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') || '';
2022-01-01 20:38:47 +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 (!this.testModes.includes(testMode)) {
2020-01-29 21:22:26 +00:00
throw new Error(`Invalid testMode ${testMode}`);
}
if (enableCodeCoverage !== 'true' && enableCodeCoverage !== 'false') {
throw new Error(`Invalid enableCodeCoverage "${enableCodeCoverage}"`);
}
2020-01-29 22:42:41 +00:00
if (!this.isValidFolderName(rawProjectPath)) {
throw new Error(`Invalid projectPath "${rawProjectPath}"`);
}
2022-01-01 20:38:47 +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 editorVersion =
unityVersion === 'auto' ? UnityVersionParser.read(projectPath) : unityVersion;
2020-01-29 21:22:26 +00:00
// Return sanitised input
return {
editorVersion,
customImage,
2020-01-29 21:22:26 +00:00
projectPath,
2022-01-01 20:38:47 +00:00
customParameters,
2020-01-29 21:22:26 +00:00
testMode,
enableCodeCoverage,
2020-01-29 21:22:26 +00:00
artifactsPath,
2020-04-01 20:24:13 +00:00
useHostNetwork,
sshAgent,
2022-01-01 20:38:47 +00:00
gitPrivateToken,
githubToken,
checkName,
2020-01-29 21:22:26 +00:00
};
},
};
2020-01-29 21:22:26 +00:00
export default Input;