| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import pytest |
| 6 |
from scalene.scalene_analysis import ScaleneAnalysis |
| 7 |
from unittest.mock import patch |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture |
| 11 |
def cleanup_imports(): |
| 12 |
|
| 13 |
import sys |
| 14 |
|
| 15 |
before = set(sys.modules.keys()) |
| 16 |
yield |
| 17 |
after = set(sys.modules.keys()) |
| 18 |
for extra in after - before: |
| 19 |
del sys.modules[extra] |
| 20 |
|
| 21 |
|
| 22 |
def test_get_native_imported_modules(cleanup_imports): |
| 23 |
|
| 24 |
with patch.object(ScaleneAnalysis, "is_native", return_value=True): |
| 25 |
source_code = """ |
| 26 |
import math |
| 27 |
import os |
| 28 |
from sys import path |
| 29 |
""" |
| 30 |
expected_imports = ["import math", "import os", "from sys import path"] |
| 31 |
actual_imports = ScaleneAnalysis.get_native_imported_modules(source_code) |
| 32 |
assert set(actual_imports) == set( |
| 33 |
expected_imports |
| 34 |
), "The list of native imports does not match the expected list." |
| 35 |
|
| 36 |
with patch.object(ScaleneAnalysis, "is_native", return_value=False): |
| 37 |
source_code = """ |
| 38 |
import math |
| 39 |
import os |
| 40 |
from sys import path |
| 41 |
""" |
| 42 |
expected_imports = [] |
| 43 |
actual_imports = ScaleneAnalysis.get_native_imported_modules(source_code) |
| 44 |
assert ( |
| 45 |
actual_imports == expected_imports |
| 46 |
), "The list of native imports should be empty." |