Fix unit tests

This commit is contained in:
Sergey Dolin 2023-07-12 02:08:24 +02:00
parent 99ea9d4780
commit d4fc9881c5
2 changed files with 68 additions and 35 deletions

View file

@ -40,6 +40,8 @@ describe('setup-go', () => {
let existsSpy: jest.SpyInstance;
let readFileSpy: jest.SpyInstance;
let mkdirpSpy: jest.SpyInstance;
let mkdirSpy: jest.SpyInstance;
let symlinkSpy: jest.SpyInstance;
let execSpy: jest.SpyInstance;
let getManifestSpy: jest.SpyInstance;
let getAllVersionsSpy: jest.SpyInstance;
@ -93,6 +95,11 @@ describe('setup-go', () => {
readFileSpy = jest.spyOn(fs, 'readFileSync');
mkdirpSpy = jest.spyOn(io, 'mkdirP');
// fs
mkdirSpy = jest.spyOn(fs, 'mkdir');
symlinkSpy = jest.spyOn(fs, 'symlinkSync');
symlinkSpy.mockImplementation(() => {});
// gets
getManifestSpy.mockImplementation(() => <tc.IToolRelease[]>goTestManifest);
@ -958,39 +965,4 @@ use .
}
);
});
describe('Windows performance workaround', () => {
it('addExecutablesToCache should depends on env[RUNNER_TOOL_CACHE]', async () => {
const statSpy = jest.spyOn(fs, 'statSync');
// @ts-ignore - not implement unused methods
statSpy.mockImplementation(() => ({
isDirectory: () => true
}));
const readdirSpy = jest.spyOn(fs, 'readdirSync');
readdirSpy.mockImplementation(() => []);
const writeFileSpy = jest.spyOn(fs, 'writeFileSync');
writeFileSpy.mockImplementation(() => {});
const rmRFSpy = jest.spyOn(io, 'rmRF');
rmRFSpy.mockImplementation(() => Promise.resolve());
const mkdirPSpy = jest.spyOn(io, 'mkdirP');
mkdirPSpy.mockImplementation(() => Promise.resolve());
const cpSpy = jest.spyOn(io, 'cp');
cpSpy.mockImplementation(() => Promise.resolve());
const info: IGoVersionInfo = {
type: 'dist',
downloadUrl: 'http://nowhere.com',
resolvedVersion: '1.2.3',
fileName: 'ignore'
};
process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache1';
const cacheDir1 = await addExecutablesToCache('/qzx', info, 'arch');
expect(cacheDir1).toBe('/faked-hostedtoolcache1/go/1.2.3/arch');
process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache2';
const cacheDir2 = await addExecutablesToCache('/qzx', info, 'arch');
expect(cacheDir2).toBe('/faked-hostedtoolcache2/go/1.2.3/arch');
});
});
});

View file

@ -0,0 +1,61 @@
import fs from 'fs';
import * as io from '@actions/io';
import {addExecutablesToCache, IGoVersionInfo} from '../src/installer';
describe('Windows performance workaround', () => {
let mkdirSpy: jest.SpyInstance;
let symlinkSpy: jest.SpyInstance;
let statSpy: jest.SpyInstance;
let readdirSpy: jest.SpyInstance;
let writeFileSpy: jest.SpyInstance;
let rmRFSpy: jest.SpyInstance;
let mkdirPSpy: jest.SpyInstance;
let cpSpy: jest.SpyInstance;
let runnerToolCache: string | undefined;
beforeEach(() => {
mkdirSpy = jest.spyOn(fs, 'mkdir');
symlinkSpy = jest.spyOn(fs, 'symlinkSync');
statSpy = jest.spyOn(fs, 'statSync');
readdirSpy = jest.spyOn(fs, 'readdirSync');
writeFileSpy = jest.spyOn(fs, 'writeFileSync');
rmRFSpy = jest.spyOn(io, 'rmRF');
mkdirPSpy = jest.spyOn(io, 'mkdirP');
cpSpy = jest.spyOn(io, 'cp');
// default implementations
// @ts-ignore - not implement unused methods
statSpy.mockImplementation(() => ({
isDirectory: () => true
}));
readdirSpy.mockImplementation(() => []);
writeFileSpy.mockImplementation(() => {});
mkdirSpy.mockImplementation(() => {});
symlinkSpy.mockImplementation(() => {});
rmRFSpy.mockImplementation(() => Promise.resolve());
mkdirPSpy.mockImplementation(() => Promise.resolve());
cpSpy.mockImplementation(() => Promise.resolve());
runnerToolCache = process.env['RUNNER_TOOL_CACHE'];
});
afterEach(() => {
jest.clearAllMocks();
process.env['RUNNER_TOOL_CACHE'] = runnerToolCache;
});
it('addExecutablesToCache should depend on env[RUNNER_TOOL_CACHE]', async () => {
const info: IGoVersionInfo = {
type: 'dist',
downloadUrl: 'http://nowhere.com',
resolvedVersion: '1.2.3',
fileName: 'ignore'
};
process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache1';
const cacheDir1 = await addExecutablesToCache('/qzx', info, 'arch');
expect(cacheDir1).toBe('/faked-hostedtoolcache1/go/1.2.3/arch');
process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache2';
const cacheDir2 = await addExecutablesToCache('/qzx', info, 'arch');
expect(cacheDir2).toBe('/faked-hostedtoolcache2/go/1.2.3/arch');
});
});