Skip to content

Commit a04ffa5

Browse files
committed
added tests for check functions
1 parent d08b5b9 commit a04ffa5

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = (context) => {
2+
// Register this plugin
3+
context.addPlugin({
4+
name: 'Example Plugin',
5+
description: 'I help test plugins',
6+
snapshot: async (context) => {
7+
context.addedSnapshot = true
8+
},
9+
customChecks: {
10+
checkThing: async (rule, context) => {
11+
return {
12+
pass: true,
13+
message: 'Yeah good check!'
14+
}
15+
},
16+
checkSecondThing: async (rule, context) => {
17+
return {
18+
pass: false,
19+
message: 'Boooo failed check'
20+
}
21+
}
22+
}
23+
})
24+
25+
return context
26+
}

__tests__/__mocks__/mockContext.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const realThing = require('gluegun')
2+
const realSolidarityContext = require('../../src/extensions/solidarity-extension')
3+
realSolidarityContext(realThing)
24

35
const noConfigSolidarity = {
46
checkRequirement: jest.fn(),
@@ -11,7 +13,7 @@ const noConfigSolidarity = {
1113

1214
const mockContext = {
1315
...realThing,
14-
outputMode: null,
16+
outputMode: undefined,
1517
system: {
1618
startTimer: jest.fn(() => jest.fn()),
1719
},
@@ -21,6 +23,8 @@ const mockContext = {
2123
info: jest.fn(),
2224
spin: jest.fn(() => ({
2325
stop: jest.fn(),
26+
fail: jest.fn(),
27+
succeed: jest.fn()
2428
})),
2529
table: jest.fn(),
2630
xmark: jest.fn(),
@@ -37,7 +41,6 @@ const mockContext = {
3741
},
3842
},
3943
printSeparator: jest.fn(),
40-
_pluginsList: [],
4144
parameters: {
4245
options: {},
4346
},

__tests__/command_helpers/checkRequirement.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,6 @@ describe('checkRequirement', () => {
222222
const result = await checkRequirement(rule, context)
223223
expect(result).toEqual([customError])
224224
})
225+
225226
})
226227
})
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import checkRequirement from '../../src/extensions/functions/checkRequirement'
2+
import { toPairs } from 'ramda'
3+
const examplePlugin = require('examplePlugin')
4+
const mockContext = examplePlugin(require('mockContext'))
5+
6+
describe('checkRequirement Plugins', () => {
7+
test('successful CUSTOM rule check', async () => {
8+
const rule = toPairs({
9+
TestRequirement: [{ rule: 'custom', plugin: 'Example Plugin', name: 'checkThing' }],
10+
})[0]
11+
const result = await checkRequirement(rule, mockContext)
12+
expect(result).toEqual([[]])
13+
})
14+
15+
test('failed CUSTOM rule check', async () => {
16+
const rule = toPairs({
17+
TestRequirement: [{ rule: 'custom', plugin: 'Example Plugin', name: 'checkSecondThing' }],
18+
})[0]
19+
const result = await checkRequirement(rule, mockContext)
20+
expect(result).toEqual(['Boooo failed check'])
21+
})
22+
23+
test('failed to find plugin', async () => {
24+
const rule = toPairs({
25+
TestRequirement: [{ rule: 'custom', plugin: 'I do not exist', name: 'checkSecondThing' }],
26+
})[0]
27+
const result = await checkRequirement(rule, mockContext)
28+
expect(result).toEqual(["Plugin not found 'I do not exist'"])
29+
})
30+
31+
test('failed to find check function', async () => {
32+
const rule = toPairs({
33+
TestRequirement: [{ rule: 'custom', plugin: 'Example Plugin', name: 'notRealName' }],
34+
})[0]
35+
const result = await checkRequirement(rule, mockContext)
36+
expect(result).toEqual(["NOT FOUND: Custom rule from 'Example Plugin' plugin with check function 'notRealName'"])
37+
})
38+
39+
test('failed CUSTOM rule with custom message', async () => {
40+
const error = 'CUSTOM ERROR'
41+
const rule = toPairs({
42+
TestRequirement: [{ rule: 'custom', plugin: 'Example Plugin', name: 'checkSecondThing', error }],
43+
})[0]
44+
const result = await checkRequirement(rule, mockContext)
45+
expect(result).toEqual([error])
46+
})
47+
48+
})

src/extensions/functions/checkRequirement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ module.exports = async (
118118
case 'custom':
119119
// find correct rule function
120120
const correctPlugin = head(filter(plugin => plugin.name === rule.plugin, context._pluginsList))
121+
if (correctPlugin === undefined) return addFailure(`Plugin not found '${rule.plugin}'`)
121122
const customChecker = correctPlugin.customChecks && correctPlugin.customChecks[rule.name]
122123
if (correctPlugin && customChecker) {
123124
const customResult = await customChecker(rule, context)
@@ -131,7 +132,6 @@ module.exports = async (
131132
return addFailure(rule.error || failMessage)
132133
}
133134
} else {
134-
135135
return addFailure(`NOT FOUND: Custom rule from '${rule.plugin}' plugin with check function '${rule.name}'`)
136136
}
137137
default:

0 commit comments

Comments
 (0)