hardhat
Version:
Hardhat is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.
88 lines • 4.26 kB
JavaScript
import { HardhatError } from "@nomicfoundation/hardhat-errors";
import chalk from "chalk";
import { errorResult } from "../../../../../utils/result.js";
import { checkFunctionGasSnapshots, extractFunctionGasSnapshots, logFunctionGasSnapshotsSection, writeFunctionGasSnapshots, } from "../../function-gas-snapshots.js";
import { checkSnapshotCheatcodes, extractSnapshotCheatcodes, logSnapshotCheatcodesSection, writeSnapshotCheatcodes, } from "../../snapshot-cheatcodes.js";
const runSolidityTests = async (args, hre, runSuper) => {
const superResult = await runSuper(args);
const testsPassed = superResult.success;
const solidityTestRunResult = testsPassed
? superResult.value
: superResult.error;
const suiteResults = solidityTestRunResult.suiteResults;
const rootPath = hre.config.paths.root;
if (args.snapshot && args.snapshotCheck) {
throw new HardhatError(HardhatError.ERRORS.CORE.SOLIDITY_TESTS.MUTUALLY_EXCLUSIVE_SNAPSHOT_FLAGS);
}
let snapshotCheckPassed = true;
if (args.snapshot) {
const snapshotResult = await handleSnapshot(rootPath, suiteResults, testsPassed);
logSnapshotResult(snapshotResult);
}
else if (testsPassed && args.snapshotCheck) {
const snapshotCheckResult = await handleSnapshotCheck(rootPath, suiteResults);
logSnapshotCheckResult(snapshotCheckResult);
snapshotCheckPassed =
snapshotCheckResult.functionGasSnapshotsCheck.passed &&
snapshotCheckResult.snapshotCheatcodesCheck.passed;
}
if (!snapshotCheckPassed) {
return errorResult(solidityTestRunResult);
}
return superResult;
};
export async function handleSnapshot(basePath, suiteResults, testsPassed) {
if (testsPassed) {
const functionGasSnapshots = extractFunctionGasSnapshots(suiteResults);
await writeFunctionGasSnapshots(basePath, functionGasSnapshots);
}
const snapshotCheatcodes = extractSnapshotCheatcodes(suiteResults);
await writeSnapshotCheatcodes(basePath, snapshotCheatcodes);
return {
functionGasSnapshotsWritten: testsPassed,
};
}
export function logSnapshotResult(result, logger = console.log) {
if (result.functionGasSnapshotsWritten) {
logger(chalk.green("Function gas snapshots written successfully"));
logger();
}
}
export async function handleSnapshotCheck(basePath, suiteResults) {
const functionGasSnapshotsCheck = await checkFunctionGasSnapshots(basePath, suiteResults);
const snapshotCheatcodesCheck = await checkSnapshotCheatcodes(basePath, suiteResults);
return {
functionGasSnapshotsCheck,
snapshotCheatcodesCheck,
};
}
export function logSnapshotCheckResult({ functionGasSnapshotsCheck, snapshotCheatcodesCheck }, logger = console.log) {
logger(functionGasSnapshotsCheck.passed && snapshotCheatcodesCheck.passed
? chalk.green("Snapshot check passed")
: chalk.red("Snapshot check failed"));
const functionGasHasOutput = functionGasSnapshotsCheck.written ||
functionGasSnapshotsCheck.comparison.changed.length > 0 ||
functionGasSnapshotsCheck.comparison.added.length > 0 ||
functionGasSnapshotsCheck.comparison.removed.length > 0;
const snapshotCheatcodesHasOutput = snapshotCheatcodesCheck.written ||
snapshotCheatcodesCheck.comparison.changed.length > 0 ||
snapshotCheatcodesCheck.comparison.added.length > 0 ||
snapshotCheatcodesCheck.comparison.removed.length > 0;
// Add an extra newline if function gas snapshots have output
if (functionGasHasOutput) {
logger();
}
logFunctionGasSnapshotsSection(functionGasSnapshotsCheck, logger);
// Add an extra newline if only snapshot cheatcodes have output
// (logFunctionGasSnapshotsSection adds one if it has output)
if (!functionGasHasOutput && snapshotCheatcodesHasOutput) {
logger();
}
logSnapshotCheatcodesSection(snapshotCheatcodesCheck, logger);
if (!functionGasSnapshotsCheck.passed || !snapshotCheatcodesCheck.passed) {
logger(chalk.yellow("To update snapshots, run your tests with --snapshot"));
logger();
}
}
export default runSolidityTests;
//# sourceMappingURL=task-action.js.map