Skip to content
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

WIP - test for cli args #70

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
87 changes: 83 additions & 4 deletions test/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,92 @@
const { test } = require('tap')
const { spawn } = require('child_process')
const subsystems = require('../lib/rules/subsystem')
const rules = require('../lib/rules')
const { promisify } = require('util')
const fs = require('fs')


const readFile = promisify(fs.readFile)


const cmd = './bin/cmd.js'

test('Test cli flags', (t) => {
t.test('test usage', (tt) => {
const usage = spawn(cmd, ['--help'])
let chunk = ''
usage.stdout.on('data', (data) => {
chunk += data
})

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

usage.on('close', async () => {
const usageTxt = await readFile(`${process.cwd()}/bin/usage.txt`, 'utf8')
tt.equal(chunk, usageTxt, 'helps should be equal')
tt.end()
})
})

t.test('test version', (tt) => {
const version = spawn(cmd, ['--version'])
let chunk = ''

version.stdout.on('data', (data) => {
chunk += data
})

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

version.on('close', (code) => {
tt.equal(chunk.trim(),
`core-validate-commit v${require('../package.json').version}`,
'Versions should be equal to the version in the package.json')
tt.end()
})

})

t.test('test list flag', (tt) => {
const list = spawn(cmd, ['--list'])
let chunk = ''
list.stdout.on('data', (data) => {
chunk += data
})

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

list.on('close', (code) => {
const rulesFromOutput = chunk.trim().split('\n')
const missing = []
for (const rule in rules) {
const filtered = rulesFromOutput.find((x) => {
return x.trim()
=== `${rules[rule].id} ${rules[rule].meta.description}`
})

if (!filtered) {
missing.push(rule)
}
}

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

tt.end()
})
})

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

ls.stderr.on('data', (data) => {
Expand All @@ -19,7 +98,7 @@ test('Test cli flags', (t) => {
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 subsystemsFromOutput = chunk.match(/[\w'-]+/g)
const defaultSubsystems = subsystems.defaults.subsystems

tt.equal(subsystemsFromOutput.length,
Expand Down