/
policy.spec.js
152 lines (133 loc) · 4.33 KB
/
policy.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* eslint-disable ava/use-t-well */
const test = require('ava')
const fs = require('node:fs')
const path = require('node:path')
const {
createScenarioFromScaffold,
runAndTestScenario,
} = require('lavamoat-core/test/util')
const {
runScenario,
runBrowserify,
createWatchifyBundler,
prepareBrowserifyScenarioOnDisk,
} = require('./util')
test('policy - default policy path is generated with autoconfig if path is not specified', async (t) => {
const scenario = createScenarioFromScaffold({
opts: {
writeAutoPolicy: true,
},
contextName: 'browserify',
})
const { policyDir } = await prepareBrowserifyScenarioOnDisk({
scenario,
log: t.log.bind(t),
})
const expectedPath = path.join(policyDir, 'policy.json')
t.false(fs.existsSync(expectedPath), 'Policy file does not yet exist')
await runBrowserify({ scenario })
t.true(fs.existsSync(expectedPath), 'Policy file exists')
})
test('Policy is applied if not specified and already exists at default path', async (t) => {
const scenario = createScenarioFromScaffold({
defineOne: () => {
module.exports = require('three')
},
defineTwo: () => {
module.exports = 30
},
defineThree: () => {
module.exports = require('two')
},
config: {
resources: {
three: {
packages: {
two: true,
},
},
},
},
expectedResult: 30,
})
await runAndTestScenario(t, scenario, runScenario)
})
test('Policy - Applies writeAutoPolicyDebug plugin option and dumps module object to disk', async (t) => {
const scenario = createScenarioFromScaffold({
opts: {
writeAutoPolicy: true,
writeAutoPolicyDebug: true,
},
})
const { policyDir } = await prepareBrowserifyScenarioOnDisk({
scenario,
log: t.log.bind(t),
})
const expectedPath = path.join(policyDir, 'policy-debug.json')
t.false(fs.existsSync(expectedPath), 'Module data does not yet exist')
await runBrowserify({ scenario })
t.true(fs.existsSync(expectedPath), 'Module data does not yet exist')
})
test('Policy - watchify listens for policy file changes', async (t) => {
const scenario = createScenarioFromScaffold({
opts: {
writeAutoPolicy: true,
},
})
const { projectDir, policyDir } = await prepareBrowserifyScenarioOnDisk({
scenario,
log: t.log.bind(t),
})
await runBrowserify({ scenario })
const watchedFiles = []
const bundler = createWatchifyBundler({
projectRoot: projectDir,
policyName: 'browserify',
})
bundler.on('file', (file) => {
watchedFiles.push(file)
})
// need to wait a tick to get file events
await new Promise((resolve) => setTimeout(resolve))
const relativeFiles = watchedFiles
.map((file) => path.relative(policyDir, file))
.sort()
t.deepEqual(relativeFiles, ['policy-override.json', 'policy.json'])
})
// this test is not written correctly, im disabling it for now
// it tests features we dont support:
// - automatic watchify writeAutoPolicy rebuilds when policy override is modified
// - hey, we support this now
// - it uses invalid policy values
// - it expects policy-override to be merged into the policy and then written to disk
// test('Policy edits trigger re-bundle if using watchify', async (t) => {
// const configDefault = {
// resources: {
// '$root$': {
// packages: {
// 'two': true
// }
// },
// 'two': {
// packages: {
// 'three': 12345678
// }
// }
// }
// }
// const bundler = await createWatchifyBundle({
// writeAutoPolicy: true,
// })
// const overridePath = './lavamoat/policy-override.json'
// const configPath = './lavamoat/policy.json'
// await new Promise(resolve => setTimeout(resolve, 1000))
// const configFileString = fs.readFileSync(configPath, 'utf8')
// const overrideString = JSON.stringify(configDefault)
// fs.writeFileSync(overridePath, overrideString)
// await new Promise(resolve => bundler.once('update', () => resolve()))
// await createBundleFromRequiresArray([], {})
// const updatedPolicyFileString = fs.readFileSync(configPath, 'utf8')
// rimraf.sync('./lavamoat')
// t.false(configFileString.includes('"three": 12345678'), 'original policy should not have updated content')
// t.true(updatedPolicyFileString.includes('"three": 12345678'), 'policy should be updated')
// })