Coverage for circular_deps/formatter.py: 96%

1 from __future__ import annotations

2

3 from pathlib import Path

4 from typing import TYPE_CHECKING

5

6 if TYPE_CHECKING:

7 from circular_deps.models import Cycle

8

9

10 def format_text(cycles):

11 output = []

12 output.append("\nCircular Dependencies")

13 output.append("─────────────────────")

14

15 if cycles:

16 for i, cycle in enumerate(cycles, 1):

17 output.append(

18 f"\033[33m⚠ Warning: cycle detected ({cycle.depth} files)\033[0m"

19 )

20 for from_file, to_file, line in cycle.edges:

21 from_name = Path(from_file).name

22 to_name = Path(to_file).name

23 output.append(f" {from_name}:{line} → {to_name}")

24 output.append("")

25 output.append(f"{len(cycles)} cycle(s) found")

26 else:

27 output.append("\033[32m✓ No circular dependencies found\033[0m")

28

29 return "\n".join(output)

30

31

32 def format_json(cycles):

33 cycle_list = []

34 for cycle in cycles:

35 cycle_list.append(

36 {

37 "severity": "warning",

38 "depth": cycle.depth,

39 "files": cycle.files,

40 "edges": [

41 {"from": f, "to": t, "line": line} for f, t, line in cycle.edges

42 ],

43 }

44 )

45

46 import json

47

48 return json.dumps({"cycles": cycle_list, "total_cycles": len(cycles)}, indent=2)