Coverage for hdl_registers/test/lint/test_file_format.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-29 22:03 +0000

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

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

3# 

4# This file is part of the hdl_registers project, a HDL register generator fast enough to be run 

5# in real time. 

6# https://hdl-registers.com 

7# https://gitlab.com/tsfpga/hdl_registers 

8# -------------------------------------------------------------------------------------------------- 

9 

10from tsfpga.git_utils import find_git_files 

11from tsfpga.test.lint.test_file_format import ( 

12 check_file_ends_with_newline, 

13 check_file_for_carriage_return, 

14 check_file_for_line_length, 

15 check_file_for_tab_character, 

16 check_file_for_trailing_whitespace, 

17 open_file_with_encoding, 

18) 

19 

20from hdl_registers import REPO_ROOT, HDL_REGISTERS_DOC 

21 

22 

23def test_all_checked_in_files_are_properly_encoded(): 

24 """ 

25 To avoid problems with different editors and different file encodings, all checked in files 

26 should contain only ASCII characters. 

27 """ 

28 for file in files_to_test(): 

29 open_file_with_encoding(file) 

30 

31 

32def test_all_checked_in_files_end_with_newline(): 

33 """ 

34 All checked in files should end with a UNIX style line break (\n). 

35 Otherwise UNIX doesn't consider them actual text files. 

36 """ 

37 test_ok = True 

38 for file in files_to_test(): 

39 test_ok &= check_file_ends_with_newline(file) 

40 assert test_ok 

41 

42 

43def test_no_checked_in_files_contain_tabs(): 

44 """ 

45 To avoid problems with files looking different in different editors, no checked in files may 

46 contain TAB characters. 

47 """ 

48 test_ok = True 

49 for file in files_to_test(): 

50 test_ok &= check_file_for_tab_character(file) 

51 assert test_ok 

52 

53 

54def test_no_checked_in_files_contain_carriage_return(): 

55 """ 

56 All checked in files should use UNIX style line breaks (\n not \r\n). Some Linux editors and 

57 tools will display or interpret the \r as something other than a line break. 

58 """ 

59 test_ok = True 

60 for file in files_to_test(): 

61 test_ok &= check_file_for_carriage_return(file) 

62 assert test_ok 

63 

64 

65def test_no_checked_in_files_contain_trailing_whitespace(): 

66 """ 

67 Trailing whitespace is not allowed. Some motivation here: 

68 https://softwareengineering.stackexchange.com/questions/121555 

69 """ 

70 test_ok = True 

71 for file in files_to_test(): 

72 test_ok &= check_file_for_trailing_whitespace(file) 

73 assert test_ok 

74 

75 

76def test_no_checked_in_files_have_too_long_lines(): 

77 test_ok = True 

78 excludes = [ 

79 # YAML format seems hard to break lines in 

80 REPO_ROOT / ".gitlab-ci.yml", 

81 # We list the license text exactly as the original, with no line breaks 

82 REPO_ROOT / "license.rst", 

83 # Impossible to break RST syntax 

84 REPO_ROOT / "readme.rst", 

85 HDL_REGISTERS_DOC / "sphinx" / "html_generator.rst", 

86 ] 

87 for file_path in files_to_test(excludes=excludes): 

88 test_ok &= check_file_for_line_length(file_path=file_path) 

89 

90 assert test_ok 

91 

92 

93def files_to_test(excludes=None): 

94 excludes = [] if excludes is None else excludes 

95 # Do not test binary image files 

96 return find_git_files( 

97 directory=REPO_ROOT, 

98 exclude_directories=excludes, 

99 file_endings_avoid=("png", "svg"), 

100 )