ic-mops 2.14.1 → 2.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/bundle/cli.tgz +0 -0
- package/cli.ts +143 -3
- package/commands/build.ts +30 -77
- package/commands/check-stable.ts +4 -0
- package/commands/check.ts +26 -22
- package/commands/deployed.ts +162 -0
- package/commands/generate.ts +201 -0
- package/commands/lint.ts +6 -3
- package/dist/cli.js +75 -3
- package/dist/commands/build.d.ts +6 -0
- package/dist/commands/build.js +26 -52
- package/dist/commands/check-stable.d.ts +2 -0
- package/dist/commands/check-stable.js +2 -2
- package/dist/commands/check.d.ts +2 -0
- package/dist/commands/check.js +20 -21
- package/dist/commands/deployed.d.ts +10 -0
- package/dist/commands/deployed.js +97 -0
- package/dist/commands/generate.d.ts +6 -0
- package/dist/commands/generate.js +124 -0
- package/dist/commands/lint.d.ts +2 -0
- package/dist/commands/lint.js +1 -1
- package/dist/helpers/autofix-motoko.d.ts +2 -0
- package/dist/helpers/autofix-motoko.js +52 -49
- package/dist/helpers/migrations.d.ts +1 -1
- package/dist/helpers/migrations.js +8 -4
- package/dist/helpers/moc-args.d.ts +25 -0
- package/dist/helpers/moc-args.js +58 -0
- package/dist/package.json +6 -6
- package/dist/tests/check-fix-utf8.test.d.ts +1 -0
- package/dist/tests/check-fix-utf8.test.js +38 -0
- package/dist/tests/check.test.js +21 -6
- package/dist/tests/deployed.test.d.ts +1 -0
- package/dist/tests/deployed.test.js +173 -0
- package/dist/tests/generate.test.d.ts +1 -0
- package/dist/tests/generate.test.js +107 -0
- package/dist/tests/helpers.d.ts +8 -0
- package/dist/tests/helpers.js +28 -3
- package/dist/tests/migrate.test.js +5 -19
- package/dist/types.d.ts +3 -0
- package/helpers/autofix-motoko.ts +73 -64
- package/helpers/migrations.ts +8 -3
- package/helpers/moc-args.ts +123 -0
- package/package.json +6 -6
- package/tests/__snapshots__/check-fix-utf8.test.ts.snap +27 -0
- package/tests/__snapshots__/check.test.ts.snap +4 -17
- package/tests/__snapshots__/deployed.test.ts.snap +10 -0
- package/tests/__snapshots__/generate.test.ts.snap +39 -0
- package/tests/check/fix-utf8/mops.toml +5 -0
- package/tests/check/fix-utf8/multibyte.mo +15 -0
- package/tests/check-fix-utf8.test.ts +51 -0
- package/tests/check.test.ts +24 -11
- package/tests/deployed/basic/main.mo +4 -0
- package/tests/deployed/basic/mops.toml +8 -0
- package/tests/deployed/multi/bar.mo +1 -0
- package/tests/deployed/multi/foo.mo +1 -0
- package/tests/deployed/multi/mops.toml +11 -0
- package/tests/deployed.test.ts +249 -0
- package/tests/generate/basic/candid/bar.did +4 -0
- package/tests/generate/basic/mops.toml +12 -0
- package/tests/generate/basic/src/Bar.mo +5 -0
- package/tests/generate/basic/src/Foo.mo +5 -0
- package/tests/generate/error/mops.toml +8 -0
- package/tests/generate/error/src/Broken.mo +5 -0
- package/tests/generate.test.ts +140 -0
- package/tests/helpers.ts +31 -3
- package/tests/migrate.test.ts +7 -22
- package/types.ts +3 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { cli, normalizePaths, useTempFixtures } from "./helpers";
|
|
6
|
+
|
|
7
|
+
describe("deployed", () => {
|
|
8
|
+
const makeTempFixture = useTempFixtures(
|
|
9
|
+
path.join(import.meta.dirname, "deployed"),
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
describe("init", () => {
|
|
13
|
+
test("creates baseline .most and sets [check-stable].path", async () => {
|
|
14
|
+
const cwd = await makeTempFixture("basic");
|
|
15
|
+
const result = await cli(["deployed", "init"], { cwd });
|
|
16
|
+
expect(result.exitCode).toBe(0);
|
|
17
|
+
|
|
18
|
+
const baseline = path.join(cwd, "deployed", "backend.most");
|
|
19
|
+
expect(existsSync(baseline)).toBe(true);
|
|
20
|
+
expect(readFileSync(baseline, "utf-8")).toBe(
|
|
21
|
+
"// Version: 1.0.0\nactor { };\n",
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
const toml = readFileSync(path.join(cwd, "mops.toml"), "utf-8");
|
|
25
|
+
expect(toml).toMatch(/\[canisters\.backend\.check-stable\]/);
|
|
26
|
+
expect(toml).toMatch(/path\s*=\s*"deployed\/backend\.most"/);
|
|
27
|
+
|
|
28
|
+
expect({
|
|
29
|
+
exitCode: result.exitCode,
|
|
30
|
+
stdout: normalizePaths(result.stdout),
|
|
31
|
+
stderr: normalizePaths(result.stderr),
|
|
32
|
+
}).toMatchSnapshot();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("idempotent — second run is a no-op", async () => {
|
|
36
|
+
const cwd = await makeTempFixture("basic");
|
|
37
|
+
await cli(["deployed", "init"], { cwd });
|
|
38
|
+
const tomlBefore = readFileSync(path.join(cwd, "mops.toml"), "utf-8");
|
|
39
|
+
|
|
40
|
+
const result = await cli(["deployed", "init"], { cwd });
|
|
41
|
+
expect(result.exitCode).toBe(0);
|
|
42
|
+
|
|
43
|
+
const tomlAfter = readFileSync(path.join(cwd, "mops.toml"), "utf-8");
|
|
44
|
+
expect(tomlAfter).toBe(tomlBefore);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("warns when [check-stable].path is set elsewhere", async () => {
|
|
48
|
+
const cwd = await makeTempFixture("basic");
|
|
49
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
50
|
+
const toml = readFileSync(tomlPath, "utf-8");
|
|
51
|
+
await writeFile(
|
|
52
|
+
tomlPath,
|
|
53
|
+
toml +
|
|
54
|
+
'\n[canisters.backend.check-stable]\npath = "elsewhere/backend.most"\n',
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const result = await cli(["deployed", "init"], { cwd });
|
|
58
|
+
expect(result.exitCode).toBe(0);
|
|
59
|
+
expect(result.stderr).toMatch(
|
|
60
|
+
/check-stable\]\.path = "elsewhere\/backend\.most"/,
|
|
61
|
+
);
|
|
62
|
+
expect(result.stderr).toMatch(/does not match where `mops deployed`/);
|
|
63
|
+
expect(existsSync(path.join(cwd, "deployed", "backend.most"))).toBe(true);
|
|
64
|
+
|
|
65
|
+
// mops.toml should not have been rewritten
|
|
66
|
+
expect(readFileSync(tomlPath, "utf-8")).toBe(
|
|
67
|
+
toml +
|
|
68
|
+
'\n[canisters.backend.check-stable]\npath = "elsewhere/backend.most"\n',
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("respects [deployed].dir", async () => {
|
|
73
|
+
const cwd = await makeTempFixture("basic");
|
|
74
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
75
|
+
const toml = readFileSync(tomlPath, "utf-8");
|
|
76
|
+
await writeFile(tomlPath, `${toml}\n[deployed]\ndir = "snapshots"\n`);
|
|
77
|
+
|
|
78
|
+
const result = await cli(["deployed", "init"], { cwd });
|
|
79
|
+
expect(result.exitCode).toBe(0);
|
|
80
|
+
expect(existsSync(path.join(cwd, "snapshots", "backend.most"))).toBe(
|
|
81
|
+
true,
|
|
82
|
+
);
|
|
83
|
+
expect(readFileSync(tomlPath, "utf-8")).toMatch(
|
|
84
|
+
/path\s*=\s*"snapshots\/backend\.most"/,
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("init for shorthand canister entry promotes it to a table", async () => {
|
|
89
|
+
const cwd = await makeTempFixture("basic");
|
|
90
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
91
|
+
await writeFile(
|
|
92
|
+
tomlPath,
|
|
93
|
+
'[toolchain]\nmoc = "1.5.0"\n\n[canisters]\nbackend = "main.mo"\n',
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const result = await cli(["deployed", "init"], { cwd });
|
|
97
|
+
expect(result.exitCode).toBe(0);
|
|
98
|
+
|
|
99
|
+
const after = readFileSync(tomlPath, "utf-8");
|
|
100
|
+
expect(after).toMatch(/\[canisters\.backend\]/);
|
|
101
|
+
expect(after).toMatch(/main\s*=\s*"main\.mo"/);
|
|
102
|
+
expect(after).toMatch(/\[canisters\.backend\.check-stable\]/);
|
|
103
|
+
expect(after).toMatch(/path\s*=\s*"deployed\/backend\.most"/);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("errors on unknown canister name", async () => {
|
|
107
|
+
const cwd = await makeTempFixture("basic");
|
|
108
|
+
const result = await cli(["deployed", "init", "ghost"], { cwd });
|
|
109
|
+
expect(result.exitCode).toBe(1);
|
|
110
|
+
expect(result.stderr).toMatch(/not found in mops\.toml/);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("preserves an existing baseline file when wiring [check-stable].path", async () => {
|
|
114
|
+
const cwd = await makeTempFixture("basic");
|
|
115
|
+
const baseline = path.join(cwd, "deployed", "backend.most");
|
|
116
|
+
const customMost =
|
|
117
|
+
"// Version: 1.0.0\nactor {\n stable var x : Nat\n};\n";
|
|
118
|
+
// pre-create the baseline (e.g. real prod snapshot copied in by the user)
|
|
119
|
+
await mkdir(path.join(cwd, "deployed"), { recursive: true });
|
|
120
|
+
await writeFile(baseline, customMost);
|
|
121
|
+
|
|
122
|
+
const result = await cli(["deployed", "init"], { cwd });
|
|
123
|
+
expect(result.exitCode).toBe(0);
|
|
124
|
+
expect(readFileSync(baseline, "utf-8")).toBe(customMost);
|
|
125
|
+
expect(readFileSync(path.join(cwd, "mops.toml"), "utf-8")).toMatch(
|
|
126
|
+
/\[canisters\.backend\.check-stable\][\s\S]*path\s*=\s*"deployed\/backend\.most"/,
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe("post-deploy hook", () => {
|
|
132
|
+
test("copies built .most into deployed/", async () => {
|
|
133
|
+
const cwd = await makeTempFixture("basic");
|
|
134
|
+
const buildResult = await cli(["build"], { cwd });
|
|
135
|
+
expect(buildResult.exitCode).toBe(0);
|
|
136
|
+
|
|
137
|
+
const result = await cli(["deployed"], { cwd });
|
|
138
|
+
expect(result.exitCode).toBe(0);
|
|
139
|
+
|
|
140
|
+
const built = path.join(cwd, ".mops", ".build", "backend.most");
|
|
141
|
+
const promoted = path.join(cwd, "deployed", "backend.most");
|
|
142
|
+
expect(existsSync(promoted)).toBe(true);
|
|
143
|
+
expect(readFileSync(promoted, "utf-8")).toBe(
|
|
144
|
+
readFileSync(built, "utf-8"),
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("errors when source .most is missing", async () => {
|
|
149
|
+
const cwd = await makeTempFixture("basic");
|
|
150
|
+
const result = await cli(["deployed", "backend"], { cwd });
|
|
151
|
+
expect(result.exitCode).toBe(1);
|
|
152
|
+
expect(result.stderr).toMatch(/No built \.most/);
|
|
153
|
+
expect(result.stderr).toMatch(/Run `mops build backend` first/);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("warns when [check-stable].path differs from <dir>/<name>.most", async () => {
|
|
157
|
+
const cwd = await makeTempFixture("basic");
|
|
158
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
159
|
+
const toml = readFileSync(tomlPath, "utf-8");
|
|
160
|
+
await writeFile(
|
|
161
|
+
tomlPath,
|
|
162
|
+
toml +
|
|
163
|
+
'\n[canisters.backend.check-stable]\npath = "elsewhere/backend.most"\n',
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
const buildResult = await cli(["build"], { cwd });
|
|
167
|
+
expect(buildResult.exitCode).toBe(0);
|
|
168
|
+
|
|
169
|
+
const result = await cli(["deployed"], { cwd });
|
|
170
|
+
expect(result.exitCode).toBe(0);
|
|
171
|
+
expect(result.stderr).toMatch(
|
|
172
|
+
/check-stable\]\.path = "elsewhere\/backend\.most"/,
|
|
173
|
+
);
|
|
174
|
+
expect(result.stderr).toMatch(/won't see updates from `mops deployed`/);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("respects [deployed].dir from config", async () => {
|
|
178
|
+
const cwd = await makeTempFixture("basic");
|
|
179
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
180
|
+
const toml = readFileSync(tomlPath, "utf-8");
|
|
181
|
+
await writeFile(tomlPath, `${toml}\n[deployed]\ndir = "snapshots"\n`);
|
|
182
|
+
|
|
183
|
+
await cli(["build"], { cwd });
|
|
184
|
+
const result = await cli(["deployed"], { cwd });
|
|
185
|
+
expect(result.exitCode).toBe(0);
|
|
186
|
+
expect(existsSync(path.join(cwd, "snapshots", "backend.most"))).toBe(
|
|
187
|
+
true,
|
|
188
|
+
);
|
|
189
|
+
expect(existsSync(path.join(cwd, "deployed", "backend.most"))).toBe(
|
|
190
|
+
false,
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("--build-dir and --dir overrides", async () => {
|
|
195
|
+
const cwd = await makeTempFixture("basic");
|
|
196
|
+
const customOut = "custom-build";
|
|
197
|
+
const customDir = "snapshots";
|
|
198
|
+
|
|
199
|
+
const buildResult = await cli(
|
|
200
|
+
["build", "backend", "--output", customOut],
|
|
201
|
+
{ cwd },
|
|
202
|
+
);
|
|
203
|
+
expect(buildResult.exitCode).toBe(0);
|
|
204
|
+
|
|
205
|
+
const result = await cli(
|
|
206
|
+
["deployed", "--build-dir", customOut, "--dir", customDir],
|
|
207
|
+
{ cwd },
|
|
208
|
+
);
|
|
209
|
+
expect(result.exitCode).toBe(0);
|
|
210
|
+
expect(existsSync(path.join(cwd, customDir, "backend.most"))).toBe(true);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test("with no canister names promotes all canisters", async () => {
|
|
214
|
+
const cwd = await makeTempFixture("multi");
|
|
215
|
+
const buildResult = await cli(["build"], { cwd });
|
|
216
|
+
expect(buildResult.exitCode).toBe(0);
|
|
217
|
+
|
|
218
|
+
const result = await cli(["deployed"], { cwd });
|
|
219
|
+
expect(result.exitCode).toBe(0);
|
|
220
|
+
expect(existsSync(path.join(cwd, "deployed", "foo.most"))).toBe(true);
|
|
221
|
+
expect(existsSync(path.join(cwd, "deployed", "bar.most"))).toBe(true);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("named canister filters to that canister only", async () => {
|
|
225
|
+
const cwd = await makeTempFixture("multi");
|
|
226
|
+
await cli(["build"], { cwd });
|
|
227
|
+
|
|
228
|
+
const result = await cli(["deployed", "foo"], { cwd });
|
|
229
|
+
expect(result.exitCode).toBe(0);
|
|
230
|
+
expect(existsSync(path.join(cwd, "deployed", "foo.most"))).toBe(true);
|
|
231
|
+
expect(existsSync(path.join(cwd, "deployed", "bar.most"))).toBe(false);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
describe("end-to-end with check-stable", () => {
|
|
236
|
+
test("init + build + deployed wires the check-stable baseline", async () => {
|
|
237
|
+
const cwd = await makeTempFixture("basic");
|
|
238
|
+
|
|
239
|
+
await cli(["deployed", "init"], { cwd });
|
|
240
|
+
await cli(["build"], { cwd });
|
|
241
|
+
const promoteResult = await cli(["deployed"], { cwd });
|
|
242
|
+
expect(promoteResult.exitCode).toBe(0);
|
|
243
|
+
|
|
244
|
+
const checkResult = await cli(["check-stable"], { cwd });
|
|
245
|
+
expect(checkResult.exitCode).toBe(0);
|
|
246
|
+
expect(checkResult.stdout).toMatch(/Stable compatibility check passed/);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { afterEach, describe, expect, jest, test } from "@jest/globals";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { cp, rm } from "node:fs/promises";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { cli, cliSnapshot } from "./helpers";
|
|
6
|
+
|
|
7
|
+
const fixturesDir = path.join(import.meta.dirname, "generate");
|
|
8
|
+
|
|
9
|
+
describe("generate candid", () => {
|
|
10
|
+
jest.setTimeout(120_000);
|
|
11
|
+
|
|
12
|
+
const tempDirs: string[] = [];
|
|
13
|
+
|
|
14
|
+
async function makeTempFixture(fixture: string): Promise<string> {
|
|
15
|
+
const src = path.join(fixturesDir, fixture);
|
|
16
|
+
const dest = path.join(fixturesDir, `_tmp_${fixture}_${Date.now()}`);
|
|
17
|
+
await cp(src, dest, { recursive: true });
|
|
18
|
+
tempDirs.push(dest);
|
|
19
|
+
return dest;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
afterEach(async () => {
|
|
23
|
+
for (const dir of tempDirs) {
|
|
24
|
+
await rm(dir, { recursive: true, force: true });
|
|
25
|
+
}
|
|
26
|
+
tempDirs.length = 0;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("default path: writes <main-dir>/<name>.did and sets [canisters.<name>].candid", async () => {
|
|
30
|
+
const cwd = await makeTempFixture("basic");
|
|
31
|
+
const tomlBefore = readFileSync(path.join(cwd, "mops.toml"), "utf-8");
|
|
32
|
+
expect(tomlBefore).not.toMatch(/"src\/foo\.did"/);
|
|
33
|
+
|
|
34
|
+
await cliSnapshot(["generate", "candid", "foo"], { cwd }, 0);
|
|
35
|
+
|
|
36
|
+
const did = readFileSync(path.join(cwd, "src/foo.did"), "utf-8");
|
|
37
|
+
expect(did).toMatchSnapshot("src/foo.did");
|
|
38
|
+
|
|
39
|
+
const tomlAfter = readFileSync(path.join(cwd, "mops.toml"), "utf-8");
|
|
40
|
+
expect(tomlAfter).toMatch(/candid\s*=\s*"src\/foo\.did"/);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("configured path: overwrites in place and does not touch mops.toml", async () => {
|
|
44
|
+
const cwd = await makeTempFixture("basic");
|
|
45
|
+
const tomlBefore = readFileSync(path.join(cwd, "mops.toml"), "utf-8");
|
|
46
|
+
const didBefore = readFileSync(path.join(cwd, "candid/bar.did"), "utf-8");
|
|
47
|
+
expect(didBefore).toMatch(/Hand-curated/);
|
|
48
|
+
|
|
49
|
+
await cliSnapshot(["generate", "candid", "bar"], { cwd }, 0);
|
|
50
|
+
|
|
51
|
+
const didAfter = readFileSync(path.join(cwd, "candid/bar.did"), "utf-8");
|
|
52
|
+
expect(didAfter).not.toMatch(/Hand-curated/);
|
|
53
|
+
expect(didAfter).toMatch(/greet/);
|
|
54
|
+
expect(readFileSync(path.join(cwd, "mops.toml"), "utf-8")).toBe(tomlBefore);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("no canister names: processes all canisters", async () => {
|
|
58
|
+
const cwd = await makeTempFixture("basic");
|
|
59
|
+
await cliSnapshot(["generate", "candid"], { cwd }, 0);
|
|
60
|
+
|
|
61
|
+
expect(existsSync(path.join(cwd, "src/foo.did"))).toBe(true);
|
|
62
|
+
const barDid = readFileSync(path.join(cwd, "candid/bar.did"), "utf-8");
|
|
63
|
+
expect(barDid).toMatch(/greet/);
|
|
64
|
+
expect(barDid).not.toMatch(/Hand-curated/);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("--output writes to the given path and does not touch mops.toml", async () => {
|
|
68
|
+
const cwd = await makeTempFixture("basic");
|
|
69
|
+
const outPath = path.join(cwd, "out/custom.did");
|
|
70
|
+
const tomlBefore = readFileSync(path.join(cwd, "mops.toml"), "utf-8");
|
|
71
|
+
|
|
72
|
+
const result = await cli(["generate", "candid", "foo", "-o", outPath], {
|
|
73
|
+
cwd,
|
|
74
|
+
});
|
|
75
|
+
expect(result.exitCode).toBe(0);
|
|
76
|
+
expect(existsSync(outPath)).toBe(true);
|
|
77
|
+
expect(readFileSync(outPath, "utf-8")).toMatch(/call/);
|
|
78
|
+
expect(existsSync(path.join(cwd, "src/foo.did"))).toBe(false);
|
|
79
|
+
expect(readFileSync(path.join(cwd, "mops.toml"), "utf-8")).toBe(tomlBefore);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("--output rejected with multiple canisters", async () => {
|
|
83
|
+
const cwd = await makeTempFixture("basic");
|
|
84
|
+
const result = await cli(
|
|
85
|
+
["generate", "candid", "foo", "bar", "-o", "x.did"],
|
|
86
|
+
{ cwd },
|
|
87
|
+
);
|
|
88
|
+
expect(result.exitCode).toBe(1);
|
|
89
|
+
expect(result.stderr).toMatch(/single canister/i);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("unknown canister name errors", async () => {
|
|
93
|
+
const cwd = await makeTempFixture("basic");
|
|
94
|
+
const result = await cli(["generate", "candid", "nope"], { cwd });
|
|
95
|
+
expect(result.exitCode).toBe(1);
|
|
96
|
+
expect(result.stderr).toMatch(/not found in mops\.toml/i);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("moc failure: leaves destination and mops.toml untouched", async () => {
|
|
100
|
+
const cwd = await makeTempFixture("error");
|
|
101
|
+
const tomlBefore = readFileSync(path.join(cwd, "mops.toml"), "utf-8");
|
|
102
|
+
|
|
103
|
+
const result = await cli(["generate", "candid", "broken"], { cwd });
|
|
104
|
+
expect(result.exitCode).toBe(1);
|
|
105
|
+
expect(result.stderr).toMatch(/Failed to generate Candid/);
|
|
106
|
+
expect(existsSync(path.join(cwd, "src/broken.did"))).toBe(false);
|
|
107
|
+
expect(readFileSync(path.join(cwd, "mops.toml"), "utf-8")).toBe(tomlBefore);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("rejects destination inside .mops/", async () => {
|
|
111
|
+
const cwd = await makeTempFixture("basic");
|
|
112
|
+
const result = await cli(
|
|
113
|
+
["generate", "candid", "foo", "-o", ".mops/foo.did"],
|
|
114
|
+
{ cwd },
|
|
115
|
+
);
|
|
116
|
+
expect(result.exitCode).toBe(1);
|
|
117
|
+
expect(result.stderr).toMatch(/\.mops\//);
|
|
118
|
+
expect(result.stderr).toMatch(/Refusing/i);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("creates file when [canisters.<name>].candid points to a missing path", async () => {
|
|
122
|
+
const cwd = await makeTempFixture("basic");
|
|
123
|
+
// bar's candid path points to candid/bar.did — delete it first
|
|
124
|
+
await rm(path.join(cwd, "candid/bar.did"));
|
|
125
|
+
|
|
126
|
+
const result = await cli(["generate", "candid", "bar"], { cwd });
|
|
127
|
+
expect(result.exitCode).toBe(0);
|
|
128
|
+
expect(existsSync(path.join(cwd, "candid/bar.did"))).toBe(true);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("--output collision with another canister's candid prints a warning", async () => {
|
|
132
|
+
const cwd = await makeTempFixture("basic");
|
|
133
|
+
const result = await cli(
|
|
134
|
+
["generate", "candid", "foo", "-o", "candid/bar.did"],
|
|
135
|
+
{ cwd },
|
|
136
|
+
);
|
|
137
|
+
expect(result.exitCode).toBe(0);
|
|
138
|
+
expect(result.stderr).toMatch(/collides with \[canisters\.bar\]\.candid/);
|
|
139
|
+
});
|
|
140
|
+
});
|
package/tests/helpers.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { expect } from "@jest/globals";
|
|
1
|
+
import { afterEach, expect } from "@jest/globals";
|
|
2
2
|
import { execa } from "execa";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { cp, rm } from "node:fs/promises";
|
|
4
|
+
import path, { dirname } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
5
6
|
|
|
6
7
|
export interface CliOptions {
|
|
7
8
|
cwd?: string;
|
|
@@ -69,3 +70,30 @@ export const cliSnapshot = async (
|
|
|
69
70
|
}).toMatchSnapshot();
|
|
70
71
|
return result;
|
|
71
72
|
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Set up `afterEach` cleanup and return a `makeTempFixture(name)` function
|
|
76
|
+
* that copies a fixture subtree into a unique scratch dir under `fixturesDir`.
|
|
77
|
+
*
|
|
78
|
+
* Use in tests that mutate fixture files (toml edits, baseline file creation, etc.)
|
|
79
|
+
* so they don't pollute the source-controlled fixture.
|
|
80
|
+
*/
|
|
81
|
+
export function useTempFixtures(fixturesDir: string) {
|
|
82
|
+
const tempDirs: string[] = [];
|
|
83
|
+
|
|
84
|
+
afterEach(async () => {
|
|
85
|
+
for (const dir of tempDirs) {
|
|
86
|
+
await rm(dir, { recursive: true, force: true });
|
|
87
|
+
}
|
|
88
|
+
tempDirs.length = 0;
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return async (fixture: string): Promise<string> => {
|
|
92
|
+
const src = path.join(fixturesDir, fixture);
|
|
93
|
+
const suffix = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
94
|
+
const dest = path.join(fixturesDir, `_tmp_${fixture}_${suffix}`);
|
|
95
|
+
await cp(src, dest, { recursive: true });
|
|
96
|
+
tempDirs.push(dest);
|
|
97
|
+
return dest;
|
|
98
|
+
};
|
|
99
|
+
}
|
package/tests/migrate.test.ts
CHANGED
|
@@ -1,24 +1,16 @@
|
|
|
1
|
-
import { describe, expect, test
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
2
|
import { readdirSync, readFileSync } from "node:fs";
|
|
3
|
-
import {
|
|
4
|
-
import path from "path";
|
|
5
|
-
import { cli, cliSnapshot, normalizePaths } from "./helpers";
|
|
3
|
+
import { rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { cli, cliSnapshot, normalizePaths, useTempFixtures } from "./helpers";
|
|
6
6
|
|
|
7
7
|
const normalizeTimestamp = (text: string) =>
|
|
8
8
|
text.replace(/\d{8}_\d{6}/g, "<TIMESTAMP>");
|
|
9
9
|
|
|
10
|
-
const fixturesDir = path.join(import.meta.dirname, "migrate");
|
|
11
|
-
|
|
12
10
|
describe("migrate", () => {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const src = path.join(fixturesDir, fixture);
|
|
17
|
-
const dest = path.join(fixturesDir, `_tmp_${fixture}_${Date.now()}`);
|
|
18
|
-
await cp(src, dest, { recursive: true });
|
|
19
|
-
tempDirs.push(dest);
|
|
20
|
-
return dest;
|
|
21
|
-
}
|
|
11
|
+
const makeTempFixture = useTempFixtures(
|
|
12
|
+
path.join(import.meta.dirname, "migrate"),
|
|
13
|
+
);
|
|
22
14
|
|
|
23
15
|
async function patchMigrations(cwd: string, extra: string): Promise<void> {
|
|
24
16
|
const tomlPath = path.join(cwd, "mops.toml");
|
|
@@ -32,13 +24,6 @@ describe("migrate", () => {
|
|
|
32
24
|
);
|
|
33
25
|
}
|
|
34
26
|
|
|
35
|
-
afterEach(async () => {
|
|
36
|
-
for (const dir of tempDirs) {
|
|
37
|
-
await rm(dir, { recursive: true, force: true });
|
|
38
|
-
}
|
|
39
|
-
tempDirs.length = 0;
|
|
40
|
-
});
|
|
41
|
-
|
|
42
27
|
describe("migrate new", () => {
|
|
43
28
|
test("creates a migration file with timestamp and template", async () => {
|
|
44
29
|
const cwd = await makeTempFixture("basic");
|