Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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# -------------------------------------------------------------------------------------------------- 

8 

9import pytest 

10 

11from tsfpga import DEFAULT_FILE_ENCODING, REPO_ROOT, TSFPGA_DOC 

12from tsfpga.git_utils import find_git_files 

13from tsfpga.system_utils import create_file 

14 

15 

16def open_file_with_encoding(file): 

17 print(file) 

18 with open(file, encoding="ascii") as file_handle: 

19 file_handle.read() 

20 

21 

22def test_all_checked_in_files_are_properly_encoded(): 

23 """ 

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

25 should contain only ASCII characters. 

26 

27 Avoid one of the documentation files that uses wonky characters to illustrate a directory tree. 

28 """ 

29 for file in files_to_test(exclude_directories=[TSFPGA_DOC / "sphinx" / "module_structure.rst"]): 

30 open_file_with_encoding(file) 

31 

32 

33def check_file_ends_with_newline(file): 

34 test_ok = True 

35 with open(file, encoding=DEFAULT_FILE_ENCODING) as file_handle: 

36 file_data = file_handle.read() 

37 if len(file_data) != 0: 

38 if file_data[-1] != "\n": 

39 print(f"File {file} didn't end with newline") 

40 test_ok = False 

41 return test_ok 

42 

43 

44def test_all_checked_in_files_end_with_newline(): 

45 """ 

46 All checked in non-empty files should end with a UNIX style line break (\n). 

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

48 """ 

49 test_ok = True 

50 for file in files_to_test(): 

51 test_ok &= check_file_ends_with_newline(file) 

52 assert test_ok 

53 

54 

55def check_file_for_tab_character(file): 

56 test_ok = True 

57 with open(file, encoding=DEFAULT_FILE_ENCODING) as file_handle: 

58 for idx, line in enumerate(file_handle.readlines()): 

59 if "\t" in line: 

60 test_ok = False 

61 print(f"TAB charatcher (\\t) on line {idx + 1} in {file}") 

62 return test_ok 

63 

64 

65def test_no_checked_in_files_contain_tabs(): 

66 """ 

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

68 contain TAB characters. 

69 """ 

70 test_ok = True 

71 for file in files_to_test(): 

72 test_ok &= check_file_for_tab_character(file) 

73 assert test_ok 

74 

75 

76def check_file_for_carriage_return(file): 

77 test_ok = True 

78 with open(file, encoding=DEFAULT_FILE_ENCODING, newline="") as file_handle: 

79 if "\r" in file_handle.read(): 

80 test_ok = False 

81 print(f"Windows style line breaks (\\r\\n aka CR/LF) in {file}") 

82 return test_ok 

83 

84 

85def test_no_checked_in_files_contain_carriage_return(): 

86 """ 

87 All checked in files should use UNIX style line breaks (\n not \r\n). 

88 Some Linux editors and tools will display or interpret the \r as something other than a line 

89 break. 

90 """ 

91 test_ok = True 

92 for file in files_to_test(): 

93 test_ok &= check_file_for_carriage_return(file) 

94 assert test_ok 

95 

96 

97def check_file_for_trailing_whitespace(file): 

98 test_ok = True 

99 with open(file, encoding=DEFAULT_FILE_ENCODING) as file_handle: 

100 for idx, line in enumerate(file_handle.readlines()): 

101 if " \n" in line: 

102 test_ok = False 

103 print(f"Trailing whitespace on line {idx + 1} in {file}") 

104 return test_ok 

105 

106 

107def test_no_checked_in_files_contain_trailing_whitespace(): 

108 """ 

109 Trailing whitespace is not allowed. Some motivation here: 

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

111 """ 

112 test_ok = True 

113 for file in files_to_test(): 

114 test_ok &= check_file_for_trailing_whitespace(file) 

115 assert test_ok 

116 

117 

118def files_to_test(exclude_directories=None): 

119 # Do not test binary image files 

120 return find_git_files( 

121 directory=REPO_ROOT, 

122 exclude_directories=exclude_directories, 

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

124 ) 

125 

126 

127def test_open_file_with_encoding_should_raise_exception_on_bad_file(tmp_path): 

128 """ 

129 Sanity check that the function we use actually triggers on bad files. 

130 """ 

131 file = tmp_path / "temp_file_for_test.txt" 

132 with open(file, "w", encoding="utf-8") as file_handle: 

133 # Swedish word for island = non-ASCII character 

134 data = "\N{LATIN CAPITAL LETTER O WITH DIAERESIS}" 

135 file_handle.write(data) 

136 

137 with pytest.raises(UnicodeDecodeError): 

138 open_file_with_encoding(file) 

139 

140 

141def test_check_file_for_tab_character_should_fail_on_bad_file(tmp_path): 

142 """ 

143 Sanity check that the function we use actually triggers on bad files. 

144 """ 

145 data = "Apa\thest" 

146 file = create_file(tmp_path / "temp_file_for_test.txt", data) 

147 assert not check_file_for_tab_character(file) 

148 

149 

150def test_check_file_for_carriage_return_should_fail_on_bad_file(tmp_path): 

151 """ 

152 Sanity check that the function we use actually triggers on bad files. 

153 """ 

154 file = tmp_path / "temp_file_for_test.txt" 

155 data = b"Apa\r\nhest" 

156 with file.open("wb") as file_handle: 

157 file_handle.write(data) 

158 assert not check_file_for_carriage_return(file) 

159 

160 

161def test_check_file_for_trailing_whitespace(tmp_path): 

162 """ 

163 Sanity check that the function we use actually triggers on bad files. 

164 """ 

165 data = "Apa \nhest \nzebra" 

166 file = create_file(tmp_path / "temp_file_for_test.txt", data) 

167 assert not check_file_for_trailing_whitespace(file)