Skip to content

Commit

Permalink
chore: and .env file to unit test buck run
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Oct 17, 2023
1 parent 6539304 commit c4b12a6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
3 changes: 2 additions & 1 deletion core/api/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ madge_check(

test_unit(
name = "test-unit",
srcs = [":src"] + [":test_src"] + glob["galoy.yaml"],
srcs = [":src"] + [":test_src"] + glob([".env", "galoy.yaml"]),
config_file = "test/unit/jest.config.js",
env_file = ".env",
env = {
"LOGLEVEL": "warn",
}
Expand Down
6 changes: 6 additions & 0 deletions toolchains/workspace-pnpm/macros.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ def _npm_test_impl(
cmd_args([build_context.workspace_root, ctx.label.package], delimiter = "/"),
"--bin",
cmd_args(program_run_info),
"--env-file",
ctx.attrs.env_file,
"--",
program_args,
])
Expand Down Expand Up @@ -904,6 +906,10 @@ _test_unit = rule(
attrs.string(),
doc = """File name and relative path for jest config.""",
),
"env_file": attrs.option(
attrs.string(),
doc = """File name and relative path for env variables required.""",
),
"env": attrs.dict(
key = attrs.string(),
value = attrs.arg(),
Expand Down
22 changes: 17 additions & 5 deletions toolchains/workspace-pnpm/run_in_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
Runs a program in a directory.
"""
import argparse
import json
import os
import subprocess
import sys

def compute_path(arg: str) -> str:
if arg.endswith("::abspath"):
return os.path.abspath(arg.removesuffix("::abspath"))
def merge_env_from_file(file_path):
# Shell command to source the .env file and then get the environment as JSON using jq
if os.path.exists(file_path):
cmd = f'source {file_path} && jq -n env'
else:
return arg
cmd = f'jq -n env'

result = subprocess.run(cmd, capture_output=True, text=True, shell=True, executable="/bin/bash")
if result.returncode != 0:
raise RuntimeError(result.stderr)

return json.loads(result.stdout)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
Expand All @@ -24,6 +31,10 @@ def compute_path(arg: str) -> str:
"--bin",
help="Binary to execute program with",
)
parser.add_argument(
"--env-file",
help="Env file to load variables from",
)
parser.add_argument(
"args",
help="Program and arguments",
Expand All @@ -34,6 +45,7 @@ def compute_path(arg: str) -> str:
bin_args = args.args[1:] # ignore '--' separator
cmd = [os.path.abspath(args.bin), *bin_args]

exit_code = subprocess.call(cmd, cwd=args.cwd)
env = merge_env_from_file(args.env_file)
exit_code = subprocess.call(cmd, cwd=args.cwd, env=env)

sys.exit(exit_code)

0 comments on commit c4b12a6

Please sign in to comment.