-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos_test.py
More file actions
53 lines (39 loc) · 1.69 KB
/
os_test.py
File metadata and controls
53 lines (39 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Operating System Interface and File Wildcards."""
import glob
import os
import shutil
from pathlib import Path
from typing import Generator, List
def test_environ() -> None:
"""Setting and getting environment variables."""
os.environ["TEST"] = "tester"
assert os.getenv("TEST") == "tester"
def test_copy_delete_file(tmp_path: Path) -> None:
"""Copying and deleting a file."""
sample_file = Path(__file__).parent.joinpath("sample.txt")
sample_file_tmp = tmp_path.joinpath("sample.txt")
assert not os.path.exists(sample_file_tmp)
shutil.copyfile(sample_file, sample_file_tmp)
assert os.path.isfile(sample_file_tmp)
# pathlib.Path equivalent
assert sample_file_tmp.is_file()
os.remove(sample_file_tmp)
assert not os.path.exists(sample_file_tmp)
# pathlib.Path equivalent
assert not sample_file_tmp.exists()
def test_glob(tmp_path: Path) -> None:
"""Listing files using glob wildcards."""
sample_original = Path(__file__).parent.joinpath("sample.txt")
tmp_sample1 = tmp_path.joinpath("sample1.txt")
shutil.copyfile(sample_original, tmp_sample1)
tmp_sample2 = tmp_path.joinpath("sample2.txt")
shutil.copyfile(sample_original, tmp_sample2)
tmp_sub_dir = tmp_path.joinpath("subdir")
tmp_sub_dir.mkdir()
tmp_sample3 = tmp_sub_dir.joinpath("sample3.txt")
shutil.copyfile(sample_original, tmp_sample3)
files: List[str] = glob.glob(str(tmp_path.joinpath("**/*.txt")), recursive=True)
assert files == [str(tmp_sample1), str(tmp_sample2), str(tmp_sample3)]
# pathlib.Path equivalent
file_paths: Generator[Path, None, None] = tmp_path.glob("**/*.txt")
assert [str(path) for path in file_paths] == files