Skip to content

Commit

Permalink
Parse only env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
schaable committed Jun 25, 2024
1 parent 22abbd5 commit 665a755
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 39 deletions.
16 changes: 11 additions & 5 deletions v-next/core/src/internal/global-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,21 @@ export function resolveGlobalOptions(
-- GlobalOptions is empty for user extension, so we need to cast it to
assign the value. */
(userProvidedGlobalOptions as Record<string, string | undefined>)[name];
if (value === undefined) {
value = process.env[`HARDHAT_${camelToSnakeCase(name).toUpperCase()}`];
}

let parsedValue: ParameterValue;
// if the value is provided in the user options, it's already parsed
// and it takes precedence over env vars
if (value !== undefined) {
parsedValue = parseParameterValue(value, option.parameterType, name);
parsedValue = value;
} else {
parsedValue = option.defaultValue;
value = process.env[`HARDHAT_${camelToSnakeCase(name).toUpperCase()}`];
if (value !== undefined) {
// if the value is provided via an env var, it needs to be parsed
parsedValue = parseParameterValue(value, option.parameterType, name);
} else {
// if the value is not provided by the user or env var, use the default
parsedValue = option.defaultValue;
}
}

/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
Expand Down
37 changes: 3 additions & 34 deletions v-next/core/test/internal/global-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ describe("Global Options", () => {

const globalOptions = resolveGlobalOptions(
{
param1: "false",
param1: false,
param2: "user",
},
globalOptionsMap,
Expand Down Expand Up @@ -340,7 +340,7 @@ describe("Global Options", () => {

const globalOptions = resolveGlobalOptions(
{
param1: "false",
param1: false,
param2: "user",
},
globalOptionsMap,
Expand Down Expand Up @@ -371,7 +371,7 @@ describe("Global Options", () => {

const globalOptions = resolveGlobalOptions(
{
param1: "false",
param1: false,
param2: "user",
},
globalOptionsMap,
Expand All @@ -382,37 +382,6 @@ describe("Global Options", () => {
});
});

it("should throw if the provided option is not valid", () => {
const globalOptionsMap = buildGlobalOptionsMap([
{
id: "plugin1",
globalOptions: [
buildGlobalOptionDefinition({
name: "param1",
description: "param1 description",
parameterType: ParameterType.BOOLEAN,
defaultValue: true,
}),
],
},
]);

assert.throws(
() =>
resolveGlobalOptions(
{
param1: "not a boolean",
},
globalOptionsMap,
),
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
value: "not a boolean",
name: "param1",
type: ParameterType.BOOLEAN,
}),
);
});

it("should throw if the environment variable is not valid", () => {
const globalOptionsMap = buildGlobalOptionsMap([
{
Expand Down

0 comments on commit 665a755

Please sign in to comment.