Skip to content

Commit

Permalink
refactor: change env_json target handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Feb 16, 2024
1 parent 0c3f0e3 commit f9cbbd4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 38 deletions.
4 changes: 2 additions & 2 deletions core/api/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ jest_test(
name = "unit-tests",
srcs = [":src"] + [":test_src"] + glob(["galoy.yaml"]),
config_file = "test/unit/jest.config.js",
env_json_target = "//dev:env.json",
env_json = "//dev:env.json",
env = {
"LOGLEVEL": "warn",
}
Expand All @@ -194,7 +194,7 @@ jest_test(
prod_deps_srcs = prod_deps_srcs,
config_file = "test/integration/jest.config.js",
run_serially = True,
env_json_target = "//dev:env.json",
env_json = "//dev:env.json",
env = {
"LOGLEVEL": "warn",
}
Expand Down
10 changes: 5 additions & 5 deletions toolchains/workspace-pnpm/macros.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -634,9 +634,9 @@ def _npm_test_impl(
cmd_args(program_run_info),
])

if hasattr(ctx.attrs, 'env_json_target'):
run_cmd_args.add("--env-json-target")
run_cmd_args.add(ctx.attrs.env_json_target)
if hasattr(ctx.attrs, 'env_json'):
run_cmd_args.add("--env-json")
run_cmd_args.add(cmd_args(ctx.attrs.env_json))

run_cmd_args.add("--")
run_cmd_args.add(program_args)
Expand Down Expand Up @@ -1165,8 +1165,8 @@ _jest_test = rule(
default = False,
doc = "Run all tests serially in the current process"
),
"env_json_target": attrs.option(
attrs.string(),
"env_json": attrs.option(
attrs.source(),
doc = """buck2 target for json file with env variables required.""",
default = None,
),
Expand Down
36 changes: 5 additions & 31 deletions toolchains/workspace-pnpm/run_npm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,6 @@
import subprocess
import sys

def env_dict_from_target(target):
repo_root_cmd = ["git", "rev-parse", "--show-toplevel"]
repo_root_res = subprocess.run(repo_root_cmd, capture_output=True, text=True)
if repo_root_res.returncode != 0:
raise RuntimeError(repo_root_res.stderr)
repo_root = repo_root_res.stdout.strip()

env_json_cmd = [
"buck2",
"--isolation-dir",
"buck-out",
"uquery",
"inputs(deps('{}'))".format(target),
]
env_json_res = subprocess.run(env_json_cmd, capture_output=True, text=True)
if env_json_res.returncode != 0:
raise RuntimeError(env_json_res.stderr)
env_json = env_json_res.stdout.strip()


abs_file_path = "{}/{}".format(repo_root, env_json)
with open(abs_file_path, 'r') as file:
env_dict = json.load(file)

return env_dict


if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
Expand All @@ -46,7 +19,7 @@ def env_dict_from_target(target):
help="Binary to execute program with",
)
parser.add_argument(
"--env-json-target",
"--env-json",
help="Env file to load variables from",
)
parser.add_argument(
Expand All @@ -58,9 +31,10 @@ def env_dict_from_target(target):
args = parser.parse_args()

env = os.environ.copy()
if (args.env_json_target):
env_json = env_dict_from_target(args.env_json_target)
env = {**env, **env_json}
if (args.env_json):
with open(args.env_json, 'r') as file:
env_dict = json.load(file)
env = {**env, **env_dict}

bin_args = args.args[1:] # ignore '--' separator
if env.get("TEST"):
Expand Down

0 comments on commit f9cbbd4

Please sign in to comment.