Skip to content

Commit f78944d

Browse files
committed
test(variables): add failing tests for noop mode support
These tests document the bug where the variables plugin does not respect the nop flag. When nop=true, the plugin should: - Return NopCommand objects instead of making API calls - NOT make actual GitHub API calls Currently all noop tests fail, demonstrating the bug exists. Signed-off-by: Kyle Harding <kyle@balena.io>
1 parent 447021f commit f78944d

1 file changed

Lines changed: 135 additions & 4 deletions

File tree

test/unit/lib/plugins/variables.test.js

Lines changed: 135 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { when } = require('jest-when')
22
const Variables = require('../../../../lib/plugins/variables')
3+
const NopCommand = require('../../../../lib/nopcommand')
34

45
describe('Variables', () => {
56
let github
@@ -10,13 +11,13 @@ describe('Variables', () => {
1011
return variables
1112
}
1213

13-
function configure () {
14-
const log = { debug: console.debug, error: console.error }
14+
function configure (nop = false) {
15+
const log = { debug: jest.fn(), error: console.error }
1516
const errors = []
16-
return new Variables(undefined, github, { owner: org, repo }, [{ name: 'test', value: 'test' }], log, errors)
17+
return new Variables(nop, github, { owner: org, repo }, [{ name: 'test', value: 'test' }], log, errors)
1718
}
1819

19-
beforeAll(() => {
20+
beforeEach(() => {
2021
github = {
2122
request: jest.fn().mockReturnValue(Promise.resolve(true))
2223
}
@@ -76,4 +77,134 @@ describe('Variables', () => {
7677
)
7778
})
7879
})
80+
81+
describe('noop mode', () => {
82+
describe('add', () => {
83+
it('should return NopCommand and not make API call when nop is true', async () => {
84+
const plugin = configure(true)
85+
const variable = { name: 'NEW_VAR', value: 'new-value' }
86+
87+
const result = await plugin.add(variable)
88+
89+
expect(result).toBeInstanceOf(NopCommand)
90+
expect(result.plugin).toBe('Variables')
91+
expect(github.request).not.toHaveBeenCalledWith(
92+
'POST /repos/:org/:repo/actions/variables',
93+
expect.anything()
94+
)
95+
})
96+
97+
it('should make API call when nop is false', async () => {
98+
const plugin = configure(false)
99+
const variable = { name: 'NEW_VAR', value: 'new-value' }
100+
101+
await plugin.add(variable)
102+
103+
expect(github.request).toHaveBeenCalledWith(
104+
'POST /repos/:org/:repo/actions/variables',
105+
expect.objectContaining({
106+
org,
107+
repo,
108+
name: 'NEW_VAR',
109+
value: 'new-value'
110+
})
111+
)
112+
})
113+
})
114+
115+
describe('remove', () => {
116+
it('should return NopCommand and not make API call when nop is true', async () => {
117+
const plugin = configure(true)
118+
const existing = { name: 'EXISTING_VAR', value: 'existing-value' }
119+
120+
const result = await plugin.remove(existing)
121+
122+
expect(result).toBeInstanceOf(NopCommand)
123+
expect(result.plugin).toBe('Variables')
124+
expect(github.request).not.toHaveBeenCalledWith(
125+
'DELETE /repos/:org/:repo/actions/variables/:variable_name',
126+
expect.anything()
127+
)
128+
})
129+
130+
it('should make API call when nop is false', async () => {
131+
const plugin = configure(false)
132+
const existing = { name: 'EXISTING_VAR', value: 'existing-value' }
133+
134+
await plugin.remove(existing)
135+
136+
expect(github.request).toHaveBeenCalledWith(
137+
'DELETE /repos/:org/:repo/actions/variables/:variable_name',
138+
expect.objectContaining({
139+
org,
140+
repo,
141+
variable_name: 'EXISTING_VAR'
142+
})
143+
)
144+
})
145+
})
146+
147+
describe('update', () => {
148+
it('should return NopCommand and not make API calls when nop is true', async () => {
149+
const plugin = configure(true)
150+
const existing = [{ name: 'VAR1', value: 'old-value' }]
151+
const updated = [{ name: 'VAR1', value: 'new-value' }]
152+
153+
const result = await plugin.update(existing, updated)
154+
155+
expect(result).toBeInstanceOf(NopCommand)
156+
expect(result.plugin).toBe('Variables')
157+
expect(github.request).not.toHaveBeenCalledWith(
158+
'PATCH /repos/:org/:repo/actions/variables/:variable_name',
159+
expect.anything()
160+
)
161+
})
162+
163+
it('should return NopCommand when adding new variable in update with nop true', async () => {
164+
const plugin = configure(true)
165+
const existing = []
166+
const updated = [{ name: 'NEW_VAR', value: 'new-value' }]
167+
168+
const result = await plugin.update(existing, updated)
169+
170+
expect(result).toBeInstanceOf(NopCommand)
171+
expect(github.request).not.toHaveBeenCalledWith(
172+
'POST /repos/:org/:repo/actions/variables',
173+
expect.anything()
174+
)
175+
})
176+
177+
it('should return NopCommand when deleting variable in update with nop true', async () => {
178+
const plugin = configure(true)
179+
const existing = [{ name: 'OLD_VAR', value: 'old-value' }]
180+
const updated = []
181+
182+
const result = await plugin.update(existing, updated)
183+
184+
expect(result).toBeInstanceOf(NopCommand)
185+
expect(github.request).not.toHaveBeenCalledWith(
186+
'DELETE /repos/:org/:repo/actions/variables/:variable_name',
187+
expect.anything()
188+
)
189+
})
190+
191+
it('should make API calls when nop is false', async () => {
192+
const plugin = configure(false)
193+
const existing = [{ name: 'VAR1', value: 'old-value' }]
194+
const updated = [{ name: 'VAR1', value: 'new-value' }]
195+
196+
await plugin.update(existing, updated)
197+
198+
expect(github.request).toHaveBeenCalledWith(
199+
'PATCH /repos/:org/:repo/actions/variables/:variable_name',
200+
expect.objectContaining({
201+
org,
202+
repo,
203+
variable_name: 'VAR1',
204+
value: 'new-value'
205+
})
206+
)
207+
})
208+
})
209+
})
79210
})

0 commit comments

Comments
 (0)