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