Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    def transform_test(dirname):
        filename = os.path.join(dirname, "foo.yaml")
        assert not os.path.exists(filename)
        yaml = YamlFile(filename)
        assert yaml.change_count == 1
        # save so we aren't dirty due to nonexistent file
        yaml.save()
        assert yaml.change_count == 2
        assert os.path.exists(filename)

        def transformer(tree):
            # return True means don't make changes after all
            return True

        assert not yaml._dirty
        yaml.transform_yaml(transformer)
        assert not yaml._dirty
        yaml.save()
        assert yaml.change_count == 2

        import codecs
        with codecs.open(filename, 'r', 'utf-8') as file:
            changed = file.read()
            expected = """
# yaml file
{}
"""[1:]

            assert expected == changed
Ejemplo n.º 3
0
    def transform_test(dirname):
        filename = os.path.join(dirname, "foo.yaml")
        assert not os.path.exists(filename)
        yaml = YamlFile(filename)
        assert yaml.change_count == 1
        # save so we aren't dirty due to nonexistent file
        yaml.save()
        assert os.path.exists(filename)
        assert yaml.change_count == 2

        def transformer(tree):
            tree['foo'] = dict()
            tree['foo']['bar'] = 42

        assert not yaml._dirty
        yaml.transform_yaml(transformer)
        assert yaml._dirty
        yaml.save()
        assert yaml.change_count == 3

        import codecs
        with codecs.open(filename, 'r', 'utf-8') as file:
            changed = file.read()
            expected = """
# yaml file
foo:
  bar: 42
"""[1:]

            assert expected == changed

        yaml2 = YamlFile(filename)
        value2 = yaml2.get_value(["foo", "bar"])
        assert 42 == value2
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    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())
Ejemplo n.º 9
0
    def check_roundtrip(filename):
        yaml = YamlFile(filename)
        yaml._previous_content = "not the actual previous content"
        yaml.save()
        new_content = open(filename, 'r').read()
        print("the re-saved version of the file was:")
        print(new_content)
        assert original_content != new_content

        # We don't require that the YAML backend preserves every
        # formatting detail, but it can't reorder things or lose
        # comments because if it did users would be annoyed.
        # Minor whitespace changes are OK, though ideally we'd
        # avoid even those.
        def canonicalize(content):
            if content.startswith("\n"):
                content = content[1:]
            return content.replace(" ", "").replace("\n\n", "\n")

        original_canon = canonicalize(original_content)
        new_canon = canonicalize(new_content)
        assert original_canon == new_canon
Ejemplo n.º 10
0
    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