import * as main from '../src/main'; import * as core from '@actions/core'; import * as sass from '../src/install-dart-sass'; import {readFileSync} from 'fs'; import * as io from '@actions/io'; import path from 'path'; import {Tool, Action} from '../src/constants'; import {FetchError} from 'node-fetch'; import {clearMockFetchResponses, mockFetchResponse} from './mocks/node-fetch'; import jsonTestBrew from './data/brew.json'; // import jsonTestGithub from './data/github.json'; jest.setTimeout(30000); describe('Integration testing run()', () => { beforeEach(() => { jest.resetModules(); }); afterEach(async () => { const workDir = path.join(`${process.env.HOME}`, Action.WorkDirName); await io.rmRF(workDir); delete process.env['INPUT_HUGO-VERSION']; delete process.env['INPUT_EXTENDED']; clearMockFetchResponses(); }); test('succeed in installing a custom version', async () => { const testVersion = Tool.TestVersionSpec; process.env['INPUT_HUGO-VERSION'] = testVersion; const result: main.ActionResult = await main.run(); expect(result.exitcode).toBe(0); expect(result.output).toMatch(`hugo v${testVersion}`); }); test('succeed in installing a custom extended version', async () => { const testVersion = Tool.TestVersionSpec; process.env['INPUT_HUGO-VERSION'] = testVersion; process.env['INPUT_EXTENDED'] = 'true'; const result: main.ActionResult = await main.run(); expect(result.exitcode).toBe(0); expect(result.output).toMatch(`hugo v${testVersion}`); expect(result.output).toMatch(`extended`); }); test('succeed in installing the latest version', async () => { const testVersion = 'latest'; process.env['INPUT_HUGO-VERSION'] = testVersion; mockFetchResponse(`https://formulae.brew.sh/api/formula/${Tool.Repo}.json`, 200, jsonTestBrew); const result: main.ActionResult = await main.run(); expect(result.exitcode).toBe(0); expect(result.output).toMatch(`hugo v${Tool.TestVersionLatest}`); }); test('succeed in installing the latest extended version', async () => { const testVersion = 'latest'; process.env['INPUT_HUGO-VERSION'] = testVersion; process.env['INPUT_EXTENDED'] = 'true'; mockFetchResponse(`https://formulae.brew.sh/api/formula/${Tool.Repo}.json`, 200, jsonTestBrew); const result: main.ActionResult = await main.run(); expect(result.exitcode).toBe(0); expect(result.output).toMatch(`hugo v${Tool.TestVersionLatest}`); expect(result.output).toMatch(`extended`); }); test('fail to install the latest version due to 404 of brew', async () => { process.env['INPUT_HUGO-VERSION'] = 'latest'; mockFetchResponse(`https://formulae.brew.sh/api/formula/${Tool.Repo}.json`, 404); await expect(main.run()).rejects.toThrow(FetchError); }); }); describe('showVersion()', () => { let result: main.ActionResult = { exitcode: 0, output: '' }; test('return version', async () => { result = await main.showVersion('git', ['--version']); expect(result.exitcode).toBe(0); expect(result.output).toMatch(/git version/); }); test('return not found', async () => { await expect(main.showVersion('gitgit', ['--version'])).rejects.toThrow('spawn gitgit ENOENT'); }); }); describe('Dart Sass token input plumbing', () => { afterEach(() => jest.restoreAllMocks()); test('declares the runner-resolved GitHub token as the default', () => { const metadata = readFileSync(path.join(__dirname, '..', 'action.yml'), 'utf8'); expect(metadata).toMatch(/github-token:[\s\S]*?default: \$\{\{ github.token \}\}/); }); test.each(['runner-resolved-default-token', 'explicit-token', ''])( 'passes the resolved github-token input %j explicitly to Sass', async token => { jest.spyOn(core, 'getInput').mockImplementation(name => { const inputs: Record = { 'hugo-version': Tool.TestVersionSpec, 'dart-sass-version': '1.108.2', 'github-token': token }; return inputs[name] || ''; }); const install = jest.spyOn(sass, 'installDartSass').mockResolvedValue(); await main.run(); expect(install).toHaveBeenCalledWith('1.108.2', token); } ); });