From 4a1ba21271ee534b1904b40373eea3ced3d4232e Mon Sep 17 00:00:00 2001 From: minh Date: Wed, 10 Jul 2024 15:35:30 +0700 Subject: [PATCH] feat: add --no-interactive option to gis push fix: #10 --- src/main/java/org/nqm/command/GitCommand.java | 11 +++--- .../org/nqm/command/GitCommandIntTest.java | 35 +++++++++++++++---- .../java/org/nqm/command/GitCommandTest.java | 10 +++--- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/nqm/command/GitCommand.java b/src/main/java/org/nqm/command/GitCommand.java index f0ec669..24c54b1 100644 --- a/src/main/java/org/nqm/command/GitCommand.java +++ b/src/main/java/org/nqm/command/GitCommand.java @@ -147,16 +147,17 @@ void removeBranch(@Parameters(index = "0", paramLabel = "") String @Command(name = "push", aliases = "pus", description = "Update remotes refs along with associated objects") void push(@Parameters(index = "0", paramLabel = "") String branch, - @Option(names = "-f", description = "force to update remote origin branch") boolean isForce, - @Option(names = "-r", description = "push to remote origin branch") boolean isNewRemoteBranch) + @Option(names = "-f", description = "force to update remote origin branch") boolean force, + @Option(names = "-r", description = "push to remote origin branch") boolean newRemoteBranch, + @Option(names = "--no-interactive", description = "do not prompt for user input") boolean noInteractive) throws IOException { - if (!isConfirmed("Sure you want to push to remote '%s' [Y/n]".formatted(branch))) { + if (!noInteractive && !isConfirmed("Sure you want to push to remote '%s' [Y/n]".formatted(branch))) { return; } - var args = isNewRemoteBranch + var args = newRemoteBranch ? new String[] {"push", "-u", ORIGIN, branch} - : shouldForcePush(isForce); + : shouldForcePush(force); forEachModuleWith(path -> isSameBranchUnderPath(branch, path), args); } diff --git a/src/test/java/org/nqm/command/GitCommandIntTest.java b/src/test/java/org/nqm/command/GitCommandIntTest.java index e895b75..4829452 100644 --- a/src/test/java/org/nqm/command/GitCommandIntTest.java +++ b/src/test/java/org/nqm/command/GitCommandIntTest.java @@ -150,12 +150,12 @@ void listBranchesWithRemote_withoutModuleNames_OK() throws IOException { gis.spinOff("bb1"); commitFile(repos); System.setIn(new ByteArrayInputStream("y".getBytes())); - gis.push("bb1", true, true); + gis.push("bb1", true, true, false); gis.spinOff("bb2"); commitFile(repos); System.setIn(new ByteArrayInputStream("yeS".getBytes())); - gis.push("bb2", true, true); + gis.push("bb2", true, true, false); resetOutputStreamTest(); // when: @@ -277,7 +277,7 @@ void pushOrigin_OK() throws IOException { // when: System.setIn(new ByteArrayInputStream("YES".getBytes())); - gis.push("master", true, true); + gis.push("master", true, true, false); // then: var out = Optional.of(outCaptor.toString()) @@ -292,6 +292,27 @@ void pushOrigin_OK() throws IOException { " branch 'master' set up to track 'origin/master'."); } + @Test + void pushOrigin_withoutUserPrompt_OK() throws IOException { + // given: + var repos = create_clone_gitRepositories("hori_1_h", "hori_2_hh", "hori_3_hhh"); + commitFile(repos); + gis.init(); + resetOutputStreamTest(); + + // when: + gis.push("master", true, true, true); + + // then: + assertThat(stripColors.apply(outCaptor.toString())).contains( + "hori_1_h", + " branch 'master' set up to track 'origin/master'.", + "hori_2_hh", + " branch 'master' set up to track 'origin/master'.", + "hori_3_hhh", + " branch 'master' set up to track 'origin/master'."); + } + @Test void pushOrigin_withoutSettingRemote_OK() throws IOException { // given: @@ -302,7 +323,7 @@ void pushOrigin_withoutSettingRemote_OK() throws IOException { // when: System.setIn(new ByteArrayInputStream("Yes".getBytes())); - gis.push("master", true, false); + gis.push("master", true, false, false); // then: var out = Optional.of(outCaptor.toString()) @@ -322,7 +343,7 @@ void pushOrigin_withSpecifiedRepos_OK() throws IOException { // when: System.setIn(new ByteArrayInputStream("y".getBytes())); - gis.push("batabranch", false, false); + gis.push("batabranch", false, false, false); // then: var out = Optional.of(outCaptor.toString()) @@ -347,7 +368,7 @@ void pushOrigin_withIOException_NOK() throws IOException { // when + then: System.setIn(new ByteArrayInputStream("Y".getBytes())); - assertThatThrownBy(() -> gis.push("batabranch", false, false)) + assertThatThrownBy(() -> gis.push("batabranch", false, false, false)) .isInstanceOf(GisException.class) .hasMessage("nope!!,"); } @@ -383,7 +404,7 @@ void fetchOrigin_OK() throws IOException { gis.spinOff("batabranch"); commitFile(repos); System.setIn(new ByteArrayInputStream("yes".getBytes())); - gis.push("batabranch", true, true); + gis.push("batabranch", true, true, false); gis.spinOff("master"); commitFile(repos); diff --git a/src/test/java/org/nqm/command/GitCommandTest.java b/src/test/java/org/nqm/command/GitCommandTest.java index 772ce5a..bd57783 100644 --- a/src/test/java/org/nqm/command/GitCommandTest.java +++ b/src/test/java/org/nqm/command/GitCommandTest.java @@ -351,7 +351,7 @@ void pushOrigin_withWrongAnswerToPromp_NOK() throws IOException { System.setIn(new ByteArrayInputStream("yesn't".getBytes())); // when: - gis.push("batabranch", true, true); + gis.push("batabranch", true, true, false); // then: verify(exe, times(0)).submit((Callable) any()); @@ -372,7 +372,7 @@ void pushOrigin_OK() throws IOException { System.setIn(new ByteArrayInputStream("yes".getBytes())); // when: - gis.push("master", true, true); + gis.push("master", true, true, false); // then: verify(exe, times(2)).submit((Callable) any()); @@ -429,7 +429,7 @@ void generateCompletionToFile_withFileAlreadyExist_shouldOverwrite() throws IOEx } @Test - public void confirmYesPattern_false() throws Exception { + void confirmYesPattern_false() throws Exception { // given: var pattern = GitCommand.CONFIRM_YES; @@ -445,7 +445,7 @@ public void confirmYesPattern_false() throws Exception { } @Test - public void confirmYesPattern_true() throws Exception { + void confirmYesPattern_true() throws Exception { // given: var pattern = GitCommand.CONFIRM_YES; @@ -463,7 +463,7 @@ public void confirmYesPattern_true() throws Exception { } @Test - public void gisAutocompleteFileName() throws Exception { + void gisAutocompleteFileName() throws Exception { assertThat(GIS_AUTOCOMPLETE_FILE).isEqualTo("_gis"); } }