2022-06-14 13:00:18 +02:00
|
|
|
import {expect, test, beforeEach} from '@jest/globals'
|
2022-06-13 19:55:08 +02:00
|
|
|
import {readConfig} from '../src/config'
|
2022-05-26 15:54:59 -07:00
|
|
|
|
2022-06-14 13:00:18 +02:00
|
|
|
// 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) {
|
|
|
|
|
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() {
|
2022-06-14 07:04:26 +02:00
|
|
|
delete process.env['INPUT_FAIL-ON-SEVERITY']
|
2022-06-14 09:46:59 +02:00
|
|
|
delete process.env['INPUT_ALLOW-LICENSES']
|
2022-06-14 06:49:18 +02:00
|
|
|
delete process.env['INPUT_DENY-LICENSES']
|
2022-06-14 13:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
clearInputs()
|
2022-06-13 19:55:08 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('it defaults to low severity', async () => {
|
2022-06-14 07:42:13 +02:00
|
|
|
const options = readConfig()
|
2022-06-13 19:55:08 +02:00
|
|
|
expect(options.fail_on_severity).toEqual('low')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('it reads custom configs', async () => {
|
2022-06-14 13:00:18 +02:00
|
|
|
setInput('fail-on-severity', 'critical')
|
|
|
|
|
setInput('allow-licenses', ' BSD, GPL 2')
|
2022-06-13 19:55:08 +02:00
|
|
|
|
2022-06-14 07:42:13 +02:00
|
|
|
const options = readConfig()
|
2022-06-01 12:09:11 +02:00
|
|
|
expect(options.fail_on_severity).toEqual('critical')
|
|
|
|
|
expect(options.allow_licenses).toEqual(['BSD', 'GPL 2'])
|
2022-05-26 15:54:59 -07:00
|
|
|
})
|
|
|
|
|
|
2022-06-13 19:55:08 +02:00
|
|
|
test('it defaults to empty allow/deny lists ', async () => {
|
2022-06-14 07:42:13 +02:00
|
|
|
const options = readConfig()
|
2022-05-26 15:54:59 -07:00
|
|
|
|
2022-06-06 17:07:26 +02:00
|
|
|
expect(options.allow_licenses).toEqual(undefined)
|
|
|
|
|
expect(options.deny_licenses).toEqual(undefined)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('it raises an error if both an allow and denylist are specified', async () => {
|
2022-06-14 13:00:18 +02:00
|
|
|
setInput('allow-licenses', 'MIT')
|
|
|
|
|
setInput('deny-licenses', 'BSD')
|
|
|
|
|
|
2022-06-13 19:55:08 +02:00
|
|
|
expect(() => readConfig()).toThrow()
|
2022-06-06 17:07:26 +02:00
|
|
|
})
|
2022-06-13 19:55:08 +02:00
|
|
|
|
2022-06-14 07:42:51 +02:00
|
|
|
test('it raises an error when given an unknown severity', async () => {
|
2022-06-14 13:00:18 +02:00
|
|
|
setInput('fail-on-severity', 'zombies')
|
2022-06-14 07:42:51 +02:00
|
|
|
expect(() => readConfig()).toThrow()
|
|
|
|
|
})
|