mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(cli): end every error message with what to do next (#58)
* fix(cli): end every error message with what to do next Docker, snapshot, update and config profile errors now say how to recover (check Docker, run doctor or status, fix or delete the profile file, pass the missing profile name) instead of stopping at the failure. A test keeps new error messages to the convention. Signed-off-by: Hector Ventura <hectorvent@gmail.com> * fix(cli): keep the next step after docker's own output and name the instance in snapshot hints Docker's error output ends in a newline and can span lines, which read as 'already has a next step' and hid the hint. The exception now knows whether its text is docker's output or the CLI's own advice. Snapshot hints select the same instance again. Signed-off-by: Hector Ventura <hectorvent@gmail.com> --------- Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
6a74d7d2d5
commit
bd4fa0f134
18 files changed
+252
-31
No files matched your search
@@ -0,0 +1,73 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* AGENTS.md: every {@code printer.error(...)} ends with a suggested next step. A call that writes
|
||||
* its own text must either put a second line in it or pass the next step as a second argument;
|
||||
* a call that only forwards a message (a variable or {@code e.getMessage()}) is trusted to forward
|
||||
* one that already has it. {@code e.withNextStep(...)} on a DockerException is the third way: the
|
||||
* exception knows whether its text is docker's raw output or already carries advice.
|
||||
*/
|
||||
class ErrorHintConventionTest {
|
||||
|
||||
@Test
|
||||
void everyErrorWithItsOwnTextEndsWithANextStep() throws Exception {
|
||||
List<String> missing = new ArrayList<>();
|
||||
try (Stream<Path> files = Files.walk(Path.of("src/main/java"))) {
|
||||
for (Path file : files.filter(p -> p.toString().endsWith(".java")).toList()) {
|
||||
String src = Files.readString(file);
|
||||
int at = 0;
|
||||
while ((at = src.indexOf("printer.error(", at)) >= 0) {
|
||||
int start = at + "printer.error(".length();
|
||||
String args = argumentsFrom(src, start);
|
||||
if (args.contains("\"") && !args.contains("\\n") && !hasTopLevelComma(args)
|
||||
&& !args.contains(".withNextStep(")) {
|
||||
int line = (int) src.substring(0, at).chars().filter(c -> c == '\n').count() + 1;
|
||||
missing.add(file + ":" + line);
|
||||
}
|
||||
at = start;
|
||||
}
|
||||
}
|
||||
}
|
||||
assertEquals(List.of(), missing, "error messages without a next step");
|
||||
}
|
||||
|
||||
// The text between the opening parenthesis at 'start' and its matching close, string-aware.
|
||||
private static String argumentsFrom(String src, int start) {
|
||||
int depth = 1;
|
||||
int i = start;
|
||||
while (depth > 0) {
|
||||
char c = src.charAt(i);
|
||||
if (c == '"') {
|
||||
i++;
|
||||
while (src.charAt(i) != '"') i += src.charAt(i) == '\\' ? 2 : 1;
|
||||
} else if (c == '(') {
|
||||
depth++;
|
||||
} else if (c == ')') {
|
||||
depth--;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return src.substring(start, i - 1);
|
||||
}
|
||||
|
||||
private static boolean hasTopLevelComma(String args) {
|
||||
String noStrings = args.replaceAll("\"(\\\\.|[^\"\\\\])*\"", "\"\"");
|
||||
int depth = 0;
|
||||
for (char c : noStrings.toCharArray()) {
|
||||
if (c == '(') depth++;
|
||||
else if (c == ')') depth--;
|
||||
else if (c == ',' && depth == 0) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import io.floci.cli.docker.DockerException;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.OutputFormat;
|
||||
import io.floci.cli.output.Printer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.DisabledOnOs;
|
||||
import org.junit.jupiter.api.condition.OS;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PrinterErrorTest {
|
||||
|
||||
private static String errorOf(String message, String nextStep) {
|
||||
Ansi.disable();
|
||||
ByteArrayOutputStream err = new ByteArrayOutputStream();
|
||||
new Printer(new PrintStream(new ByteArrayOutputStream()), new PrintStream(err), OutputFormat.text, false)
|
||||
.error(message, nextStep);
|
||||
return err.toString().strip();
|
||||
}
|
||||
|
||||
@Test
|
||||
void theNextStepFollowsAOneLineMessage() {
|
||||
assertEquals("Error: Failed to stop container: boom\nCheck Docker.",
|
||||
errorOf("Failed to stop container: boom", "Check Docker."));
|
||||
}
|
||||
|
||||
/** A message that already brings its own next step (a second line) is not given another. */
|
||||
@Test
|
||||
void aMessageWithItsOwnNextStepIsLeftAlone() {
|
||||
assertEquals("Error: Timed out.\nRe-run it.", errorOf("Timed out.\nRe-run it.", "Check Docker."));
|
||||
}
|
||||
|
||||
/** A trailing newline is not a second line, so the next step still follows. */
|
||||
@Test
|
||||
void aTrailingNewlineDoesNotHideTheNextStep() {
|
||||
assertEquals("Error: Failed to stop container: boom\nCheck Docker.",
|
||||
errorOf("Failed to stop container: boom\n", "Check Docker."));
|
||||
}
|
||||
|
||||
/** Docker's own output never counts as advice, whether it ends in a newline or spans several lines. */
|
||||
@Test
|
||||
void dockersOwnOutputAlwaysGetsTheNextStep() {
|
||||
assertEquals("Error response from daemon: boom\nCheck Docker.",
|
||||
DockerException.ofOutput("Error response from daemon: boom\n").withNextStep("Check Docker."));
|
||||
assertEquals("Error response from daemon: boom\nSee 'docker run --help'.\nCheck Docker.",
|
||||
DockerException.ofOutput("Error response from daemon: boom\nSee 'docker run --help'.\n")
|
||||
.withNextStep("Check Docker."));
|
||||
}
|
||||
|
||||
/** An error in the CLI's own words that already says what to do is not given a second next step. */
|
||||
@Test
|
||||
void anErrorWithItsOwnAdviceKeepsIt() {
|
||||
assertEquals("'docker stop' did not finish within 30s.\nCheck that the Docker daemon is responsive.",
|
||||
new DockerException("'docker stop' did not finish within 30s.\nCheck that the Docker daemon is responsive.")
|
||||
.withNextStep("Check Docker."));
|
||||
assertEquals("Interrupted\nCheck Docker.", new DockerException("Interrupted").withNextStep("Check Docker."));
|
||||
}
|
||||
|
||||
/** Through the real docker call: stderr ending in a newline reaches the user with the next step. */
|
||||
@Test
|
||||
@DisabledOnOs(OS.WINDOWS)
|
||||
void aFailedDockerCallCarriesTheNextStep(@TempDir Path dir) throws Exception {
|
||||
Path fakeDocker = dir.resolve("docker");
|
||||
Files.writeString(fakeDocker, "#!/bin/sh\necho 'Error response from daemon: cannot stop container' >&2\nexit 1\n");
|
||||
assertTrue(fakeDocker.toFile().setExecutable(true));
|
||||
|
||||
DockerException e = assertThrows(DockerException.class,
|
||||
() -> new DockerClient(fakeDocker.toString()).stopContainer("floci", 1));
|
||||
|
||||
assertEquals("Error response from daemon: cannot stop container\n" + DockerClient.CHECK_DOCKER,
|
||||
e.withNextStep(DockerClient.CHECK_DOCKER));
|
||||
}
|
||||
}
|
||||
@@ -103,4 +103,16 @@ class SnapshotCommandsTest {
|
||||
|
||||
assertTrue(r[2].contains("Snapshot API not available"), r[2]);
|
||||
}
|
||||
|
||||
/** The hint has to check the instance that failed, not the default one. */
|
||||
@ParameterizedTest
|
||||
@CsvSource({"list,", "delete, nightly", "save, nightly", "load, nightly"})
|
||||
void aFailedSnapshotCallPointsAtTheSameInstance(String command, String name) {
|
||||
status = 500;
|
||||
|
||||
String[] r = name == null ? run("snapshot", command) : run("snapshot", command, name);
|
||||
|
||||
assertEquals("1", r[0]);
|
||||
assertTrue(r[2].contains("floci status --container floci-snapshot-test-none"), r[2]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user