Coverage for /Users/les/Projects/crackerjack/crackerjack/managers/hook_manager.py: 0%

72 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-03 15:39 -0700

1import subprocess 

2import typing as t 

3from pathlib import Path 

4 

5from rich.console import Console 

6 

7from crackerjack.config.hooks import HookConfigLoader 

8from crackerjack.executors.hook_executor import HookExecutor 

9from crackerjack.models.task import HookResult 

10 

11 

12class HookManagerImpl: 

13 def __init__( 

14 self, 

15 console: Console, 

16 pkg_path: Path, 

17 verbose: bool = False, 

18 quiet: bool = False, 

19 ) -> None: 

20 self.console = console 

21 self.pkg_path = pkg_path 

22 self.executor = HookExecutor(console, pkg_path, verbose, quiet) 

23 self.config_loader = HookConfigLoader() 

24 self._config_path: Path | None = None 

25 

26 def set_config_path(self, config_path: Path) -> None: 

27 self._config_path = config_path 

28 

29 def run_fast_hooks(self) -> list[HookResult]: 

30 strategy = self.config_loader.load_strategy("fast") 

31 

32 if self._config_path: 

33 for hook in strategy.hooks: 

34 hook.config_path = self._config_path 

35 execution_result = self.executor.execute_strategy(strategy) 

36 return execution_result.results 

37 

38 def run_comprehensive_hooks(self) -> list[HookResult]: 

39 strategy = self.config_loader.load_strategy("comprehensive") 

40 

41 if self._config_path: 

42 for hook in strategy.hooks: 

43 hook.config_path = self._config_path 

44 execution_result = self.executor.execute_strategy(strategy) 

45 return execution_result.results 

46 

47 def run_hooks(self) -> list[HookResult]: 

48 fast_results = self.run_fast_hooks() 

49 comprehensive_results = self.run_comprehensive_hooks() 

50 return fast_results + comprehensive_results 

51 

52 def validate_hooks_config(self) -> bool: 

53 try: 

54 result = subprocess.run( 

55 ["uv", "run", "pre-commit", "validate-config"], 

56 cwd=self.pkg_path, 

57 capture_output=True, 

58 text=True, 

59 check=False, 

60 ) 

61 return result.returncode == 0 

62 except Exception: 

63 return False 

64 

65 def get_hook_ids(self) -> list[str]: 

66 fast_strategy = self.config_loader.load_strategy("fast") 

67 comprehensive_strategy = self.config_loader.load_strategy("comprehensive") 

68 

69 all_hooks = fast_strategy.hooks + comprehensive_strategy.hooks 

70 return [hook.name for hook in all_hooks] 

71 

72 def install_hooks(self) -> bool: 

73 try: 

74 result = subprocess.run( 

75 ["uv", "run", "pre-commit", "install"], 

76 check=False, 

77 cwd=self.pkg_path, 

78 capture_output=True, 

79 text=True, 

80 timeout=30, 

81 ) 

82 if result.returncode == 0: 

83 self.console.print("[green]✅[/ green] Pre-commit hooks installed") 

84 return True 

85 self.console.print( 

86 f"[red]❌[/ red] Failed to install hooks: {result.stderr}", 

87 ) 

88 return False 

89 except Exception as e: 

90 self.console.print(f"[red]❌[/ red] Error installing hooks: {e}") 

91 return False 

92 

93 def update_hooks(self) -> bool: 

94 try: 

95 result = subprocess.run( 

96 ["uv", "run", "pre-commit", "autoupdate"], 

97 check=False, 

98 cwd=self.pkg_path, 

99 capture_output=True, 

100 text=True, 

101 timeout=60, 

102 ) 

103 if result.returncode == 0: 

104 self.console.print("[green]✅[/ green] Pre-commit hooks updated") 

105 return True 

106 self.console.print( 

107 f"[red]❌[/ red] Failed to update hooks: {result.stderr}", 

108 ) 

109 return False 

110 except Exception as e: 

111 self.console.print(f"[red]❌[/ red] Error updating hooks: {e}") 

112 return False 

113 

114 def get_hook_summary(self, results: list[HookResult]) -> dict[str, t.Any]: 

115 if not results: 

116 return { 

117 "total": 0, 

118 "passed": 0, 

119 "failed": 0, 

120 "errors": 0, 

121 "total_duration": 0, 

122 "success_rate": 0, 

123 } 

124 

125 passed = sum(1 for r in results if r.status == "passed") 

126 failed = sum(1 for r in results if r.status == "failed") 

127 errors = sum(1 for r in results if r.status in ("timeout", "error")) 

128 total_duration = sum(r.duration for r in results) 

129 

130 return { 

131 "total": len(results), 

132 "passed": passed, 

133 "failed": failed, 

134 "errors": errors, 

135 "total_duration": total_duration, 

136 "success_rate": (passed / len(results)) * 100 if results else 0, 

137 }