Skip to content

Commit

Permalink
chore: simplify the test artifact discovery filter
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh committed Oct 24, 2024
1 parent 0b00cc1 commit 9dd05af
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 98 deletions.
49 changes: 19 additions & 30 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion v-next/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.3",
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.3",
"@ignored/hardhat-vnext-zod-utils": "workspace:^3.0.0-next.3",
"@nomicfoundation/slang": "^0.18.2",
"@nomicfoundation/solidity-analyzer": "^0.1.0",
"@sentry/node": "^5.18.1",
"adm-zip": "^0.4.16",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import type { ArtifactsManager } from "../../../types/artifacts.js";
import type { Artifact } from "@ignored/edr";

import { HardhatError } from "@ignored/hardhat-vnext-errors";
import { exists, readUtf8File } from "@ignored/hardhat-vnext-utils/fs";
import { exists } from "@ignored/hardhat-vnext-utils/fs";
import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path";
import { NonterminalKind, TerminalKind } from "@nomicfoundation/slang/cst";
import { Parser } from "@nomicfoundation/slang/parser";

export async function getArtifacts(
hardhatArtifacts: ArtifactsManager,
Expand Down Expand Up @@ -49,74 +47,20 @@ export async function isTestArtifact(
root: string,
artifact: Artifact,
): Promise<boolean> {
const { name, source, solcVersion } = artifact.id;
const { source } = artifact.id;

if (!source.endsWith(".t.sol")) {
return false;
}

// NOTE: We also check whether the file exists in the workspace to filter out
// the artifacts from node modules.
const sourcePath = resolveFromRoot(root, source);
const sourceExists = await exists(sourcePath);

if (!sourceExists) {
return false;
}

const content = await readUtf8File(sourcePath);
const parser = Parser.create(solcVersion);
const cursor = parser
.parse(NonterminalKind.SourceUnit, content)
.createTreeCursor();

while (
cursor.goToNextNonterminalWithKind(NonterminalKind.ContractDefinition)
) {
const nameCursor = cursor.spawn();
if (!nameCursor.goToNextTerminalWithKind(TerminalKind.Identifier)) {
continue;
}
if (nameCursor.node.unparse() !== name) {
continue;
}

const abstractCursor = cursor.spawn();
if (abstractCursor.goToNextTerminalWithKind(TerminalKind.AbstractKeyword)) {
return false;
}

const functionCursor = cursor.spawn();

while (
functionCursor.goToNextNonterminalWithKind(
NonterminalKind.FunctionDefinition,
)
) {
const functionNameCursor = functionCursor.spawn();
if (
!functionNameCursor.goToNextTerminalWithKind(TerminalKind.Identifier)
) {
continue;
}

const functionName = functionNameCursor.node.unparse();
if (
functionName.startsWith("test") ||
functionName.startsWith("invariant")
) {
const publicCursor = functionCursor.spawn();
if (publicCursor.goToNextTerminalWithKind(TerminalKind.PublicKeyword)) {
return true;
}

const externalCursor = functionCursor.spawn();
if (
externalCursor.goToNextTerminalWithKind(TerminalKind.ExternalKeyword)
) {
return true;
}
}
}
}

return false;
return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { isTestArtifact } from "../../../../src/internal/builtin-plugins/solidit
const testCases = [
{
contract: "Abstract",
expected: false,
expected: true,
},
{
contract: "NoTest",
expected: false,
expected: true,
},
{
contract: "PublicTest",
Expand All @@ -24,11 +24,11 @@ const testCases = [
},
{
contract: "PrivateTest",
expected: false,
expected: true,
},
{
contract: "InternalTest",
expected: false,
expected: true,
},
{
contract: "PublicInvariant",
Expand All @@ -40,11 +40,11 @@ const testCases = [
},
{
contract: "PrivateInvariant",
expected: false,
expected: true,
},
{
contract: "InternalInvariant",
expected: false,
expected: true,
},
];

Expand Down

0 comments on commit 9dd05af

Please sign in to comment.