Skip to content
Open
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
5 changes: 2 additions & 3 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { exec } from 'node:child_process'
import fs from 'node:fs'
import http from 'node:http'
import https from 'node:https'
import path from 'node:path'
import { parseArgs } from 'node:util'
import pretty from '../lib/format-pretty.js'
import formatTap from '../lib/format-tap.js'
Expand Down Expand Up @@ -48,14 +47,14 @@ const { values: parsed, positionals: args } = parseArgs({
})

if (parsed.help) {
const usagePath = path.join(new URL(import.meta.url).pathname, '../usage.txt')
const usagePath = new URL('usage.txt', import.meta.url)
const help = await fs.promises.readFile(usagePath, 'utf8')
console.log(help)
process.exit(0)
}

if (parsed.version) {
const pkgJsonPath = path.join(new URL(import.meta.url).pathname, '../../package.json')
const pkgJsonPath = new URL('../package.json', import.meta.url)
const pkgJson = await fs.promises.readFile(pkgJsonPath, 'utf8')
const { version } = JSON.parse(pkgJson)
console.log(`core-validate-commit v${version}`)
Expand Down
44 changes: 43 additions & 1 deletion test/cli-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, test } from 'node:test'
import { readFileSync } from 'node:fs'
import { cpSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { spawn } from 'node:child_process'
import subsystems from '../lib/rules/subsystem.js'

Expand Down Expand Up @@ -160,6 +162,46 @@ describe('Test cli flags', () => {
})
})

test('test version flag when the install path needs decoding', async (t) => {
// `new URL(import.meta.url).pathname` keeps percent-encoding, and on
// Windows it also keeps the leading `/` in front of the drive letter.
// Run the CLI from a directory whose name contains a space so that
// decoding is not a no-op on POSIX either.
const repoRoot = fileURLToPath(new URL('..', import.meta.url))
// Kept inside the repository so `node_modules` still resolves.
const scratch = mkdtempSync(path.join(repoRoot, 'tmp cli '))

try {
cpSync(new URL('../bin', import.meta.url), path.join(scratch, 'bin'), { recursive: true })
cpSync(new URL('../lib', import.meta.url), path.join(scratch, 'lib'), { recursive: true })
cpSync(new URL('../package.json', import.meta.url), path.join(scratch, 'package.json'))

const ls = spawn(process.execPath, [path.join(scratch, 'bin', 'cmd.js'), '--version'])
let compiledData = ''
let errorData = ''

ls.stdout.on('data', (data) => {
compiledData += data
})

ls.stderr.on('data', (data) => {
errorData += data
})

await waitForClose(ls, (code) => {
const pkgJson = readFileSync(new URL('../package.json', import.meta.url), { encoding: 'utf8' })
const { version } = JSON.parse(pkgJson)
t.assert.strictEqual(errorData, '', 'no error output')
t.assert.strictEqual(code, 0, 'CLI exits with zero code')
t.assert.strictEqual(compiledData.trim(),
`core-validate-commit v${version}`,
'output is equal')
})
} finally {
rmSync(scratch, { recursive: true, force: true })
}
})

test('test stdin with valid JSON', async (t) => {
const validCommit = {
id: '2b98d02b52',
Expand Down