setup-python/__tests__/finder.test.ts

162 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-05-04 11:40:08 +02:00
import * as io from '@actions/io';
2022-05-09 11:53:48 +02:00
import os from 'os';
import fs from 'fs';
import path from 'path';
2019-08-20 10:27:52 -04:00
const toolDir = path.join(
__dirname,
'runner',
path.join(Math.random().toString(36).substring(7)),
2019-08-20 10:27:52 -04:00
'tools'
);
const tempDir = path.join(
__dirname,
'runner',
path.join(Math.random().toString(36).substring(7)),
2019-08-20 10:27:52 -04:00
'temp'
);
process.env['RUNNER_TOOL_CACHE'] = toolDir;
process.env['RUNNER_TEMP'] = tempDir;
import * as tc from '@actions/tool-cache';
2022-05-04 11:40:08 +02:00
import * as core from '@actions/core';
2019-08-20 10:27:52 -04:00
import * as finder from '../src/find-python';
import * as installer from '../src/install-python';
const manifestData = require('./data/versions-manifest.json');
2019-08-20 10:27:52 -04:00
describe('Finder tests', () => {
2022-05-09 11:16:24 +02:00
let writeSpy: jest.SpyInstance;
beforeEach(() => {
writeSpy = jest.spyOn(process.stdout, 'write');
writeSpy.mockImplementation(() => {});
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
2022-05-09 12:53:56 +02:00
jest.restoreAllMocks();
});
2019-08-20 10:27:52 -04:00
it('Finds Python if it is installed', async () => {
2022-05-04 11:40:08 +02:00
const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput');
2022-05-09 10:48:49 +02:00
getBooleanInputSpy.mockImplementation(input => false);
2022-05-04 11:40:08 +02:00
2019-08-20 10:27:52 -04:00
const pythonDir: string = path.join(toolDir, 'Python', '3.0.0', 'x64');
await io.mkdirP(pythonDir);
fs.writeFileSync(`${pythonDir}.complete`, 'hello');
// This will throw if it doesn't find it in the cache and in the manifest (because no such version exists)
2022-05-06 09:50:12 +02:00
await finder.useCpythonVersion('3.x', 'x64', false);
2019-08-20 10:27:52 -04:00
});
it('Finds stable Python version if it is not installed, but exists in the manifest', async () => {
const findSpy: jest.SpyInstance = jest.spyOn(tc, 'getManifestFromRepo');
findSpy.mockImplementation(() => <tc.IToolRelease[]>manifestData);
2022-05-04 11:40:08 +02:00
const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput');
2022-05-09 10:48:49 +02:00
getBooleanInputSpy.mockImplementation(input => false);
2022-05-04 11:40:08 +02:00
const installSpy: jest.SpyInstance = jest.spyOn(
installer,
'installCpythonFromRelease'
);
installSpy.mockImplementation(async () => {
const pythonDir: string = path.join(toolDir, 'Python', '1.2.3', 'x64');
await io.mkdirP(pythonDir);
fs.writeFileSync(`${pythonDir}.complete`, 'hello');
});
// This will throw if it doesn't find it in the cache and in the manifest (because no such version exists)
2022-05-06 09:50:12 +02:00
await finder.useCpythonVersion('1.2.3', 'x64', false);
});
it('Finds pre-release Python version in the manifest', async () => {
const findSpy: jest.SpyInstance = jest.spyOn(tc, 'getManifestFromRepo');
findSpy.mockImplementation(() => <tc.IToolRelease[]>manifestData);
2022-05-04 11:40:08 +02:00
const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput');
2022-05-09 10:48:49 +02:00
getBooleanInputSpy.mockImplementation(input => false);
2022-05-04 11:40:08 +02:00
const installSpy: jest.SpyInstance = jest.spyOn(
installer,
'installCpythonFromRelease'
);
installSpy.mockImplementation(async () => {
const pythonDir: string = path.join(
toolDir,
'Python',
'1.2.3-beta.2',
'x64'
);
await io.mkdirP(pythonDir);
fs.writeFileSync(`${pythonDir}.complete`, 'hello');
});
// This will throw if it doesn't find it in the manifest (because no such version exists)
2022-05-06 09:50:12 +02:00
await finder.useCpythonVersion('1.2.3-beta.2', 'x64', false);
});
2022-05-04 11:40:08 +02:00
it('Check-latest true, finds the latest version in the manifest', async () => {
const findSpy: jest.SpyInstance = jest.spyOn(tc, 'getManifestFromRepo');
findSpy.mockImplementation(() => <tc.IToolRelease[]>manifestData);
const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput');
2022-05-09 10:48:49 +02:00
getBooleanInputSpy.mockImplementation(input => true);
2022-05-04 11:40:08 +02:00
const cnSpy: jest.SpyInstance = jest.spyOn(process.stdout, 'write');
cnSpy.mockImplementation(line => {
// uncomment to debug
// process.stderr.write('write:' + line + '\n');
});
const infoSpy: jest.SpyInstance = jest.spyOn(core, 'info');
2022-05-09 10:48:49 +02:00
infoSpy.mockImplementation(() => {});
2022-05-04 11:40:08 +02:00
const debugSpy: jest.SpyInstance = jest.spyOn(core, 'debug');
2022-05-09 10:48:49 +02:00
debugSpy.mockImplementation(() => {});
2022-05-04 11:40:08 +02:00
const pythonDir: string = path.join(toolDir, 'Python', '1.2.2', 'x64');
const expPath: string = path.join(toolDir, 'Python', '1.2.3', 'x64');
const installSpy: jest.SpyInstance = jest.spyOn(
installer,
'installCpythonFromRelease'
);
installSpy.mockImplementation(async () => {
await io.mkdirP(expPath);
fs.writeFileSync(`${expPath}.complete`, 'hello');
});
2022-05-09 13:04:52 +02:00
const tcFindSpy: jest.SpyInstance = jest.spyOn(tc, 'find');
tcFindSpy.mockImplementationOnce(() => "").mockImplementationOnce(() => expPath);
2022-05-04 11:40:08 +02:00
await io.mkdirP(pythonDir);
2022-05-09 12:53:56 +02:00
await io.rmRF(path.join(toolDir, 'Python', '1.2.3'));
2022-05-09 10:48:49 +02:00
2022-05-04 11:40:08 +02:00
fs.writeFileSync(`${pythonDir}.complete`, 'hello');
// This will throw if it doesn't find it in the cache and in the manifest (because no such version exists)
2022-05-06 09:50:12 +02:00
await finder.useCpythonVersion('1.2', 'x64', true);
2022-05-04 11:40:08 +02:00
expect(infoSpy).toHaveBeenCalledWith("Resolved as '1.2.3'");
2022-05-09 10:48:49 +02:00
expect(infoSpy).toHaveBeenCalledWith(
'Version 1.2.3 was not found in the local cache'
);
2022-05-09 12:53:56 +02:00
expect(infoSpy).toBeCalledWith(
'Version 1.2.3 is available for downloading'
);
expect(installSpy).toHaveBeenCalled();
2022-05-04 11:40:08 +02:00
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${os.EOL}`);
});
2019-08-20 10:27:52 -04:00
it('Errors if Python is not installed', async () => {
// This will throw if it doesn't find it in the cache and in the manifest (because no such version exists)
2019-08-20 10:27:52 -04:00
let thrown = false;
try {
2022-05-06 09:50:12 +02:00
await finder.useCpythonVersion('3.300000', 'x64', false);
2019-08-20 10:27:52 -04:00
} catch {
thrown = true;
}
expect(thrown).toBeTruthy();
});
});