|
| 1 | +package org.archicontribs.modelrepository.grafico; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.nio.file.StandardCopyOption; |
| 13 | +import java.util.logging.Logger; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +public class GitExecutorTest { |
| 18 | + |
| 19 | + private static final Logger LOGGER = Logger.getLogger(GitExecutorTest.class.getName()); |
| 20 | + |
| 21 | + private GitExecutor underTest; |
| 22 | + |
| 23 | + public GitExecutorTest() throws GitExecutionException { |
| 24 | + underTest = new GitExecutor(TestData.GIT_PATH, TestData.GIT_REPO); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void canFindGitOnPath() throws IOException, InterruptedException { |
| 29 | + Process process = Runtime.getRuntime().exec(new String[] { "which", "git" }); |
| 30 | + assertEquals(0, process.waitFor()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void canGitVersion() throws GitExecutionException { |
| 35 | + GitExecutionResult res = underTest.version(); |
| 36 | + LOGGER.finest(res.outputLine()); |
| 37 | + assertEquals(0, res.exitCode()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void canGitReset() throws GitExecutionException { |
| 42 | + GitExecutionResult res = underTest.reset(true, TestData.GIT_HISTORICAL_COMMIT_ID); |
| 43 | + LOGGER.finest(res.outputLine()); |
| 44 | + assertEquals(0, res.exitCode()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void canGitClean() throws GitExecutionException, IOException { |
| 49 | + File f = new File(TestData.GIT_FOLDER, "cleanMe"); |
| 50 | + assertTrue(f.createNewFile()); |
| 51 | + assertTrue(f.exists()); |
| 52 | + |
| 53 | + GitExecutionResult res = underTest.clean(); |
| 54 | + LOGGER.finest(res.outputLine()); |
| 55 | + assertEquals(0, res.exitCode()); |
| 56 | + |
| 57 | + assertFalse(f.exists()); |
| 58 | + } |
| 59 | + |
| 60 | + private void resetTestScenario() { |
| 61 | + try { |
| 62 | + canGitReset(); |
| 63 | + canGitClean(); |
| 64 | + } catch (GitExecutionException e) { |
| 65 | + throw new RuntimeException(e); |
| 66 | + } catch (IOException e) { |
| 67 | + throw new RuntimeException(e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void canGitFetch() throws GitExecutionException { |
| 73 | + GitExecutionResult res = underTest.fetch(); |
| 74 | + LOGGER.finest(res.outputLine()); |
| 75 | + assertEquals(0, res.exitCode()); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + @Test |
| 80 | + public void canGitCommit() throws GitExecutionException { |
| 81 | + resetTestScenario(); // arrange |
| 82 | + |
| 83 | + GitExecutionResult res = underTest.commit("empty commit", false, true, false); |
| 84 | + LOGGER.finest(res.outputLine()); |
| 85 | + assertEquals(0, res.exitCode()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void canGitPull() throws GitExecutionException { |
| 90 | + resetTestScenario(); // arrange |
| 91 | + |
| 92 | + GitExecutionResult res = underTest.pull(); // FF_ONLY |
| 93 | + LOGGER.finest(res.outputLine()); |
| 94 | + assertEquals(0, res.exitCode()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void canGitPullDetectConflict() throws GitExecutionException { |
| 99 | + resetTestScenario(); // arrange |
| 100 | + |
| 101 | + GitExecutionResult resCommit = underTest.commit("empty commit", false, true, false); |
| 102 | + LOGGER.finest(resCommit.outputLine()); |
| 103 | + |
| 104 | + GitExecutionResult res = underTest.pull(); |
| 105 | + LOGGER.finest(res.outputLine()); |
| 106 | + assertEquals(128, res.exitCode()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void canGitPullRebase() throws GitExecutionException { |
| 111 | + resetTestScenario(); // arrange |
| 112 | + |
| 113 | + GitExecutionResult resCommit = underTest.commit("empty commit", false, true, false); |
| 114 | + LOGGER.finest(resCommit.outputLine()); |
| 115 | + |
| 116 | + GitExecutionResult res = underTest.pull(GitExecutor.PullMode.REBASE_MERGE); |
| 117 | + LOGGER.finest(res.outputLine()); |
| 118 | + assertEquals(0, res.exitCode()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void canGitRebaseAbort() throws GitExecutionException, IOException { |
| 123 | + resetTestScenario(); // arrange |
| 124 | + |
| 125 | + File targetF = Paths.get(TestData.GIT_FOLDER.getPath(), TestData.GIT_FILE.getName()).toFile(); |
| 126 | + Files.move(TestData.GIT_FILE.toPath(), targetF.toPath(), |
| 127 | + StandardCopyOption.REPLACE_EXISTING); |
| 128 | + |
| 129 | + GitExecutionResult resAdd = underTest.add(TestData.GIT_FILE); |
| 130 | + assertEquals(0, resAdd.exitCode()); |
| 131 | + |
| 132 | + GitExecutionResult resCommit = underTest.commit("file deleted accidentaly oopsy"); |
| 133 | + LOGGER.finest(resCommit.outputLine()); |
| 134 | + |
| 135 | + GitExecutionResult res = underTest.rebase(TestData.GIT_HISTORICAL_ONTO_COMMIT_ID); |
| 136 | + LOGGER.finest(res.outputLine()); |
| 137 | + assertEquals(1, res.exitCode()); |
| 138 | + |
| 139 | + GitExecutionResult abortResult = underTest.rebaseAbort(); |
| 140 | + assertEquals(0, abortResult.exitCode()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void canDetectUncommittedFile() throws GitExecutionException, IOException { |
| 145 | + resetTestScenario(); // arrange |
| 146 | + |
| 147 | + assertFalse(underTest.hasChanges()); |
| 148 | + |
| 149 | + Files.move(TestData.GIT_FILE.toPath(), Paths.get(TestData.GIT_FOLDER.getPath(), TestData.GIT_FILE.getName()), |
| 150 | + StandardCopyOption.REPLACE_EXISTING); |
| 151 | + |
| 152 | + assertTrue(underTest.hasChanges()); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void canStageAllFiles() throws GitExecutionException, IOException { |
| 157 | + resetTestScenario(); // arrange |
| 158 | + |
| 159 | + Files.move(TestData.GIT_FILE.toPath(), Paths.get(TestData.GIT_FOLDER.getPath(), TestData.GIT_FILE.getName()), |
| 160 | + StandardCopyOption.REPLACE_EXISTING); |
| 161 | + |
| 162 | + assertEquals(0, underTest.addAll().exitCode()); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void canStageFile() throws GitExecutionException, IOException { |
| 167 | + resetTestScenario(); // arrange |
| 168 | + |
| 169 | + File targetF = Paths.get(TestData.GIT_FOLDER.getPath(), TestData.GIT_FILE.getName()).toFile(); |
| 170 | + assertTrue(targetF.createNewFile()); |
| 171 | + |
| 172 | + assertEquals(0, underTest.add(targetF).exitCode()); |
| 173 | + |
| 174 | + File nonExistentF = Paths.get(TestData.GIT_FOLDER.getPath(), "nonExistentFile").toFile(); |
| 175 | + assertFalse(nonExistentF.exists()); |
| 176 | + |
| 177 | + assertNotEquals(0, underTest.add(nonExistentF).exitCode()); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void canRemoteSshCredentials() { |
| 182 | + // TODO |
| 183 | + } |
| 184 | +} |
0 commit comments