forked from pascalgn/automerge-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.test.js
113 lines (101 loc) · 3.76 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
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
const fse = require("fs-extra");
const git = require("../lib/git");
const { tmpdir } = require("../lib/common");
async function init(dir) {
await fse.mkdirs(dir);
await git.git(dir, "init");
}
async function commit(dir, message = "C%d", count = 1) {
for (let i = 1; i <= count; i++) {
await git.git(
dir,
"commit",
"--allow-empty",
"-m",
message.replace(/%d/g, i)
);
}
}
test("clone creates the target directory", async () => {
await tmpdir(async path => {
await init(`${path}/origin`);
await commit(`${path}/origin`);
await git.clone(`file://${path}/origin`, `${path}/ws`, "master", 1);
expect(await fse.exists(`${path}/ws`)).toBe(true);
});
});
test("fetchUntilMergeBase finds the correct merge base", async () => {
await tmpdir(async path => {
const origin = `${path}/origin`;
await init(origin);
await commit(origin, "base %d", 10);
const base = await git.head(origin);
await git.git(origin, "checkout", "-b", "br1");
await commit(origin, "br1 %d", 20);
await git.git(origin, "checkout", "master");
await commit(origin, "master %d", 20);
const ws = `${path}/ws`;
await git.clone(`file://${path}/origin`, ws, "br1");
await git.fetch(ws, "master");
expect(await git.fetchUntilMergeBase(ws, "master", 10000)).toBe(base);
});
}, 15000);
test("fetchUntilMergeBase finds the earliest merge base 1", async () => {
await tmpdir(async path => {
const origin = `${path}/origin`;
await init(origin);
await commit(origin, "base %d", 10);
const base = await git.head(origin);
await git.git(origin, "branch", "br1");
await commit(origin, "master %d", 10);
await git.git(origin, "checkout", "br1");
await commit(origin, "br1 before merge %d", 5);
await git.git(origin, "merge", "--no-ff", "master");
await commit(origin, "br1 after merge %d", 10);
await git.git(origin, "checkout", "master");
await commit(origin, "master after merge %d", 10);
const ws = `${path}/ws`;
await git.clone(`file://${path}/origin`, ws, "br1");
await git.fetch(ws, "master");
expect(await git.fetchUntilMergeBase(ws, "master", 10000)).toBe(base);
});
}, 15000);
test("fetchUntilMergeBase finds the earliest merge base 2", async () => {
await tmpdir(async path => {
const origin = `${path}/origin`;
await init(origin);
await commit(origin, "base a%d", 5);
const base = await git.head(origin);
await commit(origin, "base b%d", 5);
await git.git(origin, "branch", "br1");
await commit(origin, "master %d", 10);
await git.git(origin, "checkout", "br1");
await commit(origin, "br1 before merge %d", 5);
await git.git(origin, "merge", "--no-ff", "master");
await commit(origin, "br1 after merge %d", 10);
await git.git(origin, "checkout", "master");
await commit(origin, "master after merge %d", 10);
await git.git(origin, "checkout", "-b", "br2", base);
await commit(origin, "br2");
await git.git(origin, "checkout", "br1");
await git.git(origin, "merge", "--no-ff", "br2");
const ws = `${path}/ws`;
await git.clone(`file://${path}/origin`, ws, "br1");
await git.fetch(ws, "master");
expect(await git.fetchUntilMergeBase(ws, "master", 10000)).toBe(base);
});
}, 15000);
test("mergeCommits returns the correct commits", async () => {
await tmpdir(async path => {
await init(path);
await commit(path, "master %d", 2);
const head1 = await git.head(path);
await git.git(path, "checkout", "-b", "branch", "HEAD^");
const head2 = await git.head(path);
await git.git(path, "merge", "--no-ff", "master");
const commits = await git.mergeCommits(path, "HEAD^");
expect(commits).toHaveLength(1);
expect(commits[0][0]).toBe(head2);
expect(commits[0][1]).toBe(head1);
});
});