From 084fe88beda22d90ea03ad433d7798924a434d4c Mon Sep 17 00:00:00 2001 From: Jamie Rolfs Date: Fri, 10 Jul 2020 13:13:58 -0700 Subject: [PATCH 1/3] fix(scripts/commit): strip rogue commit arg coming from yarn commit 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. --- src/scripts/commit.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/scripts/commit.js b/src/scripts/commit.js index 5780df29..530557da 100644 --- a/src/scripts/commit.js +++ b/src/scripts/commit.js @@ -10,9 +10,22 @@ const cliPath = [ fromRoot('./node_modules/commitizen'), ].find(fs.existsSync) -bootstrap({ - cliPath, - config: { - path: '@commitlint/prompt', +// 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', + }, }, -}) + args, +) From 437255ab383c82bbd906aaa4059446ecac5d22bb Mon Sep 17 00:00:00 2001 From: Jamie Rolfs Date: Fri, 10 Jul 2020 13:35:44 -0700 Subject: [PATCH 2/3] fix(scripts/commit): use `require.resolve` to avoid `node_modules` path Use `require.resolve` to resolve 'commitizen' path for use in `bootstrap`ing prompt adapter to make resolution more resilient and remove the dependency on `node_modules` directory. --- src/scripts/commit.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/scripts/commit.js b/src/scripts/commit.js index 530557da..9628b85d 100644 --- a/src/scripts/commit.js +++ b/src/scripts/commit.js @@ -1,14 +1,7 @@ -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 cliPath = [ - here('../../node_modules/commitizen'), - fromRoot('./node_modules/commitizen'), -].find(fs.existsSync) +const commitizenPaths = require.resolve('commitizen/package.json').split('/') +const cliPath = commitizenPaths.slice(0, -1).join('/') // eslint-disable-next-line no-warning-comments // FIXME: for some reason invoking this script with `yarn commit` now passes From 4c182b5cce02714df0db2d36bb172fd3295ce931 Mon Sep 17 00:00:00 2001 From: Jamie Rolfs Date: Fri, 10 Jul 2020 14:18:45 -0700 Subject: [PATCH 3/3] test(scripts/commit): add tests for git-cz `bootstrap` invocation --- .../__tests__/__snapshots__/commit.js.snap | 45 ++++++++++++++ src/scripts/__tests__/commit.js | 62 +++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/scripts/__tests__/__snapshots__/commit.js.snap create mode 100644 src/scripts/__tests__/commit.js 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'], + }, + }, +)