From 496058bd67142b998803429fe752af738d921043 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Mon, 7 Feb 2022 20:49:43 +0100 Subject: [PATCH] Monkeypatched away --- requirements_dev.txt | 4 +- src/criteriaSorter/modules/fileops.py | 16 +++-- tests/modules/test_fileops.py | 89 ++++++++++++++++++++------- 3 files changed, 82 insertions(+), 27 deletions(-) diff --git a/requirements_dev.txt b/requirements_dev.txt index 4db63fc..9e40cd0 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -3,4 +3,6 @@ pytest-cov==2.12.1 mypy===0.910 pyyaml==6.0 flake8==4.0.1 -tox==3.24.3 \ No newline at end of file +tox==3.24.3 +rich~=11.1.0 +criteriaSorter~=0.1 \ No newline at end of file diff --git a/src/criteriaSorter/modules/fileops.py b/src/criteriaSorter/modules/fileops.py index e3a1e5e..b3a81e4 100644 --- a/src/criteriaSorter/modules/fileops.py +++ b/src/criteriaSorter/modules/fileops.py @@ -35,22 +35,28 @@ class DirectoryHandler: def get_dir_size_in_gb(self): return self.get_dir_size() / (1024 * 1024 * 1024) - def get_file_list(self): + def get_full_list(self): return os.listdir(self.dir_path) def get_files(self): - for file in self.get_file_list(): + for file in self.get_full_list(): fullname = os.path.join(self.dir_path, file) if os.path.isfile(fullname): yield fullname + def get_file_list(self): + return list(self.get_files()) + def get_directories(self): - for file in self.get_file_list(): + for file in self.get_full_list(): if os.path.isdir(os.path.join(self.dir_path, file)): yield file - def get_file_list_with_size(self): - file_list = self.get_file_list() + def get_directory_list(self): + return list(self.get_directories()) + + def get_full_list_with_size(self): + file_list = self.get_full_list() file_list_with_size = [] for file in file_list: file_path = os.path.join(self.dir_path, file) diff --git a/tests/modules/test_fileops.py b/tests/modules/test_fileops.py index 9d4291a..2d50577 100644 --- a/tests/modules/test_fileops.py +++ b/tests/modules/test_fileops.py @@ -1,18 +1,60 @@ # Test file for fileops.py + from criteriaSorter.modules import fileops +import pathlib +import os -def _mock_get_file_list(dir_path): - # Mock function for testing - return ["file1", "file2", "file3"] +_BASE_PATH = pathlib.Path("/Test/Path") + +class MockOS: + @staticmethod + def getsize(path): + if pathlib.Path(path) == _BASE_PATH / 'a': + return 2*(1024 * 1024) + else: + return 1024 * 1024 + + @staticmethod + def listdir(path): + if pathlib.Path(path) == _BASE_PATH / 'a': + return ['b', 'c'] + else: + return ['a', 'b', 'c'] + + @staticmethod + def path_exists(path): + if path in [_BASE_PATH / i for i in ['a', 'b', 'c', 'a/b', 'a/c', 'a\\b', 'a\\c']]: + return True + return True + + @staticmethod + def isdir(path): + if pathlib.Path(path) == _BASE_PATH / 'a': + return True + else: + return False + + @staticmethod + def isfile(path): + if pathlib.Path(path) != _BASE_PATH / 'a': + return True + else: + return False + + @staticmethod + def join(path, *args): + return pathlib.Path(path).joinpath(*args) + + @staticmethod + def stat(path): + if pathlib.Path(path) == _BASE_PATH / 'a': + return MockOS.getsize(path) + else: + return MockOS.getsize(path) -def _mock_get_file_list_with_size(dir_path): - # Mock function for testing - return [("file1", 1), ("file2", 2), ("file3", 3)] - - -def test_DirectoryHandler(): +def test_DirectoryHandler(monkeypatch): # Regular invocation handler = fileops.DirectoryHandler(".") @@ -22,24 +64,29 @@ def test_DirectoryHandler(): handler.get_dir_size() handler.get_dir_size_in_mb() handler.get_dir_size_in_gb() - [f for f in handler.get_files()] - [f for f in handler.get_directories()] - handler.get_file_list_with_size() + assert [f for f in handler.get_files()] + assert [f for f in handler.get_directories()] + handler.get_full_list_with_size() # Using a mock function to test return value handler = fileops.DirectoryHandler("/Test/Path") - handler.get_file_list=_mock_get_file_list - handler.get_file_list_with_size=_mock_get_file_list_with_size + monkeypatch.setattr(os, 'listdir', MockOS.listdir) + monkeypatch.setattr(os.path, 'isdir', MockOS.isdir) + monkeypatch.setattr(os.path, 'isfile', MockOS.isfile) + monkeypatch.setattr(os.path, 'getsize', MockOS.getsize) + monkeypatch.setattr(os.path, 'join', MockOS.join) + monkeypatch.setattr(os, 'stat', MockOS.stat) # Test return values - assert handler.get_dir_path() == "/Test/Path" + assert pathlib.Path(handler.get_dir_path()) == _BASE_PATH assert handler.get_dir_name() == "Path" - assert handler.get_dir_size() == 6 - assert handler.get_dir_size_in_mb() == 0.06 - assert handler.get_dir_size_in_gb() == 0.00006 - assert handler.get_files() == ["file1", "file2", "file3"] - - + assert handler.get_dir_size() == (1024 * 1024) * 1 + assert handler.get_dir_size_in_mb() == 1 + assert handler.get_dir_size_in_gb() == 1 / 1024 + assert handler.get_full_list() == ["a", "b", "c"] + assert handler.get_full_list_with_size() == [("a", (1024 * 1024) * 2), ("b", (1024 * 1024) * 1), ("c", (1024 * 1024) * 1)] + assert handler.get_file_list() == [_BASE_PATH / "b", _BASE_PATH / "c"] + assert handler.get_directory_list() == ["a"] # def test_FileHandler(): # handler = fileops.FileHandler(".")