Skip to content

Commit

Permalink
Fix cli and inclusion file tests (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanv authored Sep 2, 2024
1 parent b31ea58 commit c6b9dbf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
25 changes: 8 additions & 17 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys

import pytest

Expand All @@ -9,24 +8,16 @@
example_calendar = os.path.join(basedir, "../example/test_calendar.yaml")


def test_cli(monkeypatch):
with monkeypatch.context() as m:
m.setattr(sys, "argv", ["yaml2ics.py", example_calendar])
main()
def test_cli():
main(["yaml2ics.py", example_calendar])

with monkeypatch.context() as m:
m.setattr(sys, "argv", ["yaml2ics.py"])

with pytest.raises(RuntimeError) as e:
main()
assert "Usage:" in str(e)

with monkeypatch.context() as m:
m.setattr(sys, "argv", ["yaml2ics.py", "syzygy.yaml"])
with pytest.raises(RuntimeError) as e:
main(["yaml2ics.py"])
assert "Usage:" in str(e)

with pytest.raises(RuntimeError) as e:
main()
assert "is not a file" in str(e)
with pytest.raises(RuntimeError) as e:
main(["yaml2ics.py", "syzygy.yaml"])
assert "is not a file" in str(e)


def test_errors():
Expand Down
9 changes: 8 additions & 1 deletion tests/test_include.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import io
import os
import textwrap

from yaml2ics import files_to_calendar


def _read(f, mode=None):
f = os.path.relpath(f) # Changes ./a.yaml to a.yaml
if f == "a.yaml":
return io.StringIO(
textwrap.dedent(
Expand All @@ -31,7 +33,7 @@ def _read(f, mode=None):
"""
)
)
else:
elif f in ("c.yaml", "d.yaml"):
# Return template with summary of the letters
return io.StringIO(
textwrap.dedent(
Expand All @@ -43,11 +45,16 @@ def _read(f, mode=None):
% (f[0].upper() * 5)
)
)
else:
raise RuntimeError("Attempting to load invalid test file")


def test_include_calendars(monkeypatch):
"""Calendar that includes other calendars"""
monkeypatch.setitem(__builtins__, "open", _read)
monkeypatch.setattr(os.path, "dirname", lambda _: ".")
test_files = ["a.yaml", "b.yaml", "c.yaml", "d.yaml"]
monkeypatch.setattr(os.path, "exists", lambda f: os.path.relpath(f) in test_files)
cal = files_to_calendar(["a.yaml"])
cal_str = cal.serialize()
assert cal_str.startswith("BEGIN:VCALENDAR")
Expand Down
13 changes: 8 additions & 5 deletions yaml2ics.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ def files_to_events(files: list) -> (ics.Calendar, str):
if hasattr(f, "read"):
calendar_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
else:
calendar_yaml = yaml.load(open(f), Loader=yaml.FullLoader)
if not os.path.exists(f):
raise RuntimeError(f"Cannot find included yaml file `{f}`.")
with open(f, "rb") as fh:
calendar_yaml = yaml.load(fh, Loader=yaml.FullLoader)
tz = calendar_yaml.get("timezone", None)
if tz is not None:
tz = gettz(tz)
Expand Down Expand Up @@ -198,11 +201,11 @@ def files_to_calendar(files: list) -> ics.Calendar:
# `main` is separate from `cli` to facilitate testing.
# The only difference being that `main` raises errors while
# `cli` prints them and exits with errorcode 1
def main():
if len(sys.argv) < 2:
def main(argv: list):
if len(argv) < 2:
raise RuntimeError("Usage: yaml2ics.py FILE1.yaml FILE2.yaml ...")

files = sys.argv[1:]
files = argv[1:]
for f in files:
if not os.path.isfile(f):
raise RuntimeError(f"Error: {f} is not a file")
Expand All @@ -214,7 +217,7 @@ def main():

def cli():
try:
main()
main(sys.argv)
except Exception as e:
print(e, file=sys.stderr)
sys.exit(-1)
Expand Down

0 comments on commit c6b9dbf

Please sign in to comment.