feat(profile): warn when a profile sets a key floci does not read (#37)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-08 22:47:51 -05:00
1 parent 01d6133548
commit 22668a1200
7 files changed
+125

No files matched your search

@@ -321,4 +321,35 @@ class ConfigCommandsTest {
assertTrue(out.contains("\"container\" : \"floci-snap\""), out);
assertTrue(out.contains("\"persistDir\" : \"/from-the-parse\""), out);
}
/** config profile show reads the file directly; a misspelled key must still be reported. */
@Test
void profileShowWarnsAboutAMisspelledKey() throws Exception {
writeProfile("typo-key", "container: floci-typo\npersist_dir: /data\n");
Run r = profileCmd(ProductProfile.AWS, "show", "typo-key");
assertEquals(0, r.exit());
assertTrue(r.err().contains("'persist_dir'"), r.err());
}
/** config show reads the profile once and reports a misspelled key exactly once. */
@Test
void configShowWarnsOnceAboutAMisspelledKey() throws Exception {
writeProfile("typo-twice", "container: floci-typo\npersist_dir: /data\n");
CommandLine cmd = FlociCli.buildCommandLine(store());
PrintStream out = System.out;
PrintStream err = System.err;
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
System.setOut(new PrintStream(new ByteArrayOutputStream()));
System.setErr(new PrintStream(errBuf));
try {
assertEquals(0, cmd.execute("config", "show", "--profile", "typo-twice"));
} finally {
System.setOut(out);
System.setErr(err);
}
assertEquals(1, errBuf.toString().split("'persist_dir'", -1).length - 1, errBuf.toString());
}
}
@@ -89,6 +89,23 @@ class MissingProfileTest {
assertTrue(out.toString().contains("enforced"), out.toString()); // help wraps the image value
}
/** BL-013: a misspelled key is still ignored, but no longer silently. */
@Test
void aMisspelledKeyIsReportedOnceAndTheProfileStillApplies() throws Exception {
writeProfile("typo-key", "container: floci-typo\npersist_dir: /data\n");
int[] exit = new int[1];
String err = captureStderr(() -> {
cli().parseArgs("start", "--profile", "typo-key");
return 0;
}, exit);
assertEquals(0, exit[0]);
assertTrue(err.contains("Profile 'typo-key' sets 'persist_dir'"), err);
assertTrue(err.contains("persistDir"), err);
assertEquals(1, err.split("Warning").length - 1, err);
}
@Test
void helpStillWorksWithABadProfile() {
int[] exit = new int[1];
@@ -10,8 +10,11 @@ import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@@ -206,4 +209,27 @@ class ProfileStoreTest {
assertTrue(e.getMessage().contains("'..\u0000x'"), e.getMessage());
assertFalse(e.getMessage().contains(".yaml"), e.getMessage());
}
@Test
void unknownKeysAreRecordedButNeverWrittenBack() throws Exception {
Files.createDirectories(tempDir);
Files.writeString(tempDir.resolve("typo.yaml"), "name: typo\npersist_dir: /data\nport: 4599\n");
Profile p = store().get("typo").orElseThrow();
assertEquals(List.of("persist_dir"), p.unknownKeys());
assertEquals(4599, p.port);
store().save(p);
assertFalse(Files.readString(tempDir.resolve("typo.yaml")).contains("persist_dir"));
assertFalse(Files.readString(tempDir.resolve("typo.yaml")).contains("unknownKeys"));
}
@Test
void knownKeysAreExactlyTheProfileFields() {
List<String> fields = Arrays.stream(Profile.class.getFields())
.filter(f -> !Modifier.isStatic(f.getModifiers()))
.map(Field::getName)
.sorted().toList();
assertEquals(fields, Profile.KNOWN_KEYS.stream().sorted().toList());
}
}