-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgit.test.js
88 lines (72 loc) · 3.27 KB
/
git.test.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
const path = require('path');
const test = require('ava');
const {outputFile, appendFile} = require('fs-extra');
const {add, getModifiedFiles, commit, gitHead, push} = require('../lib/git.js');
const {gitRepo, gitCommits, gitGetCommits, gitStaged, gitRemoteHead} = require('./helpers/git-utils.js');
test('Add file to index', async (t) => {
// Create a git repository, set the current working directory at the root of the repo
const {cwd} = await gitRepo();
// Create files
await outputFile(path.resolve(cwd, 'file1.js'), '');
// Add files and commit
await add(['.'], {cwd});
await t.deepEqual(await gitStaged({cwd}), ['file1.js']);
});
test('Get the modified files, including files in .gitignore but including untracked ones', async (t) => {
// Create a git repository, set the current working directory at the root of the repo
const {cwd} = await gitRepo();
// Create files
await outputFile(path.resolve(cwd, 'file1.js'), '');
await outputFile(path.resolve(cwd, 'dir/file2.js'), '');
await outputFile(path.resolve(cwd, 'file3.js'), '');
// Create .gitignore to ignore file3.js
await outputFile(path.resolve(cwd, '.gitignore'), 'file.3.js');
// Add files and commit
await add(['.'], {cwd});
await commit('Test commit', {cwd});
// Update file1.js, dir/file2.js and file3.js
await appendFile(path.resolve(cwd, 'file1.js'), 'Test content');
await appendFile(path.resolve(cwd, 'dir/file2.js'), 'Test content');
await appendFile(path.resolve(cwd, 'file3.js'), 'Test content');
// Add untracked file
await outputFile(path.resolve(cwd, 'file4.js'), 'Test content');
await t.deepEqual(
(await getModifiedFiles({cwd})).sort(),
['file1.js', 'dir/file2.js', 'file3.js', 'file4.js'].sort()
);
});
test('Returns [] if there is no modified files', async (t) => {
// Create a git repository, set the current working directory at the root of the repo
const {cwd} = await gitRepo();
await t.deepEqual(await getModifiedFiles({cwd}), []);
});
test('Commit added files', async (t) => {
// Create a git repository, set the current working directory at the root of the repo
const {cwd} = await gitRepo();
// Create files
await outputFile(path.resolve(cwd, 'file1.js'), '');
// Add files and commit
await add(['.'], {cwd});
await commit('Test commit', {cwd});
await t.true((await gitGetCommits(undefined, {cwd})).length === 1);
});
test('Get the last commit sha', async (t) => {
// Create a git repository, set the current working directory at the root of the repo
const {cwd} = await gitRepo();
// Add commits to the master branch
const [{hash}] = await gitCommits(['First'], {cwd});
const result = await gitHead({cwd});
t.is(result, hash);
});
test('Throw error if the last commit sha cannot be found', async (t) => {
// Create a git repository, set the current working directory at the root of the repo
const {cwd} = await gitRepo();
await t.throwsAsync(gitHead({cwd}));
});
test('Push commit to remote repository', async (t) => {
// Create a git repository with a remote, set the current working directory at the root of the repo
const {cwd, repositoryUrl} = await gitRepo(true);
const [{hash}] = await gitCommits(['Test commit'], {cwd});
await push(repositoryUrl, 'master', {cwd});
t.is(await gitRemoteHead(repositoryUrl, {cwd}), hash);
});