Exemplo n.º 1
0
def test_remove():
    branch = Branch()
    insert_op = FileOpInsert("filename", "DATA")
    patch = Patch([insert_op])
    branch.insert_patch(patch)

    remove_op = FileOpRemove("filename")
    patch = Patch([remove_op])
    branch.insert_patch(patch)
Exemplo n.º 2
0
def test_change_contents():
    branch = Branch()
    binary_file = BinaryFile("filename", "")
    insertOp = FileOpInsert("filename", binary_file)
    patch = Patch([insertOp])
    branch.insert_patch(patch)

    changeOp = BinaryOpChangeContents("filename", "new_data")
    patch = Patch([changeOp])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert branch.states[-1].files["filename"].file_contents == "new_data"
Exemplo n.º 3
0
def test_remove_line():
    branch = Branch()
    text_file = TextFile("filename", ["new_line"])
    insertOp = FileOpInsert("filename", text_file)
    patch = Patch([insertOp])
    branch.insert_patch(patch)

    appendOp = TextOpRemoveLine("filename", 0)
    patch = Patch([appendOp])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert len(branch.states[-1].files["filename"].file_contents) == 0
Exemplo n.º 4
0
def test_insert_line():
    branch = Branch()
    text_file = TextFile("filename", ["", ""])
    insertOp = FileOpInsert("filename", text_file)
    patch = Patch([insertOp])
    branch.insert_patch(patch)

    appendOp = TextOpInsertLine("filename", 1, "new_line")
    patch = Patch([appendOp])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files["filename"].file_contents) == 3
    assert branch.states[-1].files["filename"].file_contents[1] == "new_line"
Exemplo n.º 5
0
def test_insert_twice_fails():
    branch = Branch()
    insert_op1 = FileOpInsert("filename", "DATA")
    insert_op2 = FileOpInsert("filename", "DATA1")
    patch = Patch([insert_op1, insert_op2])
    with pytest.raises(Exception):
        branch.insert_patch(patch)
Exemplo n.º 6
0
def test_patch_to_from_string():
    text_file = TextFile("filename", [""])
    insertOp = FileOpInsert("filename", text_file)
    remove_op = FileOpRemove("filename")
    patch = Patch([insertOp, remove_op])

    patch_string = patch.to_string()
    patch1 = Patch.from_string(patch_string)

    assert len(patch.operations) == len(patch1.operations)
    assert patch.operations[0].file_name == patch1.operations[0].file_name
    assert patch.operations[0].file.file_contents == patch1.operations[
        0].file.file_contents
    assert patch.operations[1].file_name == patch1.operations[0].file_name
    assert isinstance(patch1.operations[0], FileOpInsert)
    assert isinstance(patch1.operations[1], FileOpRemove)
Exemplo n.º 7
0
def test_insert():
    branch = Branch()
    insert_op = FileOpInsert("filename", "DATA")
    patch = Patch(set([insert_op]))
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert branch.states[-1].files["filename"] == "DATA"
Exemplo n.º 8
0
def test_insert():
    branch = Branch()
    binary_file = BinaryFile("filename", "")
    insertOp = FileOpInsert("filename", binary_file)
    patch = Patch([insertOp])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert branch.states[-1].files["filename"] == binary_file
Exemplo n.º 9
0
def test_create():
    branch = Branch()
    json_file = JSONFile("filename", dict())
    insert_op = FileOpInsert("filename", json_file)
    patch = Patch([insert_op])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert branch.states[-1].files["filename"] == json_file
Exemplo n.º 10
0
def test_insert():
    branch = Branch()
    text_file = TextFile("filename", [""])
    insertOp = FileOpInsert("filename", text_file)
    patch = Patch([insertOp])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert branch.states[-1].files["filename"] == text_file
Exemplo n.º 11
0
def test_list_remove():
    branch = Branch()
    json_file = JSONFile("filename", [1])
    insert_op = FileOpInsert("filename", json_file)
    op = JSONOpListRemove("filename", ["0"])
    patch = Patch([insert_op, op])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert len(branch.states[-1].files["filename"].file_contents) == 0
Exemplo n.º 12
0
def test_remove_dict():
    branch = Branch()
    json_file = JSONFile("filename", {'key': 123})
    insert_op = FileOpInsert("filename", json_file)
    del_dict_op = JSONOpDictRemove("filename", ["key"])
    patch = Patch([insert_op, del_dict_op])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert len(branch.states[-1].files["filename"].file_contents) == 0
Exemplo n.º 13
0
def test_primitive_change():
    branch = Branch()
    json_file = JSONFile("filename", {'key': 123})
    insert_op = FileOpInsert("filename", json_file)
    op = JSONOpPrimitiveChange("filename", ["key"], 124)
    patch = Patch([insert_op, op])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert len(branch.states[-1].files["filename"].file_contents) == 1
    assert branch.states[-1].files["filename"].file_contents['key'] == 124
Exemplo n.º 14
0
def test_insert_dict():
    branch = Branch()
    json_file = JSONFile("filename", {})
    insert_op = FileOpInsert("filename", json_file)
    insert_dict_op = JSONOpDictInsert("filename", ["key"], 123)
    patch = Patch([insert_op, insert_dict_op])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert len(branch.states[-1].files["filename"].file_contents) == 1
    assert branch.states[-1].files["filename"].file_contents['key'] == 123
Exemplo n.º 15
0
def get_curr_state(vcs_path, include_working_commit=True):
    branch = Branch()

    for patch_file_name in os.listdir(vcs_path + "/branch"):
        f = open(vcs_path + "/branch/" + patch_file_name, "r")
        patch_file_text = f.read().strip()
        f.close()
        patch = Patch.from_string(patch_file_text)
        branch.add_patch(patch)
        
    if include_working_commit:
        f = open(vcs_path + "/curr_commit", "r")
        patch_file_text = f.read().strip()
        f.close()
        if patch_file_text == "":
            return branch.curr_state()
        patch = Patch.from_string(patch_file_text)
        branch.add_patch(patch)

    return branch.curr_state()
Exemplo n.º 16
0
def test_list_insert():
    branch = Branch()
    json_file = JSONFile("filename", [1])
    insert_op = FileOpInsert("filename", json_file)
    op = JSONOpListInsert("filename", ["1"], 2)
    patch = Patch([insert_op, op])
    branch.insert_patch(patch)

    assert len(branch.states[-1].files) == 1
    assert len(branch.states[-1].files["filename"].file_contents) == 2
    assert branch.states[-1].files["filename"].file_contents[0] == 1
    assert branch.states[-1].files["filename"].file_contents[1] == 2
Exemplo n.º 17
0
def test_remove_not_exist_fails():
    branch = Branch()
    remove_op = FileOpRemove("filename")
    patch = Patch([remove_op])
    with pytest.raises(Exception):
        branch.insert_patch(patch)