CodebaseMerger { void generateMergedFile( Codebase original, Codebase modified, Codebase destination, MergeResult.Builder resultBuilder, String filename) { File origFile = original.getFile(filename); boolean origExists = filesystem.exists(origFile); File modFile = modified.getFile(filename); boolean modExists = filesystem.exists(modFile); File destFile = destination.getFile(filename); boolean destExists = filesystem.exists(destFile); if (!destExists && !modExists) { throw new MoeProblem( "%s doesn't exist in either %s nor %s. This should not be possible.", filename, destination, modified); } else if (origExists && modExists && !destExists) { if (areDifferent(filename, origFile, modFile)) { destFile = new File("/dev/null"); } else { return; } } else if (origExists && !modExists && destExists) { return; } else if (!origExists && !(modExists && destExists)) { File existingFile = (modExists ? modFile : destFile); copyToMergedCodebase(resultBuilder.mergedCodebase(), filename, existingFile); return; } else if (!origExists && modExists && destExists) { origFile = new File("/dev/null"); } File mergedFile = copyToMergedCodebase(resultBuilder.mergedCodebase(), filename, destFile); try { cmd.runCommand( resultBuilder.mergedCodebase().path().getAbsolutePath(), "merge", ImmutableList.of( mergedFile.getAbsolutePath(), origFile.getAbsolutePath(), modFile.getAbsolutePath())); resultBuilder.mergedFilesBuilder().add(mergedFile.getAbsolutePath()); } catch (CommandException e) { if (e.returnStatus == 1) { resultBuilder.failedFilesBuilder().add(mergedFile.getAbsolutePath()); } else { throw new MoeProblem( e, "Merge returned with unexpected status %d when trying to run \"merge -p %s %s %s\"", e.returnStatus, destFile.getAbsolutePath(), origFile.getAbsolutePath(), modFile.getAbsolutePath()); } } } @Inject  CodebaseMerger(Ui ui, FileSystem filesystem, CommandRunner cmd, FileDiffer differ); MergeResult merge(Codebase original, Codebase modified, Codebase destination);  }
@Test public void testGenerateMergedFileDestDelete() throws Exception { when(fileSystem.exists(origFile)).thenReturn(true); when(fileSystem.exists(destFile)).thenReturn(false); when(fileSystem.exists(modFile)).thenReturn(true); CodebaseMerger merger = new CodebaseMerger(ui, fileSystem, cmd, fileDiffer); merger.generateMergedFile(orig, mod, dest, resultBuilder, "foo"); verify(cmd) .runCommand( "", "diff", ImmutableList.of("-N", "-u", origFile.getAbsolutePath(), modFile.getAbsolutePath())); }
@Test public void testGenerateMergedFileModDeleteConflict() throws Exception { FileDiffer differ = mock(FileDiffer.class); FileDifference difference = mock(FileDifference.class); when(differ.diffFiles(anyString(), any(), any())).thenReturn(difference); when(difference.isDifferent()).thenReturn(true); resultBuilder.setMergedCodebase(merged); when(fileSystem.exists(origFile)).thenReturn(true); when(fileSystem.exists(destFile)).thenReturn(true); when(fileSystem.exists(modFile)).thenReturn(false); when(cmd.runCommand(anyString(), eq("merge"), Mockito.anyListOf(String.class))) .thenThrow(new CommandException("merge", ImmutableList.of(), "", "", 1)); CodebaseMerger merger = new CodebaseMerger(ui, fileSystem, cmd, differ); merger.generateMergedFile(orig, mod, dest, resultBuilder, "foo"); assertThat(resultBuilder.failedFilesBuilder().build()).isEmpty(); assertThat(resultBuilder.mergedFilesBuilder().build()).isEmpty(); }
@Test public void testGenerateMergedFileAddFile() throws Exception { File mergedFile = new File("merged_codebase_7/foo"); FileDiffer differ = mock(FileDiffer.class); FileDifference difference = mock(FileDifference.class); when(differ.diffFiles(anyString(), any(), any())).thenReturn(difference); when(difference.isDifferent()).thenReturn(true); when(fileSystem.exists(origFile)).thenReturn(false); when(fileSystem.exists(destFile)).thenReturn(false); when(fileSystem.exists(modFile)).thenReturn(true); CodebaseMerger merger = new CodebaseMerger(ui, fileSystem, cmd, differ); merger.generateMergedFile(orig, mod, dest, resultBuilder, "foo"); assertThat(resultBuilder.mergedFilesBuilder().build()).isEmpty(); assertThat(resultBuilder.failedFilesBuilder().build()).isEmpty(); verify(fileSystem).makeDirsForFile(mergedFile); verify(fileSystem).copyFile(modFile, mergedFile); }
@Test public void testGenerateMergedFileClean() throws Exception { File mergedFile = new File("merged_codebase_7/foo"); when(fileSystem.exists(origFile)).thenReturn(true); when(fileSystem.exists(destFile)).thenReturn(true); when(fileSystem.exists(modFile)).thenReturn(true); List<String> mergeArgs = ImmutableList.of( mergedFile.getAbsolutePath(), origFile.getAbsolutePath(), modFile.getAbsolutePath()); when(cmd.runCommand(mergedCodebaseLocation.getAbsolutePath(), "merge", mergeArgs)) .thenReturn(""); CodebaseMerger merger = new CodebaseMerger(ui, fileSystem, cmd, null); merger.generateMergedFile(orig, mod, dest, resultBuilder, "foo"); assertThat(resultBuilder.failedFilesBuilder().build()).isEmpty(); assertThat(resultBuilder.mergedFilesBuilder().build()) .containsExactly(mergedFile.getAbsolutePath()); verify(fileSystem).makeDirsForFile(mergedFile); verify(fileSystem).copyFile(destFile, mergedFile); verify(cmd).runCommand(mergedCodebaseLocation.getAbsolutePath(), "merge", mergeArgs); }
@Test public void testGenerateMergedFileConflict() throws Exception { File mergedFile = new File("merged_codebase_7/foo"); when(fileSystem.exists(origFile)).thenReturn(true); when(fileSystem.exists(destFile)).thenReturn(true); when(fileSystem.exists(modFile)).thenReturn(true); List<String> mergeArgs = ImmutableList.of( mergedFile.getAbsolutePath(), origFile.getAbsolutePath(), modFile.getAbsolutePath()); when(cmd.runCommand(mergedCodebaseLocation.getAbsolutePath(), "merge", mergeArgs)) .thenThrow(new CommandRunner.CommandException("merge", mergeArgs, "", "", 1)); CodebaseMerger merger = new CodebaseMerger(ui, fileSystem, cmd, null); merger.generateMergedFile(orig, mod, dest, resultBuilder, "foo"); assertThat(resultBuilder.mergedFilesBuilder().build()).isEmpty(); assertThat(resultBuilder.failedFilesBuilder().build()).contains(mergedFile.getAbsolutePath()); verify(fileSystem).makeDirsForFile(mergedFile); verify(fileSystem).copyFile(destFile, mergedFile); verify(cmd).runCommand(mergedCodebaseLocation.getAbsolutePath(), "merge", mergeArgs); }
@Test public void testGenerateMergedFileDestOnly() throws Exception { when(fileSystem.exists(origFile)).thenReturn(false); when(fileSystem.exists(destFile)).thenReturn(true); when(fileSystem.exists(modFile)).thenReturn(false); CodebaseMerger merger = new CodebaseMerger(ui, fileSystem, cmd, null); merger.generateMergedFile(orig, mod, dest, resultBuilder, "foo"); assertThat(resultBuilder.failedFilesBuilder().build()).isEmpty(); assertThat(resultBuilder.mergedFilesBuilder().build()).isEmpty(); }
@Test public void testGenerateMergedFileNoOrig() throws Exception { File mergedFile = new File("merged_codebase_7/foo"); when(fileSystem.exists(origFile)).thenReturn(false); when(fileSystem.exists(destFile)).thenReturn(true); when(fileSystem.exists(modFile)).thenReturn(true); List<String> mergeArgs = ImmutableList.of(mergedFile.getAbsolutePath(), "/dev/null", modFile.getAbsolutePath()); when(cmd.runCommand(mergedCodebaseLocation.getAbsolutePath(), "merge", mergeArgs)) .thenReturn(""); CodebaseMerger merger = new CodebaseMerger(ui, fileSystem, cmd, null); merger.generateMergedFile(orig, mod, dest, resultBuilder, "foo"); assertThat(resultBuilder.failedFilesBuilder().build()).isEmpty(); assertThat(resultBuilder.mergedFilesBuilder().build()) .containsExactly(mergedFile.getAbsolutePath()); verify(fileSystem).makeDirsForFile(mergedFile); verify(fileSystem).copyFile(destFile, mergedFile); verify(cmd).runCommand(mergedCodebaseLocation.getAbsolutePath(), "merge", mergeArgs); }
@Test public void testGenerateMergedFileNoOrigConflict() throws Exception { File mergedFile = new File("merged_codebase_7/foo"); when(fileSystem.exists(origFile)).thenReturn(false); when(fileSystem.exists(destFile)).thenReturn(true); when(fileSystem.exists(modFile)).thenReturn(true); List<String> mergeArgs = ImmutableList.of( mergedFile.getAbsolutePath(), origFile.getAbsolutePath(), modFile.getAbsolutePath()); when(cmd.runCommand(anyString(), eq("merge"), Mockito.anyListOf(String.class))) .thenThrow(new CommandRunner.CommandException("merge", mergeArgs, "", "", 1)); CodebaseMerger merger = new CodebaseMerger(ui, fileSystem, cmd, null); merger.generateMergedFile(orig, mod, dest, resultBuilder, "foo"); verify(fileSystem).makeDirsForFile(mergedFile); verify(fileSystem).copyFile(destFile, mergedFile); verify(cmd).runCommand(anyString(), eq("merge"), Mockito.anyListOf(String.class)); assertThat(resultBuilder.mergedFilesBuilder().build()).isEmpty(); assertThat(resultBuilder.failedFilesBuilder().build()) .containsExactly(mergedFile.getAbsolutePath()); }