diff --git a/src/test/java/io/floci/cli/unit/ConfigCommandsTest.java b/src/test/java/io/floci/cli/unit/ConfigCommandsTest.java index d1689ba..a9591df 100644 --- a/src/test/java/io/floci/cli/unit/ConfigCommandsTest.java +++ b/src/test/java/io/floci/cli/unit/ConfigCommandsTest.java @@ -1,5 +1,6 @@ package io.floci.cli.unit; +import com.fasterxml.jackson.databind.ObjectMapper; import io.floci.cli.FlociCli; import io.floci.cli.ProductProfile; import io.floci.cli.commands.config.ConfigProfileCommand; @@ -18,7 +19,10 @@ import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Optional; +import java.util.Set; import static org.junit.jupiter.api.Assertions.*; @@ -179,7 +183,7 @@ class ConfigCommandsTest { @Test void showReportsTheNamespaceAProfilesContainerImplies() throws Exception { writeProfile("b", "container: floci-b\n"); - writeProfile("plain", "port: 4599\n"); + writeProfile("plain", "container: floci\nport: 4599\n"); // default container, whatever FLOCI_CONTAINER says assertTrue(runShow(ProductProfile.AWS, "--profile", "b", "-o", "json") .contains("\"namespace\" : \"b\"")); @@ -352,4 +356,49 @@ class ConfigCommandsTest { assertEquals(1, errBuf.toString().split("'persist_dir'", -1).length - 1, errBuf.toString()); } + + private static String plain(String s) { + return s.replaceAll("\u001B\\[[;\\d]*m", "").replace("\r\n", "\n"); + } + + // A set: which keys appear is the contract, not the order they are printed in. + private static Set jsonKeys(String json) throws IOException { + Set keys = new HashSet<>(); + new ObjectMapper().readTree(json).fieldNames().forEachRemaining(keys::add); + return keys; + } + + /** BL-016: the text output is a user-facing surface, so its lines are pinned. */ + @Test + void textOutputWithoutAProfile() { + // The layout is pinned; the values follow FLOCI_GCP_* if the developer's shell sets them. + String endpoint = Optional.ofNullable(System.getenv("FLOCI_GCP_ENDPOINT")).orElse("http://localhost:4588"); + String container = Optional.ofNullable(System.getenv("FLOCI_GCP_CONTAINER")).orElse("floci-gcp"); + assertEquals(""" + Active Configuration (Floci GCP) + + Profile: default + Endpoint: %s + Container: %s + Output: text + """.formatted(endpoint, container), plain(runShow(ProductProfile.GCP))); + } + + /** BL-016: the -o json key set is a contract; start settings appear only when the profile sets them. */ + @Test + void jsonKeySetGrowsOnlyWithTheStartSettingsTheProfileDeclares() throws Exception { + Set base = Set.of("profile", "endpoint", "container", "output"); + assertEquals(base, jsonKeys(runShow(ProductProfile.AWS, "-o", "json"))); + + // The default container pins "no namespace", whatever FLOCI_CONTAINER the shell sets. + writeProfile("full", "container: floci\nimage: floci/floci:x\nport: 4599\npersistDir: /d\nservices: s3\n"); + Set full = new HashSet<>(base); + full.addAll(Set.of("image", "port", "persistDir", "services")); + assertEquals(full, jsonKeys(runShow(ProductProfile.AWS, "--profile", "full", "-o", "json"))); + + writeProfile("partial", "container: floci\nport: 4599\n"); + Set partial = new HashSet<>(base); + partial.add("port"); + assertEquals(partial, jsonKeys(runShow(ProductProfile.AWS, "--profile", "partial", "-o", "json"))); + } } diff --git a/src/test/java/io/floci/cli/unit/MissingProfileTest.java b/src/test/java/io/floci/cli/unit/MissingProfileTest.java index cbfc808..190e4a0 100644 --- a/src/test/java/io/floci/cli/unit/MissingProfileTest.java +++ b/src/test/java/io/floci/cli/unit/MissingProfileTest.java @@ -106,6 +106,25 @@ class MissingProfileTest { assertEquals(1, err.split("Warning").length - 1, err); } + /** BL-017: -V takes the same bail-out as --help, so a bad profile cannot break it. */ + @Test + void versionStillWorksWithABadProfile() { + int[] exit = new int[1]; + captureStderr(() -> cli().execute("start", "--profile", "typo", "-V"), exit); + + assertEquals(0, exit[0]); + } + + /** BL-017: config show used to warn and exit 0 for a missing profile; it exits 2 like every command. */ + @Test + void configShowWithAMissingProfileExitsTwo() { + int[] exit = new int[1]; + String err = captureStderr(() -> cli().execute("config", "show", "--profile", "missing"), exit); + + assertEquals(2, exit[0]); + assertTrue(err.contains("Profile 'missing' not found"), err); + } + @Test void helpStillWorksWithABadProfile() { int[] exit = new int[1]; diff --git a/src/test/java/io/floci/cli/unit/ProfilePrecedenceTest.java b/src/test/java/io/floci/cli/unit/ProfilePrecedenceTest.java index 2bbc129..12dd2a8 100644 --- a/src/test/java/io/floci/cli/unit/ProfilePrecedenceTest.java +++ b/src/test/java/io/floci/cli/unit/ProfilePrecedenceTest.java @@ -1,6 +1,7 @@ package io.floci.cli.unit; import io.floci.cli.FlociCli; +import io.floci.cli.GlobalOptions; import io.floci.cli.ProductProfile; import io.floci.cli.commands.StartCommand; import io.floci.cli.config.ProfileDefaultValueProvider; @@ -8,12 +9,15 @@ import io.floci.cli.config.ProfileStore; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import picocli.CommandLine; +import picocli.CommandLine.Command; +import picocli.CommandLine.Mixin; import picocli.CommandLine.Model.CommandSpec; import picocli.CommandLine.ParseResult; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Map; import static org.junit.jupiter.api.Assertions.*; @@ -166,4 +170,40 @@ class ProfilePrecedenceTest { assertEquals(System.getenv("HOME") + "/floci-data", leafSpec("start", "--profile", "interp").findOption("--persist").getValue()); } + + /** A command with the real GlobalOptions mixin, but environment variables from a map. */ + @Command(name = "probe") + static class EnvProbe implements Runnable { + @Mixin + GlobalOptions global; + + EnvProbe(Map env) { + this.global = new GlobalOptions(ProductProfile.AWS, env::get); + } + + @Override + public void run() {} + } + + private String containerWith(Map env, String... args) { + EnvProbe probe = new EnvProbe(env); + new CommandLine(probe) + .setDefaultValueProvider(new ProfileDefaultValueProvider(new ProfileStore(tempDir))) + .parseArgs(args); + return probe.global.container; + } + + /** BL-012: flag > --profile > FLOCI_* env var > product default, every rung pinned. */ + @Test + void theProfileBeatsTheEnvironmentAndTheFlagBeatsBoth() throws Exception { + writeProfile("named", "container: from-profile\n"); + writeProfile("silent", "port: 4599\n"); + Map env = Map.of("FLOCI_CONTAINER", "from-env"); + + assertEquals("floci", containerWith(Map.of())); + assertEquals("from-env", containerWith(env)); + assertEquals("from-profile", containerWith(env, "--profile", "named")); + assertEquals("from-env", containerWith(env, "--profile", "silent")); + assertEquals("from-flag", containerWith(env, "--profile", "named", "--container", "from-flag")); + } }