Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Jul 12, 2023
1 parent 99ea9d4 commit d4fc988
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 35 deletions.
42 changes: 7 additions & 35 deletions __tests__/setup-go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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');
});
});
});
61 changes: 61 additions & 0 deletions __tests__/windows-performance.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit d4fc988

Please sign in to comment.