Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 .changeset/quiet-melons-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
# docs: https://github.com/NomicFoundation/hardhat-website/pull/278
"hardhat": patch
---

Make `hardhat test solidity --snapshot-check` read-only so it no longer rewrites or deletes `.gas-snapshot` or `snapshots/*.json` files; it now reports differences, fails when a stored gas value changed, and suggests `--snapshot` when there's no baseline.
Comment thread
schaable marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface FunctionGasSnapshotChange {
export interface FunctionGasSnapshotCheckResult {
passed: boolean;
comparison: FunctionGasSnapshotComparison;
written: boolean;
noBaseline: boolean;
}

export function getFunctionGasSnapshotsPath(basePath: string): string {
Expand Down Expand Up @@ -323,16 +323,14 @@ export async function checkFunctionGasSnapshots(
previousFunctionGasSnapshots = await readFunctionGasSnapshots(basePath);
} catch (error) {
if (error instanceof FileNotFoundError) {
await writeFunctionGasSnapshots(basePath, functionGasSnapshots);

return {
passed: true,
comparison: {
added: [],
removed: [],
changed: [],
},
written: true,
noBaseline: functionGasSnapshots.length > 0,
};
}

Expand All @@ -344,67 +342,60 @@ export async function checkFunctionGasSnapshots(
functionGasSnapshots,
);

// Update snapshots when functions are added or removed (but not changed)
const hasAddedOrRemoved =
comparison.added.length > 0 || comparison.removed.length > 0;
if (comparison.changed.length === 0 && hasAddedOrRemoved) {
await writeFunctionGasSnapshots(basePath, functionGasSnapshots);
}

return {
passed: comparison.changed.length === 0,
comparison,
written: hasAddedOrRemoved,
noBaseline: false,
};
}

export function logFunctionGasSnapshotsSection(
result: FunctionGasSnapshotCheckResult,
logger: typeof console.log = console.log,
): void {
const { comparison, written } = result;
const { comparison, noBaseline } = result;
const changedLength = comparison.changed.length;
const addedLength = comparison.added.length;
const removedLength = comparison.removed.length;
const hasChanges = changedLength > 0;
const hasAdded = addedLength > 0;
const hasRemoved = removedLength > 0;
const hasAnyDifferences = hasChanges || hasAdded || hasRemoved;
const isFirstTimeWrite = written && !hasAnyDifferences;

// Nothing to report
if (!isFirstTimeWrite && !hasAnyDifferences) {
if (!noBaseline && !hasAnyDifferences) {
return;
}

logger(
formatSectionHeader("Function gas snapshots", {
changedLength,
addedLength,
removedLength,
}),
);

if (isFirstTimeWrite) {
logger();
if (noBaseline) {
logger(
styleText(
"green",
" No existing snapshots found. Function gas snapshots written successfully",
"yellow",
"Function gas snapshots: no baseline found. Run your tests with --snapshot to create one.",
),
);
logger();
return;
}

logger(
formatSectionHeader("Function gas snapshots", {
changedLength,
addedLength,
removedLength,
}),
);

if (hasChanges) {
logger();
printFunctionGasSnapshotChanges(comparison.changed, logger);
}

if (hasAdded) {
logger();
logger(` Added ${comparison.added.length} function(s):`);
logger(
` ${comparison.added.length} function(s) produced by this run are not in the baseline:`,
Comment thread
schaable marked this conversation as resolved.
Resolved
);
const addedLines = stringifyFunctionGasSnapshots(comparison.added).split(
"\n",
);
Expand All @@ -415,7 +406,9 @@ export function logFunctionGasSnapshotsSection(

if (hasRemoved) {
logger();
logger(` Removed ${comparison.removed.length} function(s):`);
logger(
` ${comparison.removed.length} stored function(s) were not produced by this run:`,
);
const removedLines = stringifyFunctionGasSnapshots(
comparison.removed,
).split("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export interface SnapshotCheatcodesComparison {
export interface SnapshotCheatcodesCheckResult {
passed: boolean;
comparison: SnapshotCheatcodesComparison;
written: boolean;
noBaseline: boolean;
renamedGroups: RenamedSnapshotGroup[];
}

Expand Down Expand Up @@ -363,20 +363,14 @@ export async function checkSnapshotCheatcodes(
previousSnapshotCheatcodes = await readSnapshotCheatcodes(basePath);
} catch (error) {
if (error instanceof FileNotFoundError) {
// Only write if there are cheatcodes to save
const written = snapshotCheatcodes.size > 0;
if (written) {
await writeSnapshotCheatcodes(basePath, snapshotCheatcodes);
}

return {
passed: true,
comparison: {
added: [],
removed: [],
changed: [],
},
written,
noBaseline: snapshotCheatcodes.size > 0,
renamedGroups,
};
}
Expand All @@ -389,17 +383,10 @@ export async function checkSnapshotCheatcodes(
snapshotCheatcodes,
);

// Update snapshots when functions are added or removed (but not changed)
const hasAddedOrRemoved =
comparison.added.length > 0 || comparison.removed.length > 0;
if (comparison.changed.length === 0 && hasAddedOrRemoved) {
await writeSnapshotCheatcodes(basePath, snapshotCheatcodes);
}

return {
passed: comparison.changed.length === 0,
comparison,
written: hasAddedOrRemoved,
noBaseline: false,
renamedGroups,
};
}
Expand All @@ -408,49 +395,49 @@ export function logSnapshotCheatcodesSection(
result: SnapshotCheatcodesCheckResult,
logger: typeof console.log = console.log,
): void {
const { comparison, written } = result;
const { comparison, noBaseline } = result;
const changedLength = comparison.changed.length;
const addedLength = comparison.added.length;
const removedLength = comparison.removed.length;
const hasChanges = changedLength > 0;
const hasAdded = addedLength > 0;
const hasRemoved = removedLength > 0;
const hasAnyDifferences = hasChanges || hasAdded || hasRemoved;
const isFirstTimeWrite = written && !hasAnyDifferences;

// Nothing to report
if (!isFirstTimeWrite && !hasAnyDifferences) {
if (!noBaseline && !hasAnyDifferences) {
return;
}

logger(
formatSectionHeader("Snapshot cheatcodes", {
changedLength,
addedLength,
removedLength,
}),
);

if (isFirstTimeWrite) {
logger();
if (noBaseline) {
logger(
styleText(
"green",
" No existing snapshots found. Snapshot cheatcodes written successfully",
"yellow",
"Snapshot cheatcodes: no baseline found. Run your tests with --snapshot to create one.",
),
);
logger();
return;
}

logger(
formatSectionHeader("Snapshot cheatcodes", {
changedLength,
addedLength,
removedLength,
}),
);

if (hasChanges) {
logger();
printSnapshotCheatcodeChanges(comparison.changed, logger);
}

if (hasAdded) {
logger();
logger(` Added ${comparison.added.length} snapshot(s):`);
logger(
` ${comparison.added.length} snapshot(s) produced by this run are not in the baseline:`,
);
const addedLines = stringifySnapshotCheatcodes(comparison.added).split(
"\n",
);
Expand All @@ -461,7 +448,9 @@ export function logSnapshotCheatcodesSection(

if (hasRemoved) {
logger();
logger(` Removed ${comparison.removed.length} snapshot(s):`);
logger(
` ${comparison.removed.length} stored snapshot(s) were not produced by this run:`,
);
const removedLines = stringifySnapshotCheatcodes(comparison.removed).split(
"\n",
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ export function logSnapshotCheckResult(
);

const functionGasHasOutput =
functionGasSnapshotsCheck.written ||
functionGasSnapshotsCheck.noBaseline ||
functionGasSnapshotsCheck.comparison.changed.length > 0 ||
functionGasSnapshotsCheck.comparison.added.length > 0 ||
functionGasSnapshotsCheck.comparison.removed.length > 0;
const snapshotCheatcodesHasOutput =
snapshotCheatcodesCheck.written ||
snapshotCheatcodesCheck.noBaseline ||
snapshotCheatcodesCheck.comparison.changed.length > 0 ||
snapshotCheatcodesCheck.comparison.added.length > 0 ||
snapshotCheatcodesCheck.comparison.removed.length > 0;
Expand Down
Loading
Loading