mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(cli): keep wait's spinner out of -o json, reject bad durations and make --verbose work (#48)
* fix(cli): keep wait's spinner out of -o json, reject bad durations and make --verbose work wait draws its spinner only for text output on a terminal and clears it before the result; --timeout values that are not a positive number with ms, s, m or h exit 2 with a hint instead of a stack trace; --verbose now echoes each docker command to stderr and prints stack traces for errors. * fix(cli): reject durations that overflow and pin the spinner rule on a terminal A duration is checked for sign before it is converted and the conversion is overflow-checked, so -9223372036854775807s no longer wraps to a positive timeout. wait takes its terminal check through a seam, with tests for json, yaml and text output on a terminal.
This commit is contained in:
1 parent
7ecc895baa
commit
52ebdee98d
8 files changed
+230
-10
No files matched your search
@@ -68,7 +68,7 @@ public class FlociCli implements Runnable {
|
|||||||
@Override
|
@Override
|
||||||
public int handleExecutionException(Exception ex, CommandLine cmd, ParseResult parseResult) {
|
public int handleExecutionException(Exception ex, CommandLine cmd, ParseResult parseResult) {
|
||||||
System.err.println(Ansi.red("Error: ") + ex.getMessage());
|
System.err.println(Ansi.red("Error: ") + ex.getMessage());
|
||||||
if (Boolean.getBoolean("floci.verbose")) {
|
if (Boolean.getBoolean(GlobalOptions.VERBOSE_PROPERTY)) {
|
||||||
ex.printStackTrace(System.err);
|
ex.printStackTrace(System.err);
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ import java.util.regex.Pattern;
|
|||||||
*/
|
*/
|
||||||
public class GlobalOptions {
|
public class GlobalOptions {
|
||||||
|
|
||||||
|
/** System property that {@code --verbose} sets; read where no GlobalOptions is at hand. */
|
||||||
|
public static final String VERBOSE_PROPERTY = "floci.verbose";
|
||||||
|
|
||||||
// The command this mixin belongs to, so hints can tell which flags were actually typed.
|
// The command this mixin belongs to, so hints can tell which flags were actually typed.
|
||||||
// Null for commands built by hand (restart's start), which were never parsed.
|
// Null for commands built by hand (restart's start), which were never parsed.
|
||||||
@Spec(Spec.Target.MIXEE)
|
@Spec(Spec.Target.MIXEE)
|
||||||
@@ -122,6 +125,10 @@ public class GlobalOptions {
|
|||||||
if (noColor || !isStdoutTty()) {
|
if (noColor || !isStdoutTty()) {
|
||||||
Ansi.disable();
|
Ansi.disable();
|
||||||
}
|
}
|
||||||
|
// Read by the exception handler (stack traces) and DockerClient (the commands it runs).
|
||||||
|
if (verbose) {
|
||||||
|
System.setProperty(VERBOSE_PROPERTY, "true");
|
||||||
|
}
|
||||||
return new Printer(System.out, System.err, output, quiet);
|
return new Printer(System.out, System.err, output, quiet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import io.floci.cli.output.Printer;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import picocli.CommandLine.*;
|
import picocli.CommandLine.*;
|
||||||
|
|
||||||
|
import java.io.Console;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
@@ -19,6 +20,7 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
|
||||||
@Command(
|
@Command(
|
||||||
name = "wait",
|
name = "wait",
|
||||||
@@ -42,12 +44,19 @@ public class WaitCommand implements Callable<Integer> {
|
|||||||
|
|
||||||
/** Test seam: {@code docker} answers the container lookup that finds the endpoint. */
|
/** Test seam: {@code docker} answers the container lookup that finds the endpoint. */
|
||||||
public WaitCommand(ProductProfile profile, DockerClient docker) {
|
public WaitCommand(ProductProfile profile, DockerClient docker) {
|
||||||
|
this(profile, docker, WaitCommand::interactive);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test seam: {@code terminal} stands in for "stdout is a terminal", which a test run is not. */
|
||||||
|
public WaitCommand(ProductProfile profile, DockerClient docker, BooleanSupplier terminal) {
|
||||||
this.profile = profile;
|
this.profile = profile;
|
||||||
this.global = new GlobalOptions(profile);
|
this.global = new GlobalOptions(profile);
|
||||||
this.docker = docker;
|
this.docker = docker;
|
||||||
|
this.terminal = terminal;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final DockerClient docker;
|
private final DockerClient docker;
|
||||||
|
private final BooleanSupplier terminal;
|
||||||
|
|
||||||
@Option(names = {"--timeout"}, description = "Maximum time to wait (e.g. 30s, 2m)", defaultValue = "30s", paramLabel = "<duration>")
|
@Option(names = {"--timeout"}, description = "Maximum time to wait (e.g. 30s, 2m)", defaultValue = "30s", paramLabel = "<duration>")
|
||||||
String timeout;
|
String timeout;
|
||||||
@@ -68,7 +77,16 @@ public class WaitCommand implements Callable<Integer> {
|
|||||||
@Override
|
@Override
|
||||||
public Integer call() {
|
public Integer call() {
|
||||||
Printer printer = global.printer();
|
Printer printer = global.printer();
|
||||||
long timeoutMillis = Durations.parseDuration(timeout);
|
long timeoutMillis;
|
||||||
|
try {
|
||||||
|
timeoutMillis = Durations.parseDuration(timeout);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
printer.error(e.getMessage());
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
// Only a person at a terminal watching text output gets the spinner: in -o json/yaml or
|
||||||
|
// in a pipe its frames would land in the data.
|
||||||
|
boolean spinner = printer.format() == OutputFormat.text && terminal.getAsBoolean();
|
||||||
// The deadline starts now, so the container lookup below counts against --timeout too.
|
// The deadline starts now, so the container lookup below counts against --timeout too.
|
||||||
Instant deadline = Instant.now().plusMillis(timeoutMillis);
|
Instant deadline = Instant.now().plusMillis(timeoutMillis);
|
||||||
String effectiveEndpoint = knownEndpoint != null ? knownEndpoint : endpointWithin(deadline);
|
String effectiveEndpoint = knownEndpoint != null ? knownEndpoint : endpointWithin(deadline);
|
||||||
@@ -79,6 +97,7 @@ public class WaitCommand implements Callable<Integer> {
|
|||||||
long delay = FIRST_DELAY_MS;
|
long delay = FIRST_DELAY_MS;
|
||||||
while (Instant.now().isBefore(deadline)) {
|
while (Instant.now().isBefore(deadline)) {
|
||||||
if (isReady(client, service, requestTimeout(Duration.between(Instant.now(), deadline)))) {
|
if (isReady(client, service, requestTimeout(Duration.between(Instant.now(), deadline)))) {
|
||||||
|
if (spinner) clearSpinner(printer);
|
||||||
if (printer.format() != OutputFormat.text) {
|
if (printer.format() != OutputFormat.text) {
|
||||||
printer.structured(Map.of("ready", true, "endpoint", effectiveEndpoint));
|
printer.structured(Map.of("ready", true, "endpoint", effectiveEndpoint));
|
||||||
} else {
|
} else {
|
||||||
@@ -86,7 +105,7 @@ public class WaitCommand implements Callable<Integer> {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
printSpinner(printer, deadline);
|
if (spinner) printSpinner(printer, deadline);
|
||||||
long remaining = Duration.between(Instant.now(), deadline).toMillis();
|
long remaining = Duration.between(Instant.now(), deadline).toMillis();
|
||||||
if (remaining <= 0) break;
|
if (remaining <= 0) break;
|
||||||
try { Thread.sleep(Math.min(delay, remaining)); } catch (InterruptedException e) {
|
try { Thread.sleep(Math.min(delay, remaining)); } catch (InterruptedException e) {
|
||||||
@@ -96,6 +115,7 @@ public class WaitCommand implements Callable<Integer> {
|
|||||||
delay = nextDelay(delay);
|
delay = nextDelay(delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (spinner) clearSpinner(printer);
|
||||||
printer.error("Timed out waiting for " + profile.displayName() + " after " + timeout
|
printer.error("Timed out waiting for " + profile.displayName() + " after " + timeout
|
||||||
+ ".\nIs the container running? Try '" + profile.commandPrefix() + " status'.");
|
+ ".\nIs the container running? Try '" + profile.commandPrefix() + " status'.");
|
||||||
return 1;
|
return 1;
|
||||||
@@ -153,6 +173,15 @@ public class WaitCommand implements Callable<Integer> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean interactive() {
|
||||||
|
Console console = System.console();
|
||||||
|
return console != null && console.isTerminal();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void clearSpinner(Printer printer) {
|
||||||
|
printer.print("\r" + " ".repeat(40) + "\r");
|
||||||
|
}
|
||||||
|
|
||||||
private void printSpinner(Printer printer, Instant deadline) {
|
private void printSpinner(Printer printer, Instant deadline) {
|
||||||
long remaining = Duration.between(Instant.now(), deadline).toSeconds();
|
long remaining = Duration.between(Instant.now(), deadline).toSeconds();
|
||||||
printer.print("\r" + Ansi.gray("Waiting... (" + remaining + "s remaining)") + " ");
|
printer.print("\r" + Ansi.gray("Waiting... (" + remaining + "s remaining)") + " ");
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package io.floci.cli.docker;
|
package io.floci.cli.docker;
|
||||||
|
|
||||||
|
import io.floci.cli.GlobalOptions;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -266,6 +268,9 @@ public class DockerClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Process runProcess(String... cmd) throws IOException {
|
private Process runProcess(String... cmd) throws IOException {
|
||||||
|
if (Boolean.getBoolean(GlobalOptions.VERBOSE_PROPERTY)) {
|
||||||
|
System.err.println("+ " + String.join(" ", cmd));
|
||||||
|
}
|
||||||
if (cmd.length > 0 && "docker".equals(cmd[0])) {
|
if (cmd.length > 0 && "docker".equals(cmd[0])) {
|
||||||
cmd = cmd.clone();
|
cmd = cmd.clone();
|
||||||
cmd[0] = binary;
|
cmd[0] = binary;
|
||||||
|
|||||||
@@ -3,15 +3,44 @@ package io.floci.cli.util;
|
|||||||
/** Parses human-friendly duration strings like {@code 30s}, {@code 2m}, {@code 500ms}. */
|
/** Parses human-friendly duration strings like {@code 30s}, {@code 2m}, {@code 500ms}. */
|
||||||
public final class Durations {
|
public final class Durations {
|
||||||
|
|
||||||
/** Returns milliseconds; a bare number is seconds; null/blank defaults to 30s. */
|
/**
|
||||||
|
* Returns milliseconds; a bare number is seconds; null/blank defaults to 30s.
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException for anything else, or a duration that is not positive, with
|
||||||
|
* a message suitable for printing to the user
|
||||||
|
*/
|
||||||
public static long parseDuration(String s) {
|
public static long parseDuration(String s) {
|
||||||
if (s == null || s.isBlank()) return 30_000;
|
if (s == null || s.isBlank()) return 30_000;
|
||||||
s = s.trim().toLowerCase();
|
String value = s.trim().toLowerCase();
|
||||||
if (s.endsWith("ms")) return Long.parseLong(s.substring(0, s.length() - 2));
|
long amount;
|
||||||
if (s.endsWith("s")) return Long.parseLong(s.substring(0, s.length() - 1)) * 1000;
|
long unitMillis;
|
||||||
if (s.endsWith("m")) return Long.parseLong(s.substring(0, s.length() - 1)) * 60_000;
|
try {
|
||||||
if (s.endsWith("h")) return Long.parseLong(s.substring(0, s.length() - 1)) * 3_600_000;
|
if (value.endsWith("ms")) { amount = number(value, 2); unitMillis = 1; }
|
||||||
return Long.parseLong(s) * 1000;
|
else if (value.endsWith("s")) { amount = number(value, 1); unitMillis = 1000; }
|
||||||
|
else if (value.endsWith("m")) { amount = number(value, 1); unitMillis = 60_000; }
|
||||||
|
else if (value.endsWith("h")) { amount = number(value, 1); unitMillis = 3_600_000; }
|
||||||
|
else { amount = number(value, 0); unitMillis = 1000; }
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new IllegalArgumentException("Invalid duration '" + s + "'.\n"
|
||||||
|
+ "Use a number with ms, s, m or h, for example 30s or 2m.");
|
||||||
|
}
|
||||||
|
// Checked before the conversion: a product that overflows can come back positive.
|
||||||
|
if (amount <= 0) {
|
||||||
|
throw new IllegalArgumentException("Duration '" + s + "' must be greater than zero.\n"
|
||||||
|
+ "Use a positive value, for example 30s.");
|
||||||
|
}
|
||||||
|
long millis;
|
||||||
|
try {
|
||||||
|
millis = Math.multiplyExact(amount, unitMillis);
|
||||||
|
} catch (ArithmeticException e) {
|
||||||
|
throw new IllegalArgumentException("Duration '" + s + "' is too long.\n"
|
||||||
|
+ "Use a smaller value, for example 30s or 2m.");
|
||||||
|
}
|
||||||
|
return millis;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long number(String value, int suffixLength) {
|
||||||
|
return Long.parseLong(value.substring(0, value.length() - suffixLength));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Durations() {}
|
private Durations() {}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package io.floci.cli.unit;
|
package io.floci.cli.unit;
|
||||||
|
|
||||||
|
import io.floci.cli.GlobalOptions;
|
||||||
import io.floci.cli.docker.DockerClient;
|
import io.floci.cli.docker.DockerClient;
|
||||||
import io.floci.cli.docker.DockerException;
|
import io.floci.cli.docker.DockerException;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -9,6 +10,8 @@ import org.junit.jupiter.api.io.TempDir;
|
|||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.ValueSource;
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
@@ -203,4 +206,26 @@ class DockerClientProcessTest {
|
|||||||
|
|
||||||
assertEquals(List.of("rm", "--", "--rm-everything"), args);
|
assertEquals(List.of("rm", "--", "--rm-everything"), args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** BL-041: with --verbose set, each docker command is echoed to stderr before it runs. */
|
||||||
|
@Test
|
||||||
|
@DisabledOnOs(OS.WINDOWS)
|
||||||
|
void verboseEchoesTheDockerCommand() throws Exception {
|
||||||
|
Path fakeDocker = tempDir.resolve("docker");
|
||||||
|
Files.writeString(fakeDocker, "#!/bin/sh\nexit 0\n");
|
||||||
|
assertTrue(fakeDocker.toFile().setExecutable(true));
|
||||||
|
PrintStream err = System.err;
|
||||||
|
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
|
||||||
|
System.setErr(new PrintStream(errBuf));
|
||||||
|
System.setProperty(GlobalOptions.VERBOSE_PROPERTY, "true");
|
||||||
|
try {
|
||||||
|
new DockerClient(fakeDocker.toString()).removeContainer("floci-x");
|
||||||
|
} finally {
|
||||||
|
System.clearProperty(GlobalOptions.VERBOSE_PROPERTY);
|
||||||
|
System.setErr(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errBuf.toString().contains("+ docker rm"), errBuf.toString());
|
||||||
|
assertTrue(errBuf.toString().contains("floci-x"), errBuf.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package io.floci.cli.unit;
|
||||||
|
|
||||||
|
import io.floci.cli.util.Durations;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/** BL-040: durations parse the documented forms and reject everything else with a usable message. */
|
||||||
|
class DurationsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void parsesEveryDocumentedUnit() {
|
||||||
|
assertEquals(500, Durations.parseDuration("500ms"));
|
||||||
|
assertEquals(90_000, Durations.parseDuration("90s"));
|
||||||
|
assertEquals(120_000, Durations.parseDuration("2m"));
|
||||||
|
assertEquals(3_600_000, Durations.parseDuration("1h"));
|
||||||
|
assertEquals(45_000, Durations.parseDuration("45"));
|
||||||
|
assertEquals(30_000, Durations.parseDuration(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {"abc", "10x", "s", "1.5s"})
|
||||||
|
void rejectsWhatItCannotRead(String value) {
|
||||||
|
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> Durations.parseDuration(value));
|
||||||
|
assertTrue(e.getMessage().contains("Invalid duration"), e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
// The last one overflows to +1000 ms when multiplied before the check.
|
||||||
|
@ValueSource(strings = {"0s", "0", "-1s", "-5", "-9223372036854775807s"})
|
||||||
|
void rejectsDurationsThatAreNotPositive(String value) {
|
||||||
|
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> Durations.parseDuration(value));
|
||||||
|
assertTrue(e.getMessage().contains("greater than zero"), e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {"9223372036854775807s", "9223372036854775807", "9223372036854775m", "2562047788016h"})
|
||||||
|
void rejectsDurationsTooLongToRepresent(String value) {
|
||||||
|
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> Durations.parseDuration(value));
|
||||||
|
assertTrue(e.getMessage().contains("too long"), e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void theLargestMillisecondValueStillParses() {
|
||||||
|
assertEquals(Long.MAX_VALUE, Durations.parseDuration("9223372036854775807ms"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,11 +8,15 @@ import io.floci.cli.config.ProfileStore;
|
|||||||
import io.floci.cli.docker.DockerClient;
|
import io.floci.cli.docker.DockerClient;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.io.TempDir;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.ServerSocket;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -149,4 +153,76 @@ class WaitCommandTimingTest {
|
|||||||
server.stop(0);
|
server.stop(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record Out(int exit, String out, String err) {}
|
||||||
|
|
||||||
|
private static Out waitWith(String... args) {
|
||||||
|
return waitWith(false, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Out waitWith(boolean terminal, String... args) {
|
||||||
|
DockerClient noContainer = new DockerClient() {
|
||||||
|
@Override
|
||||||
|
public Optional<ContainerInfo> inspectContainer(String name) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
PrintStream err = System.err;
|
||||||
|
PrintStream out = System.out;
|
||||||
|
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
|
||||||
|
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
|
||||||
|
System.setOut(new PrintStream(outBuf));
|
||||||
|
System.setErr(new PrintStream(errBuf));
|
||||||
|
try {
|
||||||
|
int exit = new CommandLine(new WaitCommand(ProductProfile.AWS, noContainer, () -> terminal)).execute(args);
|
||||||
|
return new Out(exit, outBuf.toString(), errBuf.toString());
|
||||||
|
} finally {
|
||||||
|
System.setErr(err);
|
||||||
|
System.setOut(out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** BL-039: on a terminal, -o json and -o yaml still carry no spinner frames while polling. */
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {"json", "yaml"})
|
||||||
|
void structuredOutputHasNoSpinnerEvenOnATerminal(String format) throws Exception {
|
||||||
|
Out r = waitWith(true, "--timeout", "300ms", "--endpoint", closedEndpoint(), "-o", format);
|
||||||
|
|
||||||
|
assertEquals(1, r.exit());
|
||||||
|
assertFalse(r.out().contains("Waiting"), r.out());
|
||||||
|
assertFalse(r.out().contains("\r"), r.out());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The other half: text output on a terminal does draw it, so the test above can fail. */
|
||||||
|
@Test
|
||||||
|
void textOutputOnATerminalShowsTheSpinner() throws Exception {
|
||||||
|
Out r = waitWith(true, "--timeout", "300ms", "--endpoint", closedEndpoint());
|
||||||
|
|
||||||
|
assertEquals(1, r.exit());
|
||||||
|
assertTrue(r.out().contains("Waiting"), r.out());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void textOutputInAPipeHasNoSpinner() throws Exception {
|
||||||
|
Out r = waitWith(false, "--timeout", "300ms", "--endpoint", closedEndpoint());
|
||||||
|
|
||||||
|
assertEquals(1, r.exit());
|
||||||
|
assertFalse(r.out().contains("Waiting"), r.out());
|
||||||
|
}
|
||||||
|
|
||||||
|
// A port that was just bound and released: nothing is listening, so polls are refused at once.
|
||||||
|
private static String closedEndpoint() throws Exception {
|
||||||
|
try (ServerSocket socket = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) {
|
||||||
|
return "http://127.0.0.1:" + socket.getLocalPort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** BL-040: a bad --timeout is a usage error with a hint, not a stack trace. */
|
||||||
|
@Test
|
||||||
|
void anUnreadableTimeoutExitsTwo() {
|
||||||
|
Out r = waitWith("--timeout", "abc", "--endpoint", "http://127.0.0.1:9");
|
||||||
|
|
||||||
|
assertEquals(2, r.exit());
|
||||||
|
assertTrue(r.err().contains("Invalid duration 'abc'"), r.err());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user