/* * Copyright 2026 Oliver Herb * SPDX-License-Identifier: MPL-2.0 * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ "use strict"; /* * Licence checks: the MPL-2.0 text, the header in every source file and * the package metadata. * * The files are read from disk and compared as text: each licensed file * must start with the header in the comment style of its type (block * comment, HTML/SVG comment or shell comment). */ const { test } = require("node:test"); const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const pkg = require("../package.json"); const ROOT = path.join(__dirname, ".."); const COPYRIGHT = "Copyright 2026 Oliver Herb"; const SPDX = "SPDX-License-Identifier: MPL-2.0"; const HEADER_LINES = Object.freeze([ COPYRIGHT, SPDX, "", "This Source Code Form is subject to the terms of the Mozilla Public", "License, v. 2.0. If a copy of the MPL was not distributed with this", "file, You can obtain one at https://mozilla.org/MPL/2.0/.", ]); /** The header as it must appear, by comment style. */ const HEADERS = Object.freeze({ block: [ "/*", ...HEADER_LINES.map((line) => (line ? ` * ${line}` : " *")), " */", ].join("\n"), markup: [ "", ].join("\n"), shell: HEADER_LINES.map((line) => (line ? `# ${line}` : "#")).join("\n"), }); /** * Lists the files below dir whose names match the pattern. * @param {string} dir Directory relative to ROOT. * @param {RegExp} pattern Tested against the file name (not the path). * @param {boolean} [recursive] Whether to descend into subdirectories. * Defaults to true. * @returns {string[]} Matching file paths, relative to ROOT. */ function filesIn(dir, pattern, recursive = true) { const files = []; for (const entry of fs.readdirSync(path.join(ROOT, dir), { withFileTypes: true, })) { const relative = path.join(dir, entry.name); if (entry.isDirectory()) { if (recursive) { files.push(...filesIn(relative, pattern, recursive)); } } else if (pattern.test(entry.name)) { files.push(relative); } } return files; } const LICENSED_FILES = [ ...filesIn("src", /\.(?:js|css|html|svg)$/), ...filesIn("test", /\.js$/), ...filesIn("scripts", /\.sh$/, false), "eslint.config.mjs", ]; /** * Returns the text a licensed file must start with, depending on its type: * the header itself, preceded by the doctype (HTML) or the shebang (shell). * @param {string} file File path; only the extension is used. * @returns {string} The expected beginning of the file, without the * trailing newline. * @throws {Error} If there is no header rule for the file type. */ function expectedStart(file) { switch (path.extname(file)) { case ".js": case ".mjs": case ".css": return HEADERS.block; case ".html": return `\n${HEADERS.markup}`; case ".svg": return HEADERS.markup; case ".sh": return `#!/usr/bin/env bash\n${HEADERS.shell}`; default: throw new Error(`no header rule for ${file}`); } } test("LICENSE holds the Mozilla Public License 2.0", () => { const license = fs.readFileSync(path.join(ROOT, "LICENSE"), "utf8"); assert.ok(license.startsWith("Mozilla Public License Version 2.0")); assert.match(license, /Exhibit B - "Incompatible With Secondary Licenses"/); }); test("every source, test and script file has the licence header", () => { assert.ok(LICENSED_FILES.includes(path.join("src", "lib", "marker.js"))); assert.ok( LICENSED_FILES.includes(path.join("src", "options", "options.html")), ); assert.ok(LICENSED_FILES.includes(path.join("src", "icons", "icon.svg"))); assert.ok(LICENSED_FILES.includes(path.join("test", "helpers", "dom.js"))); assert.ok(LICENSED_FILES.includes(path.join("scripts", "build.sh"))); assert.ok(LICENSED_FILES.includes("eslint.config.mjs")); for (const file of LICENSED_FILES) { const source = fs .readFileSync(path.join(ROOT, file), "utf8") .replace(/\r\n/g, "\n"); assert.ok(source.includes(SPDX), `${file}: ${SPDX} missing`); assert.ok(source.includes(COPYRIGHT), `${file}: ${COPYRIGHT} missing`); assert.ok( source.startsWith(`${expectedStart(file)}\n`), `${file}: header missing or not at the top`, ); } }); test("package.json declares MPL-2.0 and the public repository", () => { assert.equal(pkg.name, "inline-comment"); assert.equal(pkg.license, "MPL-2.0"); assert.equal(pkg.author, "Oliver Herb"); assert.deepEqual(pkg.repository, { type: "git", url: "https://git.hassio-muc.de/Oliver_Herb/inline-comment.git", }); assert.equal( pkg.homepage, "https://git.hassio-muc.de/Oliver_Herb/inline-comment", ); assert.equal( pkg.bugs, "https://git.hassio-muc.de/Oliver_Herb/inline-comment/issues", ); }); test("the build puts LICENSE into the XPI root", () => { const script = fs.readFileSync( path.join(ROOT, "scripts", "build.sh"), "utf8", ); assert.match(script, /^OUT="dist\/inline-comment-\$\{VERSION\}\.xpi"$/m); assert.match(script, /^zip -X -q -j "\$OUT" LICENSE$/m); });