-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.test.ts
163 lines (129 loc) · 3.92 KB
/
cli.test.ts
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
import { assert, assertEquals } from "./dev-deps.ts";
import { decode, encode, resolve } from "./deps.ts";
const { test, makeTempDir, readFile, writeFile } = Deno;
/**
* testing strategy
* scaffold new git repo in tmp directory for each test
* run versioning commands
*/
async function setup(config = {}) {
const projectRoot = await makeTempDir();
const projectConfig = JSON.stringify({
name: "bump",
version: "1.0.0",
replaceVersion: [
"README.md",
],
deno: {
version: "1.0.0",
},
...config,
});
const readme = "https://denopkg.com/iamnathanj/bump@v1.0.0/cli.ts";
await writeFile(`${projectRoot}/project.json`, encode(projectConfig));
await writeFile(`${projectRoot}/README.md`, encode(readme));
await run("git init");
await run("git config user.name testuser");
await run("git config user.email testuser@test.com");
await run("git config commit.gpgsign false");
await run("git add .");
await run("git commit -m 'initial'");
async function run(args: string) {
const cmd = Deno.run({
cwd: projectRoot,
cmd: args.split(" "),
stdout: "piped",
stderr: "piped",
});
const status = await cmd.status();
const stdout = decode(await cmd.output());
const stderr = decode(await cmd.stderrOutput());
cmd.close();
return { status, stdout, stderr };
}
async function bump(args: string) {
return run(
`deno run --allow-read --allow-write --allow-run ${
resolve(
"./cli.ts",
)
} ${args}`,
);
}
async function cleanup() {
const cmd = Deno.run({
cmd: ["rm", "-rf", projectRoot],
stdout: "piped",
stderr: "piped",
});
await cmd.status();
cmd.stdout?.close();
cmd.stderr?.close();
cmd.close();
}
return { projectRoot, run, bump, cleanup };
}
test("won't run on a dirty working tree", async () => {
const { run, bump, cleanup } = await setup();
await run("touch newfile.txt");
const { stderr } = await bump("major");
assert(stderr.includes("git working tree not clean"));
await cleanup();
});
test("won't recreate an exisiting version", async () => {
const { run, bump, cleanup } = await setup();
await run("git tag v2.0.0");
const { stderr } = await bump("major");
assert(stderr.includes("version already exists"));
await cleanup();
});
test("major", async () => {
const { projectRoot, bump, cleanup } = await setup();
await bump("major");
const config = JSON.parse(
decode(await readFile(`${projectRoot}/project.json`)),
);
assertEquals(config.version, "2.0.0");
await cleanup();
});
test("minor", async () => {
const { projectRoot, bump, cleanup } = await setup();
await bump("minor");
const config = JSON.parse(
decode(await readFile(`${projectRoot}/project.json`)),
);
assertEquals(config.version, "1.1.0");
await cleanup();
});
test("patch", async () => {
const { projectRoot, bump, cleanup } = await setup();
await bump("patch");
const config = JSON.parse(
decode(await readFile(`${projectRoot}/project.json`)),
);
assertEquals(config.version, "1.0.1");
await cleanup();
});
// todo test("explicit version", () => {});
test("replaces version string in files", async () => {
const { projectRoot, bump, cleanup } = await setup();
await bump("major");
const readme = decode(await readFile(`${projectRoot}/README.md`));
assert(!readme.includes("bump@v1.0.0"));
assert(readme.includes("bump@v2.0.0"));
await cleanup();
});
test("creates a new commit", async () => {
const { run, bump, cleanup } = await setup();
await bump("minor");
const { stdout } = await run("git log --oneline");
assert(stdout.includes("release: bump@v1.1.0"));
await cleanup();
});
test("creates a new tag", async () => {
const { run, bump, cleanup } = await setup();
await bump("minor");
const { stdout } = await run("git tag --list v1.1.0");
assert(stdout.includes("v1.1.0"));
await cleanup();
});