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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
from __future__ import annotations
import os
import platform
import shutil
import pytest
from monty.tempfile import ScratchDir
TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_files")
class TestScratchDir:
def setup_method(self):
self.cwd = os.getcwd()
os.chdir(TEST_DIR)
self.scratch_root = os.path.join(TEST_DIR, "..", "..", "tempscratch")
os.mkdir(self.scratch_root)
def test_with_copy(self):
# We write a pre-scratch file.
with open("pre_scratch_text", "w", encoding="utf-8") as f:
f.write("write")
with ScratchDir(
self.scratch_root,
copy_from_current_on_enter=True,
copy_to_current_on_exit=True,
) as d:
with open("scratch_text", "w", encoding="utf-8") as f:
f.write("write")
files = os.listdir(d)
assert "scratch_text" in files
assert "empty_file.txt" in files
assert "pre_scratch_text" in files
# We remove the pre-scratch file.
os.remove("pre_scratch_text")
# Make sure the tempdir is deleted.
assert not os.path.exists(d)
files = os.listdir(".")
assert "scratch_text" in files
# We check that the pre-scratch file no longer exists (because it is
# deleted in the scratch)
assert "pre_scratch_text" not in files
os.remove("scratch_text")
def test_with_copy_gzip(self):
# We write a pre-scratch file.
with open("pre_scratch_text", "w", encoding="utf-8") as f:
f.write("write")
init_gz_files = [f for f in os.listdir(os.getcwd()) if f.endswith(".gz")]
with pytest.warns(match="Both 3000_lines.txt and 3000_lines.txt.gz exist."):
with (
ScratchDir(
self.scratch_root,
copy_from_current_on_enter=True,
copy_to_current_on_exit=True,
gzip_on_exit=True,
),
open("scratch_text", "w", encoding="utf-8") as f,
):
f.write("write")
files = os.listdir(os.getcwd())
# Make sure the scratch_text.gz exists
assert "scratch_text.gz" in files
for f in files:
if f.endswith(".gz") and f not in init_gz_files:
os.remove(f)
os.remove("pre_scratch_text")
def test_with_copy_nodelete(self):
# We write a pre-scratch file.
with open("pre_scratch_text", "w", encoding="utf-8") as f:
f.write("write")
with ScratchDir(
self.scratch_root,
copy_from_current_on_enter=True,
copy_to_current_on_exit=True,
delete_removed_files=False,
) as d:
with open("scratch_text", "w", encoding="utf-8") as f:
f.write("write")
files = os.listdir(d)
assert "scratch_text" in files
assert "empty_file.txt" in files
assert "pre_scratch_text" in files
# We remove the pre-scratch file.
os.remove("pre_scratch_text")
# Make sure the tempdir is deleted.
assert not os.path.exists(d)
files = os.listdir(".")
assert "scratch_text" in files
# We check that the pre-scratch file DOES still exists
assert "pre_scratch_text" in files
os.remove("scratch_text")
os.remove("pre_scratch_text")
def test_no_copy(self):
with ScratchDir(
self.scratch_root,
copy_from_current_on_enter=False,
copy_to_current_on_exit=False,
) as d:
with open("scratch_text", "w", encoding="utf-8") as f:
f.write("write")
files = os.listdir(d)
assert "scratch_text" in files
assert "empty_file.txt" not in files
# Make sure the tempdir is deleted.
assert not os.path.exists(d)
files = os.listdir(".")
assert "scratch_text" not in files
def test_symlink(self):
if platform.system() != "Windows":
with ScratchDir(
self.scratch_root,
copy_from_current_on_enter=False,
copy_to_current_on_exit=False,
create_symbolic_link=True,
) as d:
with open("scratch_text", "w", encoding="utf-8") as f:
f.write("write")
files = os.listdir(d)
assert "scratch_text" in files
assert "empty_file.txt" not in files
# Make sure the tempdir is deleted.
assert not os.path.exists(d)
files = os.listdir(".")
assert "scratch_text" not in files
# Make sure the symlink is removed
assert not os.path.islink("scratch_link")
def test_bad_root(self):
with ScratchDir("bad_groot") as d:
assert d == TEST_DIR
def teardown_method(self):
os.chdir(self.cwd)
shutil.rmtree(self.scratch_root)
|