mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(cli): honour --no-color on parse errors and show profile defaults in --help (#35)
This commit is contained in:
1 parent
bc59dff8c3
commit
fc67e72a62
8 files changed
+126
-13
No files matched your search
@@ -31,4 +31,15 @@ class FlociCliParsingTest {
|
||||
int code = new CommandLine(new FlociCli()).execute("doctor", "--help");
|
||||
assertEquals(0, code);
|
||||
}
|
||||
|
||||
/** BL-024: a parse error is printed before any printer exists, so --no-color is read raw. */
|
||||
@Test
|
||||
void noColorFlagDisablesColourBeforeParsing() {
|
||||
assertTrue(FlociCli.colorDisabled(new String[]{"start", "--profile", "typo", "--no-color"}, false, true));
|
||||
assertFalse(FlociCli.colorDisabled(new String[]{"start", "--profile", "typo"}, false, true));
|
||||
assertTrue(FlociCli.colorDisabled(new String[]{"start"}, true, true));
|
||||
assertTrue(FlociCli.colorDisabled(new String[]{"start"}, false, false));
|
||||
assertTrue(FlociCli.colorDisabled(new String[]{"start", "--no-color=true"}, false, true));
|
||||
assertFalse(FlociCli.colorDisabled(new String[]{"start", "--no-color=false"}, false, true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.floci.cli.http.FlociHttpClient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** Request paths go before an endpoint's query, not after it. */
|
||||
class FlociHttpClientUrlTest {
|
||||
|
||||
@Test
|
||||
void theHealthPathIsInsertedBeforeTheEndpointsQuery() throws Exception {
|
||||
AtomicReference<String> seen = new AtomicReference<>();
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/", exchange -> {
|
||||
seen.set(exchange.getRequestURI().toString());
|
||||
byte[] body = "{\"version\":\"1.2.3\",\"services\":[]}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, body.length);
|
||||
try (OutputStream out = exchange.getResponseBody()) {
|
||||
out.write(body);
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
String endpoint = "http://127.0.0.1:" + server.getAddress().getPort() + "/?token=a%26b#frag";
|
||||
|
||||
assertEquals("1.2.3", new FlociHttpClient(endpoint, "/_floci").health().version());
|
||||
assertEquals("/_floci/health?token=a%26b", seen.get());
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,6 +69,26 @@ class MissingProfileTest {
|
||||
assertEquals(2, exit[0]);
|
||||
}
|
||||
|
||||
/** BL-014: help renders defaults through the provider, so a good profile's values show. */
|
||||
@Test
|
||||
void helpShowsTheProfilesDefaults() throws Exception {
|
||||
writeProfile("probe", "port: 4599\nimage: floci/floci:enforced\n");
|
||||
|
||||
PrintStream original = System.out;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(out));
|
||||
int exit;
|
||||
try {
|
||||
exit = cli().execute("start", "--profile", "probe", "--help");
|
||||
} finally {
|
||||
System.setOut(original);
|
||||
}
|
||||
|
||||
assertEquals(0, exit);
|
||||
assertTrue(out.toString().contains("(default: 4599)"), out.toString());
|
||||
assertTrue(out.toString().contains("enforced"), out.toString()); // help wraps the image value
|
||||
}
|
||||
|
||||
@Test
|
||||
void helpStillWorksWithABadProfile() {
|
||||
int[] exit = new int[1];
|
||||
|
||||
@@ -196,5 +196,12 @@ class StartCommandArgsTest {
|
||||
assertEquals("http://floci.internal:4599", StartCommand.withPort("http://floci.internal:4566", 4599));
|
||||
assertEquals("http://localhost:4599", StartCommand.withPort("http://localhost:4566", 4599));
|
||||
assertEquals("http://localhost:4599", StartCommand.withPort("not a url", 4599));
|
||||
// BL-015: only the port changes.
|
||||
assertEquals("http://user:pw@floci.internal:4599/base?x=1#f",
|
||||
StartCommand.withPort("http://user:pw@floci.internal:4566/base?x=1#f", 4599));
|
||||
// Escaped characters stay escaped: %26 must not turn into a second query parameter.
|
||||
assertEquals("http://floci.internal:4599/b?token=a%26b",
|
||||
StartCommand.withPort("http://floci.internal:4566/b?token=a%26b", 4599));
|
||||
assertEquals("http://[::1]:4599", StartCommand.withPort("http://[::1]:4566", 4599));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user