From 4cf4c3ff80153a8edf17a2d69c7038a28990c5e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Val=C3=AD=C4=8Dek?= Date: Sun, 31 Jul 2022 15:33:35 +0200 Subject: [PATCH] DiskStoredList MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Václav Valíček --- repo_cloner/lib/disk_stored_list.py | 43 +++++++++++++++ tests/lib/test_disk_stored_list.py | 83 +++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 repo_cloner/lib/disk_stored_list.py create mode 100644 tests/lib/test_disk_stored_list.py diff --git a/repo_cloner/lib/disk_stored_list.py b/repo_cloner/lib/disk_stored_list.py new file mode 100644 index 0000000..d77bc50 --- /dev/null +++ b/repo_cloner/lib/disk_stored_list.py @@ -0,0 +1,43 @@ +from typing import Optional +import os + + +class DiskStoredList: + __file: Optional[str] = None + __items: Optional[list] = None + + def __init__(self, file: str): + # file does not have to exist, but parent directory does + if not os.path.exists(file): + if not os.path.isdir(os.path.dirname(file)): + raise NotADirectoryError(f"Parent directory for file {file} does not exist!") + + self.__file = file + self.__items = [] + + if os.path.exists(file): + self._load_from_file() + + def _load_from_file(self): + with open(self.__file, 'r') as f: + for line in f.readlines(): + line = line.strip() + if len(line): + self.__items.append(line) + + def append(self, line: str): + with open(self.__file, 'a') as f: + self.__items.append(line) + f.write(f"{line}\n") + + def count(self): + return len(self.__items) + + def __contains__(self, line: str) -> bool: + return line in self.__items + + def __iter__(self): + return self.__items.__iter__() + + def __len__(self): + return len(self.__items) diff --git a/tests/lib/test_disk_stored_list.py b/tests/lib/test_disk_stored_list.py new file mode 100644 index 0000000..325c271 --- /dev/null +++ b/tests/lib/test_disk_stored_list.py @@ -0,0 +1,83 @@ +import pytest +from repo_cloner.lib.disk_stored_list import DiskStoredList + + +@pytest.fixture +def tmp_test_file(tmp_path): + test_file = tmp_path.joinpath("list") + test_file.write_text( + "Ahoj\n" + "Zkouška\n" + "Item1\n" + "Item2\n" + "\n") + + return test_file + + +def test_init_no_file_no_dir(tmp_path): + with pytest.raises(NotADirectoryError) as e: + pass + DiskStoredList(tmp_path.joinpath("nonexistent_dir", "nonexistent_file").as_posix()) + assert "nonexistent_dir/nonexistent_file does not exist!" in e.exconly() + + +def test_init_dir_exists_no_file(tmp_path): + x = DiskStoredList(tmp_path.joinpath("nonexistent_file").as_posix()) + assert isinstance(x, DiskStoredList) + + +def test_load_from_file(tmp_path): + file = tmp_path.joinpath("list") + file.write_text("line1\nline2\nline3\n") + x = DiskStoredList(file.as_posix()) + assert len(x._DiskStoredList__items) == 3 + assert x._DiskStoredList__items == [ + 'line1', + 'line2', + 'line3', + ] + + +def test_append_empty(tmp_path): + empty = tmp_path.joinpath("empty") + x = DiskStoredList(empty.as_posix()) + x.append("test_line") + assert empty.read_text() == "test_line\n" + assert "test_line" in x._DiskStoredList__items + assert len(x._DiskStoredList__items) == 1 + + +def test_append_utilized(tmp_test_file): + x = DiskStoredList(tmp_test_file.as_posix()) + x.append("Item3") + assert x._DiskStoredList__items == ['Ahoj', 'Zkouška', 'Item1', 'Item2', 'Item3'] + + +def test_count(tmp_test_file): + x = DiskStoredList(tmp_test_file.as_posix()) + assert x.count() == 4 + x.append("WTH") + assert x.count() == 5 + + +def test__contains__(tmp_test_file): + x = DiskStoredList(tmp_test_file.parent.joinpath("a")) + x.append("WTF") + assert "WTF" in x + assert "Line2" not in x + + y = DiskStoredList(tmp_test_file.as_posix()) + assert "Item2" in y + assert "Ahoj" in y + + +def test__iter__(tmp_test_file): + result = [] + for x in DiskStoredList(tmp_test_file.as_posix()): + result.append(x) + assert result == ['Ahoj', 'Zkouška', 'Item1', 'Item2'] + + +def test__len__(tmp_test_file): + assert len(DiskStoredList(tmp_test_file.as_posix())) == 4