|
| 1 | +import re |
| 2 | +import os |
| 3 | +import glob |
| 4 | + |
| 5 | + |
| 6 | +def f_657(dir_path): |
| 7 | + """ |
| 8 | + Search for occurrences of the word "error" in all text files within a |
| 9 | + specified directory and its subdirectories. |
| 10 | + |
| 11 | + Parameters: |
| 12 | + dir_path (str): The path of the directory. |
| 13 | + |
| 14 | + Returns: |
| 15 | + dict: A dictionary with relative file paths as keys and the count of |
| 16 | + occurrences of the word "error" as values. |
| 17 | + |
| 18 | + Raises: |
| 19 | + - ValueError: If directory in dir_path does not exist. |
| 20 | +
|
| 21 | + Requirements: |
| 22 | + - re: For regex pattern matching. |
| 23 | + - os: For retrieving relative file paths. |
| 24 | + - glob: For fetching all text file paths in the directory. |
| 25 | + |
| 26 | + The function specifically searches for the word "error" in text files |
| 27 | + (with the extension ".txt"). |
| 28 | + This function is NOT case sensitive, e.g. also "ERROr" will be counted. |
| 29 | + |
| 30 | + Example: |
| 31 | + >>> f_657("/path/to/directory") |
| 32 | + {'file1.txt': 2, 'subdir/file2.txt': 1} |
| 33 | +
|
| 34 | + >>> f_657("/path/to/directory") |
| 35 | + {'test.txt': 245, 'subdir/test2.txt': 0, 'subdir/sf/test3.txt': 1} |
| 36 | + """ |
| 37 | + |
| 38 | + if not os.path.isdir(dir_path): |
| 39 | + raise ValueError("Specified directory does not exist.") |
| 40 | + |
| 41 | + result = {} |
| 42 | + file_paths = glob.glob(f'{dir_path}/**/*.txt', recursive=True) |
| 43 | + for file_path in file_paths: |
| 44 | + with open(file_path, 'r') as file: |
| 45 | + content = file.read() |
| 46 | + matches = re.findall(r'\berror\b', content, re.IGNORECASE) |
| 47 | + # Always set the file's count in the result dictionary, even if it's 0 |
| 48 | + result[os.path.relpath(file_path, dir_path)] = len(matches) |
| 49 | + |
| 50 | + return result |
| 51 | + |
| 52 | +import unittest |
| 53 | +import os |
| 54 | +import shutil |
| 55 | +import tempfile |
| 56 | +class TestCases(unittest.TestCase): |
| 57 | + def setUp(self): |
| 58 | + # Create a temporary directory to simulate test environments |
| 59 | + self.test_dir = tempfile.mkdtemp() |
| 60 | + def tearDown(self): |
| 61 | + # Remove the temporary directory after the test |
| 62 | + shutil.rmtree(self.test_dir) |
| 63 | + def create_file(self, sub_path, content=""): |
| 64 | + # Helper method to create a file with given content |
| 65 | + full_path = os.path.join(self.test_dir, sub_path) |
| 66 | + os.makedirs(os.path.dirname(full_path), exist_ok=True) |
| 67 | + with open(full_path, 'w') as file: |
| 68 | + file.write(content) |
| 69 | + # Return normalized path for cross-platform compatibility |
| 70 | + return os.path.normpath(sub_path) |
| 71 | + def test_non_existent(self): |
| 72 | + # Expect ValueError for non-existent directory |
| 73 | + with self.assertRaises(ValueError): |
| 74 | + f_657(os.path.join(self.test_dir, "non_existent")) |
| 75 | + def test_empty_folder(self): |
| 76 | + # Test empty directory |
| 77 | + result = f_657(self.test_dir) |
| 78 | + self.assertEqual(result, {}) |
| 79 | + def test_files_with_errors(self): |
| 80 | + # Files with varying counts of 'error' |
| 81 | + files = { |
| 82 | + "1.txt": "error\nERROR\nErrOr", |
| 83 | + "subfolder1/2.txt": "", |
| 84 | + "subfolder2/3.txt": "error\nerror error" |
| 85 | + } |
| 86 | + expected = { |
| 87 | + os.path.normpath("1.txt"): 3, |
| 88 | + os.path.normpath("subfolder1/2.txt"): 0, |
| 89 | + os.path.normpath("subfolder2/3.txt"): 3 |
| 90 | + } |
| 91 | + for path, content in files.items(): |
| 92 | + self.create_file(path, content) |
| 93 | + result = f_657(self.test_dir) |
| 94 | + self.assertEqual(result, expected) |
| 95 | + def test_case_sensitive_and_realistic_text(self): |
| 96 | + # More complex scenarios, including nested directories |
| 97 | + file_path = self.create_file('nested/folder1/folder2/error_log.txt', 'Error\nerror\nERROR') |
| 98 | + expected = {file_path: 3} |
| 99 | + result = f_657(self.test_dir) |
| 100 | + self.assertEqual(result, expected) |
| 101 | + def test_exact_word_matching(self): |
| 102 | + # Ensure only the exact word 'error' is counted and ignore similar words like 'errors' |
| 103 | + files = { |
| 104 | + "file1.txt": "error error error", # Should count 3 times |
| 105 | + "subdir/file2.txt": "errors error erro errors", # Should count 1 time |
| 106 | + "subdir2/nested/file3.txt": "an error occurred", # Should count 1 time |
| 107 | + "subdir3/file4.txt": "no errors here", # Should count 0 times |
| 108 | + "subdir3/file5.txt": "Error and ERROR and error" # Should count 3 times, case insensitive |
| 109 | + } |
| 110 | + expected = { |
| 111 | + os.path.normpath("file1.txt"): 3, |
| 112 | + os.path.normpath("subdir/file2.txt"): 1, |
| 113 | + os.path.normpath("subdir2/nested/file3.txt"): 1, |
| 114 | + os.path.normpath("subdir3/file4.txt"): 0, |
| 115 | + os.path.normpath("subdir3/file5.txt"): 3 |
| 116 | + } |
| 117 | + for path, content in files.items(): |
| 118 | + self.create_file(path, content) |
| 119 | + result = f_657(self.test_dir) |
| 120 | + self.assertEqual(result, expected) |
0 commit comments