Skip to content

Commit

Permalink
move validation logic (#30)
Browse files Browse the repository at this point in the history
* update development instructions

* move config validation logic into config.py
  • Loading branch information
josephhaaga committed Nov 20, 2022
1 parent 78e0fd4 commit ec03fe1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,12 @@ The `JOURNAL_SAVED` hook runs whenever a user saves their journal (resulting in
##### Journal closed

The `JOURNAL_CLOSED` hook runs whenever a user closes their journal.


## Development
```bash
$ python3 -m pip install pytest pytest-cov pytest-xdist

# Re-run tests whenever a file is changed
$ PYTHONPATH=${PWD} python3 -m pytest -f
```
34 changes: 13 additions & 21 deletions clerk/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,19 @@ def __init__(
"""Initialize a clerk Application object"""
# try to read config, and ensure required values are present
# TODO: otherwise, instruct user to setup their config file (`clerk configure`)
try:
self.config = config
self.extensions = extensions
self.temp_directory = user_data_directory
self.journal_directory = self.config["DEFAULT"]["journal_directory"]
self.preferred_editor = self.config["DEFAULT"]["preferred_editor"]
self.date_format = self.config["DEFAULT"]["date_format"]
self.file_extension = self.config["DEFAULT"]["file_extension"]
self.hooks = {
"NEW_JOURNAL_CREATED": self._get_callbacks_for_hook(
"NEW_JOURNAL_CREATED"
),
"JOURNAL_OPENED": self._get_callbacks_for_hook("JOURNAL_OPENED"),
"JOURNAL_SAVED": self._get_callbacks_for_hook("JOURNAL_SAVED"),
"JOURNAL_CLOSED": self._get_callbacks_for_hook("JOURNAL_CLOSED"),
}
except KeyError as e:
print(
f"Your configuration at {config_file_path()} is missing a key '{e.args[0]}'"
)
exit(1)
self.config = config
self.extensions = extensions
self.temp_directory = user_data_directory
self.journal_directory = self.config["DEFAULT"]["journal_directory"]
self.preferred_editor = self.config["DEFAULT"]["preferred_editor"]
self.date_format = self.config["DEFAULT"]["date_format"]
self.file_extension = self.config["DEFAULT"]["file_extension"]
self.hooks = {
"NEW_JOURNAL_CREATED": self._get_callbacks_for_hook("NEW_JOURNAL_CREATED"),
"JOURNAL_OPENED": self._get_callbacks_for_hook("JOURNAL_OPENED"),
"JOURNAL_SAVED": self._get_callbacks_for_hook("JOURNAL_SAVED"),
"JOURNAL_CLOSED": self._get_callbacks_for_hook("JOURNAL_CLOSED"),
}
if not pathlib.Path(self.journal_directory).is_dir():
raise FileNotFoundError(
f"Your journal_directory ({self.journal_directory}) doesn't exist. Please create this directory and try again"
Expand Down
15 changes: 12 additions & 3 deletions clerk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@ def get_config() -> Mapping:
config_file.touch(exist_ok=True)
conf = ConfigParser()
conf.read(config_file)
conf["DEFAULT"]["journal_directory"] = str(
Path(conf["DEFAULT"]["journal_directory"]).expanduser()
)
try:
conf["DEFAULT"]["journal_directory"] = str(
Path(conf["DEFAULT"]["journal_directory"]).expanduser()
)
conf["DEFAULT"]["preferred_editor"]
conf["DEFAULT"]["date_format"]
conf["DEFAULT"]["file_extension"]
except KeyError as e:
print(
f"Your configuration at {config_file_path()} is missing a key '{e.args[0]}'"
)
exit(1)
return conf


Expand Down
9 changes: 0 additions & 9 deletions tests/app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,6 @@ def test_application_convert_to_filename(date, filename, example_app):
assert got == filename


def test_application_creation_fails_with_missing_config_item(user_data_dir):
"""Ensure application fails when a necessary configuration item is missing"""
# https://medium.com/python-pandemonium/testing-sys-exit-with-pytest-10c6e5f7726f
with pytest.raises(SystemExit) as pytest_wrapped_e:
Application({"DEFAULT": {}}, user_data_dir, {})
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1


@pytest.mark.parametrize(
"phrase,date",
[
Expand Down
19 changes: 19 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ def test_write_config_writes_successfully(patched_config_file_path):
assert file_contents == ["[DEFAULT]\n", "hello = hi there\n", "\n"]


def test_get_config_fails_with_missing_config_item():
"""Ensure application fails when a necessary configuration item is missing"""
# https://medium.com/python-pandemonium/testing-sys-exit-with-pytest-10c6e5f7726f
with patch("clerk.config.config_file_path") as patched_config_file_path:
with NamedTemporaryFile() as f:
with open(f.name, "w") as fs:
fs.writelines(
[
"[DEFAULT]\n",
"file_extension=md",
]
)
patched_config_file_path.return_value = Path(f.name)
with pytest.raises(SystemExit) as pytest_wrapped_e:
get_config()
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1


# TODO: test the validation function, rather than the config file itself.
@pytest.mark.skip
@pytest.mark.parametrize(
Expand Down

0 comments on commit ec03fe1

Please sign in to comment.