From 8bc787335620ad5664390771d969a7c7f6c712cb Mon Sep 17 00:00:00 2001 From: Pierre Seznec Date: Wed, 2 Oct 2019 18:27:58 +0200 Subject: [PATCH] feat: use env var UNS_NETWORK to select network --- src/baseCommand.ts | 12 ++++++++++++ test/__fixtures__/commands/status.ts | 12 ++++++++++++ test/commands/status.test.ts | 8 ++++++++ 3 files changed, 32 insertions(+) diff --git a/src/baseCommand.ts b/src/baseCommand.ts index 6c4705b..a419110 100644 --- a/src/baseCommand.ts +++ b/src/baseCommand.ts @@ -14,6 +14,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", @@ -49,6 +50,17 @@ export abstract class BaseCommand extends Command { /** * Configuration */ + + // This happen when providing network through env var + // see Oclif PR: https://github.com/oclif/parser/pull/64 + // will be removed if PR is accepted + 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 174f644..6b369f9 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..cd4c880 100644 --- a/test/commands/status.test.ts +++ b/test/commands/status.test.ts @@ -31,6 +31,7 @@ const applyTestCase = (testCase: any) => { }, }), ) + .env({ UNS_NETWORK: testCase.UNS_NETWORK }) .stdout() .command(testCase.args) .it(testCase.description, ctx => { @@ -49,5 +50,12 @@ 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(err => expect(err.message.match(`Expected --network=${network} to be one of`))) + // tslint:disable-next-line:no-empty + .it("Should use UNS_NETWORK env var then throw error", _ => {}); + outputCases.forEach(testCase => applyTestCase(testCase)); });