Exemple #1
0
 def check_bad_path(filename):
     yaml = YamlFile(filename)
     assert not yaml.corrupted
     with pytest.raises(ValueError) as excinfo:
         yaml.get_value(42)
     assert "YAML file path must be a string or an iterable of strings" in repr(
         excinfo.value)
Exemple #2
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)
Exemple #3
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
Exemple #4
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
Exemple #5
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
Exemple #6
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
Exemple #7
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
Exemple #8
0
    def check_abc(filename):
        yaml = YamlFile(filename)
        assert not yaml.corrupted
        assert yaml.corrupted_error_message is None
        assert yaml.change_count == 1
        # try getting with a list of keys
        value = yaml.get_value(["a", "b"])
        assert "c" == value
        # get a single string as the path
        value = yaml.get_value("a")
        assert dict(b="c") == value
        # get with a tuple to show we aren't list-specific
        value = yaml.get_value(("a", "b"))
        assert "c" == value

        assert yaml.root == dict(a=dict(b='c'))
Exemple #9
0
    def check_abc(filename):
        yaml = YamlFile(filename)
        assert not yaml.corrupted
        assert yaml.corrupted_error_message is None
        assert yaml.change_count == 1
        # try getting with a list of keys
        value = yaml.get_value(["a", "b"])
        assert "c" == value
        # get a single string as the path
        value = yaml.get_value("a")
        assert dict(b="c") == value
        # get with a tuple to show we aren't list-specific
        value = yaml.get_value(("a", "b"))
        assert "c" == value

        assert yaml.root == dict(a=dict(b='c'))
Exemple #10
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
Exemple #11
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
Exemple #12
0
 def check_read_directory(dirname):
     filename = os.path.join(dirname, "dir.yaml")
     os.makedirs(filename)
     with pytest.raises(IOError) as excinfo:
         YamlFile(filename)
     import platform
     if platform.system() == 'Windows':
         assert errno.EACCES == excinfo.value.errno
     else:
         assert errno.EISDIR == excinfo.value.errno
Exemple #13
0
    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 == ' '
Exemple #14
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())
Exemple #15
0
    def check_roundtrip(filename):
        yaml = YamlFile(filename)
        yaml._dirty = True
        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
Exemple #16
0
    def check_roundtrip(filename):
        yaml = YamlFile(filename)
        yaml._dirty = True
        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
Exemple #17
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
Exemple #18
0
    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 == ' '
Exemple #19
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)
Exemple #20
0
    def set_abc(dirname):
        filename = os.path.join(dirname, "foo.yaml")
        assert not 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 = """
# yaml file
a:
  b: 42
"""[1:]

            assert expected == changed

        yaml2 = YamlFile(filename)
        value2 = yaml2.get_value(["a", "b"])
        assert 42 == value2
Exemple #21
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
Exemple #22
0
    def unset_values(filename):
        yaml = YamlFile(filename)
        assert yaml.change_count == 1
        a_b = yaml.get_value(["a", "b"])
        assert 1 == a_b
        x_y = yaml.get_value(["x", "y"])
        assert 2 == x_y
        x_z = yaml.get_value(["x", "z"])
        assert 3 == x_z
        q = yaml.get_value("q")
        assert 4 == q

        def assert_unset_on_reload(path):
            yaml2 = YamlFile(filename)
            assert yaml2.change_count == 1
            value2 = yaml2.get_value(path, None)
            assert value2 is None

        scope = dict(last_change=yaml.change_count)

        def check_unset(path):
            assert yaml.change_count == scope['last_change']
            assert not yaml._dirty
            yaml.unset_value(path)
            assert yaml.get_value(path, None) is None
            assert yaml._dirty
            yaml.save()
            assert yaml.change_count == (scope['last_change'] + 1)
            scope['last_change'] += 1
            assert_unset_on_reload(path)

        check_unset(["a", "b"])
        check_unset(["x", "y"])
        check_unset(["x", "z"])
        check_unset("q")

        assert not yaml._dirty
        yaml.unset_value("not_in_there")
        assert not yaml._dirty
Exemple #23
0
    def set_abc(dirname):
        filename = os.path.join(dirname, "foo.yaml")
        assert not 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 = """
# yaml file
a:
  b: 42
""" [1:]

            assert expected == changed

        yaml2 = YamlFile(filename)
        value2 = yaml2.get_value(["a", "b"])
        assert 42 == value2
Exemple #24
0
    def unset_values(filename):
        yaml = YamlFile(filename)
        assert yaml.change_count == 1
        a_b = yaml.get_value(["a", "b"])
        assert 1 == a_b
        x_y = yaml.get_value(["x", "y"])
        assert 2 == x_y
        x_z = yaml.get_value(["x", "z"])
        assert 3 == x_z
        q = yaml.get_value("q")
        assert 4 == q

        def assert_unset_on_reload(path):
            yaml2 = YamlFile(filename)
            assert yaml2.change_count == 1
            value2 = yaml2.get_value(path, None)
            assert value2 is None

        scope = dict(last_change=yaml.change_count)

        def check_unset(path):
            assert yaml.change_count == scope['last_change']
            assert not yaml._dirty
            yaml.unset_value(path)
            assert yaml.get_value(path, None) is None
            assert yaml._dirty
            yaml.save()
            assert yaml.change_count == (scope['last_change'] + 1)
            scope['last_change'] += 1
            assert_unset_on_reload(path)

        check_unset(["a", "b"])
        check_unset(["x", "y"])
        check_unset(["x", "z"])
        check_unset("q")

        assert not yaml._dirty
        yaml.unset_value("not_in_there")
        assert not yaml._dirty
Exemple #25
0
 def check_a(filename):
     yaml = YamlFile(filename)
     value = yaml.get_value(["a", "b"], "default")
     assert "default" == value
Exemple #26
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())
Exemple #27
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
Exemple #28
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
Exemple #29
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
Exemple #30
0
 def check_missing(dirname):
     yaml = YamlFile(os.path.join(dirname, "nope.yaml"))
     value = yaml.get_value(["z", "b"], "default")
     assert "default" == value
Exemple #31
0
 def assert_unset_on_reload(path):
     yaml2 = YamlFile(filename)
     assert yaml2.change_count == 1
     value2 = yaml2.get_value(path, None)
     assert value2 is None
Exemple #32
0
 def assert_unset_on_reload(path):
     yaml2 = YamlFile(filename)
     assert yaml2.change_count == 1
     value2 = yaml2.get_value(path, None)
     assert value2 is None
Exemple #33
0
 def get_list_value(filename):
     yaml = YamlFile(filename)
     value = yaml.get_value("a")
     assert [1, 2, 3] == value
Exemple #34
0
 def get_list_value(filename):
     yaml = YamlFile(filename)
     value = yaml.get_value("a")
     assert [1, 2, 3] == value
Exemple #35
0
 def check_a(filename):
     yaml = YamlFile(filename)
     value = yaml.get_value(["a", "b"], "default")
     assert "default" == value
Exemple #36
0
 def check_bad_path(filename):
     yaml = YamlFile(filename)
     assert not yaml.corrupted
     with pytest.raises(ValueError) as excinfo:
         yaml.get_value(42)
     assert "YAML file path must be a string or an iterable of strings" in repr(excinfo.value)
Exemple #37
0
 def check_missing(dirname):
     yaml = YamlFile(os.path.join(dirname, "nope.yaml"))
     value = yaml.get_value(["z", "b"], "default")
     assert "default" == value