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

35 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-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/hdl_registers/hdl_registers 

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

9 

10# Third party libraries 

11from tsfpga.git_utils import find_git_files 

12from tsfpga.test.lint.test_file_format import ( 

13 check_file_ends_with_newline, 

14 check_file_for_carriage_return, 

15 check_file_for_line_length, 

16 check_file_for_tab_character, 

17 check_file_for_trailing_whitespace, 

18 open_file_with_encoding, 

19) 

20 

21# First party libraries 

22from hdl_registers import HDL_REGISTERS_DOC, REPO_ROOT 

23 

24 

25def test_all_checked_in_files_are_properly_encoded(): 

26 """ 

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

28 should contain only ASCII characters. 

29 """ 

30 for file in files_to_test(): 

31 open_file_with_encoding(file) 

32 

33 

34def test_all_checked_in_files_end_with_newline(): 

35 """ 

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

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

38 """ 

39 test_ok = True 

40 for file in files_to_test(): 

41 test_ok &= check_file_ends_with_newline(file) 

42 assert test_ok 

43 

44 

45def test_no_checked_in_files_contain_tabs(): 

46 """ 

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

48 contain TAB characters. 

49 """ 

50 test_ok = True 

51 for file in files_to_test(): 

52 test_ok &= check_file_for_tab_character(file) 

53 assert test_ok 

54 

55 

56def test_no_checked_in_files_contain_carriage_return(): 

57 """ 

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

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

60 """ 

61 test_ok = True 

62 for file in files_to_test(): 

63 test_ok &= check_file_for_carriage_return(file) 

64 assert test_ok 

65 

66 

67def test_no_checked_in_files_contain_trailing_whitespace(): 

68 """ 

69 Trailing whitespace is not allowed. Some motivation here: 

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

71 """ 

72 test_ok = True 

73 for file in files_to_test(): 

74 test_ok &= check_file_for_trailing_whitespace(file) 

75 assert test_ok 

76 

77 

78def test_no_checked_in_files_have_too_long_lines(): 

79 test_ok = True 

80 excludes = [ 

81 # YAML format seems hard to break lines in 

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

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

84 REPO_ROOT / "license.rst", 

85 # Impossible to break RST syntax 

86 REPO_ROOT / "readme.rst", 

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

88 ] 

89 for file_path in files_to_test(excludes=excludes): 

90 test_ok &= check_file_for_line_length(file_path=file_path) 

91 

92 assert test_ok 

93 

94 

95def files_to_test(excludes=None): 

96 excludes = [] if excludes is None else excludes 

97 # Do not test binary image files 

98 return find_git_files( 

99 directory=REPO_ROOT, 

100 exclude_directories=excludes, 

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

102 )