Skip to content

Commit 1449c6b

Browse files
committed
test(travis-cli): fix and enable mocked tests
1 parent c9e4c8c commit 1449c6b

File tree

7 files changed

+83
-141
lines changed

7 files changed

+83
-141
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
#!/usr/bin/env node
2-
console.log(process.argv);
2+
3+
const args = process.argv;
4+
args.shift(); // remove node
5+
console.log(
6+
args.map((item, index) => {
7+
return index === 0 ? 'commitlint' : item;
8+
})
9+
);
+8-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
#!/usr/bin/env node
2-
console.log(process.argv);
2+
3+
const args = process.argv;
4+
args.shift(); // remove node
5+
console.log(
6+
args.map((item, index) => {
7+
return index === 0 ? 'git' : item;
8+
})
9+
);

@commitlint/travis-cli/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@
4242
},
4343
"license": "MIT",
4444
"devDependencies": {
45-
"@babel/core": "7.7.7",
4645
"@babel/cli": "7.7.7",
46+
"@babel/core": "7.7.7",
4747
"@babel/register": "7.7.7",
4848
"@commitlint/test": "8.2.0",
4949
"@commitlint/utils": "^8.3.4",
5050
"babel-preset-commitlint": "^8.2.0",
51-
"cross-env": "6.0.3",
52-
"which": "2.0.1"
51+
"cross-env": "6.0.3"
5352
},
5453
"dependencies": {
5554
"@commitlint/cli": "^8.3.5",

@commitlint/travis-cli/src/cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async function lint(args, options) {
7777
}
7878

7979
async function log(hash) {
80-
const result = await execa('git', [
80+
const result = await execa(GIT, [
8181
'log',
8282
'-n',
8383
'1',
+49-120
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import execa from 'execa';
2-
// Disable ftb
3-
// import {git} from '@commitlint/test';
4-
// import which from 'which';
2+
import {git} from '@commitlint/test';
53

6-
// Disable ftb
7-
// const NODE_BIN = which.sync('node');
84
const bin = require.resolve('../lib/cli.js');
95

10-
// Disable ftb
11-
// const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint');
12-
// const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git');
13-
// const TRAVIS_BRANCH = 'TRAVIS_BRANCH';
14-
// const TRAVIS_COMMIT = 'TRAVIS_COMMIT';
6+
const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint');
7+
const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git');
8+
9+
const validBaseEnv = {
10+
TRAVIS: true,
11+
CI: true,
12+
TRAVIS_COMMIT: 'TRAVIS_COMMIT',
13+
TRAVIS_COMMITLINT_BIN: TRAVIS_COMMITLINT_BIN,
14+
TRAVIS_COMMITLINT_GIT_BIN: TRAVIS_COMMITLINT_GIT_BIN,
15+
TRAVIS_COMMIT_RANGE: 'TRAVIS_COMMIT_A.TRAVIS_COMMIT_B',
16+
TRAVIS_EVENT_TYPE: 'TRAVIS_EVENT_TYPE',
17+
TRAVIS_REPO_SLUG: 'TRAVIS_REPO_SLUG',
18+
TRAVIS_PULL_REQUEST_SLUG: 'TRAVIS_PULL_REQUEST_SLUG'
19+
};
1520

1621
const cli = async (config = {}) => {
1722
try {
@@ -43,128 +48,53 @@ test('should throw when on travis ci, but env vars are missing', async () => {
4348
);
4449
});
4550

46-
/*
47-
test('should call git with expected args on shallow repo', async () => {
51+
test('should call git with expected args', async () => {
4852
const cwd = await git.clone(
4953
'https://github.com/conventional-changelog/commitlint.git',
50-
['--depth=10']
51-
);
52-
53-
const env = {
54-
TRAVIS: true,
55-
CI: true,
56-
TRAVIS_BRANCH,
57-
TRAVIS_COMMIT,
58-
TRAVIS_COMMITLINT_BIN,
54+
['--depth=10'],
55+
__dirname,
5956
TRAVIS_COMMITLINT_GIT_BIN
60-
};
57+
);
6158

62-
const result = await cli({cwd, env});
59+
const result = await cli({
60+
cwd,
61+
env: validBaseEnv
62+
});
6363
const invocations = await getInvocations(result.stdout);
64-
t.is(invocations.length, 7);
65-
66-
const [
67-
stash,
68-
branches,
69-
unshallow,
70-
checkout,
71-
back,
72-
pop,
73-
commilint
74-
] = invocations;
75-
76-
t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']);
77-
t.deepEqual(branches, [
78-
NODE_BIN,
79-
TRAVIS_COMMITLINT_GIT_BIN,
80-
'remote',
81-
'set-branches',
82-
'origin',
83-
TRAVIS_BRANCH
84-
]);
85-
t.deepEqual(unshallow, [
86-
NODE_BIN,
87-
TRAVIS_COMMITLINT_GIT_BIN,
88-
'fetch',
89-
'--unshallow',
90-
'--quiet'
91-
]);
92-
t.deepEqual(checkout, [
93-
NODE_BIN,
94-
TRAVIS_COMMITLINT_GIT_BIN,
95-
'checkout',
96-
TRAVIS_BRANCH,
97-
'--quiet'
98-
]);
99-
t.deepEqual(back, [
100-
NODE_BIN,
101-
TRAVIS_COMMITLINT_GIT_BIN,
102-
'checkout',
103-
'-',
104-
'--quiet'
105-
]);
106-
t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']);
107-
t.deepEqual(commilint, [
108-
NODE_BIN,
109-
TRAVIS_COMMITLINT_BIN,
110-
'--from',
111-
TRAVIS_BRANCH,
112-
'--to',
113-
TRAVIS_COMMIT
114-
]);
64+
expect(invocations.length).toBe(3);
65+
66+
const [stash, branches, commilint] = invocations;
67+
68+
expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']);
69+
expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']);
70+
expect(commilint).toEqual(['commitlint']);
11571
});
11672

117-
test('should call git with expected args on unshallow repo', async () => {
73+
test('should call git with expected args on pull_request', async () => {
11874
const cwd = await git.clone(
119-
'https://github.com/conventional-changelog/commitlint.git'
120-
);
121-
122-
const env = {
123-
TRAVIS: true,
124-
CI: true,
125-
TRAVIS_BRANCH,
126-
TRAVIS_COMMIT,
127-
TRAVIS_COMMITLINT_BIN,
75+
'https://github.com/conventional-changelog/commitlint.git',
76+
['--depth=10'],
77+
__dirname,
12878
TRAVIS_COMMITLINT_GIT_BIN
129-
};
79+
);
13080

131-
const result = await cli({cwd, env});
81+
const result = await cli({
82+
cwd,
83+
env: {...validBaseEnv, TRAVIS_EVENT_TYPE: 'pull_request'}
84+
});
13285
const invocations = await getInvocations(result.stdout);
133-
t.is(invocations.length, 6);
134-
135-
const [stash, branches, checkout, back, pop, commilint] = invocations;
136-
137-
t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']);
138-
t.deepEqual(branches, [
139-
NODE_BIN,
140-
TRAVIS_COMMITLINT_GIT_BIN,
141-
'remote',
142-
'set-branches',
143-
'origin',
144-
TRAVIS_BRANCH
145-
]);
146-
t.deepEqual(checkout, [
147-
NODE_BIN,
148-
TRAVIS_COMMITLINT_GIT_BIN,
149-
'checkout',
150-
TRAVIS_BRANCH,
151-
'--quiet'
152-
]);
153-
t.deepEqual(back, [
154-
NODE_BIN,
155-
TRAVIS_COMMITLINT_GIT_BIN,
156-
'checkout',
157-
'-',
158-
'--quiet'
159-
]);
160-
t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']);
161-
t.deepEqual(commilint, [
162-
NODE_BIN,
163-
TRAVIS_COMMITLINT_BIN,
86+
expect(invocations.length).toBe(3);
87+
88+
const [stash, branches, commilint] = invocations;
89+
90+
expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']);
91+
expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']);
92+
expect(commilint).toEqual([
93+
'commitlint',
16494
'--from',
165-
TRAVIS_BRANCH,
95+
'TRAVIS_COMMIT_A',
16696
'--to',
167-
TRAVIS_COMMIT
97+
'TRAVIS_COMMIT_B'
16898
]);
16999
});
170100

@@ -182,4 +112,3 @@ function getInvocations(stdout) {
182112
.filter(Boolean)
183113
);
184114
}
185-
*/

@packages/test/src/git.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ export async function bootstrap(fixture?: string, directory?: string) {
99
return cwd;
1010
}
1111

12-
export async function clone(source: string, ...args: string[]) {
13-
const cwd = await fix.bootstrap();
12+
export async function clone(
13+
source: string,
14+
args: string[],
15+
directory?: string,
16+
gitCommand = 'git'
17+
) {
18+
const cwd = await fix.bootstrap(undefined, directory);
1419

15-
await execa('git', ['clone', ...args, source, cwd]);
16-
await setup(cwd);
20+
await execa(gitCommand, ['clone', ...args, source, cwd]);
21+
await setup(cwd, gitCommand);
1722
return cwd;
1823
}
1924

@@ -23,11 +28,13 @@ export async function init(cwd: string) {
2328
return cwd;
2429
}
2530

26-
async function setup(cwd: string) {
31+
async function setup(cwd: string, gitCommand = 'git') {
2732
try {
28-
await execa('git', ['config', 'user.name', 'ava'], {cwd});
29-
await execa('git', ['config', 'user.email', '[email protected]'], {cwd});
30-
await execa('git', ['config', 'commit.gpgsign', 'false'], {cwd});
33+
await execa(gitCommand, ['config', 'user.name', 'ava'], {cwd});
34+
await execa(gitCommand, ['config', 'user.email', '[email protected]'], {
35+
cwd
36+
});
37+
await execa(gitCommand, ['config', 'commit.gpgsign', 'false'], {cwd});
3138
} catch (err) {
3239
console.warn(`git config in ${cwd} failed`, err.message);
3340
}

yarn.lock

-7
Original file line numberDiff line numberDiff line change
@@ -10172,13 +10172,6 @@ which-module@^2.0.0:
1017210172
resolved "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a"
1017310173
integrity sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=
1017410174

10175-
10176-
version "2.0.1"
10177-
resolved "https://registry.npmjs.org/which/-/which-2.0.1.tgz#f1cf94d07a8e571b6ff006aeb91d0300c47ef0a4"
10178-
integrity sha512-N7GBZOTswtB9lkQBZA4+zAXrjEIWAUOB93AvzUiudRzRxhUdLURQ7D/gAIMY1gatT/LTbmbcv8SiYazy3eYB7w==
10179-
dependencies:
10180-
isexe "^2.0.0"
10181-
1018210175
which@^1.2.10, which@^1.2.14, which@^1.2.9, which@^1.3.0, which@^1.3.1:
1018310176
version "1.3.1"
1018410177
resolved "https://registry.npmjs.org/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a"

0 commit comments

Comments
 (0)