fix(profile): make profile names, files and exit codes consistent (#33)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-08 22:21:02 -05:00
1 parent bf9ee4037b
commit 5c0cc339bf
9 files changed
+263 -44

No files matched your search

@@ -268,4 +268,48 @@ class ConfigCommandsTest {
assertTrue(json.contains("\"container\" : \"floci-az-b\""), json);
assertTrue(json.contains("\"port\" : 14577"), json);
}
/** BL-009: list names a file without 'name:' after the file, so show must too. */
@Test
void showNamesAHandWrittenProfileAfterItsFile() throws Exception {
writeProfile("handwritten", "container: floci-hand\n");
Run r = profileCmd(ProductProfile.AWS, "show", "handwritten");
assertEquals(0, r.exit());
assertTrue(r.out().contains("handwritten"), r.out());
assertFalse(r.out().contains("null"), r.out());
}
@Test
void deleteReportsBothFilesWhenTwoSpellingsExist() throws Exception {
writeProfile("dev", "port: 4599\n");
Files.writeString(tempDir.resolve("dev.yml"), "port: 4600\n");
Run r = profileCmd(ProductProfile.AWS, "delete", "dev");
assertEquals(0, r.exit());
assertTrue(r.out().contains("dev.yaml and dev.yml"), r.out());
assertTrue(store().get("dev").isEmpty());
}
/** BL-003: same cause as a bad --profile at parse time, so the same exit code. */
@Test
void showExitsTwoWhenTheProfileIsGoneAfterTheParse() throws Exception {
writeProfile("gone", "container: floci-az-gone\n");
ConfigShowCommand show = new ConfigShowCommand(ProductProfile.AZ, store());
// A real parse: the provider reads the profile while it still exists.
new CommandLine(show)
.setDefaultValueProvider(new ProfileDefaultValueProvider(store()))
.parseArgs("--profile", "gone");
Files.delete(tempDir.resolve("gone.yaml"));
PrintStream err = System.err;
System.setErr(new PrintStream(new ByteArrayOutputStream()));
try {
assertEquals(CommandLine.ExitCode.USAGE, show.call());
} finally {
System.setErr(err);
}
}
}
@@ -103,6 +103,34 @@ class MissingProfileTest {
assertTrue(portErr.contains("port must be between 1 and 65535"), portErr);
}
/** BL-018: one cause, one exit code, whichever command validates the name. */
@Test
void anInvalidProfileNameExitsTwoFromConfigProfileAsFromProfile() {
int[] exit = new int[1];
String fromProfile = captureStderr(() -> cli().execute("start", "--profile", "../x"), exit);
assertEquals(2, exit[0], fromProfile);
for (String action : new String[]{"create", "show", "delete"}) {
String err = captureStderr(() -> cli().execute("config", "profile", action, "../x"), exit);
assertEquals(2, exit[0], action + ": " + err);
assertTrue(err.contains("Invalid profile name '../x'"), err);
}
}
/** BL-010: the message must name the file to open, which for a .yml profile is the .yml. */
@Test
void anInvalidYmlProfileNamesTheYmlFile() throws Exception {
Files.createDirectories(tempDir);
Files.writeString(tempDir.resolve("bad-port.yml"), "port: 99999\n");
int[] exit = new int[1];
String err = captureStderr(() -> cli().execute("start", "--profile", "bad-port"), exit);
assertEquals(2, exit[0]);
assertTrue(err.contains("bad-port.yml"), err);
assertFalse(err.contains("bad-port.yaml"), err);
}
@Test
void parsingRaisesTheDedicatedException() {
assertThrows(ProfileNotFoundException.class,
@@ -64,11 +64,11 @@ class ProfileStoreTest {
@Test
void missingProfileIsEmptyAndDeleteReportsIt() throws Exception {
assertTrue(store().get("absent").isEmpty());
assertFalse(store().delete("absent"));
assertTrue(store().delete("absent").isEmpty());
store().save(new Profile(ProductProfile.AWS, "here"));
assertTrue(store().delete("here"));
assertFalse(store().delete("here"));
assertFalse(store().delete("here").isEmpty());
assertTrue(store().delete("here").isEmpty());
}
@Test
@@ -76,7 +76,7 @@ class ProfileStoreTest {
Files.writeString(tempDir.resolve("staging.yml"), "container: floci-staging\n");
assertEquals("floci-staging", store().get("staging").orElseThrow().container);
assertTrue(store().delete("staging"));
assertFalse(store().delete("staging").isEmpty());
}
@Test
@@ -89,6 +89,43 @@ class ProfileStoreTest {
assertEquals("handwritten", profiles.get(0).name);
}
/** BL-011: deleting only the preferred spelling left the other one answering --profile. */
@Test
void deleteRemovesEverySpelling() throws Exception {
Files.createDirectories(tempDir);
Files.writeString(tempDir.resolve("dev.yaml"), "port: 4599\n");
Files.writeString(tempDir.resolve("dev.yml"), "port: 4600\n");
assertEquals(2, store().delete("dev").size());
assertTrue(store().get("dev").isEmpty());
}
@Test
void listSkipsFilesNoCommandCanAddress() throws Exception {
Files.createDirectories(tempDir);
Files.writeString(tempDir.resolve(".yaml"), "port: 4599\n");
Files.writeString(tempDir.resolve("ok.yaml"), "port: 4599\n");
assertEquals(List.of("ok"), store().list().stream().map(p -> p.name).toList());
}
@Test
void existingFileIsTheSpellingOnDisk() throws Exception {
Files.createDirectories(tempDir);
assertEquals(tempDir.resolve("dev.yaml").toAbsolutePath(), store().existingFile("dev").toAbsolutePath());
Files.writeString(tempDir.resolve("dev.yml"), "port: 4599\n");
assertEquals(tempDir.resolve("dev.yml").toAbsolutePath(), store().existingFile("dev").toAbsolutePath());
}
@Test
void getBackfillsTheNameAsListDoes() throws Exception {
Files.createDirectories(tempDir);
Files.writeString(tempDir.resolve("handwritten.yml"), "container: floci-hand\n");
assertEquals("handwritten", store().get("handwritten").orElseThrow().name);
}
@ParameterizedTest
@ValueSource(strings = {"ok", "ok-1", "ok_1", "ok.1", "OK"})
void acceptsOrdinaryNames(String name) {
@@ -112,7 +149,7 @@ class ProfileStoreTest {
Files.writeString(tempDir.resolve("team alpha.yaml"), "container: floci-team\n");
assertEquals("floci-team", store().get("team alpha").orElseThrow().container);
assertTrue(store().delete("team alpha"));
assertFalse(store().delete("team alpha").isEmpty());
assertTrue(store().get("team alpha").isEmpty());
}
@@ -138,7 +175,7 @@ class ProfileStoreTest {
assertEquals("floci-team", store().get("team\\alpha").orElseThrow().container);
assertEquals(tempDir.toAbsolutePath().normalize(),
store().profileFile("team\\alpha").getParent());
assertTrue(store().delete("team\\alpha"));
assertFalse(store().delete("team\\alpha").isEmpty());
}
@Test
@@ -160,4 +197,13 @@ class ProfileStoreTest {
store().profileFile(name).getParent(), name);
}
}
/** BL-002: the message names what the user typed, not the file it would have become. */
@Test
void errorsQuoteTheNameAsTyped() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> store().profileFile("..\u0000x"));
assertTrue(e.getMessage().contains("'..\u0000x'"), e.getMessage());
assertFalse(e.getMessage().contains(".yaml"), e.getMessage());
}
}
@@ -2,6 +2,7 @@ package io.floci.cli.unit;
import io.floci.cli.ProductProfile;
import io.floci.cli.commands.RestartCommand;
import io.floci.cli.config.ProfileDefaultValueProvider;
import io.floci.cli.config.ProfileStore;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -125,4 +126,39 @@ class RestartCommandTest {
System.setErr(err);
}
}
/**
* BL-003: a profile that disappears between the parse and the restart is the same user error
* as a bad --profile at parse time: exit 2, and nothing stopped.
*/
@Test
void aProfileGoneAfterTheParseExitsTwoBeforeStoppingAnything() throws Exception {
writeProfile("gone", "container: floci-az-never-started\n");
ProfileStore store = new ProfileStore(tempDir);
RestartCommand restart = new RestartCommand(ProductProfile.AZ, store);
// A real parse: the provider reads the profile while it still exists.
new CommandLine(restart)
.setDefaultValueProvider(new ProfileDefaultValueProvider(store))
.parseArgs("--profile", "gone");
Files.delete(tempDir.resolve("gone.yaml"));
PrintStream out = System.out;
PrintStream err = System.err;
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
System.setOut(new PrintStream(outBuf));
System.setErr(new PrintStream(errBuf));
int exit;
try {
exit = restart.call();
} finally {
System.setOut(out);
System.setErr(err);
}
assertEquals(CommandLine.ExitCode.USAGE, exit);
assertTrue(errBuf.toString().contains("Profile 'gone' not found"), errBuf.toString());
// Stop prints this before calling docker; its absence means nothing was stopped.
assertFalse(outBuf.toString().contains("Stopping"), outBuf.toString());
}
}