From e39526e19e2d0d9cdb964c644eacee0c060361f2 Mon Sep 17 00:00:00 2001 From: Gant Date: Sat, 3 Feb 2018 17:42:13 -0600 Subject: [PATCH] :construction: customized reports on existing reports --- __tests__/__mocks__/examplePlugin.ts | 7 +++-- .../__snapshots__/solidarityReport.ts.snap | 1 + __tests__/command_helpers/printResults.ts | 2 +- __tests__/command_helpers/reviewRule.ts | 9 ++----- __tests__/command_helpers/solidarityReport.ts | 3 ++- src/commands/report.ts | 2 +- src/extensions/functions/reviewRule.ts | 27 ++++++++++--------- src/extensions/functions/solidarityReport.ts | 18 +++++++++++-- src/types.ts | 7 +++++ 9 files changed, 48 insertions(+), 28 deletions(-) diff --git a/__tests__/__mocks__/examplePlugin.ts b/__tests__/__mocks__/examplePlugin.ts index 84130a4..a736d1c 100644 --- a/__tests__/__mocks__/examplePlugin.ts +++ b/__tests__/__mocks__/examplePlugin.ts @@ -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', }) }, }, diff --git a/__tests__/command_helpers/__snapshots__/solidarityReport.ts.snap b/__tests__/command_helpers/__snapshots__/solidarityReport.ts.snap index 766af2c..c0eeed1 100644 --- a/__tests__/command_helpers/__snapshots__/solidarityReport.ts.snap +++ b/__tests__/command_helpers/__snapshots__/solidarityReport.ts.snap @@ -2,6 +2,7 @@ exports[`solidarityReport structure the basic function generates the Result object 1`] = ` Object { + "addCLI": [Function], "basicInfo": Array [ Array [ "System Basics", diff --git a/__tests__/command_helpers/printResults.ts b/__tests__/command_helpers/printResults.ts index 211ba4c..5da08d8 100644 --- a/__tests__/command_helpers/printResults.ts +++ b/__tests__/command_helpers/printResults.ts @@ -8,7 +8,7 @@ describe('reviewRule', () => { beforeEach(() => { // fresh mock context mockContext = require('mockContext') - reportResults = createReport() + reportResults = createReport(mockContext) }) test('printResults uses table', async () => { diff --git a/__tests__/command_helpers/reviewRule.ts b/__tests__/command_helpers/reviewRule.ts index 4811b88..e9d4fdc 100644 --- a/__tests__/command_helpers/reviewRule.ts +++ b/__tests__/command_helpers/reviewRule.ts @@ -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', () => { diff --git a/__tests__/command_helpers/solidarityReport.ts b/__tests__/command_helpers/solidarityReport.ts index 123f539..ed18cf6 100644 --- a/__tests__/command_helpers/solidarityReport.ts +++ b/__tests__/command_helpers/solidarityReport.ts @@ -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() }) }) diff --git a/src/commands/report.ts b/src/commands/report.ts index e41a476..87079bb 100644 --- a/src/commands/report.ts +++ b/src/commands/report.ts @@ -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( diff --git a/src/extensions/functions/reviewRule.ts b/src/extensions/functions/reviewRule.ts index 11bae39..f8ee656 100644 --- a/src/extensions/functions/reviewRule.ts +++ b/src/extensions/functions/reviewRule.ts @@ -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 @@ -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': @@ -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') diff --git a/src/extensions/functions/solidarityReport.ts b/src/extensions/functions/solidarityReport.ts index 194267a..ee637b1 100644 --- a/src/extensions/functions/solidarityReport.ts +++ b/src/extensions/functions/solidarityReport.ts @@ -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]) + }, } } diff --git a/src/types.ts b/src/types.ts index c5d7589..8eddbba 100644 --- a/src/types.ts +++ b/src/types.ts @@ -137,4 +137,11 @@ export interface SolidarityReportResults { filesystemRules: Array> shellRules: Array> customRules?: Array> + addCLI: (cliReportConfig: CLIReportConfig) => void +} + +export interface CLIReportConfig { + binary: string + version: string + desired?: string }