Skip to content

Commit

Permalink
🚧 customized reports on existing reports
Browse files Browse the repository at this point in the history
  • Loading branch information
GantMan committed Feb 4, 2018
1 parent b315d2c commit e39526e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 28 deletions.
7 changes: 3 additions & 4 deletions __tests__/__mocks__/examplePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ module.exports = context => {
},
],
report: async (rule, context, report) => {
// report.cliRules.push(['android', location, binaryVersion, desired])
report.addCLI({
binary: 'Android SDK',
version: 10,
desired: 12,
binary: 'node',
version: '10',
desired: '12',
})
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`solidarityReport structure the basic function generates the Result object 1`] = `
Object {
"addCLI": [Function],
"basicInfo": Array [
Array [
"System Basics",
Expand Down
2 changes: 1 addition & 1 deletion __tests__/command_helpers/printResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('reviewRule', () => {
beforeEach(() => {
// fresh mock context
mockContext = require('mockContext')
reportResults = createReport()
reportResults = createReport(mockContext)
})

test('printResults uses table', async () => {
Expand Down
9 changes: 2 additions & 7 deletions __tests__/command_helpers/reviewRule.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import reviewRule from '../../src/extensions/functions/reviewRule'
import { SolidarityRunContext, SolidarityReportResults } from '../../src/types'
import { createReport } from '../../src/extensions/functions/solidarityReport'
let mockContext: SolidarityRunContext
let reportResults: SolidarityReportResults
describe('reviewRule', () => {
beforeEach(() => {
// fresh mock context
mockContext = require('mockContext')
reportResults = {
basicInfo: [['System Basics', 'Value'], ['OS', 'tacoOS'], ['CPU', 'hamsters in a wheel']],
cliRules: [['Binary', 'Location', 'Version', 'Desired']],
envRules: [['Environment Var', 'Value']],
filesystemRules: [['Location', 'Type', 'Exists']],
shellRules: [['Command', 'Pattern', 'Matches']],
}
reportResults = createReport(mockContext)
})

describe('when rule: cli', () => {
Expand Down
3 changes: 2 additions & 1 deletion __tests__/command_helpers/solidarityReport.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { SolidarityReportResults } from '../../src/types'
import { createReport } from '../../src/extensions/functions/solidarityReport'
const context = require('mockContext')

let reportResults: SolidarityReportResults
describe('solidarityReport structure', () => {
test('the basic function generates the Result object', () => {
let report = createReport()
let report = createReport(context)
expect(report).toMatchSnapshot()
})
})
2 changes: 1 addition & 1 deletion src/commands/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
process.exit(3)
}

let results: SolidarityReportResults = createReport()
let results: SolidarityReportResults = createReport(context)

// break all rules into requirements
const reportCalls = map(
Expand Down
27 changes: 15 additions & 12 deletions src/extensions/functions/reviewRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ const skipRule = require('./skipRule')
const checkDir = require('./checkDir')
const checkFile = require('./checkFile')
const checkShell = require('./checkShell')
const findPluginInfo = require('./findPluginInfo')

module.exports = async (
requirement: SolidarityRequirementChunk,
report: SolidarityReportResults,
context: SolidarityRunContext
) => {
const { print, system, solidarity } = context
const { print, solidarity } = context
const { colors, checkmark, xmark } = print
const prettyBool = (bl: boolean) => (bl ? checkmark + colors.green(' YES') : xmark + colors.red(' NO'))
// @ts-ignore - flatten will never get a string bc tail is called first
Expand All @@ -29,21 +30,18 @@ module.exports = async (
switch (rule.rule) {
// Handle CLI rule report
case 'cli':
const desired = rule.semver ? rule.semver : colors.green('*ANY*')
let location
try {
location = system.which(rule.binary)
} catch (_e) {
location = colors.red('*MISSING*')
}

let binaryVersion
try {
binaryVersion = await solidarity.getVersion(rule, context)
} catch (_e) {
binaryVersion = colors.red('*UNKNOWN*')
}
report.cliRules.push([rule.binary, location, binaryVersion, desired])

report.addCLI({
binary: rule.binary,
version: binaryVersion,
desired: rule.semver,
})
break
// Handle ENV rule report
case 'env':
Expand All @@ -66,8 +64,13 @@ module.exports = async (
report.shellRules.push([rule.command, rule.match, shellCheckPass])
break
case 'custom':
// const customRulePass = true
// report.customRules.push(['nachos'])
const customPluginRule = findPluginInfo(rule, context)
if (customPluginRule.success) {
// let plugin update the report
customPluginRule.plugin.report(rule, context, report)
} else {
throw new Error(customPluginRule.message)
}
break
default:
throw new Error('Encountered unknown rule')
Expand Down
18 changes: 16 additions & 2 deletions src/extensions/functions/solidarityReport.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { SolidarityReportResults } from '../../types'
import { SolidarityRunContext, SolidarityReportResults, CLIReportConfig } from '../../types'
import { helpers } from 'envinfo'

export const createReport = (): SolidarityReportResults => {
export const createReport = (context: SolidarityRunContext): SolidarityReportResults => {
const { print, system } = context
const { colors } = print

return {
basicInfo: [['System Basics', 'Value'], ['OS', helpers.getOperatingSystemInfo()], ['CPU', helpers.getCPUInfo()]],
cliRules: [['Binary', 'Location', 'Version', 'Desired']],
envRules: [['Environment Var', 'Value']],
filesystemRules: [['Location', 'Type', 'Exists']],
shellRules: [['Command', 'Pattern', 'Matches']],
// helper for adding CLI rules
addCLI: function (cliReportConfig: CLIReportConfig) {
const desired = cliReportConfig.desired ? cliReportConfig.desired : colors.green('*ANY*')
let location
try {
location = system.which(cliReportConfig.binary)
} catch (_e) {
location = colors.red('*MISSING*')
}
this.cliRules.push([cliReportConfig.binary, location, cliReportConfig.version, desired])
},
}
}
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,11 @@ export interface SolidarityReportResults {
filesystemRules: Array<Array<string>>
shellRules: Array<Array<string>>
customRules?: Array<Array<string>>
addCLI: (cliReportConfig: CLIReportConfig) => void
}

export interface CLIReportConfig {
binary: string
version: string
desired?: string
}

0 comments on commit e39526e

Please sign in to comment.