-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathread.test.ts
More file actions
200 lines (164 loc) Β· 6.67 KB
/
read.test.ts
File metadata and controls
200 lines (164 loc) Β· 6.67 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { test, expect } from "vitest";
import fs from "fs/promises";
import fsExtra from "fs-extra";
import path from "node:path";
import { git } from "@commitlint/test";
import { x } from "tinyexec";
import tmp from "tmp";
import read from "./read.js";
test("get edit commit message specified by the `edit` flag", async () => {
const cwd: string = await git.bootstrap();
await fs.writeFile(path.join(cwd, "commit-msg-file"), "foo");
const expected = ["foo\n"];
const actual = await read({ edit: "commit-msg-file", cwd });
expect(actual).toEqual(expected);
});
test("get edit commit message from git root", async () => {
const cwd: string = await git.bootstrap();
await fs.writeFile(path.join(cwd, "alpha.txt"), "alpha");
await x("git", ["add", "."], { nodeOptions: { cwd } });
await x("git", ["commit", "-m", "alpha"], { nodeOptions: { cwd } });
const expected = ["alpha\n\n"];
const actual = await read({ edit: true, cwd });
expect(actual).toEqual(expected);
});
test("get history commit messages", async () => {
const cwd: string = await git.bootstrap();
await fs.writeFile(path.join(cwd, "alpha.txt"), "alpha");
await x("git", ["add", "alpha.txt"], { nodeOptions: { cwd } });
await x("git", ["commit", "-m", "alpha"], { nodeOptions: { cwd } });
await x("git", ["rm", "alpha.txt"], { nodeOptions: { cwd } });
await x("git", ["commit", "-m", "remove alpha"], { nodeOptions: { cwd } });
const expected = ["remove alpha\n\n", "alpha\n\n"];
const actual = await read({ cwd });
expect(actual).toEqual(expected);
});
test("get edit commit message from git subdirectory", async () => {
const cwd: string = await git.bootstrap();
await fs.mkdir(path.join(cwd, "beta"));
await fs.writeFile(path.join(cwd, "beta/beta.txt"), "beta");
await x("git", ["add", "."], { nodeOptions: { cwd } });
await x("git", ["commit", "-m", "beta"], { nodeOptions: { cwd } });
const expected = ["beta\n\n"];
const actual = await read({ edit: true, cwd });
expect(actual).toEqual(expected);
});
test("get edit commit message while skipping first commit", async () => {
const cwd: string = await git.bootstrap();
await fs.mkdir(path.join(cwd, "beta"));
await fs.writeFile(path.join(cwd, "beta/beta.txt"), "beta");
await fs.writeFile(path.join(cwd, "alpha.txt"), "alpha");
await x("git", ["add", "alpha.txt"], { nodeOptions: { cwd } });
await x("git", ["commit", "-m", "alpha"], { nodeOptions: { cwd } });
await fs.writeFile(path.join(cwd, "beta.txt"), "beta");
await x("git", ["add", "beta.txt"], { nodeOptions: { cwd } });
await x("git", ["commit", "-m", "beta"], { nodeOptions: { cwd } });
await fs.writeFile(path.join(cwd, "gamma.txt"), "gamma");
await x("git", ["add", "gamma.txt"], { nodeOptions: { cwd } });
await x("git", ["commit", "-m", "gamma"], { nodeOptions: { cwd } });
const expected = ["beta\n\n"];
const actual = await read({ from: "HEAD~2", cwd, gitLogArgs: "--skip 1" });
expect(actual).toEqual(expected);
});
test("should only read the last commit", async () => {
const cwd: string = await git.bootstrap();
await x("git", ["commit", "--allow-empty", "-m", "commit Z"], {
nodeOptions: { cwd },
});
await x("git", ["commit", "--allow-empty", "-m", "commit Y"], {
nodeOptions: { cwd },
});
await x("git", ["commit", "--allow-empty", "-m", "commit X"], {
nodeOptions: { cwd },
});
const result = await read({ cwd, last: true });
expect(result).toEqual(["commit X"]);
});
test("should read commits from the last annotated tag", async () => {
const cwd: string = await git.bootstrap();
await x("git", ["commit", "--allow-empty", "-m", "chore: release v1.0.0"], {
nodeOptions: { cwd },
});
await x("git", ["tag", "v1.0.0", "--annotate", "-m", "v1.0.0"], {
nodeOptions: { cwd },
});
await x("git", ["commit", "--allow-empty", "-m", "commit 1"], {
nodeOptions: { cwd },
});
await x("git", ["commit", "--allow-empty", "-m", "commit 2"], {
nodeOptions: { cwd },
});
const result = await read({ cwd, fromLastTag: true });
expect(result).toEqual(["commit 2\n\n", "commit 1\n\n"]);
});
test("should read commits from the last lightweight tag", async () => {
const cwd: string = await git.bootstrap();
await x(
"git",
["commit", "--allow-empty", "-m", "chore: release v9.9.9-alpha.1"],
{ nodeOptions: { cwd } },
);
await x("git", ["tag", "v9.9.9-alpha.1"], { nodeOptions: { cwd } });
await x("git", ["commit", "--allow-empty", "-m", "commit A"], {
nodeOptions: { cwd },
});
await x("git", ["commit", "--allow-empty", "-m", "commit B"], {
nodeOptions: { cwd },
});
const result = await read({ cwd, fromLastTag: true });
expect(result).toEqual(["commit B\n\n", "commit A\n\n"]);
});
test("should not read any commits when there are no tags", async () => {
const cwd: string = await git.bootstrap();
await x("git", ["commit", "--allow-empty", "-m", "commit 7"], {
nodeOptions: { cwd },
});
await x("git", ["commit", "--allow-empty", "-m", "commit 8"], {
nodeOptions: { cwd },
});
await x("git", ["commit", "--allow-empty", "-m", "commit 9"], {
nodeOptions: { cwd },
});
const result = await read({ cwd, fromLastTag: true });
expect(result).toHaveLength(0);
});
test("get edit commit message from git worktree", async () => {
const tmpDir = tmp.dirSync({ keep: false, unsafeCleanup: true });
const mainRepoDir = path.join(tmpDir.name, "main");
const worktreeDir = path.join(tmpDir.name, "worktree");
// Initialize main repo
await fsExtra.mkdirp(mainRepoDir);
await x("git", ["init"], { nodeOptions: { cwd: mainRepoDir } });
await x("git", ["config", "user.email", "[email protected]"], {
nodeOptions: { cwd: mainRepoDir },
});
await x("git", ["config", "user.name", "test"], {
nodeOptions: { cwd: mainRepoDir },
});
await x("git", ["config", "commit.gpgsign", "false"], {
nodeOptions: { cwd: mainRepoDir },
});
// Create initial commit in main repo
await fs.writeFile(path.join(mainRepoDir, "file.txt"), "content");
await x("git", ["add", "."], { nodeOptions: { cwd: mainRepoDir } });
await x("git", ["commit", "-m", "initial"], {
nodeOptions: { cwd: mainRepoDir },
});
// Create a branch and worktree
await x("git", ["branch", "worktree-branch"], {
nodeOptions: { cwd: mainRepoDir },
});
await x("git", ["worktree", "add", worktreeDir, "worktree-branch"], {
nodeOptions: { cwd: mainRepoDir },
});
// Make a commit in the worktree
await fs.writeFile(path.join(worktreeDir, "worktree-file.txt"), "worktree");
await x("git", ["add", "."], { nodeOptions: { cwd: worktreeDir } });
await x("git", ["commit", "-m", "worktree commit"], {
nodeOptions: { cwd: worktreeDir },
});
// Read the edit commit message from the worktree
const expected = ["worktree commit\n\n"];
const actual = await read({ edit: true, cwd: worktreeDir });
expect(actual).toEqual(expected);
});