-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcli.test.ts
More file actions
216 lines (198 loc) · 8.35 KB
/
cli.test.ts
File metadata and controls
216 lines (198 loc) · 8.35 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { describe, expect, jest, test } from "@jest/globals";
import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import path from "path";
import { cli, normalizePaths } from "./helpers";
describe("cli", () => {
test("--version", async () => {
expect((await cli(["--version"])).stdout).toMatch(/CLI \d+\.\d+\.\d+/);
});
test("--help", async () => {
expect((await cli(["--help"])).stdout).toMatch(/^Usage: mops/m);
});
});
describe("install", () => {
jest.setTimeout(120_000);
test("creates mops.lock automatically on first install", async () => {
const cwd = path.join(import.meta.dirname, "install/success");
const lockFile = path.join(cwd, "mops.lock");
rmSync(lockFile, { force: true });
try {
// Unset CI so checkIntegrity uses the local default ("update")
const result = await cli(["install"], { cwd, env: { CI: undefined } });
expect(result.exitCode).toBe(0);
expect(existsSync(lockFile)).toBe(true);
expect(result.stdout).toMatch(/mops\.lock created/);
} finally {
rmSync(lockFile, { force: true });
rmSync(path.join(cwd, ".mops"), { recursive: true, force: true });
}
});
test("does not print 'mops.lock created' on subsequent installs", async () => {
const cwd = path.join(import.meta.dirname, "install/success");
const lockFile = path.join(cwd, "mops.lock");
rmSync(lockFile, { force: true });
try {
// Unset CI so checkIntegrity uses the local default ("update")
const first = await cli(["install"], { cwd, env: { CI: undefined } });
expect(first.exitCode).toBe(0);
expect(first.stdout).toMatch(/mops\.lock created/);
const result = await cli(["install"], { cwd, env: { CI: undefined } });
expect(result.exitCode).toBe(0);
expect(existsSync(lockFile)).toBe(true);
expect(result.stdout).not.toMatch(/mops\.lock created/);
} finally {
rmSync(lockFile, { force: true });
rmSync(path.join(cwd, ".mops"), { recursive: true, force: true });
}
});
test("does not create mops.lock when --lock ignore is passed", async () => {
const cwd = path.join(import.meta.dirname, "install/success");
const lockFile = path.join(cwd, "mops.lock");
rmSync(lockFile, { force: true });
try {
// Unset CI for consistency; --lock ignore bypasses auto-detection regardless
const result = await cli(["install", "--lock", "ignore"], {
cwd,
env: { CI: undefined },
});
expect(result.exitCode).toBe(0);
expect(existsSync(lockFile)).toBe(false);
} finally {
rmSync(lockFile, { force: true });
rmSync(path.join(cwd, ".mops"), { recursive: true, force: true });
}
});
// mops add/remove/update/sync are not separately tested here because they
// all route through the same checkIntegrity code path tested above.
// Regression: aliases pinning the same package@version (e.g. `core` and
// `core@1` both at "1.0.0") inflated the resolved-packageIds count and
// tripped the lockfile integrity check with a spurious
// "Mismatched number of resolved packages" error. See issue #506.
test("integrity check passes when aliases resolve to the same package@version", async () => {
const cwd = path.join(import.meta.dirname, "install/aliases");
const lockFile = path.join(cwd, "mops.lock");
rmSync(lockFile, { force: true });
try {
const result = await cli(["install"], { cwd, env: { CI: undefined } });
expect(result.stderr).not.toMatch(
/Mismatched number of resolved packages/,
);
expect(result.exitCode).toBe(0);
expect(existsSync(lockFile)).toBe(true);
} finally {
rmSync(lockFile, { force: true });
rmSync(path.join(cwd, ".mops"), { recursive: true, force: true });
}
});
// Regression: `install --lock update` used to early-return if mops.toml's
// deps hash was unchanged, even when the lockfile's per-file hashes were
// stale/corrupt. The subsequent checkLockFile would then fail and exit 1,
// so `--lock update` could never recover a broken lock — the only escape
// was `rm mops.lock`. See issue #514.
test("--lock update rewrites a lockfile with a corrupt file hash", async () => {
const cwd = path.join(import.meta.dirname, "install/success");
const lockFile = path.join(cwd, "mops.lock");
rmSync(lockFile, { force: true });
try {
const first = await cli(["install"], { cwd, env: { CI: undefined } });
expect(first.exitCode).toBe(0);
expect(existsSync(lockFile)).toBe(true);
const bad =
"BAD0000000000000000000000000000000000000000000000000000000000BAD";
const original = readFileSync(lockFile, "utf8");
const corrupted = original.replace(
/"core@1\.0\.0\/mops\.toml":\s*"[0-9a-f]{64}"/,
`"core@1.0.0/mops.toml": "${bad}"`,
);
expect(corrupted).not.toBe(original);
writeFileSync(lockFile, corrupted);
const result = await cli(["install", "--lock", "update"], {
cwd,
env: { CI: undefined },
});
expect(result.exitCode).toBe(0);
expect(readFileSync(lockFile, "utf8")).not.toContain(bad);
} finally {
rmSync(lockFile, { force: true });
rmSync(path.join(cwd, ".mops"), { recursive: true, force: true });
}
});
});
// `mops update` and `mops outdated` default to caret-bound resolution: stay
// within `0.x.y` (or `1.x.y`) and never cross majors. Fixture pins:
// base = "0.14.5" -> caret bumps within 0.14.x; --major jumps past it
// core = "1.0.0" -> caret stays put (no 1.x.y > 1.0.0); --major jumps to 2.x
describe("update / outdated bounds", () => {
jest.setTimeout(120_000);
const cwd = path.join(import.meta.dirname, "install/update-bound");
const tomlFile = path.join(cwd, "mops.toml");
const original = readFileSync(tomlFile, "utf8");
const cleanup = () => {
rmSync(path.join(cwd, "mops.lock"), { force: true });
rmSync(path.join(cwd, ".mops"), { recursive: true, force: true });
writeFileSync(tomlFile, original);
};
const baseVersion = (toml: string) =>
toml.match(/base = "(0\.\d+\.\d+)"/)?.[1];
const coreMajor = (toml: string) =>
parseInt(toml.match(/core = "(\d+)\./)?.[1] ?? "0");
test("mops update stays within the caret bound by default", async () => {
cleanup();
try {
await cli(["install"], { cwd, env: { CI: undefined } });
const result = await cli(["update"], { cwd, env: { CI: undefined } });
expect(result.exitCode).toBe(0);
const after = readFileSync(tomlFile, "utf8");
// base (pre-1.0): bumped within 0.14.x (patch bumps allowed)
expect(baseVersion(after)).toMatch(/^0\.14\./);
expect(baseVersion(after)).not.toBe("0.14.5");
// core (1.x): no 1.x.y > 1.0.0 published, so no bump across majors
expect(coreMajor(after)).toBe(1);
} finally {
cleanup();
}
});
test("mops update --major crosses the caret bound", async () => {
cleanup();
try {
await cli(["install"], { cwd, env: { CI: undefined } });
const result = await cli(["update", "--major"], {
cwd,
env: { CI: undefined },
});
expect(result.exitCode).toBe(0);
const after = readFileSync(tomlFile, "utf8");
// base: jumps past 0.14.x (next minor or major)
const baseMinor = parseInt(after.match(/base = "0\.(\d+)\./)?.[1] ?? "0");
expect(baseMinor).toBeGreaterThanOrEqual(15);
// core: jumps to 2.x or later
expect(coreMajor(after)).toBeGreaterThanOrEqual(2);
} finally {
cleanup();
}
});
test("mops outdated honors --major flag", async () => {
cleanup();
try {
await cli(["install"], { cwd, env: { CI: undefined } });
const caret = normalizePaths(
(await cli(["outdated"], { cwd, env: { CI: undefined } })).stdout,
);
const major = normalizePaths(
(await cli(["outdated", "--major"], { cwd, env: { CI: undefined } }))
.stdout,
);
// caret-bound: base bumps within 0.14.x; core (if reported) stays in 1.x
expect(caret).toMatch(/base 0\.14\.5 -> 0\.14\./);
const caretCore = caret.match(/core 1\.0\.0 -> (\d+)\./)?.[1];
if (caretCore) {
expect(parseInt(caretCore)).toBe(1);
}
// --major: both bump across their major bounds
expect(major).toMatch(/base 0\.14\.5 -> 0\.(1[5-9]|[2-9]\d)/);
expect(major).toMatch(/core 1\.0\.0 -> [2-9]/);
} finally {
cleanup();
}
});
});