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

60 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-29 20:01 +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://gitlab.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 path_relative_to, 

22 read_last_lines_of_file, 

23 run_command, 

24) 

25 

26 

27def test_delete_files_and_folders(tmp_path): 

28 # Test deleting a file, with and without wait 

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

30 assert path.exists() 

31 delete(path) 

32 assert not path.exists() 

33 

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

35 assert path.exists() 

36 delete(path, wait_until_deleted=True) 

37 assert not path.exists() 

38 

39 # Test deleting a directory, with and without wait 

40 path = create_directory(tmp_path / "temp_dir") 

41 assert path.exists() 

42 delete(path) 

43 assert not path.exists() 

44 

45 path = create_directory(tmp_path / "temp_dir") 

46 assert path.exists() 

47 delete(path, wait_until_deleted=True) 

48 assert not path.exists() 

49 

50 

51def test_path_relative_to(): 

52 assert path_relative_to(Path("/etc/fstab"), Path("/etc")) == Path("fstab") 

53 assert path_relative_to(Path("/etc/fstab"), Path("/")) == Path("etc/fstab") 

54 assert path_relative_to(Path("/etc/fstab"), Path("/home")) == Path("../etc/fstab") 

55 

56 

57def test_read_last_lines_of_file_with_short_file(tmp_path): 

58 # A file that is smaller than the buffer size 

59 data = "a\nb\nc" 

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

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

62 

63 

64def test_read_last_lines_of_file_with_long_file(tmp_path): 

65 # A file that is larger than the buffer size 

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

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

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

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

70 

71 

72def test_read_last_lines_of_file_with_trailing_newlines(tmp_path): 

73 # A file that is smaller than the buffer size 

74 data = "a\nb\n\n \n\n" 

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

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

77 

78 

79def test_read_last_lines_of_file_with_empty_file(tmp_path): 

80 data = "" 

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

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

83 

84 data = "\n" 

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

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

87 

88 

89def test_run_command_called_with_nonexisting_binary_should_raise_exception(): 

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

91 with pytest.raises(FileNotFoundError): 

92 run_command(cmd) 

93 

94 

95def test_run_command_with_non_zero_return_code_should_raise_exception(): 

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

97 with pytest.raises(subprocess.CalledProcessError): 

98 run_command(cmd) 

99 

100 

101def test_run_command_called_with_non_list_should_raise_exception(): 

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

103 run_command(cmd) 

104 

105 cmd = "ls -la" 

106 with pytest.raises(ValueError) as exception_info: 

107 run_command(cmd) 

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