Skip to content

Commit

Permalink
Merge pull request #415 from mcgalcode/bugfix/blank-yaml
Browse files Browse the repository at this point in the history
Add warning when jobflow.yaml is blank or badly formatted
  • Loading branch information
utf authored Aug 31, 2023
2 parents 9e8238b + ee1e109 commit 94a4984
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ docs = [
"sphinx==7.2.4",
]
dev = ["pre-commit>=2.12.1"]
tests = ["pytest-cov==4.1.0", "pytest==7.4.0"]
tests = ["pytest-cov==4.1.0", "pytest==7.4.0", "moto==4.2.0"]
vis = ["matplotlib", "pydot"]
fireworks = ["FireWorks"]
strict = [
Expand Down
16 changes: 14 additions & 2 deletions src/jobflow/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Settings for jobflow."""

import warnings
from collections import defaultdict
from pathlib import Path

Expand Down Expand Up @@ -136,10 +137,21 @@ def load_default_settings(cls, values):
from monty.serialization import loadfn

config_file_path: str = values.get("CONFIG_FILE", DEFAULT_CONFIG_FILE_PATH)

new_values = {}
if Path(config_file_path).exists():
new_values.update(loadfn(config_file_path))
if Path(config_file_path).stat().st_size == 0:
warnings.warn(
f"An empty JobFlow config file was located at {config_file_path}"
)
else:
try:
new_values.update(loadfn(config_file_path))
except ValueError:
raise ValueError(
f"A JobFlow configuration file was located at "
f"{config_file_path} but a problem was "
f"encountered while parsing it."
) from None

store = new_values.get("JOB_STORE")
if isinstance(store, str):
Expand Down
21 changes: 20 additions & 1 deletion tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest


def test_settings_init():
import os

Expand Down Expand Up @@ -69,7 +72,23 @@ def test_settings_object(clean_dir, test_data):
}

# set the path to lood settings from
os.environ["JOBFLOW_CONFIG_FILE"] = str(Path.cwd() / "config.yaml")
config_file_path = str(Path.cwd() / "config.yaml")
os.environ["JOBFLOW_CONFIG_FILE"] = config_file_path

# A warning should appear if config file is empty
with open(config_file_path, "w") as f:
pass

with pytest.warns(UserWarning):
settings = JobflowSettings()

# An error should be thrown if the file exists and
# contains badly formatted contents
with open(config_file_path, "w") as f:
f.write("Some text that sadly is not yaml")

with pytest.raises(ValueError, match="A JobFlow configuration"):
settings = JobflowSettings()

# assert loading monty spec from files works
dumpfn({"JOB_STORE": monty_spec}, "config.yaml")
Expand Down

0 comments on commit 94a4984

Please sign in to comment.