-
Notifications
You must be signed in to change notification settings - Fork 935
/
Copy pathget-messages.js
121 lines (93 loc) · 3.35 KB
/
get-messages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {tmpdir} from 'os';
import crypto from 'crypto';
import {join} from 'path';
import test from 'ava';
import denodeify from 'denodeify';
import execa from 'execa';
import {mkdir, writeFile} from 'mz/fs';
import exists from 'path-exists';
import rimraf from 'rimraf';
import expect from 'unexpected';
import getMessages from '../../source/library/get-messages';
import pkg from '../../package';
const rm = denodeify(rimraf);
test.beforeEach(async t => {
t.context.repos = [await initRepository()];
});
test.afterEach.always(async t => {
try {
await Promise.all(t.context.repos.map(async repo => cleanRepository(repo)));
t.context.repos = [];
} catch (err) {
console.log({err});
}
});
test.serial('get edit commit message from git root', async t => {
const [repo] = t.context.repos;
await writeFile('alpha.txt', 'alpha');
await execa('git', ['add', '.']);
await execa('git', ['commit', '-m', 'alpha']);
const expected = ['alpha\n\n'];
const actual = await getMessages({edit: true});
expect(actual, 'to equal', expected);
});
test.serial('get history commit messages', async t => {
const [repo] = t.context.repos;
await writeFile('alpha.txt', 'alpha');
await execa('git', ['add', 'alpha.txt']);
await execa('git', ['commit', '-m', 'alpha']);
await execa('git', ['rm', 'alpha.txt']);
await execa('git', ['commit', '-m', 'remove alpha']);
const expected = ['remove alpha\n\n', 'alpha\n\n'];
const actual = await getMessages({});
expect(actual, 'to equal', expected);
});
test.serial('get edit commit message from git subdirectory', async t => {
const [repo] = t.context.repos;
await mkdir('beta');
await writeFile('beta/beta.txt', 'beta');
process.chdir('beta');
await execa('git', ['add', '.']);
await execa('git', ['commit', '-m', 'beta']);
const expected = ['beta\n\n'];
const actual = await getMessages({edit: true});
expect(actual, 'to equal', expected);
});
test.serial('get history commit messages from shallow clone', async t => {
const [repo] = t.context.repos;
await writeFile('alpha.txt', 'alpha');
await execa('git', ['add', 'alpha.txt']);
await execa('git', ['commit', '-m', 'alpha']);
const clone = await cloneRepository(pkg.repository.url, repo, '--depth', '1');
t.context.repos = [...t.context.repos, clone];
const err = await t.throws(getMessages({from: 'master'}));
expect(err.message, 'to contain', 'Could not get git history from shallow clone');
});
async function initRepository() {
const previous = process.cwd();
const directory = join(tmpdir(), rand());
await execa('git', ['init', directory]);
process.chdir(directory);
await execa('git', ['config', 'user.email', '[email protected]']);
await execa('git', ['config', 'user.name', 'ava']);
return {directory, previous};
}
async function cloneRepository(source, context, ...args) {
const directory = join(tmpdir(), rand());
await execa('git', ['clone', ...args, source, directory]);
process.chdir(directory);
await execa('git', ['config', 'user.email', '[email protected]']);
await execa('git', ['config', 'user.name', 'ava']);
return {directory, previous: context.previous};
}
async function cleanRepository(repo) {
if (repo.previous && repo.previous !== process.cwd()) {
process.chdir(repo.previous);
}
if (await exists(repo.directory)) {
await rm(repo.directory);
}
}
function rand() {
return crypto.randomBytes(Math.ceil(6)).toString('hex').slice(0, 12);
}