Skip to content

Commit

Permalink
Merge pull request #98 from nqminhuit/gis-push-no-interactive
Browse files Browse the repository at this point in the history
feat: add --no-interactive option to gis push
  • Loading branch information
nqminhuit authored Jul 10, 2024
2 parents de1156d + 4a1ba21 commit ff556b9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
11 changes: 6 additions & 5 deletions src/main/java/org/nqm/command/GitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,17 @@ void removeBranch(@Parameters(index = "0", paramLabel = "<branch name>") String

@Command(name = "push", aliases = "pus", description = "Update remotes refs along with associated objects")
void push(@Parameters(index = "0", paramLabel = "<branch name>") 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);
}
Expand Down
35 changes: 28 additions & 7 deletions src/test/java/org/nqm/command/GitCommandIntTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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())
Expand All @@ -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:
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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!!,");
}
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/nqm/command/GitCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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");
}
}

0 comments on commit ff556b9

Please sign in to comment.