| 1 |
import ast |
| 2 |
from typing import Any, get_type_hints |
| 3 |
|
| 4 |
import hypothesis |
| 5 |
import libcst |
| 6 |
from hypothesis import strategies as st |
| 7 |
from hypothesmith import from_grammar, from_node |
| 8 |
|
| 9 |
import isort |
| 10 |
|
| 11 |
|
| 12 |
def _as_config(kw: dict[str, Any]) -> isort.Config: |
| 13 |
if "wrap_length" in kw and "line_length" in kw: |
| 14 |
kw["wrap_length"], kw["line_length"] = sorted([kw["wrap_length"], kw["line_length"]]) |
| 15 |
try: |
| 16 |
return isort.Config(**kw) |
| 17 |
except ValueError: |
| 18 |
kw["wrap_length"] = 0 |
| 19 |
return isort.Config(**kw) |
| 20 |
|
| 21 |
|
| 22 |
def _record_targets(code: str, prefix: str = "") -> str: |
| 23 |
# target larger inputs - the Hypothesis engine will do a multi-objective |
| 24 |
# hill-climbing search using these scores to generate 'better' examples. |
| 25 |
nodes = list(ast.walk(ast.parse(code))) |
| 26 |
import_nodes = [n for n in nodes if isinstance(n, (ast.Import, ast.ImportFrom))] |
| 27 |
uniq_nodes = {type(n) for n in nodes} |
| 28 |
for value, label in [ |
| 29 |
(len(import_nodes), "total number of import nodes"), |
| 30 |
(len(uniq_nodes), "number of unique ast node types"), |
| 31 |
]: |
| 32 |
hypothesis.target(float(value), label=prefix + label) |
| 33 |
return code |
| 34 |
|
| 35 |
|
| 36 |
def configs(**force_strategies: st.SearchStrategy) -> st.SearchStrategy: |
| 37 |
"""Generate arbitrary Config objects.""" |
| 38 |
skip = { |
| 39 |
"line_ending", |
| 40 |
"sections", |
| 41 |
"known_future_library", |
| 42 |
"forced_separate", |
| 43 |
"lines_before_imports", |
| 44 |
"lines_after_imports", |
| 45 |
"lines_between_sections", |
| 46 |
"lines_between_types", |
| 47 |
"sources", |
| 48 |
"virtual_env", |
| 49 |
"conda_env", |
| 50 |
"directory", |
| 51 |
"formatter", |
| 52 |
"formatting_function", |
| 53 |
} |
| 54 |
inferred_kwargs = { |
| 55 |
k: st.from_type(v) |
| 56 |
for k, v in get_type_hints(isort.settings._Config).items() |
| 57 |
if k not in skip |
| 58 |
} |
| 59 |
specific = { |
| 60 |
"line_length": st.integers(0, 200), |
| 61 |
"wrap_length": st.integers(0, 200), |
| 62 |
"indent": st.integers(0, 20).map(lambda n: n * " "), |
| 63 |
"default_section": st.sampled_from(sorted(isort.settings.KNOWN_SECTION_MAPPING)), |
| 64 |
"force_grid_wrap": st.integers(0, 20), |
| 65 |
"profile": st.sampled_from(sorted(isort.settings.profiles)), |
| 66 |
"py_version": st.sampled_from(("auto", *isort.settings.VALID_PY_TARGETS)), |
| 67 |
} |
| 68 |
kwargs = {**inferred_kwargs, **specific, **force_strategies} |
| 69 |
return st.fixed_dictionaries({}, optional=kwargs).map(_as_config) |
| 70 |
|
| 71 |
|
| 72 |
st.register_type_strategy(isort.Config, configs()) |
| 73 |
|
| 74 |
|
| 75 |
@hypothesis.example("import A\nimportA\r\n\n", isort.Config(), False) |
| 76 |
@hypothesis.given( |
| 77 |
source_code=st.lists( |
| 78 |
from_grammar(auto_target=False) |
| 79 |
| from_node(auto_target=False) |
| 80 |
| from_node(libcst.Import, auto_target=False) |
| 81 |
| from_node(libcst.ImportFrom, auto_target=False), |
| 82 |
min_size=1, |
| 83 |
max_size=10, |
| 84 |
).map("\n".join), |
| 85 |
config=st.builds(isort.Config), |
| 86 |
disregard_skip=st.booleans(), |
| 87 |
) |
| 88 |
@hypothesis.seed(235738473415671197623909623354096762459) |
| 89 |
@hypothesis.settings( |
| 90 |
suppress_health_check=[ |
| 91 |
hypothesis.HealthCheck.too_slow, |
| 92 |
hypothesis.HealthCheck.filter_too_much, |
| 93 |
] |
| 94 |
) |
| 95 |
def test_isort_is_idempotent(source_code: str, config: isort.Config, disregard_skip: bool) -> None: |
| 96 |
# NOTE: if this test finds a bug, please notify @Zac-HD so that it can be added to the |
| 97 |
# Hypothesmith trophy case. This really helps with research impact evaluations! |
| 98 |
_record_targets(source_code) |
| 99 |
result = isort.code(source_code, config=config, disregard_skip=disregard_skip) |
| 100 |
assert result == isort.code(result, config=config, disregard_skip=disregard_skip) |