Coverage for tsfpga/test/test_system_utils.py: 100%

101 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-07 20:51 +0000

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

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

3# 

4# This file is part of the tsfpga project, a project platform for modern FPGA development. 

5# https://tsfpga.com 

6# https://github.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9# Standard libraries 

10import subprocess 

11from pathlib import Path 

12 

13# Third party libraries 

14import pytest 

15 

16# First party libraries 

17from tsfpga.system_utils import ( 

18 create_directory, 

19 create_file, 

20 delete, 

21 file_is_in_directory, 

22 path_relative_to, 

23 read_file, 

24 read_last_lines_of_file, 

25 run_command, 

26) 

27 

28 

29def test_delete_files_and_folders(tmp_path): 

30 # Test deleting a file, with and without wait 

31 path = create_file(tmp_path / "temp.txt") 

32 assert path.exists() 

33 delete(path) 

34 assert not path.exists() 

35 

36 path = create_file(tmp_path / "temp.txt") 

37 assert path.exists() 

38 delete(path, wait_until_deleted=True) 

39 assert not path.exists() 

40 

41 # Test deleting a directory, with and without wait 

42 path = create_directory(tmp_path / "temp_dir") 

43 assert path.exists() 

44 delete(path) 

45 assert not path.exists() 

46 

47 path = create_directory(tmp_path / "temp_dir") 

48 assert path.exists() 

49 delete(path, wait_until_deleted=True) 

50 assert not path.exists() 

51 

52 

53def test_create_directory_plain(tmp_path): 

54 path = tmp_path / "temp_dir" 

55 assert not path.exists() 

56 

57 create_directory(path) 

58 assert path.exists() 

59 assert path.is_dir() 

60 

61 

62def test_create_directory_that_exists_without_empty(tmp_path): 

63 path = tmp_path / "temp_dir" 

64 sub_path = create_directory(path / "sub") 

65 

66 create_directory(path, empty=False) 

67 assert sub_path.exists() 

68 

69 

70def test_create_directory_that_exists_with_empty(tmp_path): 

71 path = tmp_path / "temp_dir" 

72 sub_path = create_directory(path / "sub") 

73 

74 create_directory(path) 

75 assert path.exists() 

76 assert not sub_path.exists() 

77 

78 

79def test_create_directory_without_empty_when_path_is_a_file(tmp_path): 

80 path = create_file(tmp_path / "file.txt", contents="data") 

81 

82 with pytest.raises(FileExistsError) as exception_info: 

83 create_directory(path, empty=False) 

84 assert str(exception_info.value) == f"Requested directory path already exists as a file: {path}" 

85 

86 assert read_file(path) == "data" 

87 

88 

89def test_file_is_in_directory(tmp_path): 

90 assert file_is_in_directory(tmp_path / "file.txt", [tmp_path]) 

91 assert not file_is_in_directory(tmp_path / "file.txt", [tmp_path / "sub"]) 

92 

93 assert file_is_in_directory( 

94 tmp_path / "sub" / "file.txt", [tmp_path / "sub", tmp_path / "sub2"] 

95 ) 

96 assert not file_is_in_directory(tmp_path / "file.txt", [tmp_path / "sub", tmp_path / "sub2"]) 

97 assert not file_is_in_directory( 

98 tmp_path / "sub" / "file.txt", [tmp_path / "sub2", tmp_path / "sub3"] 

99 ) 

100 

101 

102def test_path_relative_to(): 

103 this_file = Path(__file__) 

104 this_dir = this_file.parent 

105 parent = this_dir.parent 

106 

107 assert path_relative_to(this_file, this_dir) == Path(this_file.name) 

108 assert path_relative_to(this_file, parent) == Path(this_dir.name) / this_file.name 

109 assert ( 

110 path_relative_to(this_file, parent / "whatever") 

111 == Path("..") / this_dir.name / this_file.name 

112 ) 

113 

114 

115def test_read_last_lines_of_file_with_short_file(tmp_path): 

116 # A file that is smaller than the buffer size 

117 data = "a\nb\nc" 

118 file = create_file(tmp_path / "data.txt", contents=data) 

119 assert read_last_lines_of_file(file, num_lines=10) == data 

120 

121 

122def test_read_last_lines_of_file_with_long_file(tmp_path): 

123 # A file that is larger than the buffer size 

124 data_trash = (("a" * 700) + "\n") * 3000 

125 data_last_lines = (("b" * 700) + "\n") * 10 

126 file = create_file(tmp_path / "data.txt", contents=data_trash + data_last_lines) 

127 assert read_last_lines_of_file(file, num_lines=10) == data_last_lines 

128 

129 

130def test_read_last_lines_of_file_with_trailing_newlines(tmp_path): 

131 # A file that is smaller than the buffer size 

132 data = "a\nb\n\n \n\n" 

133 file = create_file(tmp_path / "data.txt", contents=data) 

134 assert read_last_lines_of_file(file, num_lines=10) == data 

135 

136 

137def test_read_last_lines_of_file_with_empty_file(tmp_path): 

138 data = "" 

139 file = create_file(tmp_path / "data.txt", contents=data) 

140 assert read_last_lines_of_file(file, num_lines=10) == data 

141 

142 data = "\n" 

143 file = create_file(tmp_path / "data.txt", contents=data) 

144 assert read_last_lines_of_file(file, num_lines=10) == data 

145 

146 

147def test_run_command_called_with_nonexisting_binary_should_raise_exception(): 

148 cmd = ["/apa/hest/zebra.exe", "foobar"] 

149 with pytest.raises(FileNotFoundError): 

150 run_command(cmd) 

151 

152 

153def test_run_command_with_non_zero_return_code_should_raise_exception(): 

154 cmd = ["ls", "/apa/hest/zebra"] 

155 with pytest.raises(subprocess.CalledProcessError): 

156 run_command(cmd) 

157 

158 

159def test_run_command_called_with_non_list_should_raise_exception(): 

160 cmd = ["ls", "-la"] 

161 run_command(cmd) 

162 

163 cmd = "ls -la" 

164 with pytest.raises(ValueError) as exception_info: 

165 run_command(cmd) 

166 assert str(exception_info.value).startswith("Must be called with a list") 

167 

168 

169def test_run_command_should_capture_output_as_strings(): 

170 this_dir = Path(__file__).parent.resolve() 

171 

172 cmd = ["ls", str(this_dir)] 

173 result = run_command(cmd, capture_output=True) 

174 

175 assert isinstance(result.stdout, str) 

176 assert isinstance(result.stderr, str) 

177 

178 assert result.stderr == "" 

179 

180 # Show that it is regular text with regular newlines. 

181 assert "\ntest_system_utils.py\n" in result.stdout 

182 assert "\ntest_ip_core_file.py\n" in result.stdout