Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
feat: use env var UNS_NETWORK to select network
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Oct 7, 2019
1 parent 9aa8aef commit 32b7f0b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions test/__fixtures__/commands/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
];
13 changes: 13 additions & 0 deletions test/commands/status.test.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -31,6 +33,7 @@ const applyTestCase = (testCase: any) => {
},
}),
)
.env({ UNS_NETWORK: testCase.UNS_NETWORK })
.stdout()
.command(testCase.args)
.it(testCase.description, ctx => {
Expand All @@ -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));
});

0 comments on commit 32b7f0b

Please sign in to comment.