-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathupdate.test.js
More file actions
180 lines (148 loc) · 5.22 KB
/
update.test.js
File metadata and controls
180 lines (148 loc) · 5.22 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const fs = require('fs')
const storage = require('ethernaut-common/src/io/storage')
const { Terminal, keys } = require('ethernaut-common/src/test/terminal')
const assert = require('assert')
describe('update', function () {
let terminal = new Terminal()
let cachedAutoUpdate
let cachedPkg
before('allow updates in tests', async function () {
process.env.ALLOW_UPDATE = 'true'
})
after('lock updates in tests', async function () {
process.env.ALLOW_UPDATE = 'false'
})
async function triggerUpdate() {
// Twice because this is how update notifications work
await terminal.run('hardhat', 2000)
await terminal.run('hardhat', 3000)
}
before('cache', async function () {
const config = storage.readConfig()
cachedAutoUpdate = config.general?.autoUpdate
cachedPkg = fs.readFileSync('package.json', 'utf8')
})
after('restore', async function () {
const config = storage.readConfig()
config.general.autoUpdate = cachedAutoUpdate
storage.saveConfig(config)
fs.writeFileSync('package.json', cachedPkg, 'utf8')
})
describe('when pkg version is old', function () {
let newConfig
before('modify and cache pkg version', async function () {
newConfig = JSON.parse(cachedPkg)
newConfig.version = '0.0.0'
fs.writeFileSync(
'package.json',
JSON.stringify(newConfig, null, 2),
'utf8',
)
})
describe('when auto update is never', function () {
before('modify', async function () {
const config = storage.readConfig()
config.general.autoUpdate = 'never'
storage.saveConfig(config)
})
before('start cli', async function () {
await triggerUpdate()
})
it('displays navigation', async function () {
terminal.has('Pick a task or scope')
})
})
describe('when auto update is the current version', function () {
before('modify', async function () {
const pkg = JSON.parse(cachedPkg)
pkg.version = '0.0.0'
const config = storage.readConfig()
config.general.autoUpdate = pkg.version
storage.saveConfig(config)
})
before('start cli', async function () {
await triggerUpdate()
})
it('displays navigation', async function () {
terminal.has('A new version of the ethernaut-cli is available')
})
})
describe('when auto update is undefined', function () {
const itDisplaysTheUpdatePrompt = () => {
before('modify', async function () {
const config = storage.readConfig()
config.general.autoUpdate = undefined
storage.saveConfig(config)
})
before('start cli', async function () {
await triggerUpdate()
})
it('displays the update notice', async function () {
terminal.has('is available, update with')
})
it('displays the update prompt', async function () {
terminal.has('A new version of the ethernaut-cli is available')
})
}
describe('when not installing', function () {
itDisplaysTheUpdatePrompt()
describe('when no thanks is selected', function () {
before('interact', async function () {
await terminal.input(keys.DOWN)
await terminal.input(keys.ENTER, 1000)
})
it('displays navigation', async function () {
terminal.has('Pick a task or scope')
})
})
})
describe('when installing', function () {
itDisplaysTheUpdatePrompt()
describe('when install is selected', function () {
before('interact', async function () {
await terminal.input(keys.ENTER, 2000)
})
it('displays installation message', async function () {
terminal.has('Installing update...')
})
})
})
describe('when skipping', function () {
itDisplaysTheUpdatePrompt()
describe('when skip is selected', function () {
before('interact', async function () {
await terminal.input(keys.DOWN)
await terminal.input(keys.DOWN)
await terminal.input(keys.ENTER, 1000)
})
it('displays navigation', async function () {
terminal.has('Pick a task or scope')
})
it('wrote in config', async function () {
const pkg = JSON.parse(cachedPkg)
const config = storage.readConfig()
assert.equal(config.general.autoUpdate, pkg.version)
})
})
})
describe('when skipping indefinitely', function () {
itDisplaysTheUpdatePrompt()
describe('when never is selected', function () {
before('interact', async function () {
await terminal.input(keys.DOWN)
await terminal.input(keys.DOWN)
await terminal.input(keys.DOWN)
await terminal.input(keys.ENTER, 1000)
})
it('displays navigation', async function () {
terminal.has('Pick a task or scope')
})
it('wrote in config', async function () {
const config = storage.readConfig()
assert.equal(config.general.autoUpdate, 'never')
})
})
})
})
})
})