Coverage for .nox/test-3-9/lib/python3.9/site-packages/nskit/mixer/hooks/pre_commit.py: 42%

24 statements  

« prev     ^ index     » next       coverage.py v7.4.2, created at 2024-02-25 17:38 +0000

1"""Precommit hooks. 

2 

3Contains post creation precommit install hooks. 

4""" 

5from pathlib import Path 

6import subprocess # nosec B404 

7import sys 

8from typing import Any, Dict 

9 

10from nskit._logging import logger_factory 

11from nskit.common.contextmanagers import ChDir 

12from nskit.mixer.components import Hook 

13 

14logger = logger_factory.get(__name__) 

15 

16 

17class PrecommitInstall(Hook): 

18 """Precommit install hook.""" 

19 

20 def call(self, recipe_path: Path, context: Dict[str, Any]): # noqa: U100 

21 """Run the pre-commit install and install hooks command.""" 

22 with ChDir(recipe_path): 

23 if Path('.pre-commit-config.yaml').exists(): 

24 logger.info('Installing precommit') 

25 # Install if pre-commit installed 

26 subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pre-commit']) # nosec B603 

27 logger.info('Installing hooks') 

28 with open('.pre-commit-config.yaml') as f: 

29 logger.info(f'Precommit Config: {f.read()}') 

30 # Run 

31 try: 

32 subprocess.check_output([sys.executable, '-m', 'pre_commit', 'install', '--install-hooks']) # nosec B603 

33 except subprocess.CalledProcessError as e: 

34 logger.error('Error running pre-commit', output=e.output, return_code=e.returncode) 

35 raise e from None 

36 logger.info('Done') 

37 else: 

38 logger.info('Precommit config file not detected, skipping.')