test: cover Printer and the floci update flow

PrinterTest pins where text, errors and warnings go, what --quiet hides, and the JSON and YAML rendering. UpdateCommandTest drives floci update against a stub release server: a verified replacement, a checksum mismatch, --check, up to date, a Homebrew install and a non-version API response. UpdateCommand takes the binary path and the release URLs through a constructor for this, replacing a package-private field.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-10-10 18:45:21 -05:00
1 parent 771b353733
commit 66fa924d77
4 files changed
+310 -8

No files matched your search

@@ -0,0 +1,90 @@
package io.floci.cli.unit;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
/** BL-074: where each kind of output goes, and how structured output is rendered. */
class PrinterTest {
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private final ByteArrayOutputStream err = new ByteArrayOutputStream();
@BeforeAll
static void plainText() {
Ansi.disable();
}
private Printer printer(OutputFormat format, boolean quiet) {
return new Printer(new PrintStream(out), new PrintStream(err), format, quiet);
}
private static Map<String, Object> sample() {
Map<String, Object> data = new LinkedHashMap<>();
data.put("status", "running");
data.put("services", List.of("s3", "sqs"));
return data;
}
@Test
void textGoesToStdoutAndProblemsToStderr() {
Printer p = printer(OutputFormat.text, false);
p.println("hello");
p.error("broken\nRun 'floci doctor'.");
p.warn("careful");
assertEquals("hello\n", out.toString());
assertEquals("Error: broken\nRun 'floci doctor'.\nWarning: careful\n", err.toString());
}
/** --quiet silences ordinary output, never errors or warnings. */
@Test
void quietKeepsErrorsAndWarnings() {
Printer p = printer(OutputFormat.text, true);
p.println("hello");
p.print("partial");
p.error("broken\nFix it.");
p.warn("careful");
assertEquals("", out.toString());
assertTrue(err.toString().contains("Error: broken"));
assertTrue(err.toString().contains("Warning: careful"));
}
@Test
void jsonIsIndentedAndKeepsKeyOrder() {
printer(OutputFormat.json, false).structured(sample());
String json = out.toString();
assertTrue(json.startsWith("{\n"), json);
assertTrue(json.indexOf("\"status\"") < json.indexOf("\"services\""), json);
assertTrue(json.contains("\"s3\", \"sqs\"") || json.contains("\"s3\",\"sqs\""), json);
}
@Test
void yamlHasNoDocumentMarker() {
printer(OutputFormat.yaml, false).structured(sample());
assertEquals("status: \"running\"\nservices:\n- \"s3\"\n- \"sqs\"\n", out.toString());
}
/** Structured output is the command's result, so --quiet does not hide it. */
@Test
void quietStillPrintsStructuredOutput() {
printer(OutputFormat.json, true).structured(Map.of("ok", true));
assertTrue(out.toString().contains("\"ok\" : true"), out.toString());
}
}