Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance the solidity test artifacts discovery #5829

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 19 additions & 15 deletions pnpm-lock.yaml

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

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { ArtifactsManager } from "../../../types/artifacts.js";
import type { ArtifactId, Artifact } from "@ignored/edr";
import type { Artifact } from "@ignored/edr";

import { HardhatError } from "@ignored/hardhat-vnext-errors";
import { exists } from "@ignored/hardhat-vnext-utils/fs";
import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path";

export async function buildSolidityTestsInput(
export async function getArtifacts(
hardhatArtifacts: ArtifactsManager,
isTestArtifact: (artifact: Artifact) => boolean = () => true,
): Promise<{ artifacts: Artifact[]; testSuiteIds: ArtifactId[] }> {
): Promise<Artifact[]> {
const fqns = await hardhatArtifacts.getAllFullyQualifiedNames();
const artifacts: Artifact[] = [];
const testSuiteIds: ArtifactId[] = [];

for (const fqn of fqns) {
const hardhatArtifact = await hardhatArtifacts.readArtifact(fqn);
Expand Down Expand Up @@ -38,10 +38,29 @@ export async function buildSolidityTestsInput(

const artifact = { id, contract };
artifacts.push(artifact);
if (isTestArtifact(artifact)) {
testSuiteIds.push(artifact.id);
}
}

return { artifacts, testSuiteIds };
return artifacts;
}

export async function isTestArtifact(
root: string,
artifact: Artifact,
): Promise<boolean> {
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;
}

return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { NewTaskActionFunction } from "../../../types/tasks.js";

import { finished } from "node:stream/promises";

import { buildSolidityTestsInput } from "./helpers.js";
import { getArtifacts, isTestArtifact } from "./helpers.js";
import { testReporter } from "./reporter.js";
import { run } from "./runner.js";

Expand All @@ -13,19 +13,16 @@ const runSolidityTests: NewTaskActionFunction = async ({ timeout }, hre) => {

console.log("\nRunning Solidity tests...\n");

const { artifacts, testSuiteIds } = await buildSolidityTestsInput(
hre.artifacts,
(artifact) => {
const sourceName = artifact.id.source;
const isTestArtifact =
sourceName.endsWith(".t.sol") &&
sourceName.startsWith("contracts/") &&
!sourceName.startsWith("contracts/forge-std/") &&
!sourceName.startsWith("contracts/ds-test/");

return isTestArtifact;
},
);
const artifacts = await getArtifacts(hre.artifacts);
const testSuiteIds = (
await Promise.all(
artifacts.map(async (artifact) => {
if (await isTestArtifact(hre.config.paths.root, artifact)) {
return artifact.id;
}
}),
)
).filter((artifact) => artifact !== undefined);

const config = {
projectRoot: hre.config.paths.root,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import type { Artifact } from "@ignored/edr";

import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { isTestArtifact } from "../../../../src/internal/builtin-plugins/solidity-test/helpers.js";

const testCases = [
{
contract: "Abstract",
expected: true,
},
{
contract: "NoTest",
expected: true,
},
{
contract: "PublicTest",
expected: true,
},
{
contract: "ExternalTest",
expected: true,
},
{
contract: "PrivateTest",
expected: true,
},
{
contract: "InternalTest",
expected: true,
},
{
contract: "PublicInvariant",
expected: true,
},
{
contract: "ExternalInvariant",
expected: true,
},
{
contract: "PrivateInvariant",
expected: true,
},
{
contract: "InternalInvariant",
expected: true,
},
];

describe("isTestArtifact", () => {
for (const { contract, expected } of testCases) {
it(`should return ${expected} for the ${contract} contract`, async () => {
const artifact: Artifact = {
id: {
name: contract,
source: `test-fixtures/Test.t.sol`,
solcVersion: "0.8.20",
},
contract: {
abi: "",
},
};
const actual = await isTestArtifact(import.meta.dirname, artifact);
assert.equal(actual, expected);
});
}

it("should return false if a file does not exist", async () => {
const artifact: Artifact = {
id: {
name: "Contract",
source: `test-fixtures/NonExistent.t.sol`,
solcVersion: "0.8.20",
},
contract: {
abi: "",
},
};
const actual = await isTestArtifact(import.meta.dirname, artifact);
assert.equal(actual, false);
});

it("should return false if the file has the wrong extension", async () => {
const artifact: Artifact = {
id: {
name: "Contract",
source: `test-fixtures/WrongExtension.sol`,
solcVersion: "0.8.20",
},
contract: {
abi: "",
},
};
const actual = await isTestArtifact(import.meta.dirname, artifact);
assert.equal(actual, false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

abstract contract Abstract {
function testPublic() public {}
function testExternal() external {}
function invariantPublic() public {}
function invariantExternal() external {}
}

contract NoTest {}

contract PublicTest {
function test() public {}
}

contract ExternalTest {
function test() external {}
}

contract PrivateTest {
function test() private {}
}

contract InternalTest {
function test() internal {}
}

contract PublicInvariant {
function invariant() public {}
}

contract ExternalInvariant {
function invariant() external {}
}

contract PrivateInvariant {
function invariant() private {}
}

contract InternalInvariant {
function invariant() internal {}
}