ic-mops 2.9.0 → 2.11.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 +13 -0
- package/bundle/cli.tgz +0 -0
- package/cli.ts +34 -10
- package/commands/build.ts +16 -22
- package/commands/check-stable.ts +141 -17
- package/commands/check.ts +195 -101
- package/commands/migrate.ts +165 -0
- package/declarations/main/main.did +38 -0
- package/declarations/main/main.did.d.ts +36 -0
- package/declarations/main/main.did.js +36 -0
- package/dist/cli.js +28 -10
- package/dist/commands/build.js +7 -13
- package/dist/commands/check-stable.d.ts +8 -1
- package/dist/commands/check-stable.js +101 -19
- package/dist/commands/check.d.ts +1 -1
- package/dist/commands/check.js +136 -78
- package/dist/commands/migrate.d.ts +2 -0
- package/dist/commands/migrate.js +104 -0
- package/dist/declarations/main/main.did +38 -0
- package/dist/declarations/main/main.did.d.ts +36 -0
- package/dist/declarations/main/main.did.js +36 -0
- package/dist/helpers/migrations.d.ts +10 -0
- package/dist/helpers/migrations.js +109 -0
- package/dist/helpers/resolve-canisters.d.ts +3 -1
- package/dist/helpers/resolve-canisters.js +34 -5
- package/dist/package.json +1 -1
- package/dist/tests/check-stable.test.js +18 -0
- package/dist/tests/check.test.js +23 -5
- package/dist/tests/migrate.test.d.ts +1 -0
- package/dist/tests/migrate.test.js +160 -0
- package/dist/types.d.ts +7 -0
- package/helpers/migrations.ts +166 -0
- package/helpers/resolve-canisters.ts +53 -5
- package/package.json +1 -1
- package/tests/__snapshots__/check.test.ts.snap +2 -2
- package/tests/__snapshots__/migrate.test.ts.snap +119 -0
- package/tests/check/canisters-canister-args/Warning.mo +5 -0
- package/tests/check/canisters-canister-args/mops.toml +9 -0
- package/tests/check-stable/canister-args/migrations/20250101_000000_Init.mo +8 -0
- package/tests/check-stable/canister-args/migrations/20250201_000000_AddField.mo +9 -0
- package/tests/check-stable/canister-args/mops.toml +9 -0
- package/tests/check-stable/canister-args/old.most +8 -0
- package/tests/check-stable/canister-args/src/main.mo +11 -0
- package/tests/check-stable.test.ts +21 -0
- package/tests/check.test.ts +26 -5
- package/tests/migrate/basic/deployed.most +12 -0
- package/tests/migrate/basic/migrations/20250101_000000_Init.mo +5 -0
- package/tests/migrate/basic/migrations/20250201_000000_AddName.mo +5 -0
- package/tests/migrate/basic/migrations/20250301_000000_AddEmail.mo +9 -0
- package/tests/migrate/basic/mops.toml +15 -0
- package/tests/migrate/basic/next-migration/.gitkeep +0 -0
- package/tests/migrate/basic/src/main.mo +11 -0
- package/tests/migrate/with-next/deployed.most +12 -0
- package/tests/migrate/with-next/migrations/20250101_000000_Init.mo +5 -0
- package/tests/migrate/with-next/migrations/20250201_000000_AddName.mo +5 -0
- package/tests/migrate/with-next/migrations/20250301_000000_AddEmail.mo +9 -0
- package/tests/migrate/with-next/mops.toml +15 -0
- package/tests/migrate/with-next/next-migration/20250401_000000_RenameId.mo +9 -0
- package/tests/migrate/with-next/src/main.mo +11 -0
- package/tests/migrate.test.ts +228 -0
- package/types.ts +8 -0
package/tests/check.test.ts
CHANGED
|
@@ -49,12 +49,19 @@ describe("check", () => {
|
|
|
49
49
|
await cliSnapshot(["check", "Warning.mo"], { cwd }, 1);
|
|
50
50
|
});
|
|
51
51
|
|
|
52
|
-
test("no args
|
|
52
|
+
test("no args checks all canisters", async () => {
|
|
53
53
|
const cwd = path.join(import.meta.dirname, "check/canisters");
|
|
54
54
|
await cliSnapshot(["check"], { cwd }, 0);
|
|
55
55
|
});
|
|
56
56
|
|
|
57
|
-
test("canister
|
|
57
|
+
test("canister name filters to specific canister", async () => {
|
|
58
|
+
const cwd = path.join(import.meta.dirname, "check/canisters");
|
|
59
|
+
const result = await cli(["check", "backend"], { cwd });
|
|
60
|
+
expect(result.exitCode).toBe(0);
|
|
61
|
+
expect(result.stdout).toMatch(/✓ backend/);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("canister resolved relative to config root when run from subdirectory", async () => {
|
|
58
65
|
const fixtureRoot = path.join(
|
|
59
66
|
import.meta.dirname,
|
|
60
67
|
"check/canisters-subdir",
|
|
@@ -65,21 +72,35 @@ describe("check", () => {
|
|
|
65
72
|
expect(result.stdout).toMatch(/✓/);
|
|
66
73
|
});
|
|
67
74
|
|
|
68
|
-
test("[moc] args applied
|
|
75
|
+
test("[moc] args applied to canister check", async () => {
|
|
69
76
|
const cwd = path.join(import.meta.dirname, "check/canisters-moc-args");
|
|
70
77
|
const result = await cli(["check"], { cwd });
|
|
71
78
|
expect(result.exitCode).toBe(1);
|
|
72
79
|
expect(result.stderr).toMatch(/warning \[M0194\]/);
|
|
73
80
|
});
|
|
74
81
|
|
|
75
|
-
test("
|
|
82
|
+
test("[canisters.X].args applied to canister check", async () => {
|
|
83
|
+
const cwd = path.join(import.meta.dirname, "check/canisters-canister-args");
|
|
84
|
+
const result = await cli(["check"], { cwd });
|
|
85
|
+
expect(result.exitCode).toBe(1);
|
|
86
|
+
expect(result.stderr).toMatch(/warning \[M0194\]/);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("canister with errors", async () => {
|
|
76
90
|
const cwd = path.join(import.meta.dirname, "check/canisters-error");
|
|
77
91
|
const result = await cli(["check"], { cwd });
|
|
78
92
|
expect(result.exitCode).toBe(1);
|
|
79
93
|
expect(result.stderr).toMatch(/error/i);
|
|
80
94
|
});
|
|
81
95
|
|
|
82
|
-
test("
|
|
96
|
+
test("invalid canister name errors", async () => {
|
|
97
|
+
const cwd = path.join(import.meta.dirname, "check/canisters");
|
|
98
|
+
const result = await cli(["check", "nonexistent"], { cwd });
|
|
99
|
+
expect(result.exitCode).toBe(1);
|
|
100
|
+
expect(result.stderr).toMatch(/not found in mops\.toml/);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("--fix with canister", async () => {
|
|
83
104
|
const cwd = path.join(import.meta.dirname, "check/canisters");
|
|
84
105
|
const result = await cli(["check", "--fix"], { cwd });
|
|
85
106
|
expect(result.exitCode).toBe(0);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Version: 4.0.0
|
|
2
|
+
{
|
|
3
|
+
"20250101_000000_Init" : {} -> {a : Nat};
|
|
4
|
+
"20250201_000000_AddName" : (old : {a : Nat}) -> {a : Nat; name : Text};
|
|
5
|
+
"20250301_000000_AddEmail" :
|
|
6
|
+
(old : {a : Nat; name : Text}) -> {a : Nat; email : Text; name : Text}
|
|
7
|
+
}
|
|
8
|
+
actor {
|
|
9
|
+
stable a : Nat;
|
|
10
|
+
stable email : Text;
|
|
11
|
+
stable name : Text
|
|
12
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
[toolchain]
|
|
2
|
+
moc = "1.5.0"
|
|
3
|
+
|
|
4
|
+
[moc]
|
|
5
|
+
args = ["--default-persistent-actors"]
|
|
6
|
+
|
|
7
|
+
[canisters.backend]
|
|
8
|
+
main = "src/main.mo"
|
|
9
|
+
|
|
10
|
+
[canisters.backend.migrations]
|
|
11
|
+
chain = "migrations"
|
|
12
|
+
next = "next-migration"
|
|
13
|
+
|
|
14
|
+
[canisters.backend.check-stable]
|
|
15
|
+
path = "deployed.most"
|
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Version: 4.0.0
|
|
2
|
+
{
|
|
3
|
+
"20250101_000000_Init" : {} -> {a : Nat};
|
|
4
|
+
"20250201_000000_AddName" : (old : {a : Nat}) -> {a : Nat; name : Text};
|
|
5
|
+
"20250301_000000_AddEmail" :
|
|
6
|
+
(old : {a : Nat; name : Text}) -> {a : Nat; email : Text; name : Text}
|
|
7
|
+
}
|
|
8
|
+
actor {
|
|
9
|
+
stable a : Nat;
|
|
10
|
+
stable email : Text;
|
|
11
|
+
stable name : Text
|
|
12
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
[toolchain]
|
|
2
|
+
moc = "1.5.0"
|
|
3
|
+
|
|
4
|
+
[moc]
|
|
5
|
+
args = ["--default-persistent-actors"]
|
|
6
|
+
|
|
7
|
+
[canisters.backend]
|
|
8
|
+
main = "src/main.mo"
|
|
9
|
+
|
|
10
|
+
[canisters.backend.migrations]
|
|
11
|
+
chain = "migrations"
|
|
12
|
+
next = "next-migration"
|
|
13
|
+
|
|
14
|
+
[canisters.backend.check-stable]
|
|
15
|
+
path = "deployed.most"
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { describe, expect, test, afterEach } from "@jest/globals";
|
|
2
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { cp, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { cli, cliSnapshot, normalizePaths } from "./helpers";
|
|
6
|
+
|
|
7
|
+
const normalizeTimestamp = (text: string) =>
|
|
8
|
+
text.replace(/\d{8}_\d{6}/g, "<TIMESTAMP>");
|
|
9
|
+
|
|
10
|
+
const fixturesDir = path.join(import.meta.dirname, "migrate");
|
|
11
|
+
|
|
12
|
+
describe("migrate", () => {
|
|
13
|
+
const tempDirs: string[] = [];
|
|
14
|
+
|
|
15
|
+
async function makeTempFixture(fixture: string): Promise<string> {
|
|
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
|
+
}
|
|
22
|
+
|
|
23
|
+
async function patchMigrations(cwd: string, extra: string): Promise<void> {
|
|
24
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
25
|
+
const toml = readFileSync(tomlPath, "utf-8");
|
|
26
|
+
await writeFile(
|
|
27
|
+
tomlPath,
|
|
28
|
+
toml.replace(
|
|
29
|
+
'next = "next-migration"',
|
|
30
|
+
`next = "next-migration"\n${extra}`,
|
|
31
|
+
),
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
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
|
+
describe("migrate new", () => {
|
|
43
|
+
test("creates a migration file with timestamp and template", async () => {
|
|
44
|
+
const cwd = await makeTempFixture("basic");
|
|
45
|
+
const result = await cli(["migrate", "new", "AddPhone"], { cwd });
|
|
46
|
+
expect(result.exitCode).toBe(0);
|
|
47
|
+
|
|
48
|
+
const nextDir = path.join(cwd, "next-migration");
|
|
49
|
+
const files = readdirSync(nextDir).filter((f) => f.endsWith(".mo"));
|
|
50
|
+
expect(files).toHaveLength(1);
|
|
51
|
+
expect(files[0]).toMatch(/^\d{8}_\d{6}_AddPhone\.mo$/);
|
|
52
|
+
|
|
53
|
+
const content = readFileSync(path.join(nextDir, files[0]!), "utf-8");
|
|
54
|
+
|
|
55
|
+
expect({
|
|
56
|
+
exitCode: result.exitCode,
|
|
57
|
+
stdout: normalizeTimestamp(normalizePaths(result.stdout)),
|
|
58
|
+
stderr: normalizePaths(result.stderr),
|
|
59
|
+
template: content,
|
|
60
|
+
}).toMatchSnapshot();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("errors when next already has a file", async () => {
|
|
64
|
+
const cwd = await makeTempFixture("basic");
|
|
65
|
+
await cli(["migrate", "new", "First"], { cwd });
|
|
66
|
+
const result = await cli(["migrate", "new", "Second"], { cwd });
|
|
67
|
+
expect(result.exitCode).toBe(1);
|
|
68
|
+
expect(result.stderr).toMatch(/next migration already exists/i);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("errors on invalid migration name", async () => {
|
|
72
|
+
const cwd = await makeTempFixture("basic");
|
|
73
|
+
for (const name of ["../evil", "has space", "123start", "foo/bar"]) {
|
|
74
|
+
const result = await cli(["migrate", "new", name], { cwd });
|
|
75
|
+
expect(result.exitCode).toBe(1);
|
|
76
|
+
expect(result.stderr).toMatch(/invalid migration name/i);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("errors when [migrations] not configured", async () => {
|
|
81
|
+
const cwd = await makeTempFixture("basic");
|
|
82
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
83
|
+
const toml = readFileSync(tomlPath, "utf-8");
|
|
84
|
+
await writeFile(
|
|
85
|
+
tomlPath,
|
|
86
|
+
toml.replace(/\[canisters\.backend\.migrations\][\s\S]*$/, ""),
|
|
87
|
+
);
|
|
88
|
+
const result = await cli(["migrate", "new", "Test"], { cwd });
|
|
89
|
+
expect(result.exitCode).toBe(1);
|
|
90
|
+
expect(result.stderr).toMatch(/\[migrations\]/i);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("migrate freeze", () => {
|
|
95
|
+
test("moves the file from next to chain", async () => {
|
|
96
|
+
const cwd = await makeTempFixture("with-next");
|
|
97
|
+
const result = await cli(["migrate", "freeze"], { cwd });
|
|
98
|
+
expect(result.exitCode).toBe(0);
|
|
99
|
+
|
|
100
|
+
const nextFiles = readdirSync(path.join(cwd, "next-migration")).filter(
|
|
101
|
+
(f) => f.endsWith(".mo"),
|
|
102
|
+
);
|
|
103
|
+
expect(nextFiles).toHaveLength(0);
|
|
104
|
+
|
|
105
|
+
const chainFiles = readdirSync(path.join(cwd, "migrations")).filter((f) =>
|
|
106
|
+
f.endsWith(".mo"),
|
|
107
|
+
);
|
|
108
|
+
expect(chainFiles).toContain("20250401_000000_RenameId.mo");
|
|
109
|
+
|
|
110
|
+
expect({
|
|
111
|
+
exitCode: result.exitCode,
|
|
112
|
+
stdout: normalizePaths(result.stdout),
|
|
113
|
+
stderr: normalizePaths(result.stderr),
|
|
114
|
+
}).toMatchSnapshot();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("errors when next is empty", async () => {
|
|
118
|
+
const cwd = await makeTempFixture("basic");
|
|
119
|
+
const result = await cli(["migrate", "freeze"], { cwd });
|
|
120
|
+
expect(result.exitCode).toBe(1);
|
|
121
|
+
expect(result.stderr).toMatch(/no next migration/i);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("errors when next-migration does not sort last", async () => {
|
|
125
|
+
const cwd = await makeTempFixture("basic");
|
|
126
|
+
await writeFile(
|
|
127
|
+
path.join(cwd, "next-migration", "00000000_000000_Early.mo"),
|
|
128
|
+
"module {\n public func migration(_ : {}) : {} {\n {}\n }\n}\n",
|
|
129
|
+
);
|
|
130
|
+
const result = await cli(["migrate", "freeze"], { cwd });
|
|
131
|
+
expect(result.exitCode).toBe(1);
|
|
132
|
+
expect(result.stderr).toMatch(/must sort after/i);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("check", () => {
|
|
137
|
+
test("check fails without next migration, passes with it", async () => {
|
|
138
|
+
const cwd = await makeTempFixture("with-next");
|
|
139
|
+
const nextFile = readdirSync(path.join(cwd, "next-migration")).find((f) =>
|
|
140
|
+
f.endsWith(".mo"),
|
|
141
|
+
)!;
|
|
142
|
+
const nextPath = path.join(cwd, "next-migration", nextFile);
|
|
143
|
+
const nextContent = readFileSync(nextPath, "utf-8");
|
|
144
|
+
await rm(nextPath);
|
|
145
|
+
|
|
146
|
+
await cliSnapshot(["check"], { cwd }, 1);
|
|
147
|
+
|
|
148
|
+
await writeFile(nextPath, nextContent);
|
|
149
|
+
await cliSnapshot(["check"], { cwd }, 0);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("check with trimming shows reduced chain", async () => {
|
|
153
|
+
const cwd = await makeTempFixture("basic");
|
|
154
|
+
await patchMigrations(cwd, "check-limit = 2");
|
|
155
|
+
await cliSnapshot(["check", "--verbose"], { cwd }, 0);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("build", () => {
|
|
160
|
+
test("build produces .most with full migration chain", async () => {
|
|
161
|
+
const cwd = await makeTempFixture("basic");
|
|
162
|
+
const result = await cli(["build"], { cwd });
|
|
163
|
+
expect(result.exitCode).toBe(0);
|
|
164
|
+
|
|
165
|
+
const most = readFileSync(
|
|
166
|
+
path.join(cwd, ".mops", ".build", "backend.most"),
|
|
167
|
+
"utf-8",
|
|
168
|
+
);
|
|
169
|
+
expect(most).toMatchSnapshot();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("build with build-limit produces trimmed .most", async () => {
|
|
173
|
+
const cwd = await makeTempFixture("basic");
|
|
174
|
+
await patchMigrations(cwd, "build-limit = 2");
|
|
175
|
+
const result = await cli(["build"], { cwd });
|
|
176
|
+
expect(result.exitCode).toBe(0);
|
|
177
|
+
|
|
178
|
+
const most = readFileSync(
|
|
179
|
+
path.join(cwd, ".mops", ".build", "backend.most"),
|
|
180
|
+
"utf-8",
|
|
181
|
+
);
|
|
182
|
+
expect(most).toMatchSnapshot();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("build-limit counts next migration as part of the chain", async () => {
|
|
186
|
+
const cwd = await makeTempFixture("with-next");
|
|
187
|
+
await patchMigrations(cwd, "build-limit = 2");
|
|
188
|
+
const result = await cli(["build"], { cwd });
|
|
189
|
+
expect(result.exitCode).toBe(0);
|
|
190
|
+
|
|
191
|
+
const most = readFileSync(
|
|
192
|
+
path.join(cwd, ".mops", ".build", "backend.most"),
|
|
193
|
+
"utf-8",
|
|
194
|
+
);
|
|
195
|
+
expect(most).toMatchSnapshot();
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("stable check hint", () => {
|
|
200
|
+
test("stable check fails with hint when deployed.most is incompatible", async () => {
|
|
201
|
+
const cwd = await makeTempFixture("basic");
|
|
202
|
+
await writeFile(
|
|
203
|
+
path.join(cwd, "deployed.most"),
|
|
204
|
+
"// Version: 1.0.0\nactor {\n stable var a : Nat;\n stable var name : Int\n};\n",
|
|
205
|
+
);
|
|
206
|
+
await cliSnapshot(["check"], { cwd }, 1);
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
describe("conflict detection", () => {
|
|
211
|
+
test("errors when both [migrations] and --enhanced-migration in args", async () => {
|
|
212
|
+
const cwd = await makeTempFixture("basic");
|
|
213
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
214
|
+
const toml = readFileSync(tomlPath, "utf-8");
|
|
215
|
+
await writeFile(
|
|
216
|
+
tomlPath,
|
|
217
|
+
toml.replace(
|
|
218
|
+
"[canisters.backend.migrations]",
|
|
219
|
+
'args = ["--enhanced-migration=migrations"]\n\n[canisters.backend.migrations]',
|
|
220
|
+
),
|
|
221
|
+
);
|
|
222
|
+
const result = await cli(["check"], { cwd });
|
|
223
|
+
expect(result.exitCode).toBe(1);
|
|
224
|
+
expect(result.stderr).toMatch(/--enhanced-migration/);
|
|
225
|
+
expect(result.stderr).toMatch(/managed automatically/i);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
});
|
package/types.ts
CHANGED
|
@@ -35,6 +35,13 @@ export type Config = {
|
|
|
35
35
|
};
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
+
export type MigrationsConfig = {
|
|
39
|
+
chain: string;
|
|
40
|
+
next?: string;
|
|
41
|
+
"check-limit"?: number;
|
|
42
|
+
"build-limit"?: number;
|
|
43
|
+
};
|
|
44
|
+
|
|
38
45
|
export type CanisterConfig = {
|
|
39
46
|
main?: string;
|
|
40
47
|
args?: string[];
|
|
@@ -44,6 +51,7 @@ export type CanisterConfig = {
|
|
|
44
51
|
path: string;
|
|
45
52
|
skipIfMissing?: boolean;
|
|
46
53
|
};
|
|
54
|
+
migrations?: MigrationsConfig;
|
|
47
55
|
};
|
|
48
56
|
|
|
49
57
|
export type Dependencies = Record<string, Dependency>;
|