Repository navigation
Expand file tree
/
Copy pathmagicNumbers.test.js
More file actions
180 lines (167 loc) · 6.3 KB
/
Copy pathmagicNumbers.test.js
File metadata and controls
180 lines (167 loc) · 6.3 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
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import {
bumpMagicNumbers,
checkMagicNumbers,
configPath,
incrementMagicNumber,
previousRelease,
previousReleaseTag,
readMagicNumber,
releaseMagicNumbers,
} from "./magicNumbers.js";
const config = (cmi, cmt, ast) => `let cmi_magic_number = "ResCmi${cmi}"
and ast_impl_magic_number = "ResImpl${ast}"
and ast_intf_magic_number = "ResIntf${ast}"
and ast0_impl_magic_number = "Caml1999M022"
and cmt_magic_number = "ResCmt${cmt}"
`;
test("compares with the highest release below the version", () => {
const tags = [
"v12.3.1",
"v13.0.0-alpha.6",
"v12.3.2",
"v13.0.0-alpha.7",
"vnext",
"13.0.0",
];
assert.equal(previousRelease(tags, "13.0.0-alpha.7"), "v13.0.0-alpha.6");
assert.equal(previousRelease(tags, "13.0.0"), "v13.0.0-alpha.7");
assert.equal(previousRelease(tags, "12.3.3"), "v12.3.2");
assert.equal(previousRelease(tags, "12.3.1"), null);
});
test("increments the trailing counter and keeps its width", () => {
assert.equal(incrementMagicNumber("ResCmi040"), "ResCmi041");
assert.equal(incrementMagicNumber("ResCmi099"), "ResCmi100");
assert.equal(incrementMagicNumber("ResImpl01317"), "ResImpl01318");
assert.throws(() => incrementMagicNumber("ResCmi999"), /out of digits/);
assert.throws(() => incrementMagicNumber("ResCmi"), /trailing counter/);
});
test("reads magic numbers by name", () => {
const current = config("040", "048", "01317");
assert.equal(readMagicNumber(current, "cmi_magic_number"), "ResCmi040");
assert.equal(
readMagicNumber(current, "ast_impl_magic_number"),
"ResImpl01317",
);
assert.equal(readMagicNumber(current, "missing_magic_number"), null);
});
test("bumps the magic numbers of the previous release when the formats changed", () => {
const previous = config("040", "048", "01317");
assert.equal(bumpMagicNumbers(previous, previous, false), previous);
assert.equal(
bumpMagicNumbers(previous, previous, true),
config("041", "049", "01318"),
);
// Leaves the frozen Parsetree0 numbers alone.
assert.ok(
bumpMagicNumbers(previous, previous, true).includes('"Caml1999M022"'),
);
// A number that differs from the previous release was already bumped.
const bumped = config("041", "048", "01317");
assert.equal(
bumpMagicNumbers(bumped, previous, true),
config("041", "049", "01318"),
);
assert.equal(
bumpMagicNumbers(config("041", "049", "01318"), previous, true),
config("041", "049", "01318"),
);
// So is every number when the previous release had other ones or no
// config.ml at all.
const legacy = previous.replace('"ResCmi040"', '"Caml1999I040"');
assert.equal(
bumpMagicNumbers(previous, legacy, true),
config("040", "049", "01318"),
);
assert.equal(bumpMagicNumbers(previous, null, true), previous);
assert.throws(
() =>
bumpMagicNumbers(
previous.replace("cmt_magic_number", "other"),
previous,
true,
),
/cmt_magic_number not found/,
);
});
test("bumps the magic numbers once per release when format sources changed", async t => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "rescript-magic-"));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const git = (...args) =>
execFileSync("git", args, {
cwd: root,
env: {
...process.env,
GIT_AUTHOR_NAME: "a",
GIT_AUTHOR_EMAIL: "a@example.com",
GIT_COMMITTER_NAME: "a",
GIT_COMMITTER_EMAIL: "a@example.com",
},
});
const write = async (file, text) => {
await fs.mkdir(path.dirname(path.join(root, file)), { recursive: true });
await fs.writeFile(path.join(root, file), text);
};
const commit = async (file, text) => {
await write(file, text);
git("add", ".");
git("commit", "-q", "-m", file);
};
git("init", "-q");
await commit(configPath, config("040", "048", "01317"));
assert.throws(() => previousReleaseTag(root, "1.1.0"), /git fetch --tags/);
await commit("compiler/ml/types.ml", "type t = A\n");
git("tag", "v1.0.0");
assert.equal(await releaseMagicNumbers(root, "1.1.0"), null);
await commit("compiler/syntax/printer.ml", "let print = ()\n");
assert.equal(await releaseMagicNumbers(root, "1.1.0"), null);
// A merged maintenance branch brings a newer tag of an older line, with
// other magic numbers.
git("checkout", "-q", "-b", "maintenance", "v1.0.0");
await commit(configPath, config("030", "038", "01200"));
git("tag", "v0.9.9");
git("checkout", "-q", "-");
git("merge", "-q", "-s", "ours", "-m", "Merge maintenance", "maintenance");
assert.equal(previousReleaseTag(root, "1.1.0"), "v1.0.0");
// Only committed changes count.
await write("compiler/ml/types.ml", "type t = A | B\n");
assert.equal(await releaseMagicNumbers(root, "1.1.0"), null);
await commit("compiler/ml/types.ml", "type t = A | B\n");
const bumped = await releaseMagicNumbers(root, "1.1.0");
assert.equal(bumped, config("041", "049", "01318"));
// A later run for the same release keeps the bumped numbers, also when the
// tag of the version being released already exists.
await commit(configPath, bumped);
git("tag", "v1.1.0");
assert.equal(previousReleaseTag(root, "1.1.0"), "v1.0.0");
await commit("compiler/ext/ident.ml", "type t = string\n");
assert.equal(await releaseMagicNumbers(root, "1.1.0"), null);
// The next release compares with the one before it.
assert.equal(
await releaseMagicNumbers(root, "1.2.0"),
config("042", "050", "01319"),
);
});
test("rejects pull requests that bump a magic number", () => {
const base = config("040", "048", "01317");
checkMagicNumbers(base, base, []);
checkMagicNumbers(null, base, []);
assert.throws(
() => checkMagicNumbers(base, config("040", "049", "01317"), []),
/don't bump cmt_magic_number;/,
);
assert.throws(
() => checkMagicNumbers(base, config("041", "048", "01318"), []),
/cmi_magic_number, ast_impl_magic_number, ast_intf_magic_number/,
);
checkMagicNumbers(base, config("041", "049", "01318"), ["changelog:release"]);
// Switching to another prefix is no bump.
checkMagicNumbers(base.replace('"ResCmi040"', '"Caml1999I040"'), base, [
"changelog:skip",
]);
});