|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# -------------------------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 6 | +# -------------------------------------------------------------------------------------------- |
| 7 | + |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import importlib |
| 11 | +import inspect |
| 12 | +from typing import Dict |
| 13 | +from breaking_changes_checker.detect_breaking_changes import create_function_report, create_class_report |
| 14 | + |
| 15 | +# The build_library_report function has been copied from the original implementation in detect_breaking_changes. |
| 16 | +# The function has been modified to remove the test_find_modules function and the modules variable. Instead we |
| 17 | +# are directly importing the target_module and using it to build the library report. |
| 18 | +# Everything else remains the same. |
| 19 | +def build_library_report(target_module: str) -> Dict: |
| 20 | + module = importlib.import_module(target_module) |
| 21 | + # removed for testing purposes |
| 22 | + # modules = test_find_modules(module.__path__[0]) |
| 23 | + module_name = module.__name__ |
| 24 | + public_api = {} |
| 25 | + public_api[module_name] = {"class_nodes": {}, "function_nodes": {}} |
| 26 | + module = importlib.import_module(module_name) |
| 27 | + importables = [importable for importable in dir(module)] |
| 28 | + for importable in importables: |
| 29 | + if not importable.startswith("_"): |
| 30 | + live_obj = getattr(module, importable) |
| 31 | + if inspect.isfunction(live_obj): |
| 32 | + public_api[module_name]["function_nodes"].update({importable: create_function_report(live_obj)}) |
| 33 | + elif inspect.isclass(live_obj): |
| 34 | + public_api[module_name]["class_nodes"].update({importable: create_class_report(live_obj)}) |
| 35 | + # else: # Constants, version, etc. Nothing of interest at the moment |
| 36 | + # public_api[module_name]["others"].update({importable: live_obj}) |
| 37 | + return public_api |
| 38 | + |
| 39 | +def test_base_class_properties(): |
| 40 | + sys.path.append(os.path.join(os.path.dirname(__file__), "examples", "base_class_properties")) |
| 41 | + |
| 42 | + report = build_library_report("base_class") |
| 43 | + # Check the report for properties of the base class in Bar |
| 44 | + assert report["base_class"]["class_nodes"]["Foo"]["properties"] == {"a": None} |
| 45 | + assert report["base_class"]["class_nodes"]["Bar"]["properties"] == {"a": None, "b": None} |
0 commit comments