def test_get_operations_remove_line(): text_file1 = TextFile("filename", ["123"]) text_file2 = TextFile("filename", []) operations = text_file1.get_operations(text_file2) assert len(operations) == 1 assert isinstance(operations[0], TextOpRemoveLine) assert operations[0].file_name == "filename" assert operations[0].line_number == 0
def test_get_operations_complex_changes(): text_file1 = TextFile("filename", ["a", "b", "c", "d", "e"]) text_file2 = TextFile("filename", ["a", "k", "b", "e"]) operations = text_file1.get_operations(text_file2) assert len(operations) == 3 assert isinstance(operations[0], TextOpRemoveLine) assert operations[0].file_name == "filename" assert operations[0].line_number == 2 assert isinstance(operations[1], TextOpRemoveLine) assert operations[1].file_name == "filename" assert operations[1].line_number == 2 assert isinstance(operations[2], TextOpInsertLine) assert operations[2].file_name == "filename" assert operations[2].line_number == 1 assert operations[2].line_contents == "k"
def test_get_operations_same(): text_file1 = TextFile("filename", [""]) text_file2 = TextFile("filename", [""]) operations = text_file1.get_operations(text_file2) assert len(operations) == 0 text_file1 = TextFile("filename", ["hi", "i", "am", "nate"]) text_file2 = TextFile("filename", ["hi", "i", "am", "nate"]) operations = text_file1.get_operations(text_file2) assert len(operations) == 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
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
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"
def diff(vcs_path, file_name): # if the file didn't exist curr_state = get_curr_state(vcs_path, include_working_commit=False) if file_name not in curr_state: if not os.path.exists(file_name): print("Error: no diff as file does not/didn't exist") else: if file_name.endswith(".txt"): file_obj = TextFile.from_file(file_name) else: file_obj = BinaryFile.from_file(file_name) print("File diff: {}".format(file_obj.file_name)) print("+" + file_obj.file_contents) else: if not os.path.exists(file_name): file_obj = curr_state.files[file_name] print("File diff: {}".format(file_name)) print("Deleted file") else: if file_name.endswith(".txt"): file_obj = TextFile.from_file(file_name) else: file_obj = BinaryFile.from_file(file_name) curr_state.files[file_name].print_changes(file_obj)
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)
def add_file(cwd, file_name, curr_state): # if the file existed before if file_name in curr_state: if not os.path.exists(file_name): # we are either deleting it add_op_to_curr_commit(FileOpRemove(file_name)) else: # or we are changing it prev_file = curr_state.files[file_name] file_class = type(prev_file) new_file = file_class.from_file(file_name) change_operations = prev_file.get_operations(new_file) # write them to current commit for operation in change_operations: add_op_to_curr_commit(operation) else: # otherwise, it didn't exist before, so we are adding it if file_name.endswith(".txt"): file_obj = TextFile.from_file(file_name) else: file_obj = BinaryFile.from_file(file_name) add_op_to_curr_commit(FileOpAdd(file_name, file_obj))
def test_to_from_file(): text_file = TextFile(os.getcwd() + "/temp/text", ["hi", "yo"]) text_file.to_file(os.getcwd() + "/temp/text") text_file1 = TextFile.from_file(os.getcwd() + "/temp/text") assert text_file1.file_name == os.getcwd() + "/temp/text" assert text_file1.file_contents == ["hi", "yo"]
def test_to_from_string(): text_file = TextFile("text", ["this", "is", "a", "file"]) text_file_string = text_file.to_string() text_file1 = TextFile.from_string(text_file_string) assert text_file.file_name == text_file1.file_name assert text_file.file_contents == text_file1.file_contents