Skip to content

Commit

Permalink
feat: allow the use of environment variables instead of CLI args
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbeggi committed Jan 31, 2022
1 parent 2fdd531 commit fab9ab0
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 13 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ An interface to a deployed Step Function, with a function to execute a Step Func
### Arguments

- When running tests with `jest` using `sls-test-tools` matchers there are certain parameters needed for `sls-test-tools` to make assertions.
- These are passed as command line arguments, using quotation to match `jests` convention on test arguments.
- These are passed either as command line arguments, using quotation to match `jests` convention on test arguments, or by using environment variables. CLI arguments override environment variables.

**Required**

- `'--stack=my-service-dev'` - the CloudFormation stack name of the stack under test.
- `'--stack=my-service-dev'` or `process.env.CFN_STACK_NAME` - the CloudFormation stack name of the stack under test.

**Optional**

- `'--profile=[PROFILE NAME]'` (will default to `default`)
- `'--region=[AWS Region]'` (will default to `eu-west-2`)
- `'--profile=[PROFILE NAME]'` or `process.env.AWS_PROFILE` (will default to `default`)
- `'--region=[AWS Region]'` or `process.env.AWS_REGION` (will default to `eu-west-2`)
- `'--keep=true'` - keeps testing resources up to avoid creation throttles (e.g. SQS Queue created for EventBridge assertions)

- To avoid issues we recommend `--runInBand`
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
},
"dependencies": {
"aws-sdk": "^2.711.0",
"import-all.macro": "^3.1.0"
"import-all.macro": "^3.1.0",
"yargs": "^17.3.1"
},
"devDependencies": {
"@babel/cli": "^7.16.0",
"@babel/core": "^7.16.0",
"@babel/plugin-transform-runtime": "^7.16.0",
"@babel/preset-env": "^7.16.0",
"@babel/preset-typescript": "^7.16.0",
"@types/yargs": "^17.0.8",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"@zerollup/ts-transform-paths": "^1.7.18",
Expand Down
26 changes: 18 additions & 8 deletions src/helpers/general.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import AWS, { AWSError } from "aws-sdk";
import { DescribeStacksOutput } from "aws-sdk/clients/cloudformation";
import { PromiseResult } from "aws-sdk/lib/request";

const profileArg = process.argv.filter((x) => x.startsWith("--profile="))[0];
const profile = profileArg ? profileArg.split("=")[1] : "default";
const stackArg = process.argv.filter((x) => x.startsWith("--stack="))[0];
const regionArg = process.argv.filter((x) => x.startsWith("--region="))[0];
export const region = regionArg ? regionArg.split("=")[1] : "eu-west-2";

export const stackName = stackArg.split("=")[1];
import { loadArg } from "./utils/loadArg";

export const stackName = loadArg({
cliArg: "stack",
processEnvName: "CFN_STACK_NAME",
});

const profile = loadArg({
cliArg: "profile",
processEnvName: "AWS_PROFILE",
defaultValue: "default",
});

export const region = loadArg({
cliArg: "region",
processEnvName: "AWS_REGION",
defaultValue: "eu-west-2",
});

let creds;

Expand Down
36 changes: 36 additions & 0 deletions src/helpers/utils/loadArg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import yargs from "yargs";

const argv = yargs(process.argv).argv as Record<string, unknown>;

const isNonEmptyString = (arg: unknown): arg is string =>
typeof arg === "string" && arg !== "";

export const loadArg = ({
cliArg,
processEnvName,
defaultValue,
}: {
cliArg: string;
processEnvName: string;
defaultValue?: string;
}): string => {
let arg = argv[cliArg];

if (isNonEmptyString(arg)) {
return arg;
}

arg = process.env[processEnvName];

if (isNonEmptyString(arg)) {
return arg;
}

if (defaultValue === undefined) {
throw new Error(
`--${cliArg} CLI argument or ${processEnvName} env var required.`
);
}

return defaultValue;
};
30 changes: 30 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,18 @@
dependencies:
"@types/node" "*"

"@types/yargs-parser@*":
version "20.2.1"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.1.tgz#3b9ce2489919d9e4fea439b76916abc34b2df129"
integrity sha512-7tFImggNeNBVMsn0vLrpn1H1uPrUBdnARPTpZoitY37ZrdJREzf7I16tMrlK3hen349gr1NYh8CmZQa7CTG6Aw==

"@types/yargs@^17.0.8":
version "17.0.8"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-17.0.8.tgz#d23a3476fd3da8a0ea44b5494ca7fa677b9dad4c"
integrity sha512-wDeUwiUmem9FzsyysEwRukaEdDNcwbROvQ9QGRKaLI6t+IltNzbn4/i4asmB10auvZGQCzSQ6t0GSczEThlUXw==
dependencies:
"@types/yargs-parser" "*"

"@typescript-eslint/eslint-plugin@^5.6.0":
version "5.7.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.7.0.tgz#12d54709f8ea1da99a01d8a992cd0474ad0f0aa9"
Expand Down Expand Up @@ -6087,6 +6099,11 @@ yargs-parser@^18.1.2:
camelcase "^5.0.0"
decamelize "^1.2.0"

yargs-parser@^21.0.0:
version "21.0.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.0.0.tgz#a485d3966be4317426dd56bdb6a30131b281dc55"
integrity sha512-z9kApYUOCwoeZ78rfRYYWdiU/iNL6mwwYlkkZfJoyMR1xps+NEBX5X7XmRpxkZHhXJ6+Ey00IwKxBBSW9FIjyA==

yargs@^15.0.2:
version "15.4.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.4.1.tgz#0d87a16de01aee9d8bec2bfbf74f67851730f4f8"
Expand Down Expand Up @@ -6117,6 +6134,19 @@ yargs@^16.2.0:
y18n "^5.0.5"
yargs-parser "^20.2.2"

yargs@^17.3.1:
version "17.3.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.3.1.tgz#da56b28f32e2fd45aefb402ed9c26f42be4c07b9"
integrity sha512-WUANQeVgjLbNsEmGk20f+nlHgOqzRFpiGWVaBrYGYIGANIIu3lWjoyi0fNlFmJkvfhCZ6BXINe7/W2O2bV4iaA==
dependencies:
cliui "^7.0.2"
escalade "^3.1.1"
get-caller-file "^2.0.5"
require-directory "^2.1.1"
string-width "^4.2.3"
y18n "^5.0.5"
yargs-parser "^21.0.0"

[email protected]:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
Expand Down

0 comments on commit fab9ab0

Please sign in to comment.