Files
floci-cli/src/test/java/io/floci/cli/unit/ExitCodeTableTest.java
T
Hector Ventura 8ae0a898ed fix(cli): exit 2 for every rejected value, not 1 in some commands
config default-product, config profile (unknown action, missing name, --port out of range), update --version, an invalid snapshot name, an invalid az setup container name and an unusable --persist path returned 1, the code for a command that ran and failed. They now return 2 like every other rejected value. A persist directory that cannot be created is still 1.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
2026-10-11 09:52:50 -05:00

98 lines
3.9 KiB
Java

package io.floci.cli.unit;
import io.floci.cli.FlociCli;
import io.floci.cli.config.ProfileStore;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* BL-067: the rows of README's "Exit Codes" section that need neither Docker nor a server. They
* are the ones a reader is most likely to get wrong: which rejected values are 2 and which are 1.
*/
class ExitCodeTableTest {
@TempDir
Path tempDir;
@ParameterizedTest
@CsvSource({
// The command line could not be used.
"2, status --no-such-flag",
"2, no-such-command",
"2, status --profile no-such-profile",
"2, start --port abc",
"2, wait --timeout abc",
"2, wait --timeout 0s",
"2, start --port 70000",
"2, start --pull sometimes",
"2, doctor --check no.such.check",
"2, env --shell tcsh",
"2, completion fish",
"2, status --json --no-such-flag",
"2, config profile show ../escape",
// Accepted by the command line, rejected by the command: the same code.
"2, update --version not-a-version",
"2, update --check --version not-a-version",
"2, config profile create demo --port 0",
"2, config profile create demo --port 70000",
"2, config profile bogus",
"2, config profile show",
"2, config profile create",
"2, config profile edit",
"2, config profile delete",
"2, config default-product nope",
"2, snapshot save a/b",
"2, snapshot load ..",
"2, snapshot delete a/b",
"2, gcp snapshot save a/b",
"2, az setup --container ../escape",
// It ran and the answer is no.
"1, config profile show no-such-profile",
"1, config profile edit no-such-profile",
"1, config validate -f no-such-compose-file.yml",
// Nothing wrong.
"0, --version",
"0, config profile list"})
void theDocumentedCode(int expected, String commandLine) {
PrintStream out = System.out;
PrintStream err = System.err;
// Commands that manage profiles build their own store under the home directory. It is
// pointed at the temporary one, so the developer's saved profiles cannot change a result.
String home = System.getProperty("user.home");
System.setProperty("user.home", tempDir.toString());
System.setOut(new PrintStream(new ByteArrayOutputStream()));
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
System.setErr(new PrintStream(errBuf));
int exit;
try {
ProfileStore store = new ProfileStore(tempDir.resolve(".floci").resolve("profiles"));
exit = FlociCli.buildCommandLine(store).execute(commandLine.split(" "));
} finally {
System.setOut(out);
System.setErr(err);
System.setProperty("user.home", home);
}
assertEquals(expected, exit, commandLine + "\n" + errBuf);
}
/** The guard on the test itself: a profile saved in the real home directory is not visible here. */
@Test
void profileCommandsSeeOnlyTheTemporaryHome() throws Exception {
Path profiles = Files.createDirectories(tempDir.resolve(".floci").resolve("profiles"));
Files.writeString(profiles.resolve("only-here.yaml"), "port: 4599\n");
theDocumentedCode(0, "config profile show only-here");
theDocumentedCode(1, "config profile show not-here");
}
}