Skip to content

bin: add a flag to list the subsystems #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ const formatTap = require('../lib/format-tap')
const Validator = require('../lib')
const Tap = require('../lib/tap')
const utils = require('../lib/utils')
const subsystem = require('../lib/rules/subsystem')
const knownOpts = { help: Boolean
, version: Boolean
, 'validate-metadata': Boolean
, tap: Boolean
, out: path
, list: Boolean
, 'list-subsystems': Boolean
}
const shortHand = { h: ['--help']
, v: ['--version']
, V: ['--validate-metadata']
, t: ['--tap']
, o: ['--out']
, l: ['--list']
, ls: ['--list-subsystems']
}

const parsed = nopt(knownOpts, shortHand)
Expand Down Expand Up @@ -84,6 +87,11 @@ function loadPatch(uri, cb) {

const v = new Validator(parsed)

if (parsed['list-subsystems']) {
utils.describeSubsystem(subsystem.defaults.subsystems.sort())
return
}

if (parsed.list) {
const ruleNames = Array.from(v.rules.keys())
const max = ruleNames.reduce((m, item) => {
Expand Down
1 change: 1 addition & 0 deletions bin/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ core-validate-commit - Validate the commit message for a particular commit in no
-V, --validate-metadata validate PR-URL and reviewers (on by default)
-t, --tap output in tap format
-l, --list list rules and their descriptions
-ls --list-subsystems list the available subsystems

examples:
Validate a single sha:
Expand Down
12 changes: 12 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ exports.describeRule = function describeRule(rule, max = 20) {
console.log(' %s %s', chalk.red(title), chalk.dim(desc))
}
}

exports.describeSubsystem = function describeSubsystem(subsystems, max = 20) {
if (subsystems) {
for (let sub = 0; sub < subsystems.length; sub = sub + 3) {
console.log('%s %s %s',
chalk.green(exports.leftPad(subsystems[sub] || '', max)),
chalk.green(exports.leftPad(subsystems[sub + 1] || '', max)),
chalk.green(exports.leftPad(subsystems[sub + 2] || '', max))
)
}
}
}
44 changes: 44 additions & 0 deletions test/cli-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const { test } = require('tap')
const { spawn } = require('child_process')
const subsystems = require('../lib/rules/subsystem')

test('Test cli flags', (t) => {
t.test('test list-subsystems', (tt) => {
const ls = spawn('./bin/cmd.js', ['--list-subsystems'])
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})

ls.stderr.on('data', (data) => {
tt.fail('This should not happen')
})

ls.on('close', (code) => {
// Get the list of subsytems as an Array.
// Need to match words that also have the "-" in them
const subsystemsFromOutput = compiledData.match(/[\w'-]+/g)
const defaultSubsystems = subsystems.defaults.subsystems

tt.equal(subsystemsFromOutput.length,
defaultSubsystems.length,
'Should have the same length')

// Loop through the output list and compare with the real list
// to make sure they are all there
const missing = []
subsystemsFromOutput.forEach((sub) => {
if (!defaultSubsystems.find((x) => {return x === sub})) {
missing.push(sub)
}
})

tt.equal(missing.length, 0, 'Should have no missing subsystems')
tt.end()
})
})

t.end()
})