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

buildGlobalParameterDefinition implementation & testing #5390

Merged
merged 6 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions v-next/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ParameterTypeToValueType } from "./types/common.js";
import type { ConfigurationVariable } from "./types/config.js";
import type { GlobalParameter } from "./types/global-parameters.js";
import type {
Expand Down Expand Up @@ -57,11 +58,11 @@ export function overrideTask(
/**
* Defines a global parameter.
*/
export function globalParameter(options: {
export function globalParameter<T extends ParameterType>(options: {
name: string;
description: string;
parameterType: ParameterType;
defaultValue: any;
parameterType?: T;
defaultValue: ParameterTypeToValueType<T>;
}): GlobalParameter {
return buildGlobalParameterDefinition(options);
}
Expand Down
62 changes: 49 additions & 13 deletions v-next/core/src/internal/global-parameters.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import type { ParameterType } from "../types/common.js";
import type { ParameterTypeToValueType } from "../types/common.js";
import type {
GlobalArguments,
GlobalParameter,
GlobalParameterMap,
} from "../types/global-parameters.js";
import type { HardhatPlugin } from "../types/plugins.js";

import { HardhatError } from "@nomicfoundation/hardhat-errors";

import { ParameterType } from "../types/common.js";

import {
RESERVED_PARAMETER_NAMES,
isParameterValueValid,
isValidParamNameCasing,
} from "./parameters.js";

/**
* Builds a map of the global parameters, validating them.
*
* Note: this function can be used before initializing the HRE, so the plugins
* shouldn't be consider validated. Hence, we should validate the global
* parameters.
* shouldn't be consider validated. Hence, we should validate the global
* parameters.
*/
export function buildGlobalParameterMap(
resolvedPlugins: HardhatPlugin[],
Expand Down Expand Up @@ -48,21 +58,47 @@ export function buildGlobalParameterMap(
return globalParametersIndex;
}

export function buildGlobalParameterDefinition(options: {
export function buildGlobalParameterDefinition<T extends ParameterType>({
name,
description,
parameterType,
defaultValue,
}: {
name: string;
description: string;
parameterType: ParameterType;
defaultValue: any;
parameterType?: T;
defaultValue: ParameterTypeToValueType<T>;
Copy link
Member Author

Choose a reason for hiding this comment

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

@alcuadrado I think parameterType should be just type to be consistent with task parameters. wdyt? I'll change it on a follow up PR if you agree.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added an issue to track it.

}): GlobalParameter {
// TODO: Validate name casing
// TODO: Validate default value matches with type
// TODO: Validate that the name is not one of the reserved ones in parameters.ts
const type = parameterType ?? ParameterType.STRING;

if (!isValidParamNameCasing(name)) {
throw new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_NAME, {
name,
});
}

if (RESERVED_PARAMETER_NAMES.has(name)) {
throw new HardhatError(HardhatError.ERRORS.ARGUMENTS.RESERVED_NAME, {
name,
});
}

if (!isParameterValueValid(type, defaultValue)) {
throw new HardhatError(
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
{
value: defaultValue,
name: "defaultValue",
type: parameterType,
},
);
}

return {
name: options.name,
description: options.description,
parameterType: options.parameterType,
defaultValue: options.defaultValue,
name,
description,
parameterType: type,
defaultValue,
};
}

Expand Down
6 changes: 3 additions & 3 deletions v-next/core/src/types/global-parameters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ParameterType } from "./common.js";
import type { ParameterType, ParameterTypeToValueType } from "./common.js";

/**
* A global parameter with an associated value and a default if not provided by
Expand All @@ -13,11 +13,11 @@ import type { ParameterType } from "./common.js";
*
* If both are present, the second one takes precedence.
*/
export interface GlobalParameter {
export interface GlobalParameter<T extends ParameterType = ParameterType> {
name: string;
description: string;
parameterType: ParameterType;
defaultValue: any;
defaultValue: ParameterTypeToValueType<T>;
}

/**
Expand Down
95 changes: 95 additions & 0 deletions v-next/core/test/internal/global-parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { HardhatError } from "@nomicfoundation/hardhat-errors";

import { ParameterType } from "../../src/config.js";
import { buildGlobalParameterDefinition } from "../../src/internal/global-parameters.js";
import { RESERVED_PARAMETER_NAMES } from "../../src/internal/parameters.js";

describe("Global Parameters", () => {
describe.todo("buildGlobalParameterMap", () => {
// TODO: Implement tests.
});

describe("buildGlobalParameterDefinition", () => {
it("should build a global parameter definition", () => {
const options = {
name: "foo",
description: "Foo description",
parameterType: ParameterType.BOOLEAN,
defaultValue: true,
};
const globalParameter = buildGlobalParameterDefinition(options);

assert.deepEqual(globalParameter, options);
});

it("should build a global parameter definition with a default type of STRING", () => {
const options = {
name: "foo",
description: "Foo description",
defaultValue: "bar",
};
const globalParameter = buildGlobalParameterDefinition(options);

assert.deepEqual(globalParameter, {
...options,
parameterType: ParameterType.STRING,
});
});

it("should throw if the parameter name is not valid", () => {
assert.throws(
() =>
buildGlobalParameterDefinition({
name: "foo bar",
description: "Foo description",
defaultValue: "bar",
}),
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_NAME, {
name: "foo bar",
}),
);
});

it("should throw if the parameter name is reserved", () => {
RESERVED_PARAMETER_NAMES.forEach((name) => {
assert.throws(
() =>
buildGlobalParameterDefinition({
name,
description: "Foo description",
defaultValue: "bar",
}),
new HardhatError(HardhatError.ERRORS.ARGUMENTS.RESERVED_NAME, {
name,
}),
);
});
});

it("should throw if the default value does not match the type", () => {
assert.throws(
() =>
buildGlobalParameterDefinition({
name: "foo",
description: "Foo description",
parameterType: ParameterType.BOOLEAN,
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
Intentionally testing an invalid type */
defaultValue: "bar" as any,
}),
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
value: "bar",
name: "defaultValue",
type: ParameterType.BOOLEAN,
}),
);
});
});

describe.todo("resolveGlobalArguments", () => {
// TODO: Implement tests.
});
});
9 changes: 4 additions & 5 deletions v-next/core/test/internal/tasks/task-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { describe, it } from "node:test";

import { HardhatError } from "@nomicfoundation/hardhat-errors";

import { ParameterType } from "../../../src/config.js";
import { ParameterType, globalParameter } from "../../../src/config.js";
import { createHardhatRuntimeEnvironment } from "../../../src/index.js";
import { buildGlobalParameterDefinition } from "../../../src/internal/global-parameters.js";
import {
EmptyTaskDefinitionBuilderImplementation,
NewTaskDefinitionBuilderImplementation,
Expand Down Expand Up @@ -46,7 +45,7 @@ describe("TaskManagerImplementation", () => {
.build(),
],
globalParameters: [
buildGlobalParameterDefinition({
globalParameter({
name: "globalParam1",
description: "",
parameterType: ParameterType.STRING,
Expand Down Expand Up @@ -392,7 +391,7 @@ describe("TaskManagerImplementation", () => {
{
id: "plugin2",
globalParameters: [
buildGlobalParameterDefinition({
globalParameter({
name: "param1",
description: "",
parameterType: ParameterType.STRING,
Expand Down Expand Up @@ -430,7 +429,7 @@ describe("TaskManagerImplementation", () => {
{
id: "plugin2",
globalParameters: [
buildGlobalParameterDefinition({
globalParameter({
name: "param1",
description: "",
parameterType: ParameterType.STRING,
Expand Down
Loading