Coverage for tsfpga/test/test_system_utils.py: 100%
101 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-21 20:51 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-21 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# --------------------------------------------------------------------------------------------------
9import subprocess
10from pathlib import Path
12import pytest
14from tsfpga.system_utils import (
15 create_directory,
16 create_file,
17 delete,
18 file_is_in_directory,
19 path_relative_to,
20 read_file,
21 read_last_lines_of_file,
22 run_command,
23)
26def test_delete_files_and_folders(tmp_path):
27 # Test deleting a file, with and without wait
28 path = create_file(tmp_path / "temp.txt")
29 assert path.exists()
30 delete(path)
31 assert not path.exists()
33 path = create_file(tmp_path / "temp.txt")
34 assert path.exists()
35 delete(path, wait_until_deleted=True)
36 assert not path.exists()
38 # Test deleting a directory, with and without wait
39 path = create_directory(tmp_path / "temp_dir")
40 assert path.exists()
41 delete(path)
42 assert not path.exists()
44 path = create_directory(tmp_path / "temp_dir")
45 assert path.exists()
46 delete(path, wait_until_deleted=True)
47 assert not path.exists()
50def test_create_directory_plain(tmp_path):
51 path = tmp_path / "temp_dir"
52 assert not path.exists()
54 create_directory(path)
55 assert path.exists()
56 assert path.is_dir()
59def test_create_directory_that_exists_without_empty(tmp_path):
60 path = tmp_path / "temp_dir"
61 sub_path = create_directory(path / "sub")
63 create_directory(path, empty=False)
64 assert sub_path.exists()
67def test_create_directory_that_exists_with_empty(tmp_path):
68 path = tmp_path / "temp_dir"
69 sub_path = create_directory(path / "sub")
71 create_directory(path)
72 assert path.exists()
73 assert not sub_path.exists()
76def test_create_directory_without_empty_when_path_is_a_file(tmp_path):
77 path = create_file(tmp_path / "file.txt", contents="data")
79 with pytest.raises(FileExistsError) as exception_info:
80 create_directory(path, empty=False)
81 assert str(exception_info.value) == f"Requested directory path already exists as a file: {path}"
83 assert read_file(path) == "data"
86def test_file_is_in_directory(tmp_path):
87 assert file_is_in_directory(tmp_path / "file.txt", [tmp_path])
88 assert not file_is_in_directory(tmp_path / "file.txt", [tmp_path / "sub"])
90 assert file_is_in_directory(
91 tmp_path / "sub" / "file.txt", [tmp_path / "sub", tmp_path / "sub2"]
92 )
93 assert not file_is_in_directory(tmp_path / "file.txt", [tmp_path / "sub", tmp_path / "sub2"])
94 assert not file_is_in_directory(
95 tmp_path / "sub" / "file.txt", [tmp_path / "sub2", tmp_path / "sub3"]
96 )
99def test_path_relative_to():
100 this_file = Path(__file__)
101 this_dir = this_file.parent
102 parent = this_dir.parent
104 assert path_relative_to(this_file, this_dir) == Path(this_file.name)
105 assert path_relative_to(this_file, parent) == Path(this_dir.name) / this_file.name
106 assert (
107 path_relative_to(this_file, parent / "whatever")
108 == Path("..") / this_dir.name / this_file.name
109 )
112def test_read_last_lines_of_file_with_short_file(tmp_path):
113 # A file that is smaller than the buffer size
114 data = "a\nb\nc"
115 file = create_file(tmp_path / "data.txt", contents=data)
116 assert read_last_lines_of_file(file, num_lines=10) == data
119def test_read_last_lines_of_file_with_long_file(tmp_path):
120 # A file that is larger than the buffer size
121 data_trash = (("a" * 700) + "\n") * 3000
122 data_last_lines = (("b" * 700) + "\n") * 10
123 file = create_file(tmp_path / "data.txt", contents=data_trash + data_last_lines)
124 assert read_last_lines_of_file(file, num_lines=10) == data_last_lines
127def test_read_last_lines_of_file_with_trailing_newlines(tmp_path):
128 # A file that is smaller than the buffer size
129 data = "a\nb\n\n \n\n"
130 file = create_file(tmp_path / "data.txt", contents=data)
131 assert read_last_lines_of_file(file, num_lines=10) == data
134def test_read_last_lines_of_file_with_empty_file(tmp_path):
135 data = ""
136 file = create_file(tmp_path / "data.txt", contents=data)
137 assert read_last_lines_of_file(file, num_lines=10) == data
139 data = "\n"
140 file = create_file(tmp_path / "data.txt", contents=data)
141 assert read_last_lines_of_file(file, num_lines=10) == data
144def test_run_command_called_with_nonexisting_binary_should_raise_exception():
145 cmd = ["/apa/hest/zebra.exe", "foobar"]
146 with pytest.raises(FileNotFoundError):
147 run_command(cmd)
150def test_run_command_with_non_zero_return_code_should_raise_exception():
151 cmd = ["ls", "/apa/hest/zebra"]
152 with pytest.raises(subprocess.CalledProcessError):
153 run_command(cmd)
156def test_run_command_called_with_non_list_should_raise_exception():
157 cmd = ["ls", "-la"]
158 run_command(cmd)
160 cmd = "ls -la"
161 with pytest.raises(TypeError) as exception_info:
162 run_command(cmd)
163 assert str(exception_info.value).startswith("Must be called with a list")
166def test_run_command_should_capture_output_as_strings():
167 this_dir = Path(__file__).parent.resolve()
169 cmd = ["ls", str(this_dir)]
170 result = run_command(cmd, capture_output=True)
172 assert isinstance(result.stdout, str)
173 assert isinstance(result.stderr, str)
175 assert result.stderr == ""
177 # Show that it is regular text with regular newlines.
178 assert "\ntest_system_utils.py\n" in result.stdout
179 assert "\ntest_ip_core_file.py\n" in result.stdout