2019-08-20 22:27:52 +08:00
|
|
|
import io = require('@actions/io');
|
|
|
|
import fs = require('fs');
|
|
|
|
import path = require('path');
|
|
|
|
|
|
|
|
const toolDir = path.join(
|
|
|
|
__dirname,
|
|
|
|
'runner',
|
2020-03-26 23:39:48 +08:00
|
|
|
path.join(Math.random().toString(36).substring(7)),
|
2019-08-20 22:27:52 +08:00
|
|
|
'tools'
|
|
|
|
);
|
|
|
|
const tempDir = path.join(
|
|
|
|
__dirname,
|
|
|
|
'runner',
|
2020-03-26 23:39:48 +08:00
|
|
|
path.join(Math.random().toString(36).substring(7)),
|
2019-08-20 22:27:52 +08:00
|
|
|
'temp'
|
|
|
|
);
|
|
|
|
|
|
|
|
process.env['RUNNER_TOOL_CACHE'] = toolDir;
|
|
|
|
process.env['RUNNER_TEMP'] = tempDir;
|
|
|
|
|
2020-04-30 01:57:02 +08:00
|
|
|
import * as tc from '@actions/tool-cache';
|
2022-06-29 23:00:51 +08:00
|
|
|
import * as core from '@actions/core';
|
2019-08-20 22:27:52 +08:00
|
|
|
import * as finder from '../src/find-python';
|
2020-04-30 01:57:02 +08:00
|
|
|
import * as installer from '../src/install-python';
|
|
|
|
|
2020-07-17 17:58:03 +08:00
|
|
|
const manifestData = require('./data/versions-manifest.json');
|
2019-08-20 22:27:52 +08:00
|
|
|
|
|
|
|
describe('Finder tests', () => {
|
2022-06-29 23:00:51 +08:00
|
|
|
let spyCoreAddPath: jest.SpyInstance;
|
|
|
|
let spyCoreExportVariable: jest.SpyInstance;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
spyCoreAddPath = jest.spyOn(core, 'addPath');
|
|
|
|
|
|
|
|
spyCoreExportVariable = jest.spyOn(core, 'exportVariable');
|
|
|
|
});
|
|
|
|
|
2020-04-30 01:57:02 +08:00
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
jest.clearAllMocks();
|
2022-06-29 23:00:51 +08:00
|
|
|
jest.restoreAllMocks();
|
2020-04-30 01:57:02 +08:00
|
|
|
});
|
|
|
|
|
2019-08-20 22:27:52 +08: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');
|
2020-04-30 01:57:02 +08:00
|
|
|
// This will throw if it doesn't find it in the cache and in the manifest (because no such version exists)
|
2022-06-29 23:00:51 +08:00
|
|
|
await finder.useCpythonVersion('3.x', 'x64', true);
|
|
|
|
expect(spyCoreAddPath).toHaveBeenCalled();
|
|
|
|
expect(spyCoreExportVariable).toHaveBeenCalledWith(
|
|
|
|
'pythonLocation',
|
|
|
|
expect.anything()
|
|
|
|
);
|
|
|
|
expect(spyCoreExportVariable).toHaveBeenCalledWith(
|
|
|
|
'PKG_CONFIG_PATH',
|
|
|
|
expect.anything()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Finds Python if it is installed without environment update', 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)
|
|
|
|
await finder.useCpythonVersion('3.x', 'x64', false);
|
|
|
|
expect(spyCoreAddPath).not.toHaveBeenCalled();
|
|
|
|
expect(spyCoreExportVariable).not.toHaveBeenCalled();
|
|
|
|
expect(spyCoreExportVariable).not.toHaveBeenCalled();
|
2019-08-20 22:27:52 +08:00
|
|
|
});
|
|
|
|
|
2020-07-17 17:58:03 +08: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);
|
2020-04-30 01:57:02 +08: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-06-29 23:00:51 +08:00
|
|
|
await finder.useCpythonVersion('1.2.3', 'x64', true);
|
|
|
|
expect(spyCoreAddPath).toHaveBeenCalled();
|
|
|
|
expect(spyCoreExportVariable).toHaveBeenCalledWith(
|
|
|
|
'pythonLocation',
|
|
|
|
expect.anything()
|
|
|
|
);
|
|
|
|
expect(spyCoreExportVariable).toHaveBeenCalledWith(
|
|
|
|
'PKG_CONFIG_PATH',
|
|
|
|
expect.anything()
|
|
|
|
);
|
2020-04-30 01:57:02 +08:00
|
|
|
});
|
|
|
|
|
2020-07-17 17:58:03 +08:00
|
|
|
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-06-29 23:00:51 +08:00
|
|
|
await finder.useCpythonVersion('1.2.3-beta.2', 'x64', true);
|
|
|
|
expect(spyCoreAddPath).toHaveBeenCalled();
|
|
|
|
expect(spyCoreExportVariable).toHaveBeenCalledWith(
|
|
|
|
'pythonLocation',
|
|
|
|
expect.anything()
|
|
|
|
);
|
|
|
|
expect(spyCoreExportVariable).toHaveBeenCalledWith(
|
|
|
|
'PKG_CONFIG_PATH',
|
|
|
|
expect.anything()
|
|
|
|
);
|
2020-07-17 17:58:03 +08:00
|
|
|
});
|
|
|
|
|
2019-08-20 22:27:52 +08:00
|
|
|
it('Errors if Python is not installed', async () => {
|
2020-04-30 01:57:02 +08:00
|
|
|
// This will throw if it doesn't find it in the cache and in the manifest (because no such version exists)
|
2019-08-20 22:27:52 +08:00
|
|
|
let thrown = false;
|
|
|
|
try {
|
2022-06-29 23:00:51 +08:00
|
|
|
await finder.useCpythonVersion('3.300000', 'x64', true);
|
2019-08-20 22:27:52 +08:00
|
|
|
} catch {
|
|
|
|
thrown = true;
|
|
|
|
}
|
|
|
|
expect(thrown).toBeTruthy();
|
2022-06-29 23:00:51 +08:00
|
|
|
expect(spyCoreAddPath).not.toHaveBeenCalled();
|
|
|
|
expect(spyCoreExportVariable).not.toHaveBeenCalled();
|
2019-08-20 22:27:52 +08:00
|
|
|
});
|
|
|
|
});
|