From c8b6844f3f520ecadba15b08ed63161ee9933554 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Sat, 19 Aug 2023 11:24:35 -0700 Subject: [PATCH] Throw error and exit if config file isn't readable Fixes https://github.com/pangeo-forge/pangeo-forge-runner/issues/73 --- pangeo_forge_runner/commands/base.py | 13 +++++++++++-- tests/unit/test_expand_meta.py | 10 ++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/pangeo_forge_runner/commands/base.py b/pangeo_forge_runner/commands/base.py index 30c29593..5201dbca 100644 --- a/pangeo_forge_runner/commands/base.py +++ b/pangeo_forge_runner/commands/base.py @@ -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 diff --git a/tests/unit/test_expand_meta.py b/tests/unit/test_expand_meta.py index cb5ee740..e80b3cc5 100644 --- a/tests/unit/test_expand_meta.py +++ b/tests/unit/test_expand_meta.py @@ -1,4 +1,6 @@ import json +import os +import secrets import subprocess invocations = [ @@ -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