Browse Source

tests: replace tmpdir fixture with tmp_path to avoid re-conversion into pathlib.Path

Fabian Peter Hammerle 3 years ago
parent
commit
89e27cfa4b
4 changed files with 41 additions and 44 deletions
  1. 3 0
      CHANGELOG.md
  2. 23 23
      tests/cli_test.py
  3. 0 5
      tests/conftest.py
  4. 15 16
      tests/vdir_test.py

+ 3 - 0
CHANGELOG.md

@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
+### Fixed
+- tests: fixed `TypeError` when converting to `pathlib.Path` on python3.5
+  by replacing `tmpdir` fixture with `temp_path`
 
 ## [0.1.1] - 2020-02-06
 ### Fixed

+ 23 - 23
tests/cli_test.py

@@ -33,13 +33,13 @@ def test_entrypoint_help():
 
 
 def test__main_create_all(
-    caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
+    caplog, tmp_path: pathlib.Path, google_calendar_file: io.BufferedReader
 ):
     with unittest.mock.patch("sys.stdin", google_calendar_file):
-        with unittest.mock.patch("sys.argv", ["", "--output-dir", str(temp_dir_path)]):
+        with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
             with caplog.at_level(logging.INFO):
                 ical2vdir._main()
-    created_item_paths = sorted(temp_dir_path.iterdir())
+    created_item_paths = sorted(tmp_path.iterdir())
     assert [p.name for p in created_item_paths] == [
         "1234567890qwertyuiopasdfgh@google.com.ics",
         "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics",
@@ -58,12 +58,12 @@ def test__main_create_all(
 
 
 def test__main_create_some(
-    caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
+    caplog, tmp_path: pathlib.Path, google_calendar_file: io.BufferedReader
 ):
     with unittest.mock.patch("sys.stdin", google_calendar_file):
-        with unittest.mock.patch("sys.argv", ["", "--output-dir", str(temp_dir_path)]):
+        with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
             ical2vdir._main()
-            temp_dir_path.joinpath(
+            tmp_path.joinpath(
                 "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
             ).unlink()
             google_calendar_file.seek(0)
@@ -77,15 +77,15 @@ def test__main_create_some(
 
 
 def test__main_update(
-    caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
+    caplog, tmp_path: pathlib.Path, google_calendar_file: io.BufferedReader
 ):
     with unittest.mock.patch("sys.stdin", google_calendar_file):
-        with unittest.mock.patch("sys.argv", ["", "--output-dir", str(temp_dir_path)]):
+        with unittest.mock.patch("sys.argv", ["", "--output-dir", str(tmp_path)]):
             ical2vdir._main()
-            temp_dir_path.joinpath(
+            tmp_path.joinpath(
                 "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
             ).unlink()
-            updated_path = temp_dir_path.joinpath(
+            updated_path = tmp_path.joinpath(
                 "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
             )
             updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
@@ -107,17 +107,17 @@ def test__main_update(
 
 
 def test__main_update_silent(
-    caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
+    caplog, tmp_path: pathlib.Path, google_calendar_file: io.BufferedReader
 ):
     with unittest.mock.patch("sys.stdin", google_calendar_file):
         with unittest.mock.patch(
-            "sys.argv", ["", "--output-dir", str(temp_dir_path), "--silent"]
+            "sys.argv", ["", "--output-dir", str(tmp_path), "--silent"]
         ):
             ical2vdir._main()
-            temp_dir_path.joinpath(
+            tmp_path.joinpath(
                 "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
             ).unlink()
-            updated_path = temp_dir_path.joinpath(
+            updated_path = tmp_path.joinpath(
                 "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
             )
             updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
@@ -130,17 +130,17 @@ def test__main_update_silent(
 
 
 def test__main_update_verbose(
-    caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
+    caplog, tmp_path: pathlib.Path, google_calendar_file: io.BufferedReader
 ):
     with unittest.mock.patch("sys.stdin", google_calendar_file):
         with unittest.mock.patch(
-            "sys.argv", ["", "--output-dir", str(temp_dir_path), "--verbose"]
+            "sys.argv", ["", "--output-dir", str(tmp_path), "--verbose"]
         ):
             ical2vdir._main()
-            temp_dir_path.joinpath(
+            tmp_path.joinpath(
                 "recurr1234567890qwertyuiop@google.com.20150924T090000+0200.ics"
             ).unlink()
-            updated_path = temp_dir_path.joinpath(
+            updated_path = tmp_path.joinpath(
                 "recurr1234567890qwertyuiop@google.com.20150908T090000+0200.ics"
             )
             updated_ical = updated_path.read_bytes().replace(b"20150908", b"20140703")
@@ -165,16 +165,16 @@ def test__main_update_verbose(
 
 
 def test__main_delete(
-    caplog, temp_dir_path: pathlib.Path, google_calendar_file: io.BufferedReader
+    caplog, tmp_path: pathlib.Path, google_calendar_file: io.BufferedReader
 ):
-    temp_dir_path.joinpath("will-be-deleted.ics").touch()
+    tmp_path.joinpath("will-be-deleted.ics").touch()
     with unittest.mock.patch("sys.stdin", google_calendar_file):
         with unittest.mock.patch(
-            "sys.argv", ["", "--output-dir", str(temp_dir_path), "--delete"]
+            "sys.argv", ["", "--output-dir", str(tmp_path), "--delete"]
         ):
             with caplog.at_level(logging.INFO):
                 ical2vdir._main()
-    assert len(list(temp_dir_path.iterdir())) == 3
-    assert not any(p.name == "will-be-deleted.ics" for p in temp_dir_path.iterdir())
+    assert len(list(tmp_path.iterdir())) == 3
+    assert not any(p.name == "will-be-deleted.ics" for p in tmp_path.iterdir())
     assert caplog.records[-1].message.startswith("removing")
     assert caplog.records[-1].message.endswith("will-be-deleted.ics")

+ 0 - 5
tests/conftest.py

@@ -5,11 +5,6 @@ import typing
 import pytest
 
 
-@pytest.fixture
-def temp_dir_path(tmpdir) -> pathlib.Path:
-    return pathlib.Path(tmpdir)
-
-
 @pytest.fixture
 def google_calendar_file() -> typing.Iterator[io.BufferedReader]:
     with pathlib.Path(__file__).parent.joinpath(

+ 15 - 16
tests/vdir_test.py

@@ -50,12 +50,14 @@ END:VEVENT
 
 # pylint: disable=protected-access
 
+# tmp_path fixture: https://github.com/pytest-dev/pytest/blob/5.4.3/src/_pytest/tmpdir.py#L191
 
-def test__write_event_cleanup(tmpdir):
+
+def test__write_event_cleanup(tmp_path: pathlib.Path):
     event = icalendar.cal.Event.from_ical(_SINGLE_EVENT_ICAL)
     with unittest.mock.patch("os.unlink") as unlink_mock:
         with pytest.raises(IsADirectoryError):
-            ical2vdir._write_event(event, pathlib.Path(tmpdir))
+            ical2vdir._write_event(event, tmp_path)
     unlink_mock.assert_called_once()
     unlink_args, _ = unlink_mock.call_args
     os.unlink(unlink_args[0])
@@ -92,23 +94,21 @@ def test__event_vdir_filename(event_ical, expected_filename):
 
 
 @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
-def test__sync_event_create(tmpdir, event_ical):
-    temp_path = pathlib.Path(tmpdir)
+def test__sync_event_create(tmp_path: pathlib.Path, event_ical):
     event = icalendar.cal.Event.from_ical(event_ical)
-    ical2vdir._sync_event(event, temp_path)
-    (ics_path,) = temp_path.iterdir()
+    ical2vdir._sync_event(event, tmp_path)
+    (ics_path,) = tmp_path.iterdir()
     assert ics_path.name == "1qa2ws3ed4rf5tg@google.com.ics"
     assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL
 
 
 @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
-def test__sync_event_update(tmpdir, event_ical):
-    temp_path = pathlib.Path(tmpdir)
+def test__sync_event_update(tmp_path: pathlib.Path, event_ical):
     event = icalendar.cal.Event.from_ical(event_ical)
-    ical2vdir._sync_event(event, temp_path)
+    ical2vdir._sync_event(event, tmp_path)
     event["SUMMARY"] += " suffix"
-    ical2vdir._sync_event(event, temp_path)
-    (ics_path,) = temp_path.iterdir()
+    ical2vdir._sync_event(event, tmp_path)
+    (ics_path,) = tmp_path.iterdir()
     assert ics_path.name == event["UID"] + ".ics"
     assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL.replace(
         b"party", b"party suffix"
@@ -116,12 +116,11 @@ def test__sync_event_update(tmpdir, event_ical):
 
 
 @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
-def test__sync_event_unchanged(tmpdir, event_ical):
-    temp_path = pathlib.Path(tmpdir)
+def test__sync_event_unchanged(tmp_path: pathlib.Path, event_ical):
     event = icalendar.cal.Event.from_ical(event_ical)
-    ical2vdir._sync_event(event, temp_path)
-    (ics_path,) = temp_path.iterdir()
+    ical2vdir._sync_event(event, tmp_path)
+    (ics_path,) = tmp_path.iterdir()
     old_stat = copy.deepcopy(ics_path.stat())
-    ical2vdir._sync_event(event, temp_path)
+    ical2vdir._sync_event(event, tmp_path)
     assert ics_path.stat() == old_stat
     assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL