Skip to content

Commit

Permalink
feat: define solidity test sources in config
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh committed Oct 28, 2024
1 parent 3cc4a6b commit d72a140
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 5 deletions.
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;
};
15 changes: 15 additions & 0 deletions v-next/hardhat/src/internal/builtin-plugins/solidity-test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import { task } from "../../core/config.js";

const hardhatPlugin: HardhatPlugin = {
id: "builtin:solidity-tests",
dependencies: [
async () => {
const { default: artifactsPlugin } = await import(
"../artifacts/index.js"
);
return artifactsPlugin;
},
async () => {
const { default: solidityPlugin } = await import("../solidity/index.js");
return solidityPlugin;
},
],
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
@@ -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[];
}
}
12 changes: 7 additions & 5 deletions v-next/hardhat/test/internal/core/hook-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ describe("HookManager", () => {
_context: HookContext,
givenHre: HardhatRuntimeEnvironment,
): Promise<void> => {
givenHre.config.paths.tests =
"./test-folder-from-plugin1";
givenHre.config.paths.tests = {
solidity: ["./test-folder-from-plugin1"],
};
},
} as Partial<HardhatRuntimeEnvironmentHooks>;

Expand All @@ -277,8 +278,9 @@ describe("HookManager", () => {
_context: HookContext,
givenHre: HardhatRuntimeEnvironment,
): Promise<void> => {
givenHre.config.paths.tests =
"./test-folder-from-overriding-plugin2";
givenHre.config.paths.tests = {
solidity: ["./test-folder-from-overriding-plugin2"],
};
},
} as Partial<HardhatRuntimeEnvironmentHooks>;

Expand All @@ -302,7 +304,7 @@ describe("HookManager", () => {

assert.equal(result.length, 2);
assert.equal(
hre.config.paths.tests,
hre.config.paths.tests.solidity[0],
"./test-folder-from-overriding-plugin2",
);
});
Expand Down

0 comments on commit d72a140

Please sign in to comment.