Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo 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