1
0
Fork 0
mirror of https://github.com/actions/setup-python.git synced 2024-11-24 17:38:52 +00:00
setup-python/__tests__/finder.test.ts

91 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-08-20 14:27:52 +00:00
import io = require('@actions/io');
import fs = require('fs');
import path = require('path');
const toolDir = path.join(
__dirname,
'runner',
path.join(Math.random().toString(36).substring(7)),
2019-08-20 14:27:52 +00:00
'tools'
);
const tempDir = path.join(
__dirname,
'runner',
path.join(Math.random().toString(36).substring(7)),
2019-08-20 14:27:52 +00:00
'temp'
);
process.env['RUNNER_TOOL_CACHE'] = toolDir;
process.env['RUNNER_TEMP'] = tempDir;
import * as tc from '@actions/tool-cache';
2019-08-20 14:27:52 +00: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 14:27:52 +00:00
describe('Finder tests', () => {
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
2019-08-20 14:27:52 +00:00
it('Finds Python if it is installed', async () => {
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-02-28 07:19:48 +00:00
await finder.useCpythonVersion('3.x', 'x64');
2019-08-20 14:27:52 +00: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);
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-02-28 07:19:48 +00:00
await finder.useCpythonVersion('1.2.3', 'x64');
});
it('Finds pre-release Python version in the manifest', async () => {
const findSpy: jest.SpyInstance = jest.spyOn(tc, 'getManifestFromRepo');
findSpy.mockImplementation(() => <tc.IToolRelease[]>manifestData);
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-02-28 07:19:48 +00:00
await finder.useCpythonVersion('1.2.3-beta.2', 'x64');
});
2019-08-20 14:27:52 +00: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 14:27:52 +00:00
let thrown = false;
try {
2022-02-28 07:19:48 +00:00
await finder.useCpythonVersion('3.300000', 'x64');
2019-08-20 14:27:52 +00:00
} catch {
thrown = true;
}
expect(thrown).toBeTruthy();
});
});