Skip to content

Commit c6d2fdb

Browse files
Cameron Westlandjimthedev
authored andcommitted
feat(bootstrap): add ability to load adapterConfig from bootstrap
* feat(cli): add ability to load adapterConfig from bootstrap Instead of forcing the commitizen config to be loaded via a file (package.json, .czrc, .cz.json) this adds a way to inject an adapterConfig into the bootstrap function. We use this internally at Autodesk to wrap the cli so that each of our repos doesn't need to have the commitizen config * test(cli): add tests for providing adapterConfig via bootstrap function * add proxyquire & sinon to aid unit testing * excludes src/cli from nyc since most of it is not unit-tested and this test causes coverage to be misreported
1 parent a538dce commit c6d2fdb

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@
5858
"node-uuid": "1.4.7",
5959
"nodemon": "1.9.1",
6060
"nyc": "6.4.0",
61+
"proxyquire": "^1.7.4",
6162
"rimraf": "2.5.2",
6263
"semantic-release": "^4.3.5",
63-
"semver": "5.1.0"
64+
"semver": "5.1.0",
65+
"sinon": "^1.17.3"
6466
},
6567
"dependencies": {
6668
"chalk": "1.1.3",
@@ -84,5 +86,12 @@
8486
"es2015",
8587
"stage-2"
8688
]
89+
},
90+
91+
"nyc": {
92+
"exclude": [
93+
"src/cli",
94+
"test"
95+
]
8796
}
8897
}

src/cli/git-cz.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ function bootstrap(environment = {}) {
1414

1515
// Get cli args
1616
let rawGitArgs = process.argv.slice(2, process.argv.length);
17-
18-
// Load the adapter config
19-
let adapterConfig = configLoader.load();
20-
17+
18+
let adapterConfig = environment.config || configLoader.load();
19+
2120
// Choose a strategy based on the existance the adapter config
2221
if(typeof adapterConfig !== 'undefined') {
2322
// This tells commitizen we're in business

test/tests/cli.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {expect} from 'chai';
2+
import proxyquire from 'proxyquire';
3+
import * as sinon from 'sinon';
4+
5+
describe('git-cz', () => {
6+
let bootstrap;
7+
let fakeStrategies, fakeCommitizen;
8+
9+
beforeEach(() => {
10+
fakeStrategies = {
11+
git: sinon.spy(),
12+
gitCz: sinon.spy()
13+
}
14+
15+
fakeCommitizen = {
16+
configLoader: {
17+
load: sinon.stub()
18+
}
19+
}
20+
21+
bootstrap = proxyquire('../../src/cli/git-cz', {
22+
'./strategies': fakeStrategies,
23+
'../commitizen': fakeCommitizen
24+
}).bootstrap;
25+
});
26+
27+
describe('bootstrap', () => {
28+
describe('when config is provided', () => {
29+
it('passes config to useGitCzStrategy', () => {
30+
const config = sinon.spy();
31+
32+
bootstrap({ config });
33+
34+
expect(fakeStrategies.gitCz.args[0][2]).to.equal(config);
35+
});
36+
});
37+
38+
describe('when config is not provided', () => {
39+
40+
describe('and the config is returned from configLoader.load', () => {
41+
it('uses config from configLoader.load()', () => {
42+
const config = sinon.stub();
43+
fakeCommitizen.configLoader.load.returns(config);
44+
45+
bootstrap({});
46+
47+
expect(fakeStrategies.gitCz.args[0][2]).to.equal(config);
48+
});
49+
});
50+
51+
describe('and the config is not returned from configLoader.load', () => {
52+
it('tells commitizen to use the git strategy', () => {
53+
bootstrap({});
54+
expect(fakeStrategies.git.called).to.equal(true);
55+
});
56+
});
57+
});
58+
});
59+
});

test/tests/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import './adapter';
2+
import './cli';
23
import './commit';
34
import './configLoader';
45
import './init';

0 commit comments

Comments
 (0)