|
1
|
|
|
from pathlib import Path |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import pytest |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
from pocketutils.core.exceptions import ParsingError |
|
6
|
|
|
from pocketutils.tools.filesys_tools import FilesysTools |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def load(parts): |
|
|
|
|
|
|
10
|
|
|
if isinstance(parts, str): |
|
11
|
|
|
parts = [parts] |
|
12
|
|
|
return Path(Path(__file__).parent.parent.parent / "resources" / "core", *parts) |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class TestFilesysTools: |
|
|
|
|
|
|
16
|
|
|
def test_read_lines(self): |
|
|
|
|
|
|
17
|
|
|
assert list(FilesysTools.read_lines_file(load("lines.lines"))) == [ |
|
18
|
|
|
"line1 = 5", |
|
19
|
|
|
"line2=5", |
|
20
|
|
|
"", |
|
21
|
|
|
"#line3", |
|
22
|
|
|
"line4 = a", |
|
23
|
|
|
] |
|
24
|
|
|
assert list(FilesysTools.read_lines_file(load("lines.lines"), ignore_comments=True)) == [ |
|
25
|
|
|
"line1 = 5", |
|
26
|
|
|
"line2=5", |
|
27
|
|
|
"line4 = a", |
|
28
|
|
|
] |
|
29
|
|
|
|
|
30
|
|
|
def test_read_properties(self): |
|
|
|
|
|
|
31
|
|
|
f = FilesysTools.read_properties_file |
|
|
|
|
|
|
32
|
|
|
expected = {"line1": "5", "line2": "5", "line4": "a"} |
|
33
|
|
|
assert dict(f(load("lines.lines"))) == expected |
|
34
|
|
|
with pytest.raises(ParsingError): |
|
35
|
|
|
f(load("bad1.properties")) |
|
36
|
|
|
with pytest.raises(ParsingError): |
|
37
|
|
|
f(load("bad2.properties")) |
|
38
|
|
|
|
|
39
|
|
|
def test_get_info(self): |
|
|
|
|
|
|
40
|
|
|
file = load("lines.lines") |
|
41
|
|
|
info = FilesysTools.get_info(file) |
|
42
|
|
|
assert info.is_file |
|
43
|
|
|
assert info.mod_or_create_dt is not None |
|
44
|
|
|
info = FilesysTools.get_info(".") |
|
45
|
|
|
assert info.is_dir |
|
46
|
|
|
assert not info.is_file |
|
47
|
|
|
assert info.mod_or_create_dt is not None |
|
48
|
|
|
|
|
49
|
|
|
def test_get_env_info(self): |
|
|
|
|
|
|
50
|
|
|
data = FilesysTools.get_env_info(include_insecure=True) |
|
51
|
|
|
assert len(data) > 20 |
|
52
|
|
|
assert "pid" in data |
|
53
|
|
|
assert "disk_used" in data |
|
54
|
|
|
assert "hostname" in data |
|
55
|
|
|
assert "locale" in data |
|
56
|
|
|
|
|
57
|
|
|
def test_list_imports(self): |
|
|
|
|
|
|
58
|
|
|
data = FilesysTools.list_package_versions() |
|
59
|
|
|
assert len(data) > 5 |
|
60
|
|
|
assert "orjson" in data |
|
61
|
|
|
assert data["orjson"].startswith("3.") # change when updated |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
if __name__ == "__main__": |
|
65
|
|
|
pytest.main() |
|
66
|
|
|
|