From 049bc9a6f8897f9e7ff16e277926168e69bd45f5 Mon Sep 17 00:00:00 2001 From: Ashwin Aggarwal Date: Wed, 27 Mar 2019 13:53:09 +0000 Subject: [PATCH] feat(engine): allow user to add [skip-ci] in head of commit message re #5 --- .travis.yml | 4 +- __tests__/__snapshots__/engine.spec.js.snap | 17 +++++++++ __tests__/engine.spec.js | 42 +++++++++++++++++++++ engine.js | 10 ++++- package.json | 9 ++++- 5 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 __tests__/__snapshots__/engine.spec.js.snap create mode 100644 __tests__/engine.spec.js diff --git a/.travis.yml b/.travis.yml index b065e404..d3ab4fb4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,9 +6,9 @@ cache: notifications: email: false node_js: - - '4' + - '8.9.4' before_install: - - npm i -g npm@^2.0.0 + - npm i -g npm@^5.6.0 before_script: - npm prune after_success: diff --git a/__tests__/__snapshots__/engine.spec.js.snap b/__tests__/__snapshots__/engine.spec.js.snap new file mode 100644 index 00000000..672bb6e0 --- /dev/null +++ b/__tests__/__snapshots__/engine.spec.js.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Engine should add [skip-ci] to commit message head if user said yes to skip deployment 1`] = ` +"chore(buildspec): [skip-ci] This is a test message + + + +fix #Issue01" +`; + +exports[`Engine should not add [skip-ci] to commit message head if user said no (default) to skip deployment 1`] = ` +"chore(buildspec): This is a test message + + + +fix #Issue01" +`; diff --git a/__tests__/engine.spec.js b/__tests__/engine.spec.js new file mode 100644 index 00000000..4722a511 --- /dev/null +++ b/__tests__/engine.spec.js @@ -0,0 +1,42 @@ +describe('Engine', () => { + let engine = require('../index'); + let answersMock = { + type: 'chore', + scope: 'buildspec', + subject: 'This is a test message', + body: '', + isBreaking: false, + needDeployment: true, + isIssueAffected: true, + issues: 'fix #Issue01' + }; + + let cz = { + prompt: jest.fn() + }; + + beforeEach(() => { + cz.prompt.mockReset(); + }); + + test('should not add [skip-ci] to commit message head if user said no (default) to skip deployment', (done) => { + cz.prompt.mockReturnValue(Promise.resolve(answersMock)); + engine.prompter(cz, (commitMessage) => { + expect(commitMessage).toMatchSnapshot(); + expect(/\[skip-ci\]/g.test(commitMessage)).toBeFalsy(); + done(); + }); + }); + + test('should add [skip-ci] to commit message head if user said yes to skip deployment', (done) => { + cz.prompt.mockReturnValue(Promise.resolve({ + ...answersMock, + needDeployment: false + })); + engine.prompter(cz, (commitMessage) => { + expect(commitMessage).toMatchSnapshot(); + expect(/\[skip-ci\]/g.test(commitMessage)).toBeTruthy(); + done(); + }); + }); +}); \ No newline at end of file diff --git a/engine.js b/engine.js index 68e60087..938a096e 100644 --- a/engine.js +++ b/engine.js @@ -91,6 +91,11 @@ module.exports = function (options) { when: function(answers) { return answers.isBreaking; } + }, { + type: 'confirm', + name: 'needDeployment', + message: 'Does this commit needs to be deployed?', + default: true }, { type: 'confirm', name: 'isIssueAffected', @@ -123,8 +128,11 @@ module.exports = function (options) { scope = scope.trim(); scope = scope ? '(' + scope + ')' : ''; + // add [skip-ci] in commit message to trip deployment pipeline when necessary + var skipDeploymentMsg = answers.needDeployment ? '' : '[skip-ci] '; + // Hard limit this line - var head = (answers.type + scope + ': ' + answers.subject.trim()).slice(0, maxLineWidth); + var head = (answers.type + scope + ': ' + skipDeploymentMsg + answers.subject.trim()).slice(0, maxLineWidth); // Wrap these lines at 100 characters var body = wrap(answers.body, wrapOptions); diff --git a/package.json b/package.json index b14227e7..f02707dd 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "index.js", "scripts": { "commit": "git-cz", - "test": "echo 'Tests need to be setup!'", + "test": "jest", + "test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand --no-cache", "semantic-release": "semantic-release pre && npm publish && semantic-release post" }, "homepage": "https://github.com/commitizen/cz-conventional-changelog", @@ -24,8 +25,14 @@ }, "devDependencies": { "commitizen": "2.9.6", + "jest": "^24.5.0", "semantic-release": "^6.3.2" }, + "jest": { + "testMatch": [ + "**/__tests__/**/*.spec.js" + ] + }, "config": { "commitizen": { "path": "./index.js"