Ejemplo n.º 1
0
def test_objectrewriter_in_to_out_no_out_str(temp_file_creator):
    """Object Rewriter in place edit with str object."""
    representer = ArbRepresenter()
    rewriter = filesystem.ObjectRewriter(get_arb_formatted, representer)

    path_in = temp_file_creator()
    path_in.write_text('yyy')

    with patch_logger('pypyr.utils.filesystem',
                      logging.DEBUG) as mock_logger_debug:
        rewriter.in_to_out(str(path_in), None)

    assert representer.load_payload == 'yyy'
    assert representer.load_call_count == 1
    assert representer.dump_payload == 'XyyyX'
    assert representer.dump_call_count == 1

    assert mock_logger_debug.mock_calls == [
        call(f"opening source file: {path_in}"),
        call("opening temp file for writing..."),
        call(f"moving temp file to: {path_in}")
    ]

    assert path_in.is_file()
    assert path_in.read_text() == 'XyyyX'
Ejemplo n.º 2
0
def test_objectrewriter_in_to_out_same_path(temp_file_creator):
    """Object Rewriter in place edit with path object."""
    representer = ArbRepresenter()
    rewriter = filesystem.ObjectRewriter(get_arb_formatted, representer)

    path_in = temp_file_creator()
    path_in.write_text('yyy')

    str_out = os.path.relpath(path_in)

    with patch_logger('pypyr.utils.filesystem',
                      logging.DEBUG) as mock_logger_debug:
        rewriter.in_to_out(path_in, str_out)

    assert representer.load_payload == 'yyy'
    assert representer.load_call_count == 1
    assert representer.dump_payload == 'XyyyX'
    assert representer.dump_call_count == 1

    assert mock_logger_debug.mock_calls == [
        call("in path and out path are the same file. writing to temp "
             "file and then replacing in path with the temp file."),
        call(f"opening source file: {path_in}"),
        call("opening temp file for writing..."),
        call(f"moving temp file to: {path_in}")
    ]

    assert path_in.is_file()
    assert path_in.read_text() == 'XyyyX'
Ejemplo n.º 3
0
def test_objectrewriter_in_to_out_path(temp_dir, temp_file_creator):
    """Object Rewriter writes single in to out path object."""
    representer = ArbRepresenter()
    rewriter = filesystem.ObjectRewriter(get_arb_formatted, representer)

    path_in = temp_file_creator()
    path_in.write_text('yyy')
    path_out = temp_dir.joinpath('sub', 'filename')

    with patch_logger('pypyr.utils.filesystem',
                      logging.DEBUG) as mock_logger_debug:
        rewriter.in_to_out(path_in, path_out)

    assert representer.load_payload == 'yyy'
    assert representer.load_call_count == 1
    assert representer.dump_payload == 'XyyyX'
    assert representer.dump_call_count == 1

    assert mock_logger_debug.mock_calls == [
        call(f"opening source file: {path_in}"),
        call(f"opening destination file for writing: {path_out}")
    ]

    assert path_in.is_file()
    assert path_in.read_text() == 'yyy'
    assert path_out.is_file()
    assert path_out.read_text() == 'XyyyX'