/
git-push.test.js
147 lines (109 loc) · 4.51 KB
/
git-push.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"use strict";
const execa = require("execa");
const childProcess = require("@lerna/child-process");
const cloneFixture = require("@lerna-test/clone-fixture")(__dirname);
const gitPush = require("../lib/git-push");
async function listRemoteTags(cwd) {
return execa.stdout("git", ["ls-remote", "--tags", "--refs", "--quiet"], { cwd });
}
beforeEach(() => {
jest.spyOn(childProcess, "exec");
});
afterEach(() => {
jest.restoreAllMocks();
delete process.env.GIT_REDIRECT_STDERR;
});
test("gitPush", async () => {
const { cwd } = await cloneFixture("root-manifest-only");
await execa("git", ["commit", "--allow-empty", "-m", "change"], { cwd });
await execa("git", ["tag", "v1.2.3", "-m", "v1.2.3"], { cwd });
await execa("git", ["tag", "foo@2.3.1", "-m", "foo@2.3.1"], { cwd });
await execa("git", ["tag", "bar@3.2.1", "-m", "bar@3.2.1"], { cwd });
await gitPush("origin", "master", { cwd });
expect(childProcess.exec).toHaveBeenLastCalledWith(
"git",
["push", "--follow-tags", "--no-verify", "--atomic", "origin", "master"],
{ cwd }
);
const list = await listRemoteTags(cwd);
expect(list).toMatch("v1.2.3");
expect(list).toMatch("foo@2.3.1");
expect(list).toMatch("bar@3.2.1");
});
test("remote that does not support --atomic", async () => {
const { cwd } = await cloneFixture("root-manifest-only");
await execa("git", ["commit", "--allow-empty", "-m", "change"], { cwd });
await execa("git", ["tag", "v4.5.6", "-m", "v4.5.6"], { cwd });
// the first time the command is executed, simulate remote error
childProcess.exec.mockImplementationOnce(async () => {
const stderr = "fatal: the receiving end does not support --atomic push";
const error = new Error(
["Command failed: git push --follow-tags --atomic --no-verify origin master", stderr].join("\n")
);
error.stderr = stderr;
throw error;
});
// this call should _not_ throw
await gitPush("origin", "master", { cwd });
expect(childProcess.exec).toHaveBeenCalledTimes(2);
expect(childProcess.exec).toHaveBeenLastCalledWith(
"git",
["push", "--follow-tags", "--no-verify", "origin", "master"],
{ cwd }
);
const list = await listRemoteTags(cwd);
expect(list).toMatch("v4.5.6");
});
test("remote that does not support --atomic and git stderr redirected to stdout ", async () => {
const { cwd } = await cloneFixture("root-manifest-only");
process.env.GIT_REDIRECT_STDERR = "2>&1";
await execa("git", ["commit", "--allow-empty", "-m", "change"], { cwd });
await execa("git", ["tag", "v4.5.6", "-m", "v4.5.6"], { cwd });
// the first time the command is executed, simulate remote error
childProcess.exec.mockImplementationOnce(async () => {
const stdout = "fatal: the receiving end does not support --atomic push";
const error = new Error(
["Command failed: git push --follow-tags --atomic --no-verify origin master", stdout].join("\n")
);
error.stdout = stdout;
throw error;
});
// this call should _not_ throw
await gitPush("origin", "master", { cwd });
expect(childProcess.exec).toHaveBeenCalledTimes(2);
expect(childProcess.exec).toHaveBeenLastCalledWith(
"git",
["push", "--follow-tags", "--no-verify", "origin", "master"],
{ cwd }
);
const list = await listRemoteTags(cwd);
expect(list).toMatch("v4.5.6");
});
test("git cli that does not support --atomic", async () => {
const { cwd } = await cloneFixture("root-manifest-only");
await execa("git", ["commit", "--allow-empty", "-m", "change"], { cwd });
await execa("git", ["tag", "v7.8.9", "-m", "v7.8.9"], { cwd });
// the first time the command is executed, simulate remote error
childProcess.exec.mockImplementationOnce(async () => {
const stderr = "error: unknown option `atomic'";
const error = new Error(
["Command failed: git push --follow-tags --atomic --no-verify origin master", stderr].join("\n")
);
error.stderr = stderr;
throw error;
});
await gitPush("origin", "master", { cwd });
await expect(listRemoteTags(cwd)).resolves.toMatch("v7.8.9");
});
test("unexpected git error", async () => {
const { cwd } = await cloneFixture("root-manifest-only");
childProcess.exec.mockImplementationOnce(async () => {
const stderr = "fatal: some unexpected error";
const error = new Error(
["Command failed: git push --follow-tags --atomic --no-verify origin master", stderr].join("\n")
);
error.stderr = stderr;
throw error;
});
await expect(gitPush("origin", "master", { cwd })).rejects.toThrowError(/some unexpected error/);
});