Skip to content

support .env file for environment variables. #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions commands/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module.exports = {
LAMBDA_CONFIG: "./lambdatest-config.json",
SUPPORTED_CYPRESS_VERSIONS: ["5", "6"],
WHITELISTED_ENV_VARS: ["CI_BUILD_ID", "PERCY_TOKEN", "QASE_REPORT", "QASE_RUN_ID", "QASE_RUN_NAME", "QASE_RUN_DESCRIPTION", "QASE_API_TOKEN", "QASE_API_BASE_URL", "QASE_ENVIRONMENT_ID", "QASE_SCREENSHOT_FOLDER", "QASE_SCREENSHOT_SENDING", "QASE_RUN_COMPLETE"],
BLOCKED_ENV_VARS: ["MANPATH", "NVM_CD_FLAGS", "TERM", "SHELL", "TMPDIR", "SSH_CLIENT", "NVM_PATH", "SSH_TTY", "NVM_DIR",
"USER", "PATH", "NVM_NODEJS_ORG_MIRROR", "PWD", "LANG", "SHLVL", "HOME", "LOGNAME", "SSH_CONNECTION", "LC_CTYPE", "NVM_BIN", "NVM_IOJS_ORG_MIRROR", "OLDPWD"],
BUILD_END_STATES:
"&status=running,queued,created,initiated,pqueued,error,lambda error,failed,completed,queue_timeout,idle_timeout,stopped,cancelled,passed,timeout,inactive",
BUILD_ERROR_STATES: "&status=error,lambda error,failed",
Expand Down
31 changes: 31 additions & 0 deletions commands/utils/set_args.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require("fs");
const path = require("path");
const process = require("process");
const { type } = require("os");
const dotenv = require('dotenv')

function write_file(file_path, content) {
fs.writeFileSync(file_path, content, function (err) {
Expand Down Expand Up @@ -385,6 +386,36 @@ function sync_args_from_cmd(args) {
}
}
}
let dot_env_vars = undefined;
if ("sys-env-keys" in args) {
dot_env_vars = args["sys-env-keys"];
} else if (lt_config["run_settings"] && lt_config["run_settings"]["sys_env_keys"]) {
dot_env_vars = lt_config["run_settings"]["sys_env_keys"];
}
let parsedEnv,envFile;
let envFilePath = path.join(".", `.env`)
if (dot_env_vars) {
dot_env_vars = dot_env_vars.trim();
dot_env_vars = dot_env_vars.split(",");
if ("envfl" in args) {
envFilePath = args["envfl"];
} else if (lt_config["run_settings"]["env_file"]) {
envFilePath = lt_config["run_settings"]["env_file"];
}

try {
envFile = fs.readFileSync(envFilePath, {encoding: 'utf8'})
parsedEnv = dotenv.parse(envFile)
for (index in dot_env_vars) {
let envKey = dot_env_vars[index]
let envValue = parsedEnv[envKey]
envs[envKey] = envValue
}
} catch (err) {
console.error("error in fetching environment variables from .env file",err);
}
}

lt_config["run_settings"]["sys_envs"] = envs;

if ("exclude_specs" in lt_config["run_settings"]) {
Expand Down
4 changes: 2 additions & 2 deletions commands/utils/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ module.exports = validate_config = function (lt_config, validation_configs) {
let envValue;
Object.keys(sys_envs).forEach(function (envKey) {
envValue = sys_envs[envKey];
if (envKey && !constants.WHITELISTED_ENV_VARS.includes(envKey)) {
if (envKey && constants.BLOCKED_ENV_VARS.includes(envKey)) {
reject(
`Usage of unwanted environment variable detected. Allowed variables are - ${constants.WHITELISTED_ENV_VARS}`
`Usage of unwanted environment variable detected. Blocked variables are - ${constants.BLOCKED_ENV_VARS}`
);
}
if (envValue == undefined || envValue === "") {
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ const argv = require("yargs")
describe: "system environment variables",
type: "string",
})
.option("sys-env-keys", {
alias: "sys-env-keys",
describe: "system environment variables from .env file",
type: "string",
})
.option("envfl", {
alias: "env_file",
describe: "path of .env file",
type: "string",
})
.option("npm-f", {
alias: "npm-force",
describe: "force npm install",
Expand Down
21 changes: 19 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lambdatest-cypress-cli",
"version": "3.0.18",
"version": "3.0.19",
"description": "The lambdatest-cypress-cli is LambdaTest's command-line interface (CLI) aimed to help you run your Cypress tests on LambdaTest platform.",
"homepage": "https://github.com/LambdaTest/lambdatest-cypress-cli",
"author": "LambdaTest <[email protected]>",
Expand All @@ -20,6 +20,7 @@
"@lambdatest/node-tunnel": "latest",
"archiver": "^5.1.0",
"async": "^3.2.3",
"dotenv": "^16.3.1",
"glob": "^7.1.6",
"mochawesome": "^7.1.3",
"node-stream-zip": "^1.15.0",
Expand Down