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

60 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-28 04:01 +0000

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 subprocess 

10 

11from pathlib import Path 

12 

13import pytest 

14 

15from tsfpga.system_utils import ( 

16 create_directory, 

17 create_file, 

18 delete, 

19 path_relative_to, 

20 read_last_lines_of_file, 

21 run_command, 

22) 

23 

24 

25def test_delete_files_and_folders(tmp_path): 

26 # Test deleting a file, with and without wait 

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

28 assert path.exists() 

29 delete(path) 

30 assert not path.exists() 

31 

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

33 assert path.exists() 

34 delete(path, wait_until_deleted=True) 

35 assert not path.exists() 

36 

37 # Test deleting a directory, with and without wait 

38 path = create_directory(tmp_path / "temp_dir") 

39 assert path.exists() 

40 delete(path) 

41 assert not path.exists() 

42 

43 path = create_directory(tmp_path / "temp_dir") 

44 assert path.exists() 

45 delete(path, wait_until_deleted=True) 

46 assert not path.exists() 

47 

48 

49def test_path_relative_to(): 

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

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

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

53 

54 

55def test_read_last_lines_of_file_with_short_file(tmp_path): 

56 # A file that is smaller than the buffer size 

57 data = "a\nb\nc" 

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

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

60 

61 

62def test_read_last_lines_of_file_with_long_file(tmp_path): 

63 # A file that is larger than the buffer size 

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

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

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

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

68 

69 

70def test_read_last_lines_of_file_with_trailing_newlines(tmp_path): 

71 # A file that is smaller than the buffer size 

72 data = "a\nb\n\n \n\n" 

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

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

75 

76 

77def test_read_last_lines_of_file_with_empty_file(tmp_path): 

78 data = "" 

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

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

81 

82 data = "\n" 

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

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

85 

86 

87def test_run_command_called_with_nonexisting_binary_should_raise_exception(): 

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

89 with pytest.raises(FileNotFoundError): 

90 run_command(cmd) 

91 

92 

93def test_run_command_with_non_zero_return_code_should_raise_exception(): 

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

95 with pytest.raises(subprocess.CalledProcessError): 

96 run_command(cmd) 

97 

98 

99def test_run_command_called_with_non_list_should_raise_exception(): 

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

101 run_command(cmd) 

102 

103 cmd = "ls -la" 

104 with pytest.raises(ValueError) as exception_info: 

105 run_command(cmd) 

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