Skip to content

Commit

Permalink
Merge pull request #58 from neutrinoceros/wrap_missing_json_file
Browse files Browse the repository at this point in the history
capture missing json file error
  • Loading branch information
neutrinoceros committed Feb 16, 2021
2 parents eedd3f6 + 803dd3d commit 883eb67
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apply_subs/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.5.2"
__version__ = "0.5.3"
3 changes: 3 additions & 0 deletions apply_subs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def main(argv: Optional[List[str]] = None) -> int:
try:
with open(args.subs, "r") as fh:
subs = json.load(fh)
except FileNotFoundError:
print_err(f"{args.subs} not found.")
return 1
except json.decoder.JSONDecodeError:
print_err(f"invalid json file `{args.subs}`")
return 1
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = apply_subs
version = 0.5.2
version = 0.5.3
description = A command line tool to apply substitutions to a text file
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
24 changes: 17 additions & 7 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import json
from typing import Tuple

import pytest

from apply_subs.main import main


def test_missing_target(simple_setup, capsys):
target, subs_file, expected = simple_setup
@pytest.mark.parametrize("missing", [(True, True), (True, False), (False, True)])
def test_missing_files(simple_setup, capsys, missing: Tuple[bool, bool]):
target, subs_file, _expected = simple_setup

typo_target = target.with_suffix(".this_file_doesnt_exists")
assert not typo_target.exists()
if missing[0]:
target = target.with_suffix(".this_file_doesnt_exists")
assert not target.exists()
expected_err = f"Error {target} not found."
if missing[1]:
subs_file = subs_file.with_suffix(".this_file_doesnt_exists")
assert not subs_file.exists()
expected_err = f"Error {subs_file} not found."

retval = main([str(typo_target), "-s", str(subs_file)])
assert retval != 0
ret = main([str(target), "-s", str(subs_file)])
assert ret != 0

out, err = capsys.readouterr()
assert out == ""
assert err.replace("\n", "") == f"Error {typo_target} not found."
assert err.replace("\n", "") == expected_err


def test_invalid_schema(tmp_path, capsys):
Expand Down

0 comments on commit 883eb67

Please sign in to comment.