Пример #1
0
def test_read_yaml(tmpdir):
    # Read a string
    parsed_string = util.read_yaml(TEST_YAML)
    fname = tmpdir / 'test_read_yaml.yml'

    util.write_yaml(parsed_string, fname)

    # Read file descriptor..
    with fname.open('r') as f:
        parsed_file = util.read_yaml(f)
    assert parsed_file == parsed_string

    # Read Path object
    parsed_path_obj = util.read_yaml(pathlib.Path(fname))
    assert parsed_string == parsed_path_obj
Пример #2
0
def test_write_yaml(tmpdir):
    fname = tmpdir / 'some_file.yml'

    util.write_yaml({'testing': 'foo'}, fname)
    assert fname.read_text('utf-8') == 'testing: foo\n'

    util.write_yaml({'testing': 'bar'}, fname, keep_backup=True)

    fnames = [name for name in tmpdir.listdir() if name.basename.startswith('some_file')]

    assert len(fnames) == 2

    fname2 = max(fnames, key=lambda x: len(x.basename))  # backup name is longer
    assert fname2.read_text('utf-8') == 'testing: foo\n'
    assert fname.read_text('utf-8') == 'testing: bar\n'