2022-09-27 20:47:12 +08:00
|
|
|
import * as io from '@actions/io';
|
2022-09-29 23:45:25 +08:00
|
|
|
import * as core from '@actions/core';
|
2022-09-27 20:47:12 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import os from 'os';
|
|
|
|
import path from 'path';
|
2020-09-16 00:36:09 +08:00
|
|
|
|
|
|
|
import * as setup from '../src/setup-dotnet';
|
2022-09-27 20:47:12 +08:00
|
|
|
import {IS_WINDOWS} from '../src/utils';
|
|
|
|
import {IS_LINUX} from '../src/utils';
|
|
|
|
|
|
|
|
let toolDir: string;
|
2020-09-16 00:36:09 +08:00
|
|
|
|
2022-09-27 20:47:12 +08:00
|
|
|
if (IS_WINDOWS) {
|
|
|
|
toolDir = path.join(process.env['PROGRAMFILES'] + '', 'dotnet');
|
|
|
|
} else if (IS_LINUX) {
|
|
|
|
toolDir = '/usr/share/dotnet';
|
|
|
|
} else {
|
|
|
|
toolDir = path.join(process.env['HOME'] + '', '.dotnet');
|
|
|
|
}
|
|
|
|
|
2023-03-13 19:34:56 +08:00
|
|
|
function createGlobalJsonPath(dotnetVersion: string) {
|
|
|
|
const globalJsonPath = path.join(process.cwd(), 'global.json');
|
|
|
|
const jsonContents = `{${os.EOL}"sdk": {${os.EOL}"version": "${dotnetVersion}"${os.EOL}}${os.EOL}}`;
|
|
|
|
if (!fs.existsSync(globalJsonPath)) {
|
|
|
|
fs.writeFileSync(globalJsonPath, jsonContents);
|
|
|
|
}
|
|
|
|
return globalJsonPath;
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:47:12 +08:00
|
|
|
const tempDir = path.join(__dirname, 'runner', 'temp2');
|
2020-09-16 00:36:09 +08:00
|
|
|
|
2021-08-20 21:47:49 +08:00
|
|
|
describe('setup-dotnet tests', () => {
|
2023-03-09 20:43:05 +08:00
|
|
|
const getInputSpy = jest.spyOn(core, 'getInput');
|
|
|
|
const getMultilineInputSpy = jest.spyOn(core, 'getMultilineInput');
|
|
|
|
const setOutputSpy = jest.spyOn(core, 'setOutput');
|
2022-09-29 23:45:25 +08:00
|
|
|
|
2023-03-09 20:43:05 +08:00
|
|
|
const inputs = {} as any;
|
2022-09-29 23:45:25 +08:00
|
|
|
|
2020-09-16 00:36:09 +08:00
|
|
|
beforeAll(async () => {
|
|
|
|
process.env.RUNNER_TOOL_CACHE = toolDir;
|
|
|
|
process.env.DOTNET_INSTALL_DIR = toolDir;
|
|
|
|
process.env.RUNNER_TEMP = tempDir;
|
2022-09-27 20:47:12 +08:00
|
|
|
try {
|
|
|
|
await io.rmRF(`${toolDir}/*`);
|
|
|
|
await io.rmRF(`${tempDir}/*`);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err.message);
|
|
|
|
console.log('Failed to remove test directories');
|
|
|
|
}
|
|
|
|
}, 30000);
|
2020-09-16 00:36:09 +08:00
|
|
|
|
2021-08-20 21:54:24 +08:00
|
|
|
afterEach(async () => {
|
2020-09-16 00:36:09 +08:00
|
|
|
try {
|
|
|
|
await io.rmRF(path.join(process.cwd(), 'global.json'));
|
2022-09-27 20:47:12 +08:00
|
|
|
await io.rmRF(`${toolDir}/*`);
|
|
|
|
await io.rmRF(`${tempDir}/*`);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err.message);
|
2020-09-16 00:36:09 +08:00
|
|
|
console.log('Failed to remove test directories');
|
|
|
|
}
|
|
|
|
}, 30000);
|
|
|
|
|
2020-09-24 23:26:00 +08:00
|
|
|
it('Acquires version of dotnet from global.json if no matching version is installed', async () => {
|
2023-03-13 19:34:56 +08:00
|
|
|
createGlobalJsonPath('3.1.201');
|
2020-09-16 00:36:09 +08:00
|
|
|
await setup.run();
|
|
|
|
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
expect(fs.existsSync(path.join(toolDir, 'dotnet'))).toBe(true);
|
|
|
|
}
|
2020-09-24 23:26:00 +08:00
|
|
|
}, 400000);
|
2022-09-29 23:45:25 +08:00
|
|
|
|
|
|
|
it("Sets output with the latest installed by action version if global.json file isn't specified", async () => {
|
|
|
|
inputs['dotnet-version'] = ['3.1.201', '6.0.401'];
|
|
|
|
|
|
|
|
getMultilineInputSpy.mockImplementation(input => inputs[input]);
|
|
|
|
|
|
|
|
await setup.run();
|
|
|
|
|
2023-03-09 20:43:05 +08:00
|
|
|
expect(setOutputSpy).toHaveBeenCalledWith('dotnet-version', '6.0.401');
|
2022-09-29 23:45:25 +08:00
|
|
|
}, 400000);
|
|
|
|
|
|
|
|
it("Sets output with the version specified in global.json, if it's present", async () => {
|
2023-03-13 19:34:56 +08:00
|
|
|
createGlobalJsonPath('3.0.103');
|
2022-09-29 23:45:25 +08:00
|
|
|
|
|
|
|
inputs['dotnet-version'] = ['3.1.201', '6.0.401'];
|
|
|
|
inputs['global-json-file'] = './global.json';
|
|
|
|
|
|
|
|
getMultilineInputSpy.mockImplementation(input => inputs[input]);
|
|
|
|
|
|
|
|
getInputSpy.mockImplementation(input => inputs[input]);
|
|
|
|
|
|
|
|
await setup.run();
|
|
|
|
|
2023-03-09 20:43:05 +08:00
|
|
|
expect(setOutputSpy).toHaveBeenCalledWith('dotnet-version', '3.0.103');
|
2022-09-29 23:45:25 +08:00
|
|
|
}, 400000);
|
2023-03-13 19:34:56 +08:00
|
|
|
|
|
|
|
it('Sets output with the version specified in global.json with absolute path', async () => {
|
|
|
|
const globalJsonPath = createGlobalJsonPath('3.0.103');
|
|
|
|
|
|
|
|
inputs['dotnet-version'] = ['3.1.201', '6.0.401'];
|
|
|
|
inputs['global-json-file'] = globalJsonPath;
|
|
|
|
|
|
|
|
getMultilineInputSpy.mockImplementation(input => inputs[input]);
|
|
|
|
|
|
|
|
getInputSpy.mockImplementation(input => inputs[input]);
|
|
|
|
|
|
|
|
await setup.run();
|
|
|
|
|
|
|
|
expect(setOutputSpy).toHaveBeenCalledWith('dotnet-version', '3.0.103');
|
|
|
|
}, 400000);
|
2020-09-16 00:36:09 +08:00
|
|
|
});
|