From 5f836ecfcf85969038ebc86e4f7e80733a14c96c Mon Sep 17 00:00:00 2001 From: Sergey Dolin Date: Wed, 12 Jul 2023 02:08:24 +0200 Subject: [PATCH] Fix unit tests --- __tests__/setup-go.test.ts | 43 ++++--------------- __tests__/windows-performance.test.ts | 61 +++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 35 deletions(-) create mode 100644 __tests__/windows-performance.test.ts diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index 2049b9497..2a7474c7f 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -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(() => goTestManifest); @@ -333,6 +340,7 @@ describe('setup-go', () => { const toolPath = path.normalize('/cache/go/1.13.0/x64'); extractTarSpy.mockImplementation(() => '/some/other/temp/path'); cacheSpy.mockImplementation(() => toolPath); + await main.run(); const expPath = path.join(toolPath, 'bin'); @@ -958,39 +966,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'); - }); - }); }); diff --git a/__tests__/windows-performance.test.ts b/__tests__/windows-performance.test.ts new file mode 100644 index 000000000..7e7f18ebf --- /dev/null +++ b/__tests__/windows-performance.test.ts @@ -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'); + }); +});