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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
import os
import re
from contextlib import contextmanager
import numpy as np
import pytest
from numpy.testing import assert_array_equal
import asdf
from asdf.commands import main
@pytest.fixture(params=asdf.versioning.supported_versions)
def version(request):
return request.param
@pytest.fixture
def create_editor(tmp_path):
"""
Fixture providing a function that generates an editor script.
"""
def _create_editor(pattern, replacement):
if isinstance(pattern, str):
pattern = pattern.encode("utf-8")
if isinstance(replacement, str):
replacement = replacement.encode("utf-8")
editor_path = tmp_path / "editor.py"
content = f"""import re
import sys
with open(sys.argv[1], "rb") as file:
content = file.read()
content = re.sub({pattern!r}, {replacement!r}, content, flags=(re.DOTALL | re.MULTILINE))
with open(sys.argv[1], "wb") as file:
file.write(content)
"""
with editor_path.open("w") as file:
file.write(content)
return f"python3 {editor_path}"
return _create_editor
@contextmanager
def file_not_modified(path):
"""
Assert that a file was not modified during the context.
"""
original_mtime = os.stat(path).st_mtime_ns
yield
assert os.stat(path).st_mtime_ns == original_mtime
@pytest.fixture
def mock_input(monkeypatch):
"""
Fixture providing a function that mocks the edit module's
built-in input function.
"""
@contextmanager
def _mock_input(pattern, response):
called = False
def _input(prompt=None):
nonlocal called
called = True
assert prompt is not None and re.match(pattern, prompt)
return response
with monkeypatch.context() as m:
m.setattr("builtins.input", _input)
yield
assert called, "input was not called as expected"
return _mock_input
@pytest.fixture(autouse=True)
def default_mock_input(monkeypatch):
"""
Fixture that raises an error when the program
requests unexpected input.
"""
def _input(prompt=None):
raise AssertionError(f"Received unexpected request for input: {prompt}")
monkeypatch.setattr("builtins.input", _input)
def test_no_blocks(tmp_path, create_editor, version):
file_path = str(tmp_path / "test.asdf")
with asdf.AsdfFile(version=version) as af:
af["foo"] = "bar"
af.write_to(file_path)
os.environ["EDITOR"] = create_editor(r"foo: bar", "foo: baz")
assert main.main_from_args(["edit", file_path]) == 0
with asdf.open(file_path) as af:
assert af["foo"] == "baz"
def test_no_blocks_increase_size(tmp_path, create_editor, version):
file_path = str(tmp_path / "test.asdf")
with asdf.AsdfFile(version=version) as af:
af["foo"] = "bar"
af.write_to(file_path)
new_value = "a" * 32768
os.environ["EDITOR"] = create_editor(r"foo: bar", f"foo: {new_value}")
# With no blocks, we can expand the existing file, so this case
# shouldn't require confirmation from the user.
assert main.main_from_args(["edit", file_path]) == 0
with asdf.open(file_path) as af:
assert af["foo"] == new_value
def test_no_blocks_decrease_size(tmp_path, create_editor, version):
file_path = str(tmp_path / "test.asdf")
original_value = "a" * 32768
with asdf.AsdfFile(version=version) as af:
af["foo"] = original_value
af.write_to(file_path)
os.environ["EDITOR"] = create_editor(f"foo: {original_value}", "foo: bar")
assert main.main_from_args(["edit", file_path]) == 0
with asdf.open(file_path) as af:
assert af["foo"] == "bar"
def test_with_blocks(tmp_path, create_editor, version):
file_path = str(tmp_path / "test.asdf")
array1 = np.random.rand(100)
array2 = np.random.rand(100)
with asdf.AsdfFile(version=version) as af:
af["array1"] = array1
af["array2"] = array2
af["foo"] = "bar"
af.write_to(file_path)
os.environ["EDITOR"] = create_editor(r"foo: bar", "foo: baz")
assert main.main_from_args(["edit", file_path]) == 0
with asdf.open(file_path) as af:
assert af["foo"] == "baz"
assert_array_equal(af["array1"], array1)
assert_array_equal(af["array2"], array2)
def test_with_blocks_increase_size(tmp_path, create_editor, version, mock_input):
file_path = str(tmp_path / "test.asdf")
array1 = np.random.rand(100)
array2 = np.random.rand(100)
with asdf.AsdfFile(version=version) as af:
af["array1"] = array1
af["array2"] = array2
af["foo"] = "bar"
af.write_to(file_path)
new_value = "a" * 32768
os.environ["EDITOR"] = create_editor(r"foo: bar", f"foo: {new_value}")
# Abort without updating the file
with mock_input(r"\(c\)ontinue or \(a\)bort\?", "a"):
with file_not_modified(file_path):
assert main.main_from_args(["edit", file_path]) == 1
# Agree to allow the file to be rewritten
with mock_input(r"\(c\)ontinue or \(a\)bort\?", "c"):
assert main.main_from_args(["edit", file_path]) == 0
with asdf.open(file_path) as af:
assert af["foo"] == new_value
assert_array_equal(af["array1"], array1)
assert_array_equal(af["array2"], array2)
def test_with_blocks_decrease_size(tmp_path, create_editor, version):
file_path = str(tmp_path / "test.asdf")
original_value = "a" * 32768
array1 = np.random.rand(100)
array2 = np.random.rand(100)
with asdf.AsdfFile(version=version) as af:
af["array1"] = array1
af["array2"] = array2
af["foo"] = original_value
af.write_to(file_path)
os.environ["EDITOR"] = create_editor(f"foo: {original_value}", "foo: bar")
assert main.main_from_args(["edit", file_path]) == 0
with asdf.open(file_path) as af:
assert af["foo"] == "bar"
assert_array_equal(af["array1"], array1)
assert_array_equal(af["array2"], array2)
def test_no_changes(tmp_path, create_editor, version):
file_path = str(tmp_path / "test.asdf")
with asdf.AsdfFile(version=version) as af:
af["foo"] = "bar"
af.write_to(file_path)
os.environ["EDITOR"] = create_editor(r"non-existent-string", "non-existent-string")
with file_not_modified(file_path):
assert main.main_from_args(["edit", file_path]) == 0
def test_update_asdf_standard_version(tmp_path, create_editor, version, mock_input):
file_path = str(tmp_path / "test.asdf")
with asdf.AsdfFile(version=version) as af:
af["foo"] = "bar"
af.write_to(file_path)
os.environ["EDITOR"] = create_editor(r"^#ASDF_STANDARD .*?$", "#ASDF_STANDARD 999.999.999")
with file_not_modified(file_path):
with mock_input(r"\(c\)ontinue editing or \(a\)bort\?", "a"):
assert main.main_from_args(["edit", file_path]) == 1
def test_update_yaml_version(tmp_path, create_editor, version, mock_input):
file_path = str(tmp_path / "test.asdf")
with asdf.AsdfFile(version=version) as af:
af["foo"] = "bar"
af.write_to(file_path)
os.environ["EDITOR"] = create_editor(r"^%YAML 1.1$", "%YAML 1.2")
with file_not_modified(file_path):
with mock_input(r"\(c\)ontinue editing or \(a\)bort\?", "a"):
assert main.main_from_args(["edit", file_path]) == 1
def test_bad_yaml(tmp_path, create_editor, version, mock_input):
file_path = str(tmp_path / "test.asdf")
with asdf.AsdfFile(version=version) as af:
af["foo"] = "bar"
af.write_to(file_path)
os.environ["EDITOR"] = create_editor(r"foo: bar", "foo: [")
with file_not_modified(file_path):
with mock_input(r"\(c\)ontinue editing or \(a\)bort\?", "a"):
assert main.main_from_args(["edit", file_path]) == 1
def test_validation_failure(tmp_path, create_editor, version, mock_input):
file_path = str(tmp_path / "test.asdf")
with asdf.AsdfFile(version=version) as af:
af["array"] = np.arange(100)
af.write_to(file_path)
os.environ["EDITOR"] = create_editor(r"byteorder: .*?$", "byteorder: med")
with file_not_modified(file_path):
with mock_input(r"\(c\)ontinue editing, \(f\)orce update, or \(a\)bort\?", "a"):
assert main.main_from_args(["edit", file_path]) == 1
with mock_input(r"\(c\)ontinue editing, \(f\)orce update, or \(a\)bort\?", "f"):
assert main.main_from_args(["edit", file_path]) == 0
with open(file_path, "rb") as f:
content = f.read()
assert b"byteorder: med" in content
def test_asdf_open_failure(tmp_path, create_editor, version, mock_input):
file_path = str(tmp_path / "test.asdf")
with asdf.AsdfFile(version=version) as af:
af["foo"] = "bar"
af.write_to(file_path)
os.environ["EDITOR"] = create_editor(r"^#ASDF .*?$", "#HJKL 1.0.0")
with file_not_modified(file_path):
with mock_input(r"\(c\)ontinue editing or \(a\)bort\?", "a"):
assert main.main_from_args(["edit", file_path]) == 1
def test_non_asdf_file(tmp_path):
file_path = str(tmp_path / "test.asdf")
with open(file_path, "w") as f:
f.write("Dear diary...")
with file_not_modified(file_path):
assert main.main_from_args(["edit", file_path]) == 1
|