From 2c065db2961a5d0870cae42b7865f83f795f149c Mon Sep 17 00:00:00 2001 From: Federico Builes Date: Thu, 6 Apr 2023 17:32:42 +0200 Subject: [PATCH] Add a test-helpers file. --- __tests__/config.test.ts | 30 +----------------------------- __tests__/external-config.test.ts | 30 +----------------------------- __tests__/test-helpers.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 58 deletions(-) create mode 100644 __tests__/test-helpers.ts diff --git a/__tests__/config.test.ts b/__tests__/config.test.ts index 0d2ed14..47e4ef9 100644 --- a/__tests__/config.test.ts +++ b/__tests__/config.test.ts @@ -2,35 +2,7 @@ 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 -// 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()}`] - }) -} +import {setInput, clearInputs} from './test-helpers' beforeAll(() => { jest.spyOn(Utils, 'isSPDXValid').mockReturnValue(true) diff --git a/__tests__/external-config.test.ts b/__tests__/external-config.test.ts index 9da90b3..a1a0e7c 100644 --- a/__tests__/external-config.test.ts +++ b/__tests__/external-config.test.ts @@ -1,35 +1,7 @@ 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()}`] - }) -} +import {setInput, clearInputs} from './test-helpers' const externalConfig = `fail_on_severity: 'high' allow_licenses: ['GPL-2.0-only'] diff --git a/__tests__/test-helpers.ts b/__tests__/test-helpers.ts new file mode 100644 index 0000000..e5b18e7 --- /dev/null +++ b/__tests__/test-helpers.ts @@ -0,0 +1,28 @@ +// GitHub Action inputs come in the form of environment variables +// with an INPUT prefix (e.g. INPUT_FAIL-ON-SEVERITY) +export 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. +export 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()}`] + }) +}