From 5713c253cd7405e5197fe0f5536a1826da718f1e Mon Sep 17 00:00:00 2001 From: matlag Date: Sat, 29 Jun 2024 20:24:44 -0400 Subject: [PATCH] search_file and test work --- search_file.py | 7 +++-- test/test_search_file.py | 55 ++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/search_file.py b/search_file.py index eff199d..8f41efe 100644 --- a/search_file.py +++ b/search_file.py @@ -3,6 +3,7 @@ # prompt user if multiple are found import os +import errno from fnmatch import fnmatch def search_all_match(filename, path): @@ -12,7 +13,9 @@ def search_all_match(filename, path): for root, dirs, files in os.walk(path): for name in files: if fnmatch(filename, name): - results.append(os.path.join(root, name)) + match_path = os.path.join(root, name) + if not os.path.islink(match_path): + results.append(match_path) return results def print_num_list(paths): @@ -40,6 +43,6 @@ def search_file(filename, path): paths = search_all_match(filename, path) if len(paths) == 0 : print(f"No match for \"{filename}\". Link not modified.") - raise Exception(FileNotFoundError) + raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename) p = select_path(paths) return p diff --git a/test/test_search_file.py b/test/test_search_file.py index c504ffe..1a512f5 100644 --- a/test/test_search_file.py +++ b/test/test_search_file.py @@ -1,22 +1,55 @@ import unittest -from search_file import search_file +import unittest.mock +import io +from search_file import print_num_list, search_all_match, select_path +import search_file # TODO: gros TODO icitte. Presque rien de fait! class TestSearchFile(unittest.TestCase): data_dir = "test/data/search_test" + mock_list = [data_dir+"/match1.txt",\ + data_dir+"/subdir21/match1.txt",\ + data_dir+"/subdir1/match1.txt",\ + data_dir+"/subdir2/match1.txt"] + + print_mock = "1. test/data/search_test/match1.txt\n\ +2. test/data/search_test/subdir21/match1.txt\n\ +3. test/data/search_test/subdir1/match1.txt\n\ +4. test/data/search_test/subdir2/match1.txt\n" + + target = "match1.txt" + + @unittest.mock.patch('sys.stdout', new_callable=io.StringIO) + def assert_stdout(self, lst, expected_output, mock_stdout): + print_num_list(lst) + self.assertEqual(mock_stdout.getvalue(), expected_output) def test_print_num_list(self): - p = [""] + self.assert_stdout(self.mock_list, self.print_mock) + def test_search_all_match(self): + search_result = search_all_match(self.target, self.data_dir) + self.assertEqual(search_result, self.mock_list) + search_result = search_all_match("inexistent.txt", self.data_dir) + self.assertEqual(search_result, []) - def test_search_file(self): - print(search_file("match1.txt", self.data_dir)) - with self.assertRaises(FileNotFoundError) as e: - search_file("inexistent.txt", self.data_dir) - self.assertTrue("File does not exist", e.exception) - self.assertEqual(search_file("with space.txt", self.data_dir), "test/data/search_test/subdir2/with space.txt") - print(search_file("match2.txt", self.data_dir)) - print(search_file("subdir2", self.data_dir)) - \ No newline at end of file + @unittest.mock.patch("search_file.input", create=True) + def test_select_path(self, mocked_input): + print("\n") + search_file.input.side_effect=["1", "2", "3", "4"] + search_result = search_all_match(self.target, self.data_dir) + path1 = self.data_dir+"/match1.txt" + self.assertEqual(select_path(search_result), path1) + path2 = self.data_dir+"/subdir21/match1.txt" + self.assertEqual(select_path(search_result), path2) + + @unittest.mock.patch("search_file.input", create=True) + def test_search_file(self, mock_input): + search_file.input.side_effect=["1"] + with self.assertRaises(FileNotFoundError): + search_file.search_file("inexistent.txt", self.data_dir) + search_result = search_all_match(self.target, self.data_dir) + self.assertTrue(search_result, self.data_dir+"/match1.txt") + self.assertEqual(search_file.search_file("with space.txt", self.data_dir), "test/data/search_test/subdir2/with space.txt")