Skip to content

Commit

Permalink
Throw error and exit if config file isn't readable
Browse files Browse the repository at this point in the history
Fixes #73
  • Loading branch information
yuvipanda committed Aug 19, 2023
1 parent a0fef17 commit c8b6844
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pangeo_forge_runner/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,17 @@ 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:
if os.path.exists(self.config_file):
# 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)

self.load_config_file(self.config_file)

# Allow arbitrary logging config if set
# We do this first up so any custom logging we set up ourselves
Expand Down
10 changes: 10 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,11 @@ 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")
assert proc.returncode == 1

0 comments on commit c8b6844

Please sign in to comment.