UNPKG

hardhat-spdx-license-identifier

Version:

Prepend local Solidity source files with an SPDX License Identifier

46 lines (45 loc) 2.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const package_json_1 = require("../../package.json"); const chai_1 = require("chai"); const fs_1 = __importDefault(require("fs")); const hardhat_1 = __importDefault(require("hardhat")); const task_names_1 = require("hardhat/builtin-tasks/task-names"); const TASK_PREPEND_SPDX_LICENSE = 'prepend-spdx-license'; const HEADER_BASE = '// SPDX-License-Identifier:'; const readContractSource = async (name) => { const artifact = await hardhat_1.default.artifacts.readArtifact(name); return fs_1.default.readFileSync(artifact.sourceName).toString(); }; describe(TASK_PREPEND_SPDX_LICENSE, () => { const cache = {}; before(async () => { const sourcePaths = await hardhat_1.default.run(task_names_1.TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS); for (const sourcePath of sourcePaths) { cache[sourcePath] = fs_1.default.readFileSync(sourcePath).toString(); } }); afterEach(async () => { for (const sourcePath in cache) { fs_1.default.writeFileSync(sourcePath, cache[sourcePath]); } }); it('writes license identifier to top of source file', async () => { const contentsBefore = await readContractSource('ContractWithoutLicense'); (0, chai_1.expect)(contentsBefore.includes(HEADER_BASE)).to.be.false; await hardhat_1.default.run(TASK_PREPEND_SPDX_LICENSE); const contentsAfter = await readContractSource('ContractWithoutLicense'); (0, chai_1.expect)(contentsAfter.includes(`${HEADER_BASE} ${package_json_1.license}`)).to.be.true; }); it('does not write duplicate license identifiers', async () => { const reg = new RegExp(HEADER_BASE, 'g'); const contentsBefore = await readContractSource('ContractWithLicense'); (0, chai_1.expect)((contentsBefore.match(reg) ?? []).length).to.equal(1); await hardhat_1.default.run(TASK_PREPEND_SPDX_LICENSE); const contentsAfter = await readContractSource('ContractWithLicense'); (0, chai_1.expect)((contentsAfter.match(reg) ?? []).length).to.equal(1); }); });