Skip to content

Commit 49d057b

Browse files
committed
fixed up run rule checker from custom
1 parent 33025cd commit 49d057b

File tree

4 files changed

+67
-15
lines changed

4 files changed

+67
-15
lines changed

__tests__/command_helpers/reviewRule.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,30 @@ describe('reviewRule', () => {
6060
const rule = ['SHELL', [{ rule: 'shell', command: 'ls', match: '.+' }]]
6161

6262
const result = await reviewRule(rule, reportResults, mockContext)
63-
// CLI rule was added
63+
// SHELL rule was added
6464
expect(reportResults.shellRules.length).toBe(2)
6565
})
6666
})
67+
68+
// TODO: Custom rule test
69+
// describe('when rule: custom', () => {
70+
// test('rule gets added', async () => {
71+
// const rule = ['CUSTOM', [{ rule: 'custom', plugin: 'plugin', name: 'name' }]]
72+
73+
// const result = await reviewRule(rule, reportResults, mockContext)
74+
// // CUSTOM rule was added
75+
// expect(reportResults.customRules.length).toBe(2)
76+
// })
77+
// })
78+
79+
describe('when rule: unknown', () => {
80+
test('rule gets added', async () => {
81+
const rule = ['UNKNOWN', [{ rule: 'UNKNOWN', command: 'ls', match: '.+' }]]
82+
83+
const result = await reviewRule(rule, reportResults, mockContext)
84+
// Failure in a specific rule
85+
expect(mockContext.print.error.mock.calls.length).toBe(1)
86+
})
87+
88+
})
6789
})

src/extensions/functions/checkRequirement.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = async (
1616
requirement: SolidarityRequirementChunk,
1717
context: SolidarityRunContext
1818
): Promise<void | object[]> => {
19-
const { head, tail, pipe, flatten, map } = require('ramda')
19+
const { head, tail, pipe, flatten, map, filter } = require('ramda')
2020

2121
const { print } = context
2222
const requirementName: string = head(requirement)
@@ -58,6 +58,11 @@ module.exports = async (
5858
return failureMessage
5959
}
6060

61+
const addSuccess = (successMessage: string) => {
62+
printResult(true, successMessage)
63+
return []
64+
}
65+
6166
// check each rule for requirement
6267
const ruleChecks = await map(async (rule: SolidarityRule) => {
6368
// Make sure this rule is active
@@ -71,16 +76,14 @@ module.exports = async (
7176
if (cliResult) {
7277
return addFailure(rule.error || cliResult)
7378
} else {
74-
printResult(true, ruleString)
75-
return []
79+
return addSuccess(ruleString)
7680
}
7781
// Handle ENV rule check
7882
case 'env':
7983
const envResult = await checkENV(rule, context)
8084
ruleString = `${requirementName} - ${rule.variable} env`
8185
if (envResult) {
82-
printResult(true, ruleString)
83-
return []
86+
return addSuccess(ruleString)
8487
} else {
8588
return addFailure(rule.error || `'$${rule.variable}' environment variable not found`)
8689
}
@@ -90,8 +93,7 @@ module.exports = async (
9093
const dirResult = checkDir(rule, context)
9194
ruleString = `${requirementName} - ${rule.location} directory`
9295
if (dirResult) {
93-
printResult(true, ruleString)
94-
return []
96+
return addSuccess(ruleString)
9597
} else {
9698
return addFailure(rule.error || `'${rule.location}' directory not found`)
9799
}
@@ -100,8 +102,7 @@ module.exports = async (
100102
const fileResult = checkFile(rule, context)
101103
ruleString = `${requirementName} - ${rule.location} file`
102104
if (fileResult) {
103-
printResult(true, ruleString)
104-
return []
105+
return addSuccess(ruleString)
105106
} else {
106107
return addFailure(rule.error || `'${rule.location}' file not found`)
107108
}
@@ -110,14 +111,30 @@ module.exports = async (
110111
const shellResult = await checkShell(rule, context)
111112
if (shellResult) {
112113
ruleString = `${requirementName} - '${rule.command}' matches '${rule.match}'`
113-
printResult(true, ruleString)
114-
return []
114+
return addSuccess(ruleString)
115115
} else {
116116
return addFailure(rule.error || `'${rule.command}' output did not match '${rule.match}'`)
117117
}
118+
case 'custom':
119+
// find correct rule function
120+
const correctPlugin = head(filter(plugin => plugin.name === rule.plugin, context._pluginsList))
121+
const customChecker = correctPlugin.customChecks && correctPlugin.customChecks[rule.name]
122+
if (correctPlugin && customChecker) {
123+
const customResult = await customChecker(rule, context)
124+
if (customResult && customResult.pass) {
125+
return addSuccess(customResult.message)
126+
} else {
127+
const failMessage = customResult && customResult.message
128+
? customResult.message
129+
: `${requirementName} - rule '${rule.plugin}' '${rule.name}' failed`
130+
return addFailure(rule.error || failMessage)
131+
}
118132

133+
} else {
134+
return addFailure(`Custom plugin not found with name ${rule.plugin} & ${rule.name}`)
135+
}
119136
default:
120-
return addFailure(`Encountered unknown rule`)
137+
return addFailure('Encountered unknown rule')
121138
}
122139
}, rules)
123140

src/extensions/functions/reviewRule.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ module.exports = async (
6565
const shellCheckPass = prettyBool(await checkShell(rule, context))
6666
report.shellRules.push([rule.command, rule.match, shellCheckPass])
6767
break
68+
case 'custom':
69+
// const customRulePass = true
70+
report.customRules.push(['nachos'])
71+
break
6872
default:
69-
throw 'Encountered unknown rule'
73+
throw new Error('Encountered unknown rule')
7074
}
7175
}, rules)
7276

src/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,16 @@ export interface ShellRule {
9191
readonly platform?: string | string[]
9292
}
9393

94+
export interface CustomRule {
95+
readonly rule: 'custom'
96+
readonly plugin: string
97+
readonly name: string
98+
readonly error?: string
99+
readonly platform?: string | string[]
100+
}
101+
94102
// discriminated union for rule sets
95-
export type SolidarityRule = CLIRule | ENVRule | FSRule | ShellRule
103+
export type SolidarityRule = CLIRule | ENVRule | FSRule | ShellRule | CustomRule
96104

97105
export enum SolidarityOutputMode {
98106
MODERATE,
@@ -122,4 +130,5 @@ export interface SolidarityReportResults {
122130
envRules: Array<Array<string>>
123131
filesystemRules: Array<Array<string>>
124132
shellRules: Array<Array<string>>
133+
customRules: Array<Array<string>>
125134
}

0 commit comments

Comments
 (0)