Skip to content

Commit

Permalink
Merge pull request #88 from pangeo-forge/error-on-config
Browse files Browse the repository at this point in the history
Throw error and exit if config file isn't readable
  • Loading branch information
cisaacstern authored Sep 1, 2023
2 parents ce14ad5 + fd77b7d commit 35e7732
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pangeo_forge_runner/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,19 @@ def json_excepthook(self, etype, evalue, traceback):

def initialize(self, argv=None):
super().initialize(argv)
# Load traitlets config from a config file if present
self.load_config_file(self.config_file)
# Load traitlets config from a config file if passed
if self.config_file:
self.load_config_file(self.config_file)
if (
not os.path.exists(self.config_file)
and self.config_file != "pangeo_forge_runner_config.py"
):
# Throw an explicit error and exit if config file isn't present
print(
f"Could not read config from file {self.config_file}. Make sure it exists and is readable",
file=sys.stderr,
)
sys.exit(1)

# Allow arbitrary logging config if set
# We do this first up so any custom logging we set up ourselves
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_expand_meta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import os
import secrets
import subprocess

invocations = [
Expand Down Expand Up @@ -75,3 +77,12 @@ def test_expand_meta_no_json():
out = subprocess.check_output(cmd, encoding="utf-8")
last_line = out.splitlines()[-1]
assert json.loads(last_line) == invocation["meta"]


def test_missing_config_file():
non_existent_path = secrets.token_hex() + ".py"
assert not os.path.exists(non_existent_path)
cmd = ["pangeo-forge-runner", "expand-meta", "--config", non_existent_path]
proc = subprocess.run(cmd, encoding="utf-8", capture_output=True, text=True)
assert proc.returncode == 1
assert "Could not read config from file" in proc.stderr

0 comments on commit 35e7732

Please sign in to comment.