diff --git a/src/baseCommand.ts b/src/baseCommand.ts index ffa1f68..e638759 100644 --- a/src/baseCommand.ts +++ b/src/baseCommand.ts @@ -15,6 +15,7 @@ export abstract class BaseCommand extends Command { description: "Network used to create UNIK nft token", required: true, options: UTILS.getNetworksList(), + env: "UNS_NETWORK", }), verbose: oFlags.boolean({ char: "v", @@ -48,6 +49,15 @@ export abstract class BaseCommand extends Command { /** * Configuration */ + + // This happen when providing network through env var + if (!UTILS.getNetworksList().includes(flags.network)) { + throw new Error(`Expected --network=${ + flags.network + } to be one of: ${UTILS.getNetworksListListForDescription()} + See more help with --help`); + } + const networkName = flags.network === "local" ? "testnet" : flags.network; const networkPreset = configManager.getPreset(networkName); diff --git a/test/__fixtures__/commands/status.ts b/test/__fixtures__/commands/status.ts index 05899a1..e366b3d 100644 --- a/test/__fixtures__/commands/status.ts +++ b/test/__fixtures__/commands/status.ts @@ -54,4 +54,16 @@ export const outputCases = [ args: ["status", "--network", "devnet", "--format", "table", "--verbose"], expected: infoNode + statusResultTable, }, + { + description: "Should use env var and return devnet status json", + args: ["status"], + expected: statusResultJson, + UNS_NETWORK: "devnet", + }, + { + description: "Should not use env var and return devnet status json", + args: ["status", "--network", "devnet"], + expected: statusResultJson, + UNS_NETWORK: "customnetwork", + }, ]; diff --git a/test/commands/status.test.ts b/test/commands/status.test.ts index e820f12..4ecc0b9 100644 --- a/test/commands/status.test.ts +++ b/test/commands/status.test.ts @@ -1,5 +1,7 @@ import { expect, test } from "@oclif/test"; import { NETWORKS } from "../../src/config"; +process.env.DEV_MODE = "false"; +import * as UTILS from "../../src/utils"; import { outputCases } from "../__fixtures__/commands/status"; const applyTestCase = (testCase: any) => { @@ -31,6 +33,7 @@ const applyTestCase = (testCase: any) => { }, }), ) + .env({ UNS_NETWORK: testCase.UNS_NETWORK }) .stdout() .command(testCase.args) .it(testCase.description, ctx => { @@ -49,5 +52,15 @@ describe("status command", () => { // tslint:disable-next-line:no-empty .it("Should exit with code 2 if network is not known", _ => {}); + const network = "customNetwork"; + test.env({ UNS_NETWORK: network }) + .command(["status"]) + .catch( + `Expected --network=${network} to be one of: ${UTILS.getNetworksListListForDescription()} + See more help with --help`, + ) + // tslint:disable-next-line:no-empty + .it("Should use UNS_NETWORK env var then throw error", _ => {}); + outputCases.forEach(testCase => applyTestCase(testCase)); });