mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-23 06:00: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
@@ -1,5 +1,6 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.GlobalOptions;
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import io.floci.cli.docker.DockerException;
|
||||
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.provider.ValueSource;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
@@ -203,4 +206,26 @@ class DockerClientProcessTest {
|
||||
|
||||
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 org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
@@ -149,4 +153,76 @@ class WaitCommandTimingTest {
|
||||
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