fix(env): show how to load the variables in the chosen shell and reject unknown shells (#46)

The closing hint of floci env (all four products) now uses source for fish and Invoke-Expression for PowerShell instead of always suggesting eval, and an unknown --shell exits 2 with the accepted values instead of silently printing bash syntax. zsh, sh and pwsh are accepted by name.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-09 22:17:21 -05:00
1 parent 030d468fd3
commit fbdc3ef713
7 files changed
+119 -15

No files matched your search

@@ -0,0 +1,43 @@
package io.floci.cli.unit;
import io.floci.cli.FlociCli;
import io.floci.cli.config.ProfileStore;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
/** BL-050: every env command refuses a shell it cannot write for, instead of printing bash. */
class EnvShellOptionTest {
@TempDir
Path tempDir;
@ParameterizedTest
@ValueSource(strings = {"aws", "gcp", "az", "oci"})
void anUnknownShellExitsTwo(String product) {
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 = FlociCli.buildCommandLine(new ProfileStore(tempDir)).execute(product, "env",
"--shell", "tcsh", "--endpoint", "http://localhost:1", "--container", "floci-env-test-none");
} finally {
System.setOut(out);
System.setErr(err);
}
assertEquals(2, exit);
assertTrue(errBuf.toString().contains("Unknown shell 'tcsh'"), errBuf.toString());
assertEquals("", outBuf.toString());
}
}
@@ -46,4 +46,23 @@ class ShellExportTest {
assertEquals("$env:K = 'it''s $x'",
ShellExport.formatExport("powershell", "K", "it's $x"));
}
/** BL-050: the hint says how to load the lines in the shell they were written for. */
@Test
void theLoadHintMatchesTheShell() {
assertEquals("eval \"$(floci env)\"", ShellExport.loadHint("bash", "floci env"));
assertEquals("eval \"$(floci gcp env)\"", ShellExport.loadHint("zsh", "floci gcp env"));
assertEquals("floci oci env --shell fish | source", ShellExport.loadHint("fish", "floci oci env"));
assertEquals("floci az env --shell powershell | Invoke-Expression",
ShellExport.loadHint("PowerShell", "floci az env"));
}
@Test
void onlyKnownShellsAreSupported() {
assertTrue(ShellExport.isSupported("bash"));
assertTrue(ShellExport.isSupported("Fish"));
assertTrue(ShellExport.isSupported("pwsh"));
assertFalse(ShellExport.isSupported("tcsh"));
assertFalse(ShellExport.isSupported(null));
}
}