-
Notifications
You must be signed in to change notification settings - Fork 65
/
scenarios.spec.js
81 lines (72 loc) · 2.08 KB
/
scenarios.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* eslint-disable ava/no-skip-test */
import test from 'ava'
import { loadScenarios } from 'lavamoat-core/test/scenarios/index.js'
// @ts-expect-error - needs types
import { runAndTestScenario } from 'lavamoat-core/test/util.js'
import { createScenarioRunner } from './scenario-util.js'
const GLOBAL_WRITE_REGEX = /".+?":"write"/g
/**
* If the policy file contains a writable global we have to skip it until we've
* implemented it
*
* @param {any} scenario
* @returns
* @todo Implement & remove
*/
function policyHasWritableGlobal(scenario) {
return (
GLOBAL_WRITE_REGEX.test(JSON.stringify(scenario.config)) ||
GLOBAL_WRITE_REGEX.test(JSON.stringify(scenario.configOverride))
)
}
const FAILING_SCENARIOS = new Set([
'globalRef - check default containment',
'globalRef - Webpack code in the wild works',
])
/**
* Macro to test a scenario
*/
const testScenario = test.macro(
/**
* @param {import('ava').ExecutionContext} t
* @param {any} scenario
*/
async (t, scenario) => {
await runAndTestScenario(t, scenario, createScenarioRunner(t.log.bind(t)))
}
)
// TIL: you can dynamically create tests in an async iterator
// as long as you use `test.serial()`
for await (const scenario of loadScenarios()) {
if (
!(
scenario.context &&
Object.keys(scenario.context).length === 0 &&
scenario.context.constructor === Object
)
) {
// these will never work. I don't know what they are, though.
test.skip(
`${scenario.name} - incompatible platform`,
testScenario,
scenario
)
continue
}
// TODO fix
if (scenario.name && FAILING_SCENARIOS.has(scenario.name)) {
test.failing(`${scenario.name} - `, testScenario, scenario)
continue
}
// TODO implement writable globals
if (policyHasWritableGlobal(scenario)) {
test.todo(`${scenario.name} - has writable global`)
continue
}
// TODO implement scuttling
if (scenario.name?.startsWith('scuttle')) {
test.todo(`${scenario.name} - scuttling unsupported`)
continue
}
test.serial(scenario.name ?? '(unknown scenario)', testScenario, scenario)
}