From 68d994cec26b920461c53e070593db83753bb98f Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Wed, 28 Sep 2022 10:57:54 +0200 Subject: [PATCH] Add unit-tests --- __tests__/installer.test.ts | 17 +++++++++++++++-- __tests__/setup-dotnet.test.ts | 17 +++++++++++++++++ src/installer.ts | 2 +- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 9d94eca..79a90c3 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -107,6 +107,15 @@ describe('DotnetCoreInstaller tests', () => { expect(process.env.PATH?.startsWith(toolDir)).toBe(true); }, 600000); //This needs some time to download on "slower" internet connections + it('Returns string with installed SDK version', async () => { + const version = '3.1.120'; + let installedVersion: string; + + installedVersion = await getDotnet(version); + + expect(installedVersion).toBe('3.1.120'); + }, 600000); + it('Throws if no location contains correct dotnet version', async () => { await expect(async () => { await getDotnet('1000.0.0'); @@ -267,11 +276,15 @@ function normalizeFileContents(contents: string): string { .replace(new RegExp('\r', 'g'), '\n'); } -async function getDotnet(version: string, quality: string = ''): Promise { +async function getDotnet( + version: string, + quality: string = '' +): Promise { const dotnetInstaller = new installer.DotnetCoreInstaller( version, quality as QualityOptions ); - await dotnetInstaller.installDotnet(); + const installedVersion = await dotnetInstaller.installDotnet(); installer.DotnetCoreInstaller.addToPath(); + return installedVersion; } diff --git a/__tests__/setup-dotnet.test.ts b/__tests__/setup-dotnet.test.ts index 4cd84b6..2b3df16 100644 --- a/__tests__/setup-dotnet.test.ts +++ b/__tests__/setup-dotnet.test.ts @@ -1,4 +1,5 @@ import * as io from '@actions/io'; +import * as core from '@actions/core'; import fs from 'fs'; import os from 'os'; import path from 'path'; @@ -20,6 +21,8 @@ if (IS_WINDOWS) { const tempDir = path.join(__dirname, 'runner', 'temp2'); describe('setup-dotnet tests', () => { + let getMultilineInputSpy = jest.spyOn(core, 'getMultilineInput'); + beforeAll(async () => { process.env.RUNNER_TOOL_CACHE = toolDir; process.env.DOTNET_INSTALL_DIR = toolDir; @@ -59,4 +62,18 @@ describe('setup-dotnet tests', () => { expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true); } }, 400000); + + it('Sets output with the installed version', async () => { + const versions = ['3.1.201', '6.0.401']; + + let setOutputSpy = jest.spyOn(core, 'getMultilineInput'); + + getMultilineInputSpy.mockImplementation(() => { + return versions; + }); + + await setup.run(); + + expect(setOutputSpy).toBeCalledWith('dotnet-version', '3.1.201'); + }, 400000); }); diff --git a/src/installer.ts b/src/installer.ts index b9eedb9..3d94a5d 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -164,7 +164,7 @@ export class DotnetCoreInstaller { } } - public async installDotnet() { + public async installDotnet(): Promise { const windowsDefaultOptions = [ '-NoLogo', '-Sta',