(
tmp_path: Path, request: pytest.FixtureRequest
)
| 63 | |
| 64 | @pytest.fixture |
| 65 | def create_module( |
| 66 | tmp_path: Path, request: pytest.FixtureRequest |
| 67 | ) -> Callable[[FunctionType | str, bool, str | None], ModuleType]: |
| 68 | def run( |
| 69 | source_code_or_function: FunctionType | str, |
| 70 | rewrite_assertions: bool = True, |
| 71 | module_name_prefix: str | None = None, |
| 72 | ) -> ModuleType: |
| 73 | """ |
| 74 | Create module object, execute it and return |
| 75 | Can be used as a decorator of the function from the source code of which the module will be constructed |
| 76 | |
| 77 | :param source_code_or_function string or function with body as a source code for created module |
| 78 | :param rewrite_assertions: whether to rewrite assertions in module or not |
| 79 | :param module_name_prefix: string prefix to use in the name of the module, does not affect the name of the file. |
| 80 | |
| 81 | """ |
| 82 | if isinstance(source_code_or_function, FunctionType): |
| 83 | source_code = _extract_source_code_from_function(source_code_or_function) |
| 84 | else: |
| 85 | source_code = source_code_or_function |
| 86 | |
| 87 | module_name, filename = _create_module_file(source_code, tmp_path, request.node.name) |
| 88 | if module_name_prefix: |
| 89 | module_name = module_name_prefix + module_name |
| 90 | |
| 91 | if rewrite_assertions: |
| 92 | loader = AssertionRewritingHook(config=request.config) |
| 93 | loader.mark_rewrite(module_name) |
| 94 | else: |
| 95 | loader = None |
| 96 | |
| 97 | spec = importlib.util.spec_from_file_location(module_name, filename, loader=loader) |
| 98 | sys.modules[module_name] = module = importlib.util.module_from_spec(spec) # pyright: ignore[reportArgumentType] |
| 99 | spec.loader.exec_module(module) # pyright: ignore[reportOptionalMemberAccess] |
| 100 | return module |
| 101 | |
| 102 | return run |
| 103 | |
| 104 | |
| 105 | @pytest.fixture |
no outgoing calls
no test coverage detected