Skip to content

Commit

Permalink
Merge pull request #432 from akv-platform/refactor-installer
Browse files Browse the repository at this point in the history
Refactor installer
  • Loading branch information
nikolai-laevskii committed Jun 6, 2023
2 parents 3447fd6 + 3cdb094 commit 7ed547c
Show file tree
Hide file tree
Showing 7 changed files with 472 additions and 450 deletions.
20 changes: 10 additions & 10 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,22 @@ describe('installer tests', () => {
describe('addToPath() tests', () => {
it(`should export DOTNET_ROOT env.var with value from DOTNET_INSTALL_DIR env.var`, async () => {
process.env['DOTNET_INSTALL_DIR'] = 'fictitious/dotnet/install/dir';
installer.DotnetCoreInstaller.addToPath();
installer.DotnetInstallDir.addToPath();
const dotnet_root = process.env['DOTNET_ROOT'];
expect(dotnet_root).toBe(process.env['DOTNET_INSTALL_DIR']);
});

it(`should export value from DOTNET_INSTALL_DIR env.var to the PATH`, async () => {
process.env['DOTNET_INSTALL_DIR'] = 'fictitious/dotnet/install/dir';
installer.DotnetCoreInstaller.addToPath();
installer.DotnetInstallDir.addToPath();
const path = process.env['PATH'];
expect(path).toContain(process.env['DOTNET_INSTALL_DIR']);
});
});
});

describe('DotnetVersionResolver tests', () => {
describe('createDotNetVersion() tests', () => {
describe('createDotnetVersion() tests', () => {
each([
'3.1',
'3.x',
Expand All @@ -329,7 +329,7 @@ describe('installer tests', () => {
version
);
const versionObject =
await dotnetVersionResolver.createDotNetVersion();
await dotnetVersionResolver.createDotnetVersion();

expect(!!versionObject.value).toBe(true);
}
Expand Down Expand Up @@ -368,7 +368,7 @@ describe('installer tests', () => {
);

await expect(
async () => await dotnetVersionResolver.createDotNetVersion()
async () => await dotnetVersionResolver.createDotnetVersion()
).rejects.toThrow();
}
);
Expand All @@ -380,7 +380,7 @@ describe('installer tests', () => {
version
);
const versionObject =
await dotnetVersionResolver.createDotNetVersion();
await dotnetVersionResolver.createDotnetVersion();

expect(versionObject.type.toLowerCase().includes('channel')).toBe(
true
Expand All @@ -395,7 +395,7 @@ describe('installer tests', () => {
version
);
const versionObject =
await dotnetVersionResolver.createDotNetVersion();
await dotnetVersionResolver.createDotnetVersion();

expect(versionObject.type.toLowerCase().includes('channel')).toBe(
true
Expand All @@ -411,7 +411,7 @@ describe('installer tests', () => {
version
);
const versionObject =
await dotnetVersionResolver.createDotNetVersion();
await dotnetVersionResolver.createDotnetVersion();

expect(versionObject.type.toLowerCase().includes('version')).toBe(
true
Expand All @@ -427,7 +427,7 @@ describe('installer tests', () => {
version
);
const versionObject =
await dotnetVersionResolver.createDotNetVersion();
await dotnetVersionResolver.createDotnetVersion();
const windowsRegEx = new RegExp(/^-(Version|Channel)/);
const nonWindowsRegEx = new RegExp(/^--(version|channel)/);

Expand All @@ -447,7 +447,7 @@ describe('installer tests', () => {
version
);
await expect(
async () => await dotnetVersionResolver.createDotNetVersion()
async () => await dotnetVersionResolver.createDotnetVersion()
).rejects.toThrow(
`'dotnet-version' was supplied in invalid format: ${version}! The A.B.Cxx syntax is available since the .NET 5.0 release.`
);
Expand Down
19 changes: 7 additions & 12 deletions __tests__/setup-dotnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import semver from 'semver';
import * as auth from '../src/authutil';

import * as setup from '../src/setup-dotnet';
import {DotnetCoreInstaller} from '../src/installer';
import {DotnetCoreInstaller, DotnetInstallDir} from '../src/installer';
import * as cacheUtils from '../src/cache-utils';
import * as cacheRestore from '../src/cache-restore';

Expand All @@ -28,22 +28,25 @@ describe('setup-dotnet tests', () => {
DotnetCoreInstaller.prototype,
'installDotnet'
);
const addToPathSpy = jest.spyOn(DotnetCoreInstaller, 'addToPath');

const isCacheFeatureAvailableSpy = jest.spyOn(
cacheUtils,
'isCacheFeatureAvailable'
);
const restoreCacheSpy = jest.spyOn(cacheRestore, 'restoreCache');
const configAuthenticationSpy = jest.spyOn(auth, 'configAuthentication');
const addToPathOriginal = DotnetInstallDir.addToPath;

describe('run() tests', () => {
beforeEach(() => {
DotnetInstallDir.addToPath = jest.fn();
getMultilineInputSpy.mockImplementation(input => inputs[input as string]);
getInputSpy.mockImplementation(input => inputs[input as string]);
getBooleanInputSpy.mockImplementation(input => inputs[input as string]);
});

afterEach(() => {
DotnetInstallDir.addToPath = addToPathOriginal;
jest.clearAllMocks();
jest.resetAllMocks();
});
Expand Down Expand Up @@ -104,10 +107,9 @@ describe('setup-dotnet tests', () => {
inputs['dotnet-quality'] = '';

installDotnetSpy.mockImplementation(() => Promise.resolve(''));
addToPathSpy.mockImplementation(() => {});

await setup.run();
expect(addToPathSpy).toHaveBeenCalledTimes(1);
expect(DotnetInstallDir.addToPath).toHaveBeenCalledTimes(1);
});

it('should call auth.configAuthentication() if source-url input is provided', async () => {
Expand Down Expand Up @@ -148,18 +150,16 @@ describe('setup-dotnet tests', () => {
installDotnetSpy.mockImplementation(() =>
Promise.resolve(`${inputs['dotnet-version']}`)
);
addToPathSpy.mockImplementation(() => {});

await setup.run();
expect(setOutputSpy).toHaveBeenCalledTimes(1);
expect(DotnetInstallDir.addToPath).toHaveBeenCalledTimes(1);
});

it(`shouldn't call setOutput() if parsing dotnet-installer logs failed`, async () => {
inputs['dotnet-version'] = ['6.0.300'];
const warningMessage = `Failed to output the installed version of .NET. The 'dotnet-version' output will not be set.`;

installDotnetSpy.mockImplementation(() => Promise.resolve(null));
addToPathSpy.mockImplementation(() => {});

await setup.run();
expect(warningSpy).toHaveBeenCalledWith(warningMessage);
Expand All @@ -170,8 +170,6 @@ describe('setup-dotnet tests', () => {
inputs['dotnet-version'] = [];
const warningMessage = `The 'dotnet-version' output will not be set.`;

addToPathSpy.mockImplementation(() => {});

await setup.run();

expect(infoSpy).toHaveBeenCalledWith(warningMessage);
Expand All @@ -185,7 +183,6 @@ describe('setup-dotnet tests', () => {
inputs['cache-dependency-path'] = 'fictitious.package.lock.json';

installDotnetSpy.mockImplementation(() => Promise.resolve(''));
addToPathSpy.mockImplementation(() => {});

isCacheFeatureAvailableSpy.mockImplementation(() => true);
restoreCacheSpy.mockImplementation(() => Promise.resolve());
Expand All @@ -203,7 +200,6 @@ describe('setup-dotnet tests', () => {
inputs['cache'] = false;

installDotnetSpy.mockImplementation(() => Promise.resolve(''));
addToPathSpy.mockImplementation(() => {});

isCacheFeatureAvailableSpy.mockImplementation(() => true);
restoreCacheSpy.mockImplementation(() => Promise.resolve());
Expand All @@ -218,7 +214,6 @@ describe('setup-dotnet tests', () => {
inputs['cache'] = true;

installDotnetSpy.mockImplementation(() => Promise.resolve(''));
addToPathSpy.mockImplementation(() => {});

isCacheFeatureAvailableSpy.mockImplementation(() => false);
restoreCacheSpy.mockImplementation(() => Promise.resolve());
Expand Down

0 comments on commit 7ed547c

Please sign in to comment.