Skip to content

Commit c6b9dbf

Browse files
authored
Fix cli and inclusion file tests (#104)
1 parent b31ea58 commit c6b9dbf

3 files changed

Lines changed: 24 additions & 23 deletions

File tree

tests/test_cli.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import sys
32

43
import pytest
54

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

1110

12-
def test_cli(monkeypatch):
13-
with monkeypatch.context() as m:
14-
m.setattr(sys, "argv", ["yaml2ics.py", example_calendar])
15-
main()
11+
def test_cli():
12+
main(["yaml2ics.py", example_calendar])
1613

17-
with monkeypatch.context() as m:
18-
m.setattr(sys, "argv", ["yaml2ics.py"])
19-
20-
with pytest.raises(RuntimeError) as e:
21-
main()
22-
assert "Usage:" in str(e)
23-
24-
with monkeypatch.context() as m:
25-
m.setattr(sys, "argv", ["yaml2ics.py", "syzygy.yaml"])
14+
with pytest.raises(RuntimeError) as e:
15+
main(["yaml2ics.py"])
16+
assert "Usage:" in str(e)
2617

27-
with pytest.raises(RuntimeError) as e:
28-
main()
29-
assert "is not a file" in str(e)
18+
with pytest.raises(RuntimeError) as e:
19+
main(["yaml2ics.py", "syzygy.yaml"])
20+
assert "is not a file" in str(e)
3021

3122

3223
def test_errors():

tests/test_include.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import io
2+
import os
23
import textwrap
34

45
from yaml2ics import files_to_calendar
56

67

78
def _read(f, mode=None):
9+
f = os.path.relpath(f) # Changes ./a.yaml to a.yaml
810
if f == "a.yaml":
911
return io.StringIO(
1012
textwrap.dedent(
@@ -31,7 +33,7 @@ def _read(f, mode=None):
3133
"""
3234
)
3335
)
34-
else:
36+
elif f in ("c.yaml", "d.yaml"):
3537
# Return template with summary of the letters
3638
return io.StringIO(
3739
textwrap.dedent(
@@ -43,11 +45,16 @@ def _read(f, mode=None):
4345
% (f[0].upper() * 5)
4446
)
4547
)
48+
else:
49+
raise RuntimeError("Attempting to load invalid test file")
4650

4751

4852
def test_include_calendars(monkeypatch):
4953
"""Calendar that includes other calendars"""
5054
monkeypatch.setitem(__builtins__, "open", _read)
55+
monkeypatch.setattr(os.path, "dirname", lambda _: ".")
56+
test_files = ["a.yaml", "b.yaml", "c.yaml", "d.yaml"]
57+
monkeypatch.setattr(os.path, "exists", lambda f: os.path.relpath(f) in test_files)
5158
cal = files_to_calendar(["a.yaml"])
5259
cal_str = cal.serialize()
5360
assert cal_str.startswith("BEGIN:VCALENDAR")

yaml2ics.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ def files_to_events(files: list) -> (ics.Calendar, str):
164164
if hasattr(f, "read"):
165165
calendar_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
166166
else:
167-
calendar_yaml = yaml.load(open(f), Loader=yaml.FullLoader)
167+
if not os.path.exists(f):
168+
raise RuntimeError(f"Cannot find included yaml file `{f}`.")
169+
with open(f, "rb") as fh:
170+
calendar_yaml = yaml.load(fh, Loader=yaml.FullLoader)
168171
tz = calendar_yaml.get("timezone", None)
169172
if tz is not None:
170173
tz = gettz(tz)
@@ -198,11 +201,11 @@ def files_to_calendar(files: list) -> ics.Calendar:
198201
# `main` is separate from `cli` to facilitate testing.
199202
# The only difference being that `main` raises errors while
200203
# `cli` prints them and exits with errorcode 1
201-
def main():
202-
if len(sys.argv) < 2:
204+
def main(argv: list):
205+
if len(argv) < 2:
203206
raise RuntimeError("Usage: yaml2ics.py FILE1.yaml FILE2.yaml ...")
204207

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

215218
def cli():
216219
try:
217-
main()
220+
main(sys.argv)
218221
except Exception as e:
219222
print(e, file=sys.stderr)
220223
sys.exit(-1)

0 commit comments

Comments
 (0)