test(profile): pin profile precedence, config show output and bad-profile exits (#38)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-08 23:12:40 -05:00
1 parent 22668a1200
commit af3d7814b9
3 files changed
+109 -1

No files matched your search

@@ -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<String> jsonKeys(String json) throws IOException {
Set<String> 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<String> 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<String> 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<String> partial = new HashSet<>(base);
partial.add("port");
assertEquals(partial, jsonKeys(runShow(ProductProfile.AWS, "--profile", "partial", "-o", "json")));
}
}
@@ -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];
@@ -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<String, String> env) {
this.global = new GlobalOptions(ProductProfile.AWS, env::get);
}
@Override
public void run() {}
}
private String containerWith(Map<String, String> 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<String, String> 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"));
}
}