-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: adding a test to check for circular imports
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import importlib | ||
import multiprocessing | ||
import os | ||
import pathlib | ||
|
||
import pytest | ||
|
||
|
||
def test_all_imports(): | ||
modules = get_all_modules_to_test() | ||
processes = [] | ||
for module in modules: | ||
# We create a separate process to make sure the imports do not interfere with each-other. | ||
process = multiprocessing.Process(target=import_module, args=(module,)) | ||
processes.append(process) | ||
process.start() | ||
|
||
for index, process in enumerate(processes): | ||
process.join() | ||
if process.exitcode != 0: | ||
pytest.fail( | ||
f"Unable to import '{modules[index]}'." | ||
" If all other tests are passing, check for cyclical dependencies." | ||
) | ||
|
||
|
||
def get_all_modules_to_test(): | ||
curr_path = pathlib.Path(__file__).resolve() | ||
while "test" in str(curr_path): | ||
curr_path = curr_path.parent | ||
curr_path = curr_path.joinpath("src") | ||
curr_path_len = len(str(curr_path)) + len(os.sep) | ||
modules = [] | ||
for dir_, temp, files in os.walk(curr_path): | ||
# Rather than testing every single python file we just test modules, for now. | ||
if "__init__.py" in files: | ||
braket_module = dir_[curr_path_len:].replace(os.sep, ".") | ||
modules.append(braket_module) | ||
return modules | ||
|
||
|
||
def import_module(module): | ||
importlib.import_module(module) |