Add unit-tests

This commit is contained in:
IvanZosimov 2022-09-28 10:57:54 +02:00
parent 6eb9549b62
commit 68d994cec2
3 changed files with 33 additions and 3 deletions

View File

@ -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<void> {
async function getDotnet(
version: string,
quality: string = ''
): Promise<string> {
const dotnetInstaller = new installer.DotnetCoreInstaller(
version,
quality as QualityOptions
);
await dotnetInstaller.installDotnet();
const installedVersion = await dotnetInstaller.installDotnet();
installer.DotnetCoreInstaller.addToPath();
return installedVersion;
}

View File

@ -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);
});

View File

@ -164,7 +164,7 @@ export class DotnetCoreInstaller {
}
}
public async installDotnet() {
public async installDotnet(): Promise<string> {
const windowsDefaultOptions = [
'-NoLogo',
'-Sta',