Skip to content
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

Throw error and exit if config file isn't readable #88

Merged
merged 6 commits into from
Sep 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
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
Loading