def check_dirty_handling(dirname): filename = os.path.join(dirname, "foo.yaml") assert not os.path.exists(filename) yaml = YamlFile(filename) assert yaml.change_count == 1 yaml.set_value(["a", "b"], 42) yaml.save() assert yaml.change_count == 2 assert os.path.exists(filename) time1 = os.path.getmtime(filename) yaml.save() assert time1 == os.path.getmtime(filename) assert yaml.change_count == 2 yaml.save() assert time1 == os.path.getmtime(filename) assert yaml.change_count == 2 yaml.set_value(["a", "b"], 43) assert time1 == os.path.getmtime(filename) assert yaml.change_count == 2 yaml.save() # OS mtime resolution might leave these equal assert time1 <= os.path.getmtime(filename) assert yaml.change_count == 3
def check_corrupted(filename): yaml = YamlFile(filename) assert yaml.corrupted assert "mapping values are not allowed here" in yaml.corrupted_error_message # it should raise an exception if you try to modify with pytest.raises(ValueError) as excinfo: yaml.set_value(["foo", "bar"], 42) assert "Cannot modify corrupted" in repr(excinfo.value) with pytest.raises(ValueError) as excinfo: yaml.save() assert "Cannot modify corrupted" in repr(excinfo.value) with pytest.raises(ValueError) as excinfo: def make_changes(yaml): return False yaml.transform_yaml(make_changes) assert "Cannot modify corrupted" in repr(excinfo.value) # the file should appear empty if you try to get anything, # but it shouldn't throw assert yaml._yaml is not None assert yaml.get_value(["a", "b"]) is None
def check_throw_if_cannot_create(dirname): subdir = "bar" filename = os.path.join(dirname, subdir, "foo.yaml") yaml = YamlFile(filename) yaml.set_value(["a", "b"], 42) with pytest.raises(IOError) as excinfo: yaml.save() assert "this is not EEXIST" in repr(excinfo.value)
def check_dirty_handling(dirname): filename = os.path.join(dirname, "foo.yaml") assert not os.path.exists(filename) yaml = YamlFile(filename) yaml.set_value(["a", "b"], 42) yaml.save() assert os.path.exists(filename) time1 = os.path.getmtime(filename) yaml2 = YamlFile(filename) assert time1 == os.path.getmtime(filename) assert yaml2.change_count == 1 yaml2.save() assert time1 == os.path.getmtime(filename) assert yaml2.change_count == 1
def check(filename): yaml = YamlFile(filename) assert not yaml.corrupted assert yaml.corrupted_error_message is None assert yaml.change_count == 1 value = yaml.get_value("a", None) assert value is None yaml.set_value("a", '') value = yaml.get_value("a", None) assert value == '' # only-whitespace string yaml.set_value("a", ' ') value = yaml.get_value("a", None) assert value == ' '
def change_abc(filename): yaml = YamlFile(filename) assert yaml.change_count == 1 value = yaml.get_value(["a", "b"]) assert original_value == value yaml.set_value(["a", "b"], changed_value) yaml.save() import codecs with codecs.open(filename, 'r', 'utf-8') as file: changed = file.read() assert changed_content == changed yaml2 = YamlFile(filename) assert yaml2.change_count == 1 value2 = yaml2.get_value(["a", "b"]) assert changed_value == value2
def add_section(filename): yaml = YamlFile(filename) value = yaml.get_value(["a", "b"]) assert "c" == value yaml.set_value(["x", "y"], dict(z=42, q="rs")) assert yaml.change_count == 1 yaml.save() assert yaml.change_count == 2 yaml2 = YamlFile(filename) value2 = yaml2.get_value(["a", "b"]) assert "c" == value2 added_value = yaml2.get_value(["x", "y", "z"]) assert 42 == added_value added_value_2 = yaml2.get_value(["x", "y", "q"]) assert "rs" == added_value_2 print(open(filename, 'r').read())
def set_abc(dirname): filename = os.path.join(dirname, "foo.yaml") assert os.path.exists(filename) yaml = YamlFile(filename) value = yaml.get_value(["a", "b"]) assert value is None yaml.set_value(["a", "b"], 42) yaml.save() assert os.path.exists(filename) import codecs with codecs.open(filename, 'r', 'utf-8') as file: changed = file.read() expected = """ a: b: 42 """[1:] assert expected == changed yaml2 = YamlFile(filename) value2 = yaml2.get_value(["a", "b"]) assert 42 == value2