diff --git a/src/scripts/__tests__/__snapshots__/commit.js.snap b/src/scripts/__tests__/__snapshots__/commit.js.snap new file mode 100644 index 00000000..22db73cd --- /dev/null +++ b/src/scripts/__tests__/__snapshots__/commit.js.snap @@ -0,0 +1,45 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`commit bootstraps @commitlint/prompt 1`] = ` +Object { + cliPath: /node_modules/commitizen, + config: Object { + path: @commitlint/prompt, + }, +} +`; + +exports[`commit bootstraps @commitlint/prompt 2`] = `node ../commit`; + +exports[`commit forwards arguments 1`] = ` +Object { + cliPath: /node_modules/commitizen, + config: Object { + path: @commitlint/prompt, + }, +} +`; + +exports[`commit forwards arguments 2`] = `node ../commit --retry`; + +exports[`commit strips errant "commit" argument 1`] = ` +Object { + cliPath: /node_modules/commitizen, + config: Object { + path: @commitlint/prompt, + }, +} +`; + +exports[`commit strips errant "commit" argument 2`] = `node ../commit`; + +exports[`commit strips errant "commit" argument and forwards arguments 1`] = ` +Object { + cliPath: /node_modules/commitizen, + config: Object { + path: @commitlint/prompt, + }, +} +`; + +exports[`commit strips errant "commit" argument and forwards arguments 2`] = `node ../commit --retry`; diff --git a/src/scripts/__tests__/commit.js b/src/scripts/__tests__/commit.js new file mode 100644 index 00000000..0c614af8 --- /dev/null +++ b/src/scripts/__tests__/commit.js @@ -0,0 +1,62 @@ +import cases from 'jest-in-case' +import { + unquoteSerializer, + winPathSerializer, + relativePathSerializer, +} from './helpers/serializers' + +jest.mock('commitizen/dist/cli/git-cz') + +expect.addSnapshotSerializer(unquoteSerializer) +expect.addSnapshotSerializer(winPathSerializer) +expect.addSnapshotSerializer(relativePathSerializer) + +cases( + 'commit', + ({args = [], env = {}}) => { + // beforeEach + const {bootstrap: bootstrapMock} = require('commitizen/dist/cli/git-cz') + + const originalArgv = process.argv + const originalExit = process.exit + const originalEnv = process.env + + process.exit = jest.fn() + process.argv = ['node', '../commit', ...args] + process.env = env + + try { + // tests + require('../commit') + + expect(bootstrapMock).toHaveBeenCalledTimes(1) + + const [call] = bootstrapMock.mock.calls + const [optionsArg, argsArg] = call + + expect(optionsArg).toMatchSnapshot() + expect(argsArg.join(' ')).toMatchSnapshot() + } catch (error) { + throw error + } finally { + // afterEach + process.exit = originalExit + process.argv = originalArgv + process.env = originalEnv + + jest.resetModules() + } + }, + { + 'bootstraps @commitlint/prompt': {}, + 'strips errant "commit" argument': { + args: ['commit'], + }, + 'forwards arguments': { + args: ['--retry'], + }, + 'strips errant "commit" argument and forwards arguments': { + args: ['commit', '--retry'], + }, + }, +) diff --git a/src/scripts/commit.js b/src/scripts/commit.js index 5780df29..9628b85d 100644 --- a/src/scripts/commit.js +++ b/src/scripts/commit.js @@ -1,18 +1,24 @@ -const fs = require('fs') -const path = require('path') const {bootstrap} = require('commitizen/dist/cli/git-cz') -const {fromRoot} = require('../utils') -const here = p => path.join(__dirname, p) +const commitizenPaths = require.resolve('commitizen/package.json').split('/') +const cliPath = commitizenPaths.slice(0, -1).join('/') -const cliPath = [ - here('../../node_modules/commitizen'), - fromRoot('./node_modules/commitizen'), -].find(fs.existsSync) +// eslint-disable-next-line no-warning-comments +// FIXME: for some reason invoking this script with `yarn commit` now passes +// through `'commit'` in the arguments that get parsed as "raw git arguments" +// in the adapter which blows up said parsing behavior. I'm not sure if this +// was related to a yarn change (`yarn [script] args` vs. `yarn [script] -- +// args`) or something else... +// +// Either way, this is a band-aid to filter out the rogue argument for now. +const args = process.argv.filter(arg => arg !== 'commit') -bootstrap({ - cliPath, - config: { - path: '@commitlint/prompt', +bootstrap( + { + cliPath, + config: { + path: '@commitlint/prompt', + }, }, -}) + args, +)