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_edit_equal(tmpdir, version): asdf_base, yaml_base, asdf_edit, yaml_edit = _initialize_test( tmpdir, version, _create_base_asdf) _create_edited_yaml(yaml_base, yaml_edit, b"foo: 42", b"foo: 41") args = ["edit", "-s", "-f", f"{yaml_edit}", "-o", f"{asdf_edit}"] main.main_from_args(args) assert os.path.getsize(asdf_edit) == os.path.getsize(asdf_base) with asdf.open(asdf_edit) as af: assert af.tree["foo"] == 41
def test_edit_larger_stream(tmpdir, version): asdf_base, yaml_base, asdf_edit, yaml_edit = _initialize_test( tmpdir, version, _create_base_asdf_stream) _create_edited_yaml(yaml_base, yaml_edit, b"foo: 42", b"foo: 42\nbar: 13") args = ["edit", "-s", "-f", f"{yaml_edit}", "-o", f"{asdf_edit}"] main.main_from_args(args) assert os.path.getsize(asdf_edit) - os.path.getsize(asdf_base) > 10000 with asdf.open(asdf_edit) as af: assert "bar" in af.tree assert af.tree["bar"] == 13
def _initialize_test(tmpdir, version, create_asdf): asdf_base = os.path.join(tmpdir, "base.asdf") yaml_base = os.path.join(tmpdir, "base.yaml") asdf_edit = os.path.join(tmpdir, "edit.asdf") yaml_edit = os.path.join(tmpdir, "edit.yaml") create_asdf(version, asdf_base) create_asdf(version, asdf_edit) args = ["edit", "-e", "-f", f"{asdf_base}", "-o", f"{yaml_base}"] main.main_from_args(args) return asdf_base, yaml_base, asdf_edit, yaml_edit
def _test_defragment(tmpdir, codec): x = np.arange(0, 1000, dtype=np.float) tree = { 'science_data': x, 'subset': x[3:-3], 'skipping': x[::2], 'not_shared': np.arange(100, 0, -1, dtype=np.uint8) } path = os.path.join(str(tmpdir), 'original.asdf') out_path = os.path.join(str(tmpdir), 'original.defragment.asdf') ff = AsdfFile(tree) ff.write_to(path) assert len(ff.blocks) == 2 result = main.main_from_args( ['defragment', path, '-o', out_path, '-c', codec]) assert result == 0 files = get_file_sizes(str(tmpdir)) assert 'original.asdf' in files assert 'original.defragment.asdf' in files assert files['original.defragment.asdf'] < files['original.asdf'] with asdf.open(os.path.join(str(tmpdir), 'original.defragment.asdf')) as ff: assert_tree_match(ff.tree, tree) assert len(list(ff.blocks.internal_blocks)) == 2
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
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_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_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_explode_then_implode(tmpdir): x = np.arange(0, 10, dtype=np.float) tree = { 'science_data': x, 'subset': x[3:-3], 'skipping': x[::2], 'not_shared': np.arange(10, 0, -1, dtype=np.uint8) } path = os.path.join(str(tmpdir), 'original.asdf') ff = AsdfFile(tree) # Since we're testing with small arrays, force all arrays to be stored # in internal blocks rather than letting some of them be automatically put # inline. ff.write_to(path, all_array_storage='internal') assert len(ff.blocks) == 2 result = main.main_from_args(['explode', path]) assert result == 0 files = get_file_sizes(str(tmpdir)) assert 'original.asdf' in files assert 'original_exploded.asdf' in files assert 'original_exploded0000.asdf' in files assert 'original_exploded0001.asdf' in files assert 'original_exploded0002.asdf' not in files assert files['original.asdf'] > files['original_exploded.asdf'] path = os.path.join(str(tmpdir), 'original_exploded.asdf') result = main.main_from_args(['implode', path]) assert result == 0 with asdf.open(str(tmpdir.join('original_exploded_all.asdf'))) as af: assert_tree_match(af.tree, tree) assert len(af.blocks) == 2
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_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_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_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_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_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_diff_command(): filenames = ['frames0.asdf', 'frames1.asdf'] paths = [get_test_data_path(name) for name in filenames] assert main.main_from_args(['diff'] + paths) == 0
def test_file_not_found(tmpdir): path = os.path.join(str(tmpdir), 'original.asdf') assert main.main_from_args(['explode', path]) == 2