Coverage for tsfpga/test/lint/test_python_lint.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-29 20:01 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project, a project platform for modern FPGA development. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9# Standard libraries 

10import subprocess 

11import sys 

12from pathlib import Path 

13 

14# Third party libraries 

15import pytest 

16 

17# First party libraries 

18import tsfpga 

19from tsfpga.git_utils import find_git_files 

20from tsfpga.system_utils import create_file 

21 

22THIS_DIR = Path(__file__).parent 

23 

24 

25def _files_to_check(): 

26 # Exclude doc folder, since conf.py used by sphinx does not conform 

27 return [ 

28 str(path) 

29 for path in find_git_files( 

30 directory=tsfpga.REPO_ROOT, 

31 exclude_directories=[tsfpga.TSFPGA_DOC], 

32 file_endings_include="py", 

33 ) 

34 ] 

35 

36 

37def run_pylint(files): 

38 config = THIS_DIR / "pylintrc" 

39 command = [sys.executable, "-m", "pylint", f"--rcfile={config}"] + files 

40 

41 subprocess.check_call(command) 

42 

43 

44def test_pylint(): 

45 run_pylint(_files_to_check()) 

46 

47 

48def run_black(files): 

49 command = [sys.executable, "-m", "black", "--check", "--diff"] + files 

50 subprocess.check_call(command, cwd=tsfpga.REPO_ROOT) 

51 

52 

53def test_black_formatting(): 

54 run_black(_files_to_check()) 

55 

56 

57def run_isort(files, cwd): 

58 """ 

59 Run ``isort`` Python tool 

60 

61 Arguments: 

62 files (list(pathlib.Path)): The files that shall be checked. 

63 cwd (pathlib.Path): Path where the command shall be executed. 

64 The ``pyproject.toml`` file should be located here. 

65 """ 

66 command = [sys.executable, "-m", "isort", "--check", "--diff"] + files 

67 subprocess.check_call(command, cwd=cwd) 

68 

69 

70def test_isort_formatting(): 

71 run_isort(files=_files_to_check(), cwd=tsfpga.REPO_ROOT) 

72 

73 

74def run_flake8_lint(files): 

75 command = [sys.executable, "-m", "flake8"] + files 

76 subprocess.check_call(command, cwd=tsfpga.REPO_ROOT) 

77 

78 

79def test_flake8_lint(): 

80 run_flake8_lint(_files_to_check()) 

81 

82 

83# pylint: disable=redefined-outer-name 

84@pytest.fixture 

85def invalid_python_code_file(tmp_path): 

86 ugly_code = "aa =\ndef bb:\ncc = 3" 

87 return create_file(tmp_path / "dummy_python_file.py", ugly_code) 

88 

89 

90def test_pylint_should_raise_exception_if_there_are_ugly_files(invalid_python_code_file): 

91 with pytest.raises(subprocess.CalledProcessError): 

92 run_pylint(files=[invalid_python_code_file]) 

93 

94 

95def test_flake8_lint_should_raise_exception_if_there_are_ugly_files(invalid_python_code_file): 

96 with pytest.raises(subprocess.CalledProcessError): 

97 run_flake8_lint(files=[invalid_python_code_file]) 

98 

99 

100def test_black_formatting_should_raise_exception_if_there_are_ugly_files(invalid_python_code_file): 

101 with pytest.raises(subprocess.CalledProcessError): 

102 run_black(files=[invalid_python_code_file]) 

103 

104 

105def test_isort_formatting_should_raise_exception_if_there_are_ugly_files(tmp_path): 

106 file_path = create_file( 

107 file=tmp_path / "dummy_python_file.py", 

108 contents=""" 

109import b 

110import a 

111 

112""", 

113 ) 

114 

115 with pytest.raises(subprocess.CalledProcessError): 

116 run_isort(files=[file_path], cwd=tsfpga.REPO_ROOT)