Coverage for src/pytest_vulture/plugin.py: 36.36%

20 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-09-30 15:32 +0200

1# -*- coding: utf-8 -*- 

2"""Pytest vulture plugins. Both pylint wrapper and PylintPlugin 

3""" 

4from pathlib import Path 

5 

6from pytest_vulture.call import VultureCall 

7from pytest_vulture.conf.reader import IniReader 

8from pytest_vulture.item import VulturePinningFile 

9 

10 

11def pytest_addoption(parser): 

12 """ 

13 Set the pytest options 

14 :param parser: the pytest args parser 

15 """ 

16 group = parser.getgroup("general") 

17 # Set the --vulture option in the setup.py 

18 group.addoption( 

19 '--vulture', action='store_true', help="run vulture on .py files" 

20 ) 

21 group.addoption( 

22 '--vulture-cfg-file', help="Defines the vulture config file path", 

23 default="tox.ini", 

24 ) 

25 

26 

27def pytest_sessionstart(session): 

28 """ 

29 Called at the start of a pytest run. 

30 This is when we will call vulture. 

31 :param session: the pytest session 

32 """ 

33 # 1. get the config 

34 config = session.config 

35 # 2. Check if the vulture option is set to True. 

36 if config.option.vulture: 

37 # 3. Call vulture on the root vulture_output 

38 root_dir = session.startdir 

39 reader = IniReader(path_ini=Path(config.option.vulture_cfg_file)) 

40 reader.read_ini() 

41 session.vulture = VultureCall(root_dir, reader) 

42 session.vulture.call() 

43 

44 

45def pytest_collect_file(file_path, parent): 

46 """ 

47 Called in every file during the pytest call 

48 :param file_path: the current vulture_output of the file 

49 :param parent: the parent file 

50 :return: None or nn item 

51 """ 

52 config = parent.config 

53 if config.option.vulture and file_path.suffix == '.py': # pragma: no cover 

54 return VulturePinningFile.from_parent(parent=parent, path=file_path) 

55 return None