Coverage for tsfpga/test/lint/test_python_lint.py: 100%
42 statements
« prev ^ index » next coverage.py v6.4, created at 2022-05-28 04:01 +0000
« prev ^ index » next coverage.py v6.4, created at 2022-05-28 04:01 +0000
1# --------------------------------------------------------------------------------------------------
2# Copyright (c) Lukas Vik. All rights reserved.
3#
4# This file is part of the tsfpga project.
5# https://tsfpga.com
6# https://gitlab.com/tsfpga/tsfpga
7# --------------------------------------------------------------------------------------------------
9import subprocess
10import sys
11import unittest
13from pathlib import Path
14import pytest
16import tsfpga
17from tsfpga.git_utils import find_git_files
18from tsfpga.system_utils import create_file
21THIS_DIR = Path(__file__).parent
24def _files_to_check():
25 # Exclude doc folder, since conf.py used by sphinx does not conform
26 return [
27 str(path)
28 for path in find_git_files(
29 directory=tsfpga.REPO_ROOT,
30 exclude_directories=[tsfpga.TSFPGA_DOC],
31 file_endings_include="py",
32 )
33 ]
36def run_pylint(files):
37 config = THIS_DIR / "pylintrc"
38 command = [sys.executable, "-m", "pylint", f"--rcfile={config}"] + files
40 subprocess.check_call(command)
43def test_pylint():
44 run_pylint(_files_to_check())
47def run_black(files):
48 command = [sys.executable, "-m", "black", "--check", "--diff"] + files
49 subprocess.check_call(command, cwd=tsfpga.REPO_ROOT)
52def test_black_formatting():
53 run_black(_files_to_check())
56def run_flake8_lint(files):
57 command = [sys.executable, "-m", "flake8"] + files
58 subprocess.check_call(command, cwd=tsfpga.REPO_ROOT)
61def test_flake8_lint():
62 run_flake8_lint(_files_to_check())
65@pytest.mark.usefixtures("fixture_tmp_path")
66class TestPythonLintFunctions(unittest.TestCase):
67 tmp_path = None
69 def setUp(self):
70 ugly_code = "aa =\ndef bb:\ncc = 3"
71 self.file = str(create_file(self.tmp_path / "dummy_python_file.py", ugly_code))
73 def test_pylint_should_raise_exception_if_there_are_ugly_files(self):
74 with pytest.raises(subprocess.CalledProcessError):
75 run_pylint([self.file])
77 def test_flake8_lint_should_raise_exception_if_there_are_ugly_files(self):
78 with pytest.raises(subprocess.CalledProcessError):
79 run_flake8_lint([self.file])
81 def test_black_formatting_should_raise_exception_if_there_are_ugly_files(self):
82 with pytest.raises(subprocess.CalledProcessError):
83 run_black([self.file])