Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scripts/build.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ test("rejects map rotation paths that escape game/csgo", () => {
assert.equal(validateMapRotation(rotation({ "../maps.jsonc": { Maps: "{{maps}}" } }), "x.json").length, 1);
});

test("rejects a file token embedded in longer text", () => {
const problems = validateMapRotation(rotation({ "a/maps.jsonc": { Maps: "x{{maps}}" } }), "x.json");
assert.equal(problems.length, 1);
assert.match(problems[0], /as a whole value/);
});

test("rejects map rotation tokens used in the wrong place", () => {
assert.equal(validateMapRotation(rotation({ "a/maps.jsonc": { Maps: "{{maps}}", Id: "{{id}}" } }), "x.json").length, 1);
assert.equal(
Expand Down
22 changes: 22 additions & 0 deletions scripts/validate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ function tokensIn(value) {
return [];
}

function stringsIn(value) {
if (typeof value === "string") {
return [value];
}

if (value && typeof value === "object") {
return Object.values(value).flatMap(stringsIn);
}

return [];
}

export function validateMapRotation(entry, filePath) {
const problems = [];
const fail = (message) => problems.push(`${filePath}: map_rotation ${message}`);
Expand Down Expand Up @@ -204,6 +216,16 @@ export function validateMapRotation(entry, filePath) {
fail(`"${target}" uses ${token}; files may only use ${[...MAP_ROTATION_DOCUMENT_TOKENS].join(", ")}`);
}
}

// {{maps}} is a list and {{shuffle}} a boolean; inside longer text either
// would render as "[object Object]" or "true".
for (const value of stringsIn(document)) {
const embedded = tokensIn(value).filter((token) => MAP_ROTATION_DOCUMENT_TOKENS.has(token));

if (embedded.length > 0 && value !== embedded[0]) {
fail(`"${target}" must use ${embedded.join(", ")} as a whole value, not inside "${value}"`);
}
}
}

if (!Object.values(files).some((document) => tokensIn(document).includes("{{maps}}"))) {
Expand Down
Loading