mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
docs: add an exit-code table and generated --help snapshots (#63)
* docs: add an exit-code table and generated --help snapshots README gains the exit codes scripts can rely on, per command. docs/help holds the --help text of every command in every tree, and HelpSnapshotTest fails when the files and the commands drift apart. Signed-off-by: Hector Ventura <hectorvent@gmail.com> * docs: say which rejected values exit 1 and pin the documented exit codes with a test The table promised 2 for every unusable value, but three commands report a rejected value with 1. The wording is narrowed, the three are named, and ExitCodeTableTest pins the rows that need neither Docker nor a server. Signed-off-by: Hector Ventura <hectorvent@gmail.com> * test: keep the exit-code test off the developer's saved profiles The profile commands build their own store under the home directory, so the test now runs with a temporary home. Signed-off-by: Hector Ventura <hectorvent@gmail.com> --------- Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
de3ca7f171
commit
97eaa5e820
129 files changed
+2589
No files matched your search
@@ -0,0 +1,83 @@
|
||||
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 with 1.
|
||||
"1, update --version not-a-version",
|
||||
"1, config profile create demo --port 0",
|
||||
"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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
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 picocli.CommandLine;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
/**
|
||||
* BL-069: {@code docs/help/} holds the {@code --help} text of every command in every product
|
||||
* tree, and this test fails when the committed files and the code drift apart. A change to a
|
||||
* command, a flag or a description therefore shows up in review as a change to these files.
|
||||
*
|
||||
* <p>To regenerate them after an intended change:
|
||||
* {@code mvn test -Dtest=HelpSnapshotTest -Dfloci.help.update=true}
|
||||
*/
|
||||
class HelpSnapshotTest {
|
||||
|
||||
private static final Path DIR = Path.of("docs", "help");
|
||||
private static final String UPDATE = "floci.help.update";
|
||||
private static final String HOW_TO_UPDATE =
|
||||
"Run 'mvn test -Dtest=HelpSnapshotTest -D" + UPDATE + "=true' and commit docs/help.";
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
/** File name to help text, for the root command and everything under it. */
|
||||
private Map<String, String> generate() {
|
||||
CommandLine root = FlociCli.buildCommandLine(new ProfileStore(tempDir));
|
||||
// Fixed, so the files do not depend on the terminal the test happens to run in.
|
||||
root.setUsageHelpWidth(100);
|
||||
Map<String, String> pages = new TreeMap<>();
|
||||
collect(root, pages);
|
||||
return pages;
|
||||
}
|
||||
|
||||
private static void collect(CommandLine command, Map<String, String> pages) {
|
||||
String name = command.getCommandSpec().qualifiedName("-") + ".txt";
|
||||
String text = command.getUsageMessage(CommandLine.Help.Ansi.OFF).replace("\r\n", "\n");
|
||||
pages.put(name, text);
|
||||
// 'help' is picocli's own subcommand: the same text in every group, and not ours to pin.
|
||||
command.getSubcommands().forEach((subName, sub) -> {
|
||||
if (!"help".equals(subName)) collect(sub, pages);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCommittedHelpTextMatchesTheCommands() throws IOException {
|
||||
// The defaults shown in help come from FLOCI_* variables when they are set.
|
||||
assumeTrue(System.getenv().keySet().stream().noneMatch(key -> key.startsWith("FLOCI_")),
|
||||
"FLOCI_* variables change the defaults that help prints");
|
||||
Map<String, String> pages = generate();
|
||||
|
||||
if (Boolean.getBoolean(UPDATE)) {
|
||||
write(pages);
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> drift = new ArrayList<>();
|
||||
for (Map.Entry<String, String> page : pages.entrySet()) {
|
||||
Path file = DIR.resolve(page.getKey());
|
||||
if (!Files.isRegularFile(file)) {
|
||||
drift.add("missing: " + file);
|
||||
} else if (!Files.readString(file, StandardCharsets.UTF_8).replace("\r\n", "\n").equals(page.getValue())) {
|
||||
drift.add("out of date: " + file);
|
||||
}
|
||||
}
|
||||
for (String name : committed()) {
|
||||
if (!pages.containsKey(name)) drift.add("no such command any more: " + DIR.resolve(name));
|
||||
}
|
||||
assertEquals(List.of(), drift, "docs/help does not match the commands. " + HOW_TO_UPDATE);
|
||||
}
|
||||
|
||||
/** Every tree is there: a product group that lost a command would otherwise go unnoticed. */
|
||||
@Test
|
||||
void everyProductTreeHasItsPages() {
|
||||
Map<String, String> pages = generate();
|
||||
|
||||
for (String page : List.of("floci.txt", "floci-start.txt", "floci-aws-start.txt", "floci-gcp-start.txt",
|
||||
"floci-az-start.txt", "floci-oci-start.txt", "floci-config-profile.txt", "floci-update.txt")) {
|
||||
assertTrue(pages.containsKey(page), page + " in " + pages.keySet());
|
||||
}
|
||||
assertTrue(pages.get("floci-gcp-start.txt").startsWith("Usage: floci gcp start"), pages.get("floci-gcp-start.txt"));
|
||||
assertTrue(pages.keySet().stream().noneMatch(name -> name.endsWith("-help.txt")));
|
||||
}
|
||||
|
||||
private static void write(Map<String, String> pages) throws IOException {
|
||||
Files.createDirectories(DIR);
|
||||
for (String stale : committed()) {
|
||||
if (!pages.containsKey(stale)) Files.delete(DIR.resolve(stale));
|
||||
}
|
||||
for (Map.Entry<String, String> page : pages.entrySet()) {
|
||||
Files.writeString(DIR.resolve(page.getKey()), page.getValue(), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> committed() throws IOException {
|
||||
if (!Files.isDirectory(DIR)) return List.of();
|
||||
try (Stream<Path> files = Files.list(DIR)) {
|
||||
return files.map(p -> p.getFileName().toString()).filter(n -> n.endsWith(".txt")).sorted().toList();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user