|
| 1 | +import path from 'path'; |
1 | 2 | import test from 'ava';
|
2 | 3 | import {outputFile, appendFile} from 'fs-extra';
|
3 | 4 | import {add, getModifiedFiles, commit, gitHead, push} from '../lib/git';
|
4 | 5 | import {gitRepo, gitCommits, gitGetCommits, gitStaged, gitRemoteHead} from './helpers/git-utils';
|
5 | 6 |
|
6 |
| -// Save the current working diretory |
7 |
| -const cwd = process.cwd(); |
8 |
| - |
9 |
| -test.afterEach.always(() => { |
10 |
| - // Restore the current working directory |
11 |
| - process.chdir(cwd); |
12 |
| -}); |
13 |
| - |
14 |
| -test.serial('Add file to index', async t => { |
| 7 | +test('Add file to index', async t => { |
15 | 8 | // Create a git repository, set the current working directory at the root of the repo
|
16 |
| - await gitRepo(); |
| 9 | + const {cwd} = await gitRepo(); |
17 | 10 | // Create files
|
18 |
| - await outputFile('file1.js', ''); |
| 11 | + await outputFile(path.resolve(cwd, 'file1.js'), ''); |
19 | 12 | // Add files and commit
|
20 |
| - await add(['.']); |
| 13 | + await add(['.'], {cwd}); |
21 | 14 |
|
22 |
| - await t.deepEqual(await gitStaged(), ['file1.js']); |
| 15 | + await t.deepEqual(await gitStaged({cwd}), ['file1.js']); |
23 | 16 | });
|
24 | 17 |
|
25 |
| -test.serial('Get the modified files, including files in .gitignore but including untracked ones', async t => { |
| 18 | +test('Get the modified files, including files in .gitignore but including untracked ones', async t => { |
26 | 19 | // Create a git repository, set the current working directory at the root of the repo
|
27 |
| - await gitRepo(); |
| 20 | + const {cwd} = await gitRepo(); |
28 | 21 | // Create files
|
29 |
| - await outputFile('file1.js', ''); |
30 |
| - await outputFile('dir/file2.js', ''); |
31 |
| - await outputFile('file3.js', ''); |
| 22 | + await outputFile(path.resolve(cwd, 'file1.js'), ''); |
| 23 | + await outputFile(path.resolve(cwd, 'dir/file2.js'), ''); |
| 24 | + await outputFile(path.resolve(cwd, 'file3.js'), ''); |
32 | 25 | // Create .gitignore to ignore file3.js
|
33 |
| - await outputFile('.gitignore', 'file.3.js'); |
| 26 | + await outputFile(path.resolve(cwd, '.gitignore'), 'file.3.js'); |
34 | 27 | // Add files and commit
|
35 |
| - await add(['.']); |
36 |
| - await commit('Test commit'); |
| 28 | + await add(['.'], {cwd}); |
| 29 | + await commit('Test commit', {cwd}); |
37 | 30 | // Update file1.js, dir/file2.js and file3.js
|
38 |
| - await appendFile('file1.js', 'Test content'); |
39 |
| - await appendFile('dir/file2.js', 'Test content'); |
40 |
| - await appendFile('file3.js', 'Test content'); |
| 31 | + await appendFile(path.resolve(cwd, 'file1.js'), 'Test content'); |
| 32 | + await appendFile(path.resolve(cwd, 'dir/file2.js'), 'Test content'); |
| 33 | + await appendFile(path.resolve(cwd, 'file3.js'), 'Test content'); |
41 | 34 | // Add untracked file
|
42 |
| - await outputFile('file4.js', 'Test content'); |
| 35 | + await outputFile(path.resolve(cwd, 'file4.js'), 'Test content'); |
43 | 36 |
|
44 |
| - await t.deepEqual((await getModifiedFiles()).sort(), ['file4.js', 'file3.js', 'dir/file2.js', 'file1.js'].sort()); |
| 37 | + await t.deepEqual( |
| 38 | + (await getModifiedFiles({cwd})).sort(), |
| 39 | + ['file4.js', 'file3.js', 'dir/file2.js', 'file1.js'].sort() |
| 40 | + ); |
45 | 41 | });
|
46 | 42 |
|
47 |
| -test.serial('Returns [] if there is no modified files', async t => { |
| 43 | +test('Returns [] if there is no modified files', async t => { |
48 | 44 | // Create a git repository, set the current working directory at the root of the repo
|
49 |
| - await gitRepo(); |
| 45 | + const {cwd} = await gitRepo(); |
50 | 46 |
|
51 |
| - await t.deepEqual(await getModifiedFiles(), []); |
| 47 | + await t.deepEqual(await getModifiedFiles({cwd}), []); |
52 | 48 | });
|
53 | 49 |
|
54 |
| -test.serial('Commit added files', async t => { |
| 50 | +test('Commit added files', async t => { |
55 | 51 | // Create a git repository, set the current working directory at the root of the repo
|
56 |
| - await gitRepo(); |
| 52 | + const {cwd} = await gitRepo(); |
57 | 53 | // Create files
|
58 |
| - await outputFile('file1.js', ''); |
| 54 | + await outputFile(path.resolve(cwd, 'file1.js'), ''); |
59 | 55 | // Add files and commit
|
60 |
| - await add(['.']); |
61 |
| - await commit('Test commit'); |
| 56 | + await add(['.'], {cwd}); |
| 57 | + await commit('Test commit', {cwd}); |
62 | 58 |
|
63 |
| - await t.true((await gitGetCommits()).length === 1); |
| 59 | + await t.true((await gitGetCommits(undefined, {cwd})).length === 1); |
64 | 60 | });
|
65 | 61 |
|
66 |
| -test.serial('Get the last commit sha', async t => { |
| 62 | +test('Get the last commit sha', async t => { |
67 | 63 | // Create a git repository, set the current working directory at the root of the repo
|
68 |
| - await gitRepo(); |
| 64 | + const {cwd} = await gitRepo(); |
69 | 65 | // Add commits to the master branch
|
70 |
| - const [{hash}] = await gitCommits(['First']); |
| 66 | + const [{hash}] = await gitCommits(['First'], {cwd}); |
71 | 67 |
|
72 |
| - const result = await gitHead(); |
| 68 | + const result = await gitHead({cwd}); |
73 | 69 |
|
74 | 70 | t.is(result, hash);
|
75 | 71 | });
|
76 | 72 |
|
77 |
| -test.serial('Throw error if the last commit sha cannot be found', async t => { |
| 73 | +test('Throw error if the last commit sha cannot be found', async t => { |
78 | 74 | // Create a git repository, set the current working directory at the root of the repo
|
79 |
| - await gitRepo(); |
| 75 | + const {cwd} = await gitRepo(); |
80 | 76 |
|
81 |
| - await t.throws(gitHead()); |
| 77 | + await t.throws(gitHead({cwd})); |
82 | 78 | });
|
83 | 79 |
|
84 |
| -test.serial('Push commit to remote repository', async t => { |
| 80 | +test('Push commit to remote repository', async t => { |
85 | 81 | // Create a git repository with a remote, set the current working directory at the root of the repo
|
86 |
| - const repo = await gitRepo(true); |
87 |
| - const [{hash}] = await gitCommits(['Test commit']); |
| 82 | + const {cwd, repositoryUrl} = await gitRepo(true); |
| 83 | + const [{hash}] = await gitCommits(['Test commit'], {cwd}); |
88 | 84 |
|
89 |
| - await push(repo, 'master'); |
| 85 | + await push(repositoryUrl, 'master', {cwd}); |
90 | 86 |
|
91 |
| - t.is(await gitRemoteHead(repo), hash); |
| 87 | + t.is(await gitRemoteHead(repositoryUrl), hash, {cwd}); |
92 | 88 | });
|
0 commit comments