|
| 1 | +import importlib |
| 2 | +import inspect |
| 3 | +import pkgutil |
| 4 | +import unittest |
| 5 | + |
| 6 | + |
| 7 | +def get_public_functions(module): |
| 8 | + """Get all public functions and methods in the module.""" |
| 9 | + return [ |
| 10 | + name |
| 11 | + for name, obj in inspect.getmembers(module, inspect.isfunction) |
| 12 | + if not is_private_function(name) and inspect.getmodule(obj) == module |
| 13 | + ] + [ |
| 14 | + name |
| 15 | + for name, obj in inspect.getmembers(module, inspect.ismethod) |
| 16 | + if obj.__module__ == module.__name__ and not name.startswith("_") |
| 17 | + ] |
| 18 | + |
| 19 | + |
| 20 | +def get_imported_methods(module): |
| 21 | + """Get all names in the module's namespace.""" |
| 22 | + return [ |
| 23 | + name |
| 24 | + for name in dir(module) |
| 25 | + if not is_private_function(name) and not is_standard_function(name) |
| 26 | + ] |
| 27 | + |
| 28 | + |
| 29 | +def is_private_function(name): |
| 30 | + """Check if a function is private.""" |
| 31 | + return name.startswith("_") |
| 32 | + |
| 33 | + |
| 34 | +def is_standard_function(name): |
| 35 | + """Check if a function name is a standard or built-in function.""" |
| 36 | + return name in dir(__builtins__) or ( |
| 37 | + name.startswith("__") and name.endswith("__") |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +class TestImports(unittest.TestCase): |
| 42 | + @classmethod |
| 43 | + def setUpClass(cls): |
| 44 | + """Set up the package and its __init__.py module.""" |
| 45 | + cls.package_name = "brutils" |
| 46 | + cls.package = importlib.import_module(cls.package_name) |
| 47 | + cls.init_module = importlib.import_module( |
| 48 | + f"{cls.package_name}.__init__" |
| 49 | + ) |
| 50 | + |
| 51 | + cls.all_public_methods = [] |
| 52 | + cls.imported_methods = [] |
| 53 | + |
| 54 | + # Iterate over all submodules and collect their public methods |
| 55 | + for _, module_name, _ in pkgutil.walk_packages( |
| 56 | + cls.package.__path__, cls.package.__name__ + "." |
| 57 | + ): |
| 58 | + module = importlib.import_module(module_name) |
| 59 | + cls.all_public_methods.extend(get_public_functions(module)) |
| 60 | + |
| 61 | + # Collect imported methods from __init__.py |
| 62 | + cls.imported_methods = get_imported_methods(cls.init_module) |
| 63 | + |
| 64 | + # Filter out standard or built-in functions |
| 65 | + cls.filtered_public_methods = [ |
| 66 | + method |
| 67 | + for method in cls.all_public_methods |
| 68 | + if not is_standard_function(method) |
| 69 | + ] |
| 70 | + cls.filtered_imported_methods = [ |
| 71 | + method |
| 72 | + for method in cls.imported_methods |
| 73 | + if not is_standard_function(method) |
| 74 | + ] |
| 75 | + |
| 76 | + # Remove specific old methods |
| 77 | + cls.filtered_public_methods = [ |
| 78 | + method |
| 79 | + for method in cls.filtered_public_methods |
| 80 | + if method not in {"sieve", "display", "validate"} |
| 81 | + ] |
| 82 | + |
| 83 | + # Ensure all public methods are included in __all__ |
| 84 | + cls.all_defined_names = dir(cls.init_module) |
| 85 | + cls.public_methods_in_all = getattr(cls.init_module, "__all__", []) |
| 86 | + |
| 87 | + cls.missing_imports = [ |
| 88 | + method |
| 89 | + for method in cls.filtered_public_methods |
| 90 | + if method not in cls.filtered_imported_methods |
| 91 | + ] |
| 92 | + |
| 93 | + def test_public_methods_in_imports(self): |
| 94 | + """Test that all public methods are imported or aliased.""" |
| 95 | + aliases_imports = [ |
| 96 | + method |
| 97 | + for method in self.filtered_imported_methods |
| 98 | + if method not in self.filtered_public_methods |
| 99 | + ] |
| 100 | + |
| 101 | + diff = len(self.missing_imports) - len(aliases_imports) |
| 102 | + |
| 103 | + if diff != 0: |
| 104 | + self.fail( |
| 105 | + f"{diff} public method(s) missing from imports at __init__.py. You need to import the new brutils features methods inside the brutils/__init__.py file" |
| 106 | + ) |
| 107 | + |
| 108 | + def test_public_methods_in_all(self): |
| 109 | + """Test that all public methods are included in __all__.""" |
| 110 | + missing_in_all = set(self.filtered_imported_methods) - set( |
| 111 | + self.public_methods_in_all |
| 112 | + ) |
| 113 | + |
| 114 | + if missing_in_all: |
| 115 | + self.fail( |
| 116 | + f"Public method(s) missing from __all__: {missing_in_all}. You need to add the new brutils features methods names to the list __all__ in the brutils/__init__.py file" |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + unittest.main() |
0 commit comments