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 

11import tsfpga 

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 file.open(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( 

30 exclude_directories=[tsfpga.TSFPGA_DOC / "sphinx" / "module_structure.rst"] 

31 ): 

32 open_file_with_encoding(file) 

33 

34 

35def check_file_ends_with_newline(file): 

36 test_ok = True 

37 with file.open() as file_handle: 

38 file_data = file_handle.read() 

39 if len(file_data) != 0: 

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

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

42 test_ok = False 

43 return test_ok 

44 

45 

46def test_all_checked_in_files_end_with_newline(): 

47 """ 

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

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

50 """ 

51 test_ok = True 

52 for file in files_to_test(): 

53 test_ok &= check_file_ends_with_newline(file) 

54 assert test_ok 

55 

56 

57def check_file_for_tab_character(file): 

58 test_ok = True 

59 with file.open() as file_handle: 

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

61 if "\t" in line: 

62 test_ok = False 

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

64 return test_ok 

65 

66 

67def test_no_checked_in_files_contain_tabs(): 

68 """ 

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

70 contain TAB characters. 

71 """ 

72 test_ok = True 

73 for file in files_to_test(): 

74 test_ok &= check_file_for_tab_character(file) 

75 assert test_ok 

76 

77 

78def check_file_for_carriage_return(file): 

79 test_ok = True 

80 with file.open(newline="") as file_handle: 

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

82 test_ok = False 

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

84 return test_ok 

85 

86 

87def test_no_checked_in_files_contain_carriage_return(): 

88 """ 

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

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

91 break. 

92 """ 

93 test_ok = True 

94 for file in files_to_test(): 

95 test_ok &= check_file_for_carriage_return(file) 

96 assert test_ok 

97 

98 

99def check_file_for_trailing_whitespace(file): 

100 test_ok = True 

101 with file.open() as file_handle: 

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

103 if " \n" in line: 

104 test_ok = False 

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

106 return test_ok 

107 

108 

109def test_no_checked_in_files_contain_trailing_whitespace(): 

110 """ 

111 Trailing whitespace is not allowed. Some motivation here: 

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

113 """ 

114 test_ok = True 

115 for file in files_to_test(): 

116 test_ok &= check_file_for_trailing_whitespace(file) 

117 assert test_ok 

118 

119 

120def files_to_test(exclude_directories=None): 

121 # Do not test binary image files 

122 return find_git_files( 

123 directory=tsfpga.REPO_ROOT, 

124 exclude_directories=exclude_directories, 

125 file_endings_avoid="png", 

126 ) 

127 

128 

129def test_open_file_with_encoding_should_raise_exception_on_bad_file(tmp_path): 

130 """ 

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

132 """ 

133 file = tmp_path / "temp_file_for_test.txt" 

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

135 # Swedish word for island = non-ASCII character 

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

137 file_handle.write(data) 

138 

139 with pytest.raises(UnicodeDecodeError): 

140 open_file_with_encoding(file) 

141 

142 

143def test_check_file_for_tab_character_should_fail_on_bad_file(tmp_path): 

144 """ 

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

146 """ 

147 data = "Apa\thest" 

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

149 assert not check_file_for_tab_character(file) 

150 

151 

152def test_check_file_for_carriage_return_should_fail_on_bad_file(tmp_path): 

153 """ 

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

155 """ 

156 file = tmp_path / "temp_file_for_test.txt" 

157 data = b"Apa\r\nhest" 

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

159 file_handle.write(data) 

160 assert not check_file_for_carriage_return(file) 

161 

162 

163def test_check_file_for_trailing_whitespace(tmp_path): 

164 """ 

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

166 """ 

167 data = "Apa \nhest \nzebra" 

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

169 assert not check_file_for_trailing_whitespace(file)