From 97c946575176eac95282bf84a0e0de2a9ebb223a Mon Sep 17 00:00:00 2001 From: Federico Builes Date: Wed, 5 Apr 2023 15:14:57 +0200 Subject: [PATCH] separate tests for external configs --- __tests__/config.test.ts | 70 +------------------- __tests__/external-config.test.ts | 104 ++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 67 deletions(-) create mode 100644 __tests__/external-config.test.ts diff --git a/__tests__/config.test.ts b/__tests__/config.test.ts index f41165d..6c892a8 100644 --- a/__tests__/config.test.ts +++ b/__tests__/config.test.ts @@ -1,6 +1,6 @@ -import {expect, test, beforeEach} from '@jest/globals' -import {readConfig} from '../src/config' -import {getRefs} from '../src/git-refs' +import { expect, test, beforeEach } from '@jest/globals' +import { readConfig } from '../src/config' +import { getRefs } from '../src/git-refs' import * as Utils from '../src/utils' // GitHub Action inputs come in the form of environment variables @@ -105,60 +105,6 @@ test('it raises an error when no refs are provided and the event is not a pull r ).toThrow() }) -test('it reads an external config file', async () => { - setInput('config-file', './__tests__/fixtures/config-allow-sample.yml') - - const config = await readConfig() - expect(config.fail_on_severity).toEqual('critical') - expect(config.allow_licenses).toEqual(['BSD', 'GPL 2']) -}) - -test('raises an error when the config file was not found', async () => { - setInput('config-file', 'fixtures/i-dont-exist') - await expect(readConfig()).rejects.toThrow(/Unable to fetch/) -}) - -test('it parses options from both sources', async () => { - setInput('config-file', './__tests__/fixtures/config-allow-sample.yml') - - let config = await readConfig() - expect(config.fail_on_severity).toEqual('critical') - - setInput('base-ref', 'a-custom-base-ref') - config = await readConfig() - expect(config.base_ref).toEqual('a-custom-base-ref') -}) - -test('in case of conflicts, the inline config is the source of truth', async () => { - setInput('fail-on-severity', 'low') - setInput('config-file', './__tests__/fixtures/config-allow-sample.yml') // this will set fail-on-severity to 'critical' - - const config = await readConfig() - expect(config.fail_on_severity).toEqual('low') -}) - -test('it uses the default values when loading external files', async () => { - setInput('config-file', './__tests__/fixtures/no-licenses-config.yml') - let config = await readConfig() - expect(config.allow_licenses).toEqual(undefined) - expect(config.deny_licenses).toEqual(undefined) - - setInput('config-file', './__tests__/fixtures/license-config-sample.yml') - config = await readConfig() - expect(config.fail_on_severity).toEqual('low') -}) - -test('it accepts an external configuration filename', async () => { - setInput('config-file', './__tests__/fixtures/no-licenses-config.yml') - const config = await readConfig() - expect(config.fail_on_severity).toEqual('critical') -}) - -test('it raises an error when given an unknown severity in an external config file', async () => { - setInput('config-file', './__tests__/fixtures/invalid-severity-config.yml') - await expect(readConfig()).rejects.toThrow() -}) - test('it defaults to runtime scope', async () => { const config = await readConfig() expect(config.fail_on_scopes).toEqual(['runtime']) @@ -234,16 +180,6 @@ test('it is not possible to disable both checks', async () => { ) }) -test('it supports comma-separated lists', async () => { - setInput( - 'config-file', - './__tests__/fixtures/inline-license-config-sample.yml' - ) - const config = await readConfig() - - expect(config.allow_licenses).toEqual(['MIT', 'GPL-2.0-only']) -}) - describe('licenses that are not valid SPDX licenses', () => { beforeAll(() => { jest.spyOn(Utils, 'isSPDXValid').mockReturnValue(false) diff --git a/__tests__/external-config.test.ts b/__tests__/external-config.test.ts new file mode 100644 index 0000000..02e7f47 --- /dev/null +++ b/__tests__/external-config.test.ts @@ -0,0 +1,104 @@ +import { expect, test, beforeEach } from '@jest/globals' +import { readConfig } from '../src/config' +import * as Utils from '../src/utils' + +// GitHub Action inputs come in the form of environment variables +// with an INPUT prefix (e.g. INPUT_FAIL-ON-SEVERITY) +function setInput(input: string, value: string): void { + process.env[`INPUT_${input.toUpperCase()}`] = value +} + +// We want a clean ENV before each test. We use `delete` +// since we want `undefined` values and not empty strings. +function clearInputs(): void { + const allowedOptions = [ + 'FAIL-ON-SEVERITY', + 'FAIL-ON-SCOPES', + 'ALLOW-LICENSES', + 'DENY-LICENSES', + 'ALLOW-GHSAS', + 'LICENSE-CHECK', + 'VULNERABILITY-CHECK', + 'CONFIG-FILE', + 'BASE-REF', + 'HEAD-REF', + 'COMMENT-SUMMARY-IN-PR' + ] + + // eslint-disable-next-line github/array-foreach + allowedOptions.forEach(option => { + delete process.env[`INPUT_${option.toUpperCase()}`] + }) +} + +beforeAll(() => { + jest.spyOn(Utils, 'isSPDXValid').mockReturnValue(true) +}) + +beforeEach(() => { + clearInputs() +}) + +test('it reads an external config file', async () => { + setInput('config-file', './__tests__/fixtures/config-allow-sample.yml') + + const config = await readConfig() + expect(config.fail_on_severity).toEqual('critical') + expect(config.allow_licenses).toEqual(['BSD', 'GPL 2']) +}) + +test('raises an error when the config file was not found', async () => { + setInput('config-file', 'fixtures/i-dont-exist') + await expect(readConfig()).rejects.toThrow(/Unable to fetch/) +}) + +test('it parses options from both sources', async () => { + setInput('config-file', './__tests__/fixtures/config-allow-sample.yml') + + let config = await readConfig() + expect(config.fail_on_severity).toEqual('critical') + + setInput('base-ref', 'a-custom-base-ref') + config = await readConfig() + expect(config.base_ref).toEqual('a-custom-base-ref') +}) + +test('in case of conflicts, the inline config is the source of truth', async () => { + setInput('fail-on-severity', 'low') + setInput('config-file', './__tests__/fixtures/config-allow-sample.yml') // this will set fail-on-severity to 'critical' + + const config = await readConfig() + expect(config.fail_on_severity).toEqual('low') +}) + +test('it uses the default values when loading external files', async () => { + setInput('config-file', './__tests__/fixtures/no-licenses-config.yml') + let config = await readConfig() + expect(config.allow_licenses).toEqual(undefined) + expect(config.deny_licenses).toEqual(undefined) + + setInput('config-file', './__tests__/fixtures/license-config-sample.yml') + config = await readConfig() + expect(config.fail_on_severity).toEqual('low') +}) + +test('it accepts an external configuration filename', async () => { + setInput('config-file', './__tests__/fixtures/no-licenses-config.yml') + const config = await readConfig() + expect(config.fail_on_severity).toEqual('critical') +}) + +test('it raises an error when given an unknown severity in an external config file', async () => { + setInput('config-file', './__tests__/fixtures/invalid-severity-config.yml') + await expect(readConfig()).rejects.toThrow() +}) + +test('it supports comma-separated lists', async () => { + setInput( + 'config-file', + './__tests__/fixtures/inline-license-config-sample.yml' + ) + const config = await readConfig() + + expect(config.allow_licenses).toEqual(['MIT', 'GPL-2.0-only']) +})