File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import os
2- import sys
32
43import pytest
54
98example_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
3223def test_errors ():
Original file line number Diff line number Diff line change 11import io
2+ import os
23import textwrap
34
45from yaml2ics import files_to_calendar
56
67
78def _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
4852def 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" )
Original file line number Diff line number Diff 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
215218def cli ():
216219 try :
217- main ()
220+ main (sys . argv )
218221 except Exception as e :
219222 print (e , file = sys .stderr )
220223 sys .exit (- 1 )
You can’t perform that action at this time.
0 commit comments