Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions code2llm/analysis/smell_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,27 @@ def detect_data_clumps(

for (file, args), funcs in arg_sets.items():
if len(funcs) >= 2:
for func_name in funcs:
# Names feed exported ticket identities, so hash/discovery order
# must not turn the same finding into a new ticket after restart.
ordered_args = sorted(args)
ordered_funcs = sorted(funcs)
arg_names = ", ".join(ordered_args)
for func_name in ordered_funcs:
func_info = functions[func_name]
smells.append(
CodeSmell(
name=f"Data Clump: {', '.join(args)}",
name=f"Data Clump: {arg_names}",
type="data_clump",
file=func_info.file,
line=func_info.line,
severity=0.6,
description=(
f"Arguments ({', '.join(args)}) are used together"
f" in multiple functions: {', '.join(funcs)}."
f"Arguments ({arg_names}) are used together"
f" in multiple functions: {', '.join(ordered_funcs)}."
),
context={
"clump": list(args),
"related_functions": funcs,
"clump": ordered_args,
"related_functions": ordered_funcs,
},
)
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_prompt_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_tiktoken_truncation():

engine = PromptEngine(result)
# Mock source context to be very large
engine._get_source_context = lambda f, l: "print('Hello world')\n" * 1000
engine._get_source_context = lambda f, l, *, max_lines: "print('Hello world')\n" * 1000

prompts = engine.generate_prompts()
assert len(prompts) == 1
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_template_rendering_with_metrics():
}

engine = PromptEngine(result)
engine._get_source_context = lambda f, l: "def complex_func():\n pass"
engine._get_source_context = lambda f, l, *, max_lines: "def complex_func():\n pass"

prompts = engine.generate_prompts()
prompt_content = list(prompts.values())[0]
Expand Down
51 changes: 51 additions & 0 deletions tests/test_smells_data_clumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,54 @@ def test_fewer_than_three_args_never_counted():
smells = SmellDetector(result)._detect_data_clumps()

assert smells == []


def test_exported_clump_tickets_are_stable_across_processes(tmp_path):
"""The same finding must not create new ticket keys after a process restart."""
import json
import os
from pathlib import Path
import subprocess
import sys
import textwrap

script = textwrap.dedent("""\
import json
import sys
from dataclasses import asdict
from code2llm.analysis.smells import SmellDetector
from code2llm.core.models import AnalysisResult, FunctionInfo
from code2llm.exporters.planfile_tickets import _smell_tickets

reverse = sys.argv[1] == "reverse"
functions = [("create_github_pr", 47), ("create_gitlab_mr", 80)]
arguments = ["self", "provider", "group", "template"]
if reverse:
functions.reverse()
arguments.reverse()
result = AnalysisResult(project_path="/fixture")
for name, line in functions:
result.functions[name] = FunctionInfo(
name=name, qualified_name="publish." + name,
file="/fixture/publish.py", line=line, args=arguments,
)
result.smells = SmellDetector(result)._detect_data_clumps()
tickets = [asdict(ticket) for ticket in _smell_tickets(result)]
print(json.dumps(sorted(tickets, key=lambda t: t["dedupe_key"]), sort_keys=True))
""")
outputs = []
root = str(Path(__file__).resolve().parents[1])
for seed, order in [("1", "forward"), ("2", "forward"), ("42", "reverse")]:
env = os.environ.copy()
env["PYTHONHASHSEED"] = seed
env["PYTHONPATH"] = root + os.pathsep + env.get("PYTHONPATH", "")
child = subprocess.run(
[sys.executable, "-c", script, order],
cwd=tmp_path, env=env, check=True, capture_output=True,
text=True, timeout=30,
)
tickets = json.loads(child.stdout)
assert len(tickets) == 2
assert len({ticket["dedupe_key"] for ticket in tickets}) == 2
outputs.append(tickets)
assert outputs[0] == outputs[1] == outputs[2]
Loading
Loading