Files
is-a-dev/tests/pr.test.js
T
William Harrison 70dd4633a4 Update pr.test.js
2025-01-02 20:25:34 +08:00

77 lines
2.4 KiB
JavaScript

const t = require("ava");
const fs = require("fs-extra");
const path = require("path");
const PR_AUTHOR = process.env.PR_AUTHOR;
const MODIFIED_FILES = (process.env.MODIFIED_FILES || "").split(" ");
const EVENT = process.env.EVENT;
const RUN_ID = process.env.RUN_ID;
const domainsPath = path.resolve("domains");
const headDomainsPath = path.resolve(`register-${RUN_ID}/domains`);
const admins = require("../util/administrators.json");
async function getFileContent(basePath, fileName) {
try {
return await fs.readJson(path.join(basePath, fileName));
} catch (err) {
return null;
}
}
t("Modified JSON files must be owned by the PR author", async (t) => {
if (EVENT !== "pull_request") return t.pass();
console.log("Modified files", MODIFIED_FILES);
await Promise.all(
MODIFIED_FILES.map(async (file) => {
const fileName = file.substring(file.lastIndexOf("/") + 1);
const modifiedDomain = await getFileContent(domainsPath, fileName);
const currentDomain = (await getFileContent(headDomainsPath, fileName)) || modifiedDomain;
if (!modifiedDomain || !currentDomain) {
t.fail(`${file}: Unable to read domain data`);
return;
}
t.true(
currentDomain.owner.username === PR_AUTHOR || admins.includes(PR_AUTHOR),
`${file}: Domain owner is ${currentDomain.owner.username} but ${PR_AUTHOR} is the PR author`
);
})
);
t.pass();
});
t("New JSON files must be owned by the PR author", async (t) => {
if (EVENT !== "pull_request") return t.pass();
const headDomainsFiles = fs.readdirSync(headDomainsPath);
// get list of all files that are in the base dir but not in the head dir
const newFiles = headDomainsFiles.filter((file) => !fs.existsSync(path.join(domainsPath, file)));
console.log("New files", newFiles);
await Promise.all(
newFiles.map(async (file) => {
const domain = await getFileContent(domainsPath, file.substring(file.lastIndexOf("/") + 1));
if (!domain) {
t.fail(`${file}: Unable to read domain data`);
return;
}
t.true(
domain.owner.username === PR_AUTHOR || admins.includes(PR_AUTHOR),
`${file}: Domain owner is ${domain.owner.username} but ${PR_AUTHOR} is the PR author`
);
})
);
t.pass();
});