diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Util.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Util.java index d04e3a83a9..0e9962ad03 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Util.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Util.java @@ -152,12 +152,14 @@ public class Util { } } - public static String fromStream(InputStream in) throws IOException - { - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + public static String fromStream(InputStream stream, int skip) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); StringBuilder out = new StringBuilder(); String newLine = System.getProperty("line.separator"); String line; + for (int i = 0; i < skip; i++) { + reader.readLine(); + } while ((line = reader.readLine()) != null) { out.append(line); out.append(newLine); @@ -165,4 +167,8 @@ public class Util { return out.toString(); } + public static String fromStream(InputStream stream) throws IOException { + return fromStream(stream, 0); + } + } diff --git a/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest.java b/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest.java index 274e1f7b01..27969d4ab2 100644 --- a/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest.java +++ b/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest.java @@ -65,6 +65,9 @@ public class WLGitBridgeIntegrationTest { put("canPushFilesSuccessfully", new HashMap() {{ put("state", new SnapshotAPIStateBuilder(getResourceAsStream("/canPushFilesSuccessfully/state/state.json")).build()); }}); + put("pushFailsOnFirstStageOutOfDate", new HashMap() {{ + put("state", new SnapshotAPIStateBuilder(getResourceAsStream("/pushFailsOnFirstStageOutOfDate/state/state.json")).build()); + }}); }}; @Rule @@ -295,6 +298,42 @@ public class WLGitBridgeIntegrationTest { assertEquals(0, pushExitCode); } + + private static final String EXPECTED_OUT_PUSH_FAIL_FIRST_STAGE = + "To http://127.0.0.1:33867/testproj.git\n" + + " ! [rejected] master -> master (non-fast-forward)\n" + + "error: failed to push some refs to 'http://127.0.0.1:33867/testproj.git'\n" + + "hint: Updates were rejected because the tip of your current branch is behind\n" + + "hint: its remote counterpart. Integrate the remote changes (e.g.\n" + + "hint: 'git pull ...') before pushing again.\n" + + "hint: See the 'Note about fast-forwards' in 'git push --help' for details.\n"; + + @Test + public void pushFailsOnFirstStageOutOfDate() throws IOException, GitAPIException, InterruptedException { + MockSnapshotServer server = new MockSnapshotServer(3867, getResource("/pushFailsOnFirstStageOutOfDate").toFile()); + server.start(); + server.setState(states.get("pushFailsOnFirstStageOutOfDate").get("state")); + GitBridgeApp wlgb = new GitBridgeApp(new String[] { + makeConfigFile(33867, 3867) + }); + wlgb.run(); + File dir = folder.newFolder(); + Process git = runtime.exec("git clone http://127.0.0.1:33867/testproj.git", null, dir); + int exitCode = git.waitFor(); + File testprojDir = new File(dir, "testproj"); + assertEquals(0, exitCode); + assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/pushFailsOnFirstStageOutOfDate/state/testproj"), testprojDir.toPath())); + runtime.exec("touch push.tex", null, testprojDir).waitFor(); + runtime.exec("git add -A", null, testprojDir).waitFor(); + runtime.exec("git commit -m \"push\"", null, testprojDir).waitFor(); + Process gitPush = runtime.exec("git push", null, testprojDir); + + int pushExitCode = gitPush.waitFor(); + wlgb.stop(); + assertEquals(1, pushExitCode); + assertEquals(EXPECTED_OUT_PUSH_FAIL_FIRST_STAGE, Util.fromStream(gitPush.getErrorStream(), 2)); + } + private String makeConfigFile(int port, int apiPort) throws IOException { File wlgb = folder.newFolder(); File config = folder.newFile(); diff --git a/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/state.json b/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/state.json new file mode 100644 index 0000000000..6ae1457978 --- /dev/null +++ b/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/state.json @@ -0,0 +1,46 @@ +[ + { + "project": "testproj", + "getDoc": { + "versionID": 1, + "createdAt": "2014-11-30T18:40:58.123Z", + "email": "jdleesmiller+1@gmail.com", + "name": "John+1" + }, + "getSavedVers": [ + { + "versionID": 1, + "comment": "added more info on doc GET and error details", + "email": "jdleesmiller+1@gmail.com", + "name": "John+1", + "createdAt": "2014-11-30T18:47:01.333Z" + } + ], + "getForVers": [ + { + "versionID": 1, + "srcs": [ + { + "content": "content\n", + "path": "main.tex" + }, + { + "content": "This text is from another file.", + "path": "foo/bar/test.tex" + } + ], + "atts": [ + { + "url": "http://127.0.0.1:3867/state/testproj/min_mean_wait_evm_7_eps_150dpi.png", + "path": "min_mean_wait_evm_7_eps_150dpi.png" + } + ] + } + ], + "push": "outOfDate", + "postback": { + "type": "success", + "versionID": 2 + } + } +] \ No newline at end of file diff --git a/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/testproj/foo/bar/test.tex b/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/testproj/foo/bar/test.tex new file mode 100644 index 0000000000..046794f19a --- /dev/null +++ b/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/testproj/foo/bar/test.tex @@ -0,0 +1 @@ +This text is from another file. \ No newline at end of file diff --git a/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/testproj/main.tex b/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/testproj/main.tex new file mode 100644 index 0000000000..d95f3ad14d --- /dev/null +++ b/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/testproj/main.tex @@ -0,0 +1 @@ +content diff --git a/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/testproj/min_mean_wait_evm_7_eps_150dpi.png b/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/testproj/min_mean_wait_evm_7_eps_150dpi.png new file mode 100644 index 0000000000..74e1fcd990 Binary files /dev/null and b/services/git-bridge/src/test/resources/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest/pushFailsOnFirstStageOutOfDate/state/testproj/min_mean_wait_evm_7_eps_150dpi.png differ