From 1745ec626e62f37e21e28576c7ff2255f8d9bfc5 Mon Sep 17 00:00:00 2001 From: La'Kaleigh Harris <35268101+Xlient@users.noreply.github.com> Date: Wed, 3 Nov 2021 15:45:00 +0000 Subject: [PATCH] removed dotnet-versions input & refactored tests --- __tests__/installer.test.ts | 16 ++-- __tests__/setup-dotnet.test.ts | 4 +- __tests__/versionutil.test.ts | 2 +- dist/index.js | 155 ++++++------------------------ src/installer.ts | 169 +++++---------------------------- src/setup-dotnet.ts | 28 +----- 6 files changed, 67 insertions(+), 307 deletions(-) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 85b2bc7..3c6cda3 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -34,7 +34,7 @@ describe('installer tests', () => { it('Aquires multiple versions of dotnet', async () => { const versions = ['2.2.207', '3.1.120']; - await getDotnetVersions(versions); + await getDotnet(versions); expect(fs.existsSync(path.join(toolDir, 'sdk', '2.2.207'))).toBe(true); expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.120'))).toBe(true); @@ -51,7 +51,7 @@ describe('installer tests', () => { }, 600000); it('Acquires version of dotnet if no matching version is installed', async () => { - await getDotnet('3.1.201'); + await getDotnet(['3.1.201']); expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.201'))).toBe(true); if (IS_WINDOWS) { expect(fs.existsSync(path.join(toolDir, 'dotnet.exe'))).toBe(true); @@ -66,7 +66,7 @@ describe('installer tests', () => { }, 600000); //This needs some time to download on "slower" internet connections it('Acquires generic version of dotnet if no matching version is installed', async () => { - await getDotnet('3.1'); + await getDotnet(['3.1']); var directory = fs .readdirSync(path.join(toolDir, 'sdk')) .filter(fn => fn.startsWith('3.1.')); @@ -86,7 +86,7 @@ describe('installer tests', () => { it('Throws if no location contains correct dotnet version', async () => { let thrown = false; try { - await getDotnet('1000.0.0'); + await getDotnet(['1000.0.0']); } catch { thrown = true; } @@ -141,11 +141,7 @@ function normalizeFileContents(contents: string): string { .replace(new RegExp('\r', 'g'), '\n'); } -async function getDotnet(version: string): Promise { - const dotnetInstaller = new installer.DotnetCoreInstaller(version); +async function getDotnet(versions: string[]): Promise { + const dotnetInstaller = new installer.DotnetCoreInstaller(versions); await dotnetInstaller.installDotnet(); } -async function getDotnetVersions(versions: string[]): Promise { - const dotnetInstaller = new installer.DotnetCoreInstaller('', versions); - await dotnetInstaller.installDotnetVersions(); -} diff --git a/__tests__/setup-dotnet.test.ts b/__tests__/setup-dotnet.test.ts index 7ad8be8..8317823 100644 --- a/__tests__/setup-dotnet.test.ts +++ b/__tests__/setup-dotnet.test.ts @@ -53,10 +53,10 @@ describe('setup-dotnet tests', () => { fs.writeFileSync(globalJsonPath, jsonContents); } - const version = '3.1'; + const version = ['3.1']; const installer = new dotnetInstaller.DotnetCoreInstaller(version); const patchVersion = await installer.resolveVersion( - new dotnetInstaller.DotNetVersionInfo(version) + new dotnetInstaller.DotNetVersionInfo(version[0]) ); await setup.run(); diff --git a/__tests__/versionutil.test.ts b/__tests__/versionutil.test.ts index 69681da..71a3dee 100644 --- a/__tests__/versionutil.test.ts +++ b/__tests__/versionutil.test.ts @@ -78,7 +78,7 @@ describe('version tests', () => { ); it('Resolving a nonexistent generic version fails', async () => { - const dotnetInstaller = new installer.DotnetCoreInstaller('999.1.x'); + const dotnetInstaller = new installer.DotnetCoreInstaller(['999.1.x']); try { await dotnetInstaller.resolveVersion( new installer.DotNetVersionInfo('999.1.x') diff --git a/dist/index.js b/dist/index.js index faa9d13..e03379a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8674,29 +8674,19 @@ function run() { // If a valid version still can't be identified, nothing will be installed. // Proxy, auth, (etc) are still set up, even if no version is identified // - let version = core.getInput('dotnet-version'); - let versions = core.getMultilineInput('dotnet-versions'); - if (!version && !versions.length) { + let versions = core.getMultilineInput('dotnet-version'); + if (!versions.length) { // Try to fall back to global.json core.debug('No version found, trying to find version from global.json'); const globalJsonPath = path.join(process.cwd(), 'global.json'); if (fs.existsSync(globalJsonPath)) { - version = getVersionFromGlobalJson(globalJsonPath); + versions[0] = getVersionFromGlobalJson(globalJsonPath); } } - if (version && versions) { - core.warning("Multiple version inputs have been specified, Please specify either 'dotnet-version' or 'dotnet-versions'"); - } - if (versions && !version) { + if (versions) { const includePrerelease = (core.getInput('include-prerelease') || 'false').toLowerCase() === 'true'; - const dotnetInstaller = new installer.DotnetCoreInstaller('', versions, includePrerelease); - yield dotnetInstaller.installDotnetVersions(); - } - if (version) { - const includePrerelease = (core.getInput('include-prerelease') || 'false').toLowerCase() === - 'true'; - const dotnetInstaller = new installer.DotnetCoreInstaller(version, [], includePrerelease); + const dotnetInstaller = new installer.DotnetCoreInstaller(versions, includePrerelease); yield dotnetInstaller.installDotnet(); } const sourceUrl = core.getInput('source-url'); @@ -18045,102 +18035,11 @@ class DotNetVersionInfo { } exports.DotNetVersionInfo = DotNetVersionInfo; class DotnetCoreInstaller { - constructor(version, versions = [], includePrerelease = false) { - this.version = version; + constructor(versions, includePrerelease = false) { this.versions = versions; this.includePrerelease = includePrerelease; } installDotnet() { - return __awaiter(this, void 0, void 0, function* () { - let output = ''; - let resultCode = 0; - let calculatedVersion = yield this.resolveVersion(new DotNetVersionInfo(this.version)); - var envVariables = {}; - for (let key in process.env) { - if (process.env[key]) { - let value = process.env[key]; - envVariables[key] = value; - } - } - if (IS_WINDOWS) { - let escapedScript = path - .join(__dirname, '..', 'externals', 'install-dotnet.ps1') - .replace(/'/g, "''"); - let command = `& '${escapedScript}'`; - if (calculatedVersion) { - command += ` -Version ${calculatedVersion}`; - } - if (process.env['https_proxy'] != null) { - command += ` -ProxyAddress ${process.env['https_proxy']}`; - } - // This is not currently an option - if (process.env['no_proxy'] != null) { - command += ` -ProxyBypassList ${process.env['no_proxy']}`; - } - // process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used - const powershellPath = yield io.which('powershell', true); - var options = { - listeners: { - stdout: (data) => { - output += data.toString(); - } - }, - env: envVariables - }; - resultCode = yield exec.exec(`"${powershellPath}"`, [ - '-NoLogo', - '-Sta', - '-NoProfile', - '-NonInteractive', - '-ExecutionPolicy', - 'Unrestricted', - '-Command', - command - ], options); - } - else { - let escapedScript = path - .join(__dirname, '..', 'externals', 'install-dotnet.sh') - .replace(/'/g, "''"); - fs_1.chmodSync(escapedScript, '777'); - const scriptPath = yield io.which(escapedScript, true); - let scriptArguments = []; - if (calculatedVersion) { - scriptArguments.push('--version', calculatedVersion); - } - // process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used - resultCode = yield exec.exec(`"${scriptPath}"`, scriptArguments, { - listeners: { - stdout: (data) => { - output += data.toString(); - } - }, - env: envVariables - }); - } - if (process.env['DOTNET_INSTALL_DIR']) { - core.addPath(process.env['DOTNET_INSTALL_DIR']); - core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']); - } - else { - if (IS_WINDOWS) { - // This is the default set in install-dotnet.ps1 - core.addPath(path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet')); - core.exportVariable('DOTNET_ROOT', path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet')); - } - else { - // This is the default set in install-dotnet.sh - core.addPath(path.join(process.env['HOME'] + '', '.dotnet')); - core.exportVariable('DOTNET_ROOT', path.join(process.env['HOME'] + '', '.dotnet')); - } - } - console.log(process.env['PATH']); - if (resultCode != 0) { - throw new Error(`Failed to install dotnet ${resultCode}. ${output}`); - } - }); - } - installDotnetVersions() { var e_1, _a; return __awaiter(this, void 0, void 0, function* () { let output = ''; @@ -18148,7 +18047,7 @@ class DotnetCoreInstaller { try { for (var _b = __asyncValues(this.versions), _c; _c = yield _b.next(), !_c.done;) { const version = _c.value; - let calculatedVersion = yield this.resolveVersion(new DotNetVersionInfo(version.trim())); + let calculatedVersion = yield this.resolveVersion(new DotNetVersionInfo(version)); var envVariables = {}; for (let key in process.env) { if (process.env[key]) { @@ -18212,26 +18111,6 @@ class DotnetCoreInstaller { env: envVariables }); } - if (process.env['DOTNET_INSTALL_DIR']) { - core.addPath(process.env['DOTNET_INSTALL_DIR']); - core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']); - } - else { - if (IS_WINDOWS) { - // This is the default set in install-dotnet.ps1 - core.addPath(path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet')); - core.exportVariable('DOTNET_ROOT', path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet')); - } - else { - // This is the default set in install-dotnet.sh - core.addPath(path.join(process.env['HOME'] + '', '.dotnet')); - core.exportVariable('DOTNET_ROOT', path.join(process.env['HOME'] + '', '.dotnet')); - } - } - console.log(process.env['PATH']); - if (resultCode != 0) { - throw new Error(`Failed to install dotnet ${resultCode}. ${output}`); - } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } @@ -18241,6 +18120,26 @@ class DotnetCoreInstaller { } finally { if (e_1) throw e_1.error; } } + if (process.env['DOTNET_INSTALL_DIR']) { + core.addPath(process.env['DOTNET_INSTALL_DIR']); + core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']); + } + else { + if (IS_WINDOWS) { + // This is the default set in install-dotnet.ps1 + core.addPath(path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet')); + core.exportVariable('DOTNET_ROOT', path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet')); + } + else { + // This is the default set in install-dotnet.sh + core.addPath(path.join(process.env['HOME'] + '', '.dotnet')); + core.exportVariable('DOTNET_ROOT', path.join(process.env['HOME'] + '', '.dotnet')); + } + } + console.log(process.env['PATH']); + if (resultCode != 0) { + throw new Error(`Failed to install dotnet ${resultCode}. ${output}`); + } }); } // versionInfo - versionInfo of the SDK/Runtime diff --git a/src/installer.ts b/src/installer.ts index 6ad40c6..32a5cb3 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -79,12 +79,7 @@ export class DotNetVersionInfo { } export class DotnetCoreInstaller { - constructor( - version: string, - versions: string[] = [], - includePrerelease: boolean = false - ) { - this.version = version; + constructor(versions: string[], includePrerelease: boolean = false) { this.versions = versions; this.includePrerelease = includePrerelease; } @@ -93,119 +88,9 @@ export class DotnetCoreInstaller { let output = ''; let resultCode = 0; - let calculatedVersion = await this.resolveVersion( - new DotNetVersionInfo(this.version) - ); - - var envVariables: {[key: string]: string} = {}; - for (let key in process.env) { - if (process.env[key]) { - let value: any = process.env[key]; - envVariables[key] = value; - } - } - if (IS_WINDOWS) { - let escapedScript = path - .join(__dirname, '..', 'externals', 'install-dotnet.ps1') - .replace(/'/g, "''"); - let command = `& '${escapedScript}'`; - if (calculatedVersion) { - command += ` -Version ${calculatedVersion}`; - } - if (process.env['https_proxy'] != null) { - command += ` -ProxyAddress ${process.env['https_proxy']}`; - } - // This is not currently an option - if (process.env['no_proxy'] != null) { - command += ` -ProxyBypassList ${process.env['no_proxy']}`; - } - - // process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used - const powershellPath = await io.which('powershell', true); - - var options: ExecOptions = { - listeners: { - stdout: (data: Buffer) => { - output += data.toString(); - } - }, - env: envVariables - }; - - resultCode = await exec.exec( - `"${powershellPath}"`, - [ - '-NoLogo', - '-Sta', - '-NoProfile', - '-NonInteractive', - '-ExecutionPolicy', - 'Unrestricted', - '-Command', - command - ], - options - ); - } else { - let escapedScript = path - .join(__dirname, '..', 'externals', 'install-dotnet.sh') - .replace(/'/g, "''"); - chmodSync(escapedScript, '777'); - - const scriptPath = await io.which(escapedScript, true); - - let scriptArguments: string[] = []; - if (calculatedVersion) { - scriptArguments.push('--version', calculatedVersion); - } - - // process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used - resultCode = await exec.exec(`"${scriptPath}"`, scriptArguments, { - listeners: { - stdout: (data: Buffer) => { - output += data.toString(); - } - }, - env: envVariables - }); - } - - if (process.env['DOTNET_INSTALL_DIR']) { - core.addPath(process.env['DOTNET_INSTALL_DIR']); - core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']); - } else { - if (IS_WINDOWS) { - // This is the default set in install-dotnet.ps1 - core.addPath( - path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet') - ); - core.exportVariable( - 'DOTNET_ROOT', - path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet') - ); - } else { - // This is the default set in install-dotnet.sh - core.addPath(path.join(process.env['HOME'] + '', '.dotnet')); - core.exportVariable( - 'DOTNET_ROOT', - path.join(process.env['HOME'] + '', '.dotnet') - ); - } - } - - console.log(process.env['PATH']); - - if (resultCode != 0) { - throw new Error(`Failed to install dotnet ${resultCode}. ${output}`); - } - } - - public async installDotnetVersions() { - let output = ''; - let resultCode = 0; for await (const version of this.versions) { let calculatedVersion = await this.resolveVersion( - new DotNetVersionInfo(version.trim()) + new DotNetVersionInfo(version) ); var envVariables: {[key: string]: string} = {}; @@ -280,35 +165,34 @@ export class DotnetCoreInstaller { env: envVariables }); } - - if (process.env['DOTNET_INSTALL_DIR']) { - core.addPath(process.env['DOTNET_INSTALL_DIR']); - core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']); + } + if (process.env['DOTNET_INSTALL_DIR']) { + core.addPath(process.env['DOTNET_INSTALL_DIR']); + core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']); + } else { + if (IS_WINDOWS) { + // This is the default set in install-dotnet.ps1 + core.addPath( + path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet') + ); + core.exportVariable( + 'DOTNET_ROOT', + path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet') + ); } else { - if (IS_WINDOWS) { - // This is the default set in install-dotnet.ps1 - core.addPath( - path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet') - ); - core.exportVariable( - 'DOTNET_ROOT', - path.join(process.env['LocalAppData'] + '', 'Microsoft', 'dotnet') - ); - } else { - // This is the default set in install-dotnet.sh - core.addPath(path.join(process.env['HOME'] + '', '.dotnet')); - core.exportVariable( - 'DOTNET_ROOT', - path.join(process.env['HOME'] + '', '.dotnet') - ); - } + // This is the default set in install-dotnet.sh + core.addPath(path.join(process.env['HOME'] + '', '.dotnet')); + core.exportVariable( + 'DOTNET_ROOT', + path.join(process.env['HOME'] + '', '.dotnet') + ); } + } - console.log(process.env['PATH']); + console.log(process.env['PATH']); - if (resultCode != 0) { - throw new Error(`Failed to install dotnet ${resultCode}. ${output}`); - } + if (resultCode != 0) { + throw new Error(`Failed to install dotnet ${resultCode}. ${output}`); } } @@ -403,7 +287,6 @@ export class DotnetCoreInstaller { return releasesInfo[0]['releases.json']; } - private version: string; private versions: string[]; private includePrerelease: boolean; } diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index e33b7a5..8053c15 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -13,43 +13,25 @@ export async function run() { // If a valid version still can't be identified, nothing will be installed. // Proxy, auth, (etc) are still set up, even if no version is identified // - let version = core.getInput('dotnet-version'); - let versions = core.getMultilineInput('dotnet-versions'); - if (!version && !versions.length) { + let versions = core.getMultilineInput('dotnet-version'); + if (!versions.length) { // Try to fall back to global.json core.debug('No version found, trying to find version from global.json'); const globalJsonPath = path.join(process.cwd(), 'global.json'); if (fs.existsSync(globalJsonPath)) { - version = getVersionFromGlobalJson(globalJsonPath); + versions[0] = getVersionFromGlobalJson(globalJsonPath); } } - if (version && versions) { - core.warning( - "Multiple version inputs have been specified, Please specify either 'dotnet-version' or 'dotnet-versions'" - ); - } - if (versions && !version) { + + if (versions) { const includePrerelease: boolean = (core.getInput('include-prerelease') || 'false').toLowerCase() === 'true'; const dotnetInstaller = new installer.DotnetCoreInstaller( - '', versions, includePrerelease ); - await dotnetInstaller.installDotnetVersions(); - } - if (version) { - const includePrerelease: boolean = - (core.getInput('include-prerelease') || 'false').toLowerCase() === - 'true'; - - const dotnetInstaller = new installer.DotnetCoreInstaller( - version, - [], - includePrerelease - ); await dotnetInstaller.installDotnet(); }