setup-dotnet/__tests__/setup-dotnet.test.ts

226 lines
8.3 KiB
TypeScript
Raw Permalink Normal View History

import * as core from '@actions/core';
import fs from 'fs';
import semver from 'semver';
import * as auth from '../src/authutil';
import * as setup from '../src/setup-dotnet';
2023-05-30 17:45:38 +07:00
import {DotnetCoreInstaller, DotnetInstallDir} from '../src/installer';
feat: Cache NuGet global-packages folder (#303) * feat: cache NuGet global-packages folder * fix: remove unused files * docs: fix incorrect action * ci: add e2e test for cache * docs: accept suggested changes on README * docs: add simple cache example * build: change main script path * fix: change relative path to install scripts * fix: change relative path to problem matcher * refactor: accept changes on cache-utils * fix: revert main script path changes * test: fix cache-utils unit test * test: fix cache-utils unit test * feat: add `cache-dependency-path` variables * build: change main script dist path * ci: add `cache-dependency-path` e2e test & missing lock file * fix: accept change suggestions * ci: copy NuGet lock file to root to pass "test-setup-with-cache" e2e test * docs: change README guide * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * test: fix some failed unit tests - fix `restoreCache()` test for 9703c8 - update installer script * build: rebuild dist * Update unit-tests - Additional unit test were added to setup-dotnet.test.ts * Update unit tests for unix systems * Format and lint unit tests * fix: avoid use '/' on `path.join` * fix: rebuild dist * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * build: add `DisableImplicitNuGetFallbackFolder` option also add guide on README * docs: highlight warnings and notes * docs: update note about handling NU1403 --------- Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> Co-authored-by: IvanZosimov <ivanzosimov@github.com>
2023-05-29 17:43:18 +07:00
import * as cacheUtils from '../src/cache-utils';
import * as cacheRestore from '../src/cache-restore';
2021-08-20 20:47:49 +07:00
describe('setup-dotnet tests', () => {
const inputs = {} as any;
const getInputSpy = jest.spyOn(core, 'getInput');
const getMultilineInputSpy = jest.spyOn(core, 'getMultilineInput');
feat: Cache NuGet global-packages folder (#303) * feat: cache NuGet global-packages folder * fix: remove unused files * docs: fix incorrect action * ci: add e2e test for cache * docs: accept suggested changes on README * docs: add simple cache example * build: change main script path * fix: change relative path to install scripts * fix: change relative path to problem matcher * refactor: accept changes on cache-utils * fix: revert main script path changes * test: fix cache-utils unit test * test: fix cache-utils unit test * feat: add `cache-dependency-path` variables * build: change main script dist path * ci: add `cache-dependency-path` e2e test & missing lock file * fix: accept change suggestions * ci: copy NuGet lock file to root to pass "test-setup-with-cache" e2e test * docs: change README guide * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * test: fix some failed unit tests - fix `restoreCache()` test for 9703c8 - update installer script * build: rebuild dist * Update unit-tests - Additional unit test were added to setup-dotnet.test.ts * Update unit tests for unix systems * Format and lint unit tests * fix: avoid use '/' on `path.join` * fix: rebuild dist * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * build: add `DisableImplicitNuGetFallbackFolder` option also add guide on README * docs: highlight warnings and notes * docs: update note about handling NU1403 --------- Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> Co-authored-by: IvanZosimov <ivanzosimov@github.com>
2023-05-29 17:43:18 +07:00
const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput');
const setFailedSpy = jest.spyOn(core, 'setFailed');
2023-05-18 16:11:51 +07:00
const warningSpy = jest.spyOn(core, 'warning');
const debugSpy = jest.spyOn(core, 'debug');
const infoSpy = jest.spyOn(core, 'info');
const setOutputSpy = jest.spyOn(core, 'setOutput');
const existsSyncSpy = jest.spyOn(fs, 'existsSync');
const maxSatisfyingSpy = jest.spyOn(semver, 'maxSatisfying');
const installDotnetSpy = jest.spyOn(
DotnetCoreInstaller.prototype,
'installDotnet'
);
feat: Cache NuGet global-packages folder (#303) * feat: cache NuGet global-packages folder * fix: remove unused files * docs: fix incorrect action * ci: add e2e test for cache * docs: accept suggested changes on README * docs: add simple cache example * build: change main script path * fix: change relative path to install scripts * fix: change relative path to problem matcher * refactor: accept changes on cache-utils * fix: revert main script path changes * test: fix cache-utils unit test * test: fix cache-utils unit test * feat: add `cache-dependency-path` variables * build: change main script dist path * ci: add `cache-dependency-path` e2e test & missing lock file * fix: accept change suggestions * ci: copy NuGet lock file to root to pass "test-setup-with-cache" e2e test * docs: change README guide * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * test: fix some failed unit tests - fix `restoreCache()` test for 9703c8 - update installer script * build: rebuild dist * Update unit-tests - Additional unit test were added to setup-dotnet.test.ts * Update unit tests for unix systems * Format and lint unit tests * fix: avoid use '/' on `path.join` * fix: rebuild dist * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * build: add `DisableImplicitNuGetFallbackFolder` option also add guide on README * docs: highlight warnings and notes * docs: update note about handling NU1403 --------- Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> Co-authored-by: IvanZosimov <ivanzosimov@github.com>
2023-05-29 17:43:18 +07:00
const isCacheFeatureAvailableSpy = jest.spyOn(
cacheUtils,
'isCacheFeatureAvailable'
);
const restoreCacheSpy = jest.spyOn(cacheRestore, 'restoreCache');
const configAuthenticationSpy = jest.spyOn(auth, 'configAuthentication');
2023-05-30 17:45:38 +07:00
const addToPathOriginal = DotnetInstallDir.addToPath;
describe('run() tests', () => {
beforeEach(() => {
2023-05-30 17:45:38 +07:00
DotnetInstallDir.addToPath = jest.fn();
getMultilineInputSpy.mockImplementation(input => inputs[input as string]);
getInputSpy.mockImplementation(input => inputs[input as string]);
feat: Cache NuGet global-packages folder (#303) * feat: cache NuGet global-packages folder * fix: remove unused files * docs: fix incorrect action * ci: add e2e test for cache * docs: accept suggested changes on README * docs: add simple cache example * build: change main script path * fix: change relative path to install scripts * fix: change relative path to problem matcher * refactor: accept changes on cache-utils * fix: revert main script path changes * test: fix cache-utils unit test * test: fix cache-utils unit test * feat: add `cache-dependency-path` variables * build: change main script dist path * ci: add `cache-dependency-path` e2e test & missing lock file * fix: accept change suggestions * ci: copy NuGet lock file to root to pass "test-setup-with-cache" e2e test * docs: change README guide * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * test: fix some failed unit tests - fix `restoreCache()` test for 9703c8 - update installer script * build: rebuild dist * Update unit-tests - Additional unit test were added to setup-dotnet.test.ts * Update unit tests for unix systems * Format and lint unit tests * fix: avoid use '/' on `path.join` * fix: rebuild dist * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * build: add `DisableImplicitNuGetFallbackFolder` option also add guide on README * docs: highlight warnings and notes * docs: update note about handling NU1403 --------- Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> Co-authored-by: IvanZosimov <ivanzosimov@github.com>
2023-05-29 17:43:18 +07:00
getBooleanInputSpy.mockImplementation(input => inputs[input as string]);
});
afterEach(() => {
2023-05-30 17:45:38 +07:00
DotnetInstallDir.addToPath = addToPathOriginal;
jest.clearAllMocks();
jest.resetAllMocks();
});
it('should fail the action if global-json-file input is present, but the file does not exist in the file system', async () => {
inputs['global-json-file'] = 'fictitious.json';
inputs['dotnet-version'] = [];
const expectedErrorMessage = `The specified global.json file '${inputs['global-json-file']}' does not exist`;
await setup.run();
expect(setFailedSpy).toHaveBeenCalledWith(expectedErrorMessage);
});
test(`if 'dotnet-version' and 'global-json-file' inputs aren't present, should log into debug output, try to find global.json in the repo root, fail and log message into info output`, async () => {
inputs['global-json-file'] = '';
inputs['dotnet-version'] = [];
maxSatisfyingSpy.mockImplementation(() => null);
setOutputSpy.mockImplementation(() => {});
const expectedDebugMessage =
'No version found, trying to find version from global.json';
2023-05-22 17:27:33 +07:00
const expectedInfoMessage = `The global.json wasn't found in the root directory. No .NET version will be installed.`;
await setup.run();
expect(debugSpy).toHaveBeenCalledWith(expectedDebugMessage);
expect(existsSyncSpy).toHaveBeenCalled();
expect(infoSpy).toHaveBeenCalledWith(expectedInfoMessage);
});
it('should fail the action if quality is supplied but its value is not supported', async () => {
inputs['global-json-file'] = '';
inputs['dotnet-version'] = ['6.0'];
inputs['dotnet-quality'] = 'fictitiousQuality';
2023-05-18 17:39:22 +07:00
const expectedErrorMessage = `Value '${inputs['dotnet-quality']}' is not supported for the 'dotnet-quality' option. Supported values are: daily, signed, validated, preview, ga.`;
await setup.run();
expect(setFailedSpy).toHaveBeenCalledWith(expectedErrorMessage);
});
it('should call installDotnet() multiple times if dotnet-version multiline input is provided', async () => {
inputs['global-json-file'] = '';
inputs['dotnet-version'] = ['6.0', '7.0'];
inputs['dotnet-quality'] = '';
installDotnetSpy.mockImplementation(() => Promise.resolve(''));
await setup.run();
expect(installDotnetSpy).toHaveBeenCalledTimes(2);
});
it('should call addToPath() after installation complete', async () => {
inputs['global-json-file'] = '';
inputs['dotnet-version'] = ['6.0', '7.0'];
inputs['dotnet-quality'] = '';
installDotnetSpy.mockImplementation(() => Promise.resolve(''));
await setup.run();
2023-05-30 17:45:38 +07:00
expect(DotnetInstallDir.addToPath).toHaveBeenCalledTimes(1);
});
it('should call auth.configAuthentication() if source-url input is provided', async () => {
inputs['global-json-file'] = '';
inputs['dotnet-version'] = [];
inputs['dotnet-quality'] = '';
inputs['source-url'] = 'fictitious.source.url';
configAuthenticationSpy.mockImplementation(() => {});
await setup.run();
expect(configAuthenticationSpy).toHaveBeenCalledWith(
inputs['source-url'],
undefined
);
});
it('should call auth.configAuthentication() with proper parameters if source-url and config-file inputs are provided', async () => {
inputs['global-json-file'] = '';
inputs['dotnet-version'] = [];
inputs['dotnet-quality'] = '';
inputs['source-url'] = 'fictitious.source.url';
inputs['config-file'] = 'fictitious.path';
configAuthenticationSpy.mockImplementation(() => {});
setOutputSpy.mockImplementation(() => {});
await setup.run();
expect(configAuthenticationSpy).toHaveBeenCalledWith(
inputs['source-url'],
inputs['config-file']
);
});
2023-05-18 16:11:51 +07:00
it('should call setOutput() after installation complete successfully', async () => {
inputs['dotnet-version'] = ['6.0.300'];
2023-05-18 16:11:51 +07:00
installDotnetSpy.mockImplementation(() =>
Promise.resolve(`${inputs['dotnet-version']}`)
);
await setup.run();
2023-05-30 17:45:38 +07:00
expect(DotnetInstallDir.addToPath).toHaveBeenCalledTimes(1);
});
2023-05-18 16:11:51 +07:00
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));
await setup.run();
expect(warningSpy).toHaveBeenCalledWith(warningMessage);
expect(setOutputSpy).not.toHaveBeenCalled();
});
it(`shouldn't call setOutput() if actions didn't install .NET`, async () => {
inputs['dotnet-version'] = [];
2023-05-18 17:39:22 +07:00
const warningMessage = `The 'dotnet-version' output will not be set.`;
2023-05-18 16:11:51 +07:00
await setup.run();
expect(infoSpy).toHaveBeenCalledWith(warningMessage);
expect(setOutputSpy).not.toHaveBeenCalled();
});
feat: Cache NuGet global-packages folder (#303) * feat: cache NuGet global-packages folder * fix: remove unused files * docs: fix incorrect action * ci: add e2e test for cache * docs: accept suggested changes on README * docs: add simple cache example * build: change main script path * fix: change relative path to install scripts * fix: change relative path to problem matcher * refactor: accept changes on cache-utils * fix: revert main script path changes * test: fix cache-utils unit test * test: fix cache-utils unit test * feat: add `cache-dependency-path` variables * build: change main script dist path * ci: add `cache-dependency-path` e2e test & missing lock file * fix: accept change suggestions * ci: copy NuGet lock file to root to pass "test-setup-with-cache" e2e test * docs: change README guide * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * test: fix some failed unit tests - fix `restoreCache()` test for 9703c8 - update installer script * build: rebuild dist * Update unit-tests - Additional unit test were added to setup-dotnet.test.ts * Update unit tests for unix systems * Format and lint unit tests * fix: avoid use '/' on `path.join` * fix: rebuild dist * fix: apply suggestions from code review Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> * build: add `DisableImplicitNuGetFallbackFolder` option also add guide on README * docs: highlight warnings and notes * docs: update note about handling NU1403 --------- Co-authored-by: Ivan <98037481+IvanZosimov@users.noreply.github.com> Co-authored-by: IvanZosimov <ivanzosimov@github.com>
2023-05-29 17:43:18 +07:00
it(`should get 'cache-dependency-path' and call restoreCache() if input cache is set to true and cache feature is available`, async () => {
inputs['dotnet-version'] = ['6.0.300'];
inputs['dotnet-quality'] = '';
inputs['cache'] = true;
inputs['cache-dependency-path'] = 'fictitious.package.lock.json';
installDotnetSpy.mockImplementation(() => Promise.resolve(''));
isCacheFeatureAvailableSpy.mockImplementation(() => true);
restoreCacheSpy.mockImplementation(() => Promise.resolve());
await setup.run();
expect(isCacheFeatureAvailableSpy).toHaveBeenCalledTimes(1);
expect(restoreCacheSpy).toHaveBeenCalledWith(
inputs['cache-dependency-path']
);
});
it(`shouldn't call restoreCache() if input cache isn't set to true`, async () => {
inputs['dotnet-version'] = ['6.0.300'];
inputs['dotnet-quality'] = '';
inputs['cache'] = false;
installDotnetSpy.mockImplementation(() => Promise.resolve(''));
isCacheFeatureAvailableSpy.mockImplementation(() => true);
restoreCacheSpy.mockImplementation(() => Promise.resolve());
await setup.run();
expect(restoreCacheSpy).not.toHaveBeenCalled();
});
it(`shouldn't call restoreCache() if cache feature isn't available`, async () => {
inputs['dotnet-version'] = ['6.0.300'];
inputs['dotnet-quality'] = '';
inputs['cache'] = true;
installDotnetSpy.mockImplementation(() => Promise.resolve(''));
isCacheFeatureAvailableSpy.mockImplementation(() => false);
restoreCacheSpy.mockImplementation(() => Promise.resolve());
await setup.run();
expect(restoreCacheSpy).not.toHaveBeenCalled();
});
});
});