From 665a755469f337054bcb80d64bcba9d4c3c64aaf Mon Sep 17 00:00:00 2001 From: Luis Schaab Date: Tue, 25 Jun 2024 11:26:03 -0300 Subject: [PATCH] Parse only env vars --- v-next/core/src/internal/global-options.ts | 16 ++++++--- v-next/core/test/internal/global-options.ts | 37 ++------------------- 2 files changed, 14 insertions(+), 39 deletions(-) diff --git a/v-next/core/src/internal/global-options.ts b/v-next/core/src/internal/global-options.ts index ef6f073241..07d7004c30 100644 --- a/v-next/core/src/internal/global-options.ts +++ b/v-next/core/src/internal/global-options.ts @@ -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)[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 diff --git a/v-next/core/test/internal/global-options.ts b/v-next/core/test/internal/global-options.ts index e3a8cd9abc..b8dfef14ec 100644 --- a/v-next/core/test/internal/global-options.ts +++ b/v-next/core/test/internal/global-options.ts @@ -303,7 +303,7 @@ describe("Global Options", () => { const globalOptions = resolveGlobalOptions( { - param1: "false", + param1: false, param2: "user", }, globalOptionsMap, @@ -340,7 +340,7 @@ describe("Global Options", () => { const globalOptions = resolveGlobalOptions( { - param1: "false", + param1: false, param2: "user", }, globalOptionsMap, @@ -371,7 +371,7 @@ describe("Global Options", () => { const globalOptions = resolveGlobalOptions( { - param1: "false", + param1: false, param2: "user", }, globalOptionsMap, @@ -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([ {