Skip to content

Commit

Permalink
Support for exec and sshExec with exit values in a range
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlauer committed Dec 18, 2024
1 parent 3257242 commit 2079664
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.fizzed.blaze.core;

import com.fizzed.blaze.util.CaptureOutput;
import com.fizzed.blaze.util.IntRange;
import com.fizzed.blaze.util.StreamableOutput;
import com.fizzed.blaze.util.Streamables;
import java.io.File;
Expand Down Expand Up @@ -58,8 +59,12 @@ default public T timeout(long timeout, TimeUnit units) {
default public T exitValue(Integer exitValue) {
return exitValues(new Integer[] { exitValue });
}


T exitValuesAny();

T exitValues(Integer... exitValues);

T exitValues(IntRange... exitValues);

/**
default public T captureOutput() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.fizzed.blaze.core;

import com.fizzed.blaze.util.IntRange;

import java.util.List;

/**
Expand All @@ -23,7 +25,7 @@
*/
public class UnexpectedExitValueException extends BlazeException {

final private List<Integer> expected;
final private List<IntRange> expected;
final private Integer actual;

/**
Expand All @@ -32,13 +34,13 @@ public class UnexpectedExitValueException extends BlazeException {
*
* @param msg the detail message.
*/
public UnexpectedExitValueException(String msg, List<Integer> expected, Integer actual) {
public UnexpectedExitValueException(String msg, List<IntRange> expected, Integer actual) {
super(msg + " (expected = " + expected + "; actual = " + actual + ")");
this.expected = expected;
this.actual = actual;
}

public List<Integer> getExpected() {
public List<IntRange> getExpected() {
return expected;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fizzed.blaze.internal;

import com.fizzed.blaze.util.IntRange;

import java.util.ArrayList;
import java.util.List;

public class IntRangeHelper {

static public boolean contains(List<IntRange> values, Integer value) {
for (IntRange range : values) {
if (range.matches(value)) {
return true;
}
}
return false;
}

static public Integer[] toExpandedArray(List<IntRange> values) {
// create an array of the entire int range
final List<Integer> _values = new ArrayList<>();
for (IntRange v : values) {
for (int i = v.getFrom(); i <= v.getTo(); i++) {
_values.add(i);
}
}
return _values.toArray(new Integer[0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@
import java.util.concurrent.TimeUnit;

import com.fizzed.blaze.core.UnexpectedExitValueException;
import com.fizzed.blaze.util.CommandLines;
import com.fizzed.blaze.util.ProcessReaper;
import com.fizzed.blaze.internal.IntRangeHelper;
import com.fizzed.blaze.util.*;
import org.zeroturnaround.exec.InvalidExitValueException;
import org.zeroturnaround.exec.ProcessExecutor;
import com.fizzed.blaze.system.Exec;
import com.fizzed.blaze.system.Which;
import com.fizzed.blaze.util.InputStreamPumper;
import com.fizzed.blaze.util.Streamables;

import java.io.InputStream;
import org.zeroturnaround.exec.ProcessResult;
import org.zeroturnaround.exec.StartedProcess;
Expand Down Expand Up @@ -94,7 +93,7 @@ protected Exec.Result doRun() throws BlazeException {
}

if (this.exitValues != null && !this.exitValues.isEmpty()) {
executor.exitValues(this.exitValues.toArray(new Integer[0]));
executor.exitValues(IntRangeHelper.toExpandedArray(this.exitValues));
}

if (this.timeoutMillis > 0) {
Expand Down
26 changes: 22 additions & 4 deletions blaze-core/src/main/java/com/fizzed/blaze/system/Exec.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.fizzed.blaze.util.IntRange.intRange;
import static java.util.Arrays.asList;

abstract public class Exec<T extends Exec> extends Action<Exec.Result<T>,Integer> implements PathsMixin<T>, ExecMixin<T>, VerbosityMixin<T> {
static public class Result<R extends Exec> extends com.fizzed.blaze.core.Result<R,Integer,Result<R>> {

Expand All @@ -46,7 +49,7 @@ public Result(R action, Integer value) {
protected StreamableOutput pipeOutput;
protected StreamableOutput pipeError;
protected boolean pipeErrorToOutput;
final protected List<Integer> exitValues;
final protected List<IntRange> exitValues;
protected long timeoutMillis = -1L;
protected boolean sudo;
protected boolean shell;
Expand All @@ -60,7 +63,7 @@ public Exec(Context context) {
this.pipeOutput = Streamables.standardOutput();
this.pipeError = Streamables.standardError();
this.exitValues = new ArrayList<>();
this.exitValues.add(0);
this.exitValues.add(intRange(0, 0));
this.sudo = false;
this.shell = false;
}
Expand Down Expand Up @@ -162,11 +165,26 @@ public T workingDir(String path) {
this.workingDirectory = Paths.get(path);
return (T)this;
}


@Override
public T exitValuesAny() {
this.exitValues.clear();
return (T)this;
}

@Override
public T exitValues(Integer... exitValues) {
this.exitValues.clear();
this.exitValues.addAll(Arrays.asList(exitValues));
for (Integer exitValue : exitValues) {
this.exitValues.add(intRange(exitValue, exitValue));
}
return (T)this;
}

@Override
public T exitValues(IntRange... exitValues) {
this.exitValues.clear();
this.exitValues.addAll(asList(exitValues));
return (T)this;
}

Expand Down
23 changes: 23 additions & 0 deletions blaze-core/src/main/java/com/fizzed/blaze/util/IntRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class IntRange {
public IntRange(Integer from, Integer to) {
this.from = from;
this.to = to;
if (from > to) {
throw new IllegalArgumentException("From must be less than to");
}
}

public Integer getFrom() {
Expand All @@ -22,6 +25,26 @@ public boolean matches(Integer value) {
return value >= from && value <= to;
}

@Override
public String toString() {
return from + "->" + to;
}

@Override
public final boolean equals(Object o) {
if (!(o instanceof IntRange)) return false;

IntRange intRange = (IntRange) o;
return from.equals(intRange.from) && to.equals(intRange.to);
}

@Override
public int hashCode() {
int result = from.hashCode();
result = 31 * result + to.hashCode();
return result;
}

static public IntRange intRange(Integer from, Integer to) {
return new IntRange(from, to);
}
Expand Down
10 changes: 2 additions & 8 deletions blaze-http/src/main/java/com/fizzed/blaze/http/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fizzed.blaze.core.Action;
import com.fizzed.blaze.core.BlazeException;
import com.fizzed.blaze.core.VerbosityMixin;
import com.fizzed.blaze.internal.IntRangeHelper;
import com.fizzed.blaze.util.*;
import okhttp3.*;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -225,14 +226,7 @@ public Response intercept(@NotNull Chain chain) throws IOException {

// was the status code what we expect?
if (!this.statusCodes.isEmpty()) {
boolean matched = false;
for (IntRange intRange : this.statusCodes) {
if (intRange.matches(response.code())) {
matched = true;
break;
}
}
if (!matched) {
if (!IntRangeHelper.contains(this.statusCodes, response.code())) {
throw new BlazeException("Unexpected http response code " + response.code());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.fizzed.blaze.ssh.impl;

import com.fizzed.blaze.internal.IntRangeHelper;
import com.fizzed.blaze.ssh.*;
import com.fizzed.blaze.Context;
import com.fizzed.blaze.core.UnexpectedExitValueException;
Expand Down Expand Up @@ -190,7 +191,7 @@ public void close() throws IOException {

Integer exitValue = channel.getExitStatus();

if (!this.exitValues.contains(exitValue)) {
if (!IntRangeHelper.contains(this.exitValues, exitValue)) {
throw new UnexpectedExitValueException("Process exited with unexpected value", this.exitValues, exitValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.fizzed.blaze.util.IntRange.intRange;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.contains;
Expand Down Expand Up @@ -84,7 +86,7 @@ public void unexpectedExitValue() throws Exception {
fail();
} catch (UnexpectedExitValueException e) {
assertThat(e.getActual(), is(1));
assertThat(e.getExpected(), contains(0));
assertThat(e.getExpected(), contains(intRange(0, 0)));
}
}

Expand Down

0 comments on commit 2079664

Please sign in to comment.