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: define solidity test sources in config #5870

Open
wants to merge 7 commits into
base: v-next
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions v-next/example-project/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ const config: HardhatUserConfig = {
tests: {
mocha: "test/mocha",
nodeTest: "test/node",
solidity: [
"contracts/Counter.sol",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semantics of this are slightly different than what the other tests entries use. Here this is "files to be compiled", while the others are "folders where tests can be found".

If we use the rules to find solidity tests files discussed in my other comment, the "folders" approach makes more sense.

"contracts/Counter.t.sol",
"contracts/WithForge.t.sol",
],
},
},
solidity: {
Expand Down
153 changes: 103 additions & 50 deletions v-next/hardhat/src/internal/builtin-plugins/solidity-test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,119 @@
import type { ArtifactsManager } from "../../../types/artifacts.js";
import type { Artifact } from "@ignored/edr";
import type {
Artifact as HardhatArtifact,
BuildInfo,
} from "../../../types/artifacts.js";
import type {
CompilationJobCreationError,
FailedFileBuildResult,
FileBuildResult,
} from "../../../types/solidity/build-system.js";
import type {
ArtifactId as EdrArtifactId,
Artifact as EdrArtifact,
} 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";
import path from "node:path";

export async function getArtifacts(
hardhatArtifacts: ArtifactsManager,
): Promise<Artifact[]> {
const fqns = await hardhatArtifacts.getAllFullyQualifiedNames();
const artifacts: Artifact[] = [];
import { HardhatError } from "@ignored/hardhat-vnext-errors";
import { readJsonFile } from "@ignored/hardhat-vnext-utils/fs";

for (const fqn of fqns) {
const hardhatArtifact = await hardhatArtifacts.readArtifact(fqn);
const buildInfo = await hardhatArtifacts.getBuildInfo(fqn);
import { FileBuildResultType } from "../../../types/solidity/build-system.js";

if (buildInfo === undefined) {
throw new HardhatError(
HardhatError.ERRORS.SOLIDITY_TESTS.BUILD_INFO_NOT_FOUND_FOR_CONTRACT,
{
fqn,
},
);
}
type SolidityBuildResults =
| Map<string, FileBuildResult>
| CompilationJobCreationError;
type SuccessfulSolidityBuildResults = Map<
string,
Exclude<FileBuildResult, FailedFileBuildResult>
>;

const id = {
name: hardhatArtifact.contractName,
solcVersion: buildInfo.solcVersion,
source: hardhatArtifact.sourceName,
};
/**
* This function asserts that the given Solidity build results are successful.
* It throws a HardhatError if the build results indicate that the compilation
* job failed.
*/
export function throwIfSolidityBuildFailed(
results: SolidityBuildResults,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a future PR: we should use the same implementation here and during compile.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly to have a consistent ui

): asserts results is SuccessfulSolidityBuildResults {
if ("reason" in results) {
throw new HardhatError(
HardhatError.ERRORS.SOLIDITY.COMPILATION_JOB_CREATION_ERROR,
{
reason: results.formattedReason,
rootFilePath: results.rootFilePath,
buildProfile: results.buildProfile,
},
);
}

const contract = {
abi: JSON.stringify(hardhatArtifact.abi),
bytecode: hardhatArtifact.bytecode,
deployedBytecode: hardhatArtifact.deployedBytecode,
};
const sucessful = [...results.values()].every(
({ type }) =>
type === FileBuildResultType.CACHE_HIT ||
type === FileBuildResultType.BUILD_SUCCESS,
);

const artifact = { id, contract };
artifacts.push(artifact);
if (!sucessful) {
throw new HardhatError(HardhatError.ERRORS.SOLIDITY.BUILD_FAILED);
}

return artifacts;
}

export async function isTestArtifact(
root: string,
artifact: Artifact,
): Promise<boolean> {
const { source } = artifact.id;
/**
* This function returns the artifacts generated during the compilation associated
* with the given Solidity build results. It relies on the fact that each successful
* build result has a corresponding artifact generated property.
*/
export async function getArtifacts(
results: SuccessfulSolidityBuildResults,
artifactsRootPath: string,
): Promise<EdrArtifact[]> {
const artifacts: EdrArtifact[] = [];

if (!source.endsWith(".t.sol")) {
return false;
}
for (const [source, result] of results.entries()) {
for (const artifactPath of result.contractArtifactsGenerated) {
const artifact: HardhatArtifact = await readJsonFile(artifactPath);
const buildInfo: BuildInfo = await readJsonFile(
path.join(artifactsRootPath, "build-info", `${result.buildId}.json`),
);

// 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);
const id = {
name: artifact.contractName,
solcVersion: buildInfo.solcVersion,
source,
};

if (!sourceExists) {
return false;
const contract = {
abi: JSON.stringify(artifact.abi),
bytecode: artifact.bytecode,
deployedBytecode: artifact.deployedBytecode,
};

artifacts.push({
id,
contract,
});
}
}

return true;
return artifacts;
}

/**
* This function returns the test suite ids associated with the given artifacts.
* The test suite ID is the relative path of the test file, relative to the
* project root.
*/
export async function getTestSuiteIds(
artifacts: EdrArtifact[],
rootFilePaths: string[],
projectRoot: string,
): Promise<EdrArtifactId[]> {
const testSources = rootFilePaths
.filter((p) => {
return p.endsWith(".t.sol");
})
.map((p) => path.relative(projectRoot, p));

return artifacts
.map(({ id }) => id)
.filter(({ source }) => testSources.includes(source));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { ConfigHooks } from "@ignored/hardhat-vnext/types/hooks";

import { isObject } from "@ignored/hardhat-vnext-utils/lang";
import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path";
import {
conditionalUnionType,
validateUserConfigZodType,
} from "@ignored/hardhat-vnext-zod-utils";
import { z } from "zod";

const userConfigType = z.object({
paths: z
.object({
test: conditionalUnionType(
[
[isObject, z.object({ solidity: z.string().optional() })],
[(data) => typeof data === "string", z.string()],
],
"Expected a string or an object with an optional 'solidity' property",
).optional(),
})
.optional(),
});

export default async (): Promise<Partial<ConfigHooks>> => {
const handlers: Partial<ConfigHooks> = {
validateUserConfig: async (userConfig) => {
return validateUserConfigZodType(userConfig, userConfigType);
},
resolveUserConfig: async (
userConfig,
resolveConfigurationVariable,
next,
) => {
const resolvedConfig = await next(
userConfig,
resolveConfigurationVariable,
);

let testsPaths: string[];

// TODO: use isObject when the type narrowing issue is fixed
if (
typeof userConfig.paths?.tests === "object" &&
userConfig.paths.tests.solidity !== undefined
) {
if (Array.isArray(userConfig.paths.tests.solidity)) {
testsPaths = userConfig.paths.tests.solidity;
} else {
testsPaths = [userConfig.paths.tests.solidity];
}
} else {
testsPaths = resolvedConfig.paths.sources.solidity;
}

const resolvedPaths = testsPaths.map((p) =>
resolveFromRoot(resolvedConfig.paths.root, p),
);

return {
...resolvedConfig,
paths: {
...resolvedConfig.paths,
tests: {
...resolvedConfig.paths.tests,
solidity: resolvedPaths,
},
},
};
},
};

return handlers;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import type { HardhatPlugin } from "../../../types/plugins.js";
import { ArgumentType } from "../../../types/arguments.js";
import { task } from "../../core/config.js";

import "./type-extensions.js";

const hardhatPlugin: HardhatPlugin = {
id: "builtin:solidity-tests",
hookHandlers: {
config: import.meta.resolve("./hook-handlers/config.js"),
},
tasks: [
task(["test", "solidity"], "Run the Solidity tests")
.setAction(import.meta.resolve("./task-action.js"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import type { RunOptions } from "./runner.js";
import type { TestEvent } from "./types.js";
import type { BuildOptions } from "../../../types/solidity/build-system.js";
import type { NewTaskActionFunction } from "../../../types/tasks.js";

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

import {
getAllFilesMatching,
isDirectory,
} from "@ignored/hardhat-vnext-utils/fs";
import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path";
import { createNonClosingWriter } from "@ignored/hardhat-vnext-utils/stream";

import { getArtifacts, isTestArtifact } from "./helpers.js";
import { shouldMergeCompilationJobs } from "../solidity/build-profiles.js";

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

Expand All @@ -19,25 +31,46 @@ const runSolidityTests: NewTaskActionFunction<TestActionArguments> = async (
{ timeout, noCompile },
hre,
) => {
if (!noCompile) {
await hre.tasks.getTask("compile").run({});
console.log();
}

const artifacts = await getArtifacts(hre.artifacts);
const testSuiteIds = (
const rootFilePaths = (
await Promise.all(
artifacts.map(async (artifact) => {
if (await isTestArtifact(hre.config.paths.root, artifact)) {
return artifact.id;
}
}),
hre.config.paths.tests.solidity
.map((p) => resolveFromRoot(hre.config.paths.root, p))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would change based on the other comments

.map(async (p) => {
// NOTE: The paths specified in the `paths.tests.solidity` array
// can be either directories or files.
if (await isDirectory(p)) {
return getAllFilesMatching(p, (f) => f.endsWith(".sol"));
} else if (p.endsWith(".sol") === true) {
return [p];
} else {
return [];
}
}),
)
).filter((artifact) => artifact !== undefined);
).flat(1);

const buildOptions: BuildOptions = {
// NOTE: The uncached sources will still be compiled event if `noCompile`
// is true. We could consider adding a `cacheOnly` option to support true
// `noCompile` behavior.
force: !noCompile,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I run npx hardhat test solidity it will force and recompile everything

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add an issue and address it after we have the compilation cache in place, and when we have the split between production and test compilation caches or while implementing that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also consider removing this option

buildProfile: hre.globalOptions.buildProfile,
mergeCompilationJobs: shouldMergeCompilationJobs(
hre.globalOptions.buildProfile,
),
quiet: false,
};

if (testSuiteIds.length === 0) {
return;
}
const results = await hre.solidity.build(rootFilePaths, buildOptions);

throwIfSolidityBuildFailed(results);

const artifacts = await getArtifacts(results, hre.config.paths.artifacts);
const testSuiteIds = await getTestSuiteIds(
artifacts,
rootFilePaths,
hre.config.paths.root,
);

console.log("Running Solidity tests");
console.log();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "@ignored/hardhat-vnext/types/config";

declare module "@ignored/hardhat-vnext/types/config" {
export interface TestPathsUserConfig {
solidity?: string | string[];
}

export interface TestPathsConfig {
solidity: string[];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface SolidityBuildSystemOptions {

export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
readonly #options: SolidityBuildSystemOptions;
readonly #defaultConcurrenty = Math.max(os.cpus().length - 1, 1);
readonly #defaultConcurrency = Math.max(os.cpus().length - 1, 1);
#downloadedCompilers = false;

constructor(options: SolidityBuildSystemOptions) {
Expand Down Expand Up @@ -118,7 +118,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
compilationJobs,
(compilationJob) => this.runCompilationJob(compilationJob),
{
concurrency: options?.concurrency ?? this.#defaultConcurrenty,
concurrency: options?.concurrency ?? this.#defaultConcurrency,
// An error when running the compiler is not a compilation failure, but
// a fatal failure trying to run it, so we just throw on the first error
stopOnError: true,
Expand Down
Loading
Loading