mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(profile): make profile names, files and exit codes consistent (#33)
This commit is contained in:
1 parent
bf9ee4037b
commit
5c0cc339bf
9 files changed
+263
-44
No files matched your search
@@ -2,6 +2,7 @@ package io.floci.cli.commands;
|
|||||||
|
|
||||||
import io.floci.cli.GlobalOptions;
|
import io.floci.cli.GlobalOptions;
|
||||||
import io.floci.cli.ProductProfile;
|
import io.floci.cli.ProductProfile;
|
||||||
|
import io.floci.cli.config.ProfileNotFoundException;
|
||||||
import io.floci.cli.config.ProfileStore;
|
import io.floci.cli.config.ProfileStore;
|
||||||
import io.floci.cli.output.Printer;
|
import io.floci.cli.output.Printer;
|
||||||
import picocli.CommandLine.*;
|
import picocli.CommandLine.*;
|
||||||
@@ -44,7 +45,15 @@ public class RestartCommand implements Callable<Integer> {
|
|||||||
// Resolve the profile BEFORE stopping anything. A failure here must not leave the
|
// Resolve the profile BEFORE stopping anything. A failure here must not leave the
|
||||||
// container stopped, and the values must not be re-read from a file that could change
|
// container stopped, and the values must not be re-read from a file that could change
|
||||||
// during the stop plus the one second wait below.
|
// during the stop plus the one second wait below.
|
||||||
StartCommand start = buildStartCommand();
|
StartCommand start;
|
||||||
|
try {
|
||||||
|
start = buildStartCommand();
|
||||||
|
} catch (ProfileNotFoundException e) {
|
||||||
|
// Gone or broken since the parse resolved it: nothing has been stopped yet, and the
|
||||||
|
// exit code matches a bad --profile at parse time.
|
||||||
|
printer.error(e.getMessage());
|
||||||
|
return ExitCode.USAGE;
|
||||||
|
}
|
||||||
String invalid = start.validationError();
|
String invalid = start.validationError();
|
||||||
if (invalid != null) {
|
if (invalid != null) {
|
||||||
printer.error(invalid);
|
printer.error(invalid);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import picocli.CommandLine.*;
|
|||||||
import picocli.CommandLine.Model.CommandSpec;
|
import picocli.CommandLine.Model.CommandSpec;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
@@ -78,16 +79,23 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
|||||||
public Integer call() {
|
public Integer call() {
|
||||||
Printer printer = global.printer();
|
Printer printer = global.printer();
|
||||||
|
|
||||||
return switch (action.toLowerCase()) {
|
try {
|
||||||
case "list" -> listProfiles(printer, store);
|
return switch (action.toLowerCase()) {
|
||||||
case "show" -> showProfile(printer, store);
|
case "list" -> listProfiles(printer, store);
|
||||||
case "create" -> createProfile(printer, store);
|
case "show" -> showProfile(printer, store);
|
||||||
case "delete" -> deleteProfile(printer, store);
|
case "create" -> createProfile(printer, store);
|
||||||
default -> {
|
case "delete" -> deleteProfile(printer, store);
|
||||||
printer.error("Unknown action '" + action + "'. Use: list, show, create, delete");
|
default -> {
|
||||||
yield 1;
|
printer.error("Unknown action '" + action + "'. Use: list, show, create, delete");
|
||||||
}
|
yield 1;
|
||||||
};
|
}
|
||||||
|
};
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// An invalid profile name: the same cause exits 2 from --profile, so it does here too.
|
||||||
|
// The message already ends with the next step.
|
||||||
|
printer.error(e.getMessage());
|
||||||
|
return ExitCode.USAGE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int listProfiles(Printer printer, ProfileStore store) {
|
private int listProfiles(Printer printer, ProfileStore store) {
|
||||||
@@ -159,7 +167,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
|||||||
if (name == null) { printer.error("Profile name required."); return 1; }
|
if (name == null) { printer.error("Profile name required."); return 1; }
|
||||||
try {
|
try {
|
||||||
if (store.get(name).isPresent()) {
|
if (store.get(name).isPresent()) {
|
||||||
printer.error("Profile '" + name + "' already exists. Delete it first or edit " + store.profileFile(name));
|
printer.error("Profile '" + name + "' already exists. Delete it first or edit " + store.existingFile(name));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (port != null && (port < 1 || port > 65535)) {
|
if (port != null && (port < 1 || port > 65535)) {
|
||||||
@@ -247,11 +255,15 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
|||||||
private int deleteProfile(Printer printer, ProfileStore store) {
|
private int deleteProfile(Printer printer, ProfileStore store) {
|
||||||
if (name == null) { printer.error("Profile name required."); return 1; }
|
if (name == null) { printer.error("Profile name required."); return 1; }
|
||||||
try {
|
try {
|
||||||
if (!store.delete(name)) {
|
List<Path> removed = store.delete(name);
|
||||||
printer.error("Profile '" + name + "' not found.");
|
if (removed.isEmpty()) {
|
||||||
|
printer.error("Profile '" + name + "' not found.\nRun '" + profile.commandPrefix() + " config profile list' to see available profiles.");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
printer.println(Ansi.green("Deleted") + " profile '" + name + "'");
|
String files = removed.size() > 1
|
||||||
|
? " (" + removed.get(0).getFileName() + " and " + removed.get(1).getFileName() + ")"
|
||||||
|
: "";
|
||||||
|
printer.println(Ansi.green("Deleted") + " profile '" + name + "'" + files);
|
||||||
return 0;
|
return 0;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
printer.error("Could not delete profile: " + e.getMessage());
|
printer.error("Could not delete profile: " + e.getMessage());
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import io.floci.cli.ProductProfile;
|
|||||||
import io.floci.cli.commands.StartCommand;
|
import io.floci.cli.commands.StartCommand;
|
||||||
import io.floci.cli.config.Profile;
|
import io.floci.cli.config.Profile;
|
||||||
import io.floci.cli.config.ProfileDefaultValueProvider;
|
import io.floci.cli.config.ProfileDefaultValueProvider;
|
||||||
|
import io.floci.cli.config.ProfileNotFoundException;
|
||||||
import io.floci.cli.config.ProfileStore;
|
import io.floci.cli.config.ProfileStore;
|
||||||
import io.floci.cli.output.Ansi;
|
import io.floci.cli.output.Ansi;
|
||||||
import io.floci.cli.output.OutputFormat;
|
import io.floci.cli.output.OutputFormat;
|
||||||
@@ -59,7 +60,14 @@ public class ConfigShowCommand implements Callable<Integer> {
|
|||||||
|
|
||||||
// image/port/persistDir/services have no global option, so they are read back from the
|
// image/port/persistDir/services have no global option, so they are read back from the
|
||||||
// profile itself — they only take effect on 'start'.
|
// profile itself — they only take effect on 'start'.
|
||||||
addStartSettings(data);
|
try {
|
||||||
|
addStartSettings(data);
|
||||||
|
} catch (ProfileNotFoundException e) {
|
||||||
|
// The profile went away (or broke) after the parse resolved it. Same cause, same exit
|
||||||
|
// code as a bad --profile at parse time.
|
||||||
|
printer.error(e.getMessage());
|
||||||
|
return ExitCode.USAGE;
|
||||||
|
}
|
||||||
|
|
||||||
if (printer.format() != OutputFormat.text) {
|
if (printer.format() != OutputFormat.text) {
|
||||||
printer.structured(data);
|
printer.structured(data);
|
||||||
|
|||||||
@@ -128,6 +128,6 @@ public class ProfileDefaultValueProvider implements IDefaultValueProvider {
|
|||||||
private ProfileNotFoundException invalid(CommandLine commandLine, String name, String detail) {
|
private ProfileNotFoundException invalid(CommandLine commandLine, String name, String detail) {
|
||||||
return new ProfileNotFoundException(commandLine,
|
return new ProfileNotFoundException(commandLine,
|
||||||
"Profile '" + name + "' is invalid: " + detail + "\n"
|
"Profile '" + name + "' is invalid: " + detail + "\n"
|
||||||
+ "Edit " + store.profileFile(name) + " and try again.");
|
+ "Edit " + store.existingFile(name) + " and try again.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,15 +64,11 @@ public class ProfileStore {
|
|||||||
List<Profile> profiles = new ArrayList<>();
|
List<Profile> profiles = new ArrayList<>();
|
||||||
try (var stream = Files.list(profilesDir)) {
|
try (var stream = Files.list(profilesDir)) {
|
||||||
stream.filter(p -> p.toString().endsWith(".yaml") || p.toString().endsWith(".yml"))
|
stream.filter(p -> p.toString().endsWith(".yaml") || p.toString().endsWith(".yml"))
|
||||||
|
// A file no command can address (".yaml" has an empty name) is not a profile.
|
||||||
|
.filter(p -> isUsableName(nameOf(p)))
|
||||||
.forEach(p -> {
|
.forEach(p -> {
|
||||||
try {
|
try {
|
||||||
Profile profile = YAML.readValue(p.toFile(), Profile.class);
|
profiles.add(read(p));
|
||||||
// Hand-written files often omit 'name:'; the file name is the name.
|
|
||||||
if (profile.name == null || profile.name.isBlank()) {
|
|
||||||
String file = p.getFileName().toString();
|
|
||||||
profile.name = file.substring(0, file.lastIndexOf('.'));
|
|
||||||
}
|
|
||||||
profiles.add(profile);
|
|
||||||
} catch (IOException ignored) {}
|
} catch (IOException ignored) {}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -80,9 +76,33 @@ public class ProfileStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Profile> get(String name) throws IOException {
|
public Optional<Profile> get(String name) throws IOException {
|
||||||
Path file = existingProfileFile(name);
|
Path file = existingFile(name);
|
||||||
if (!Files.exists(file)) return Optional.empty();
|
if (!Files.exists(file)) return Optional.empty();
|
||||||
return Optional.of(YAML.readValue(file.toFile(), Profile.class));
|
return Optional.of(read(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
// The one read path, so list and show agree on a profile's name.
|
||||||
|
private static Profile read(Path file) throws IOException {
|
||||||
|
Profile profile = YAML.readValue(file.toFile(), Profile.class);
|
||||||
|
// Hand-written files often omit 'name:'; the file name is the name.
|
||||||
|
if (profile.name == null || profile.name.isBlank()) {
|
||||||
|
profile.name = nameOf(file);
|
||||||
|
}
|
||||||
|
return profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isUsableName(String name) {
|
||||||
|
try {
|
||||||
|
validateName(name);
|
||||||
|
return true;
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String nameOf(Path file) {
|
||||||
|
String fileName = file.getFileName().toString();
|
||||||
|
return fileName.substring(0, fileName.lastIndexOf('.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(Profile profile) throws IOException {
|
public void save(Profile profile) throws IOException {
|
||||||
@@ -90,43 +110,59 @@ public class ProfileStore {
|
|||||||
YAML.writeValue(profileFile(profile.name).toFile(), profile);
|
YAML.writeValue(profileFile(profile.name).toFile(), profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean delete(String name) throws IOException {
|
/**
|
||||||
Path file = existingProfileFile(name);
|
* Deletes every spelling of {@code name}. Removing only the one reads prefer would leave the
|
||||||
return Files.deleteIfExists(file);
|
* other answering {@code --profile} after a delete that reported success.
|
||||||
|
*
|
||||||
|
* @return the files removed: 0 when the profile did not exist
|
||||||
|
*/
|
||||||
|
public List<Path> delete(String name) throws IOException {
|
||||||
|
List<Path> removed = new ArrayList<>();
|
||||||
|
for (Path file : List.of(profileFile(name), resolveInProfilesDir(validateName(name), ".yml"))) {
|
||||||
|
if (Files.deleteIfExists(file)) removed.add(file);
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Where {@link #save} writes {@code name}. Always {@code .yaml}. */
|
/** Where {@link #save} writes {@code name}. Always {@code .yaml}. */
|
||||||
public Path profileFile(String name) {
|
public Path profileFile(String name) {
|
||||||
return resolveInProfilesDir(validateName(name) + ".yaml");
|
return resolveInProfilesDir(validateName(name), ".yaml");
|
||||||
}
|
}
|
||||||
|
|
||||||
// The traversal guarantee: whatever the name looks like, the file it resolves to must sit
|
// The traversal guarantee: whatever the name looks like, the file it resolves to must sit
|
||||||
// directly in profilesDir. Also the place a name that is illegal on this platform but legal
|
// directly in profilesDir. Also the place a name that is illegal on this platform but legal
|
||||||
// on another (a colon on Windows) turns into a clean message instead of InvalidPathException.
|
// on another (a colon on Windows) turns into a clean message instead of InvalidPathException.
|
||||||
private Path resolveInProfilesDir(String fileName) {
|
// Messages quote the name as the user typed it, not the file name with its suffix.
|
||||||
|
private Path resolveInProfilesDir(String name, String suffix) {
|
||||||
Path dir = profilesDir.toAbsolutePath().normalize();
|
Path dir = profilesDir.toAbsolutePath().normalize();
|
||||||
Path resolved;
|
Path resolved;
|
||||||
try {
|
try {
|
||||||
resolved = dir.resolve(fileName).normalize();
|
resolved = dir.resolve(name + suffix).normalize();
|
||||||
} catch (InvalidPathException e) {
|
} catch (InvalidPathException e) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Invalid profile name '" + fileName + "': not a valid file name on this platform.\n"
|
"Invalid profile name '" + name + "': not a valid file name on this platform.\n"
|
||||||
+ "Run 'floci config profile list' to see available profiles.");
|
+ "Run 'floci config profile list' to see available profiles.");
|
||||||
}
|
}
|
||||||
if (!dir.equals(resolved.getParent())) {
|
if (!dir.equals(resolved.getParent())) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Invalid profile name '" + fileName + "': it would resolve outside " + profilesDir + ".\n"
|
"Invalid profile name '" + name + "': it would resolve outside " + profilesDir + ".\n"
|
||||||
+ "Run 'floci config profile list' to see available profiles.");
|
+ "Run 'floci config profile list' to see available profiles.");
|
||||||
}
|
}
|
||||||
return resolved;
|
return resolved;
|
||||||
}
|
}
|
||||||
|
|
||||||
// list() has always accepted .yml, so reads must too — otherwise a .yml profile shows up in
|
/**
|
||||||
// 'config profile list' and then reports "not found" when passed to --profile.
|
* The file {@code name} is read from: its {@code .yaml}, else its {@code .yml}, else the
|
||||||
private Path existingProfileFile(String name) {
|
* {@code .yaml} path {@link #save} would create. Messages that tell the user which file to
|
||||||
|
* edit name this one, not {@link #profileFile}.
|
||||||
|
*
|
||||||
|
* <p>list() has always accepted .yml, so reads must too — otherwise a .yml profile shows up in
|
||||||
|
* 'config profile list' and then reports "not found" when passed to --profile.
|
||||||
|
*/
|
||||||
|
public Path existingFile(String name) {
|
||||||
Path yaml = profileFile(name);
|
Path yaml = profileFile(name);
|
||||||
if (Files.exists(yaml)) return yaml;
|
if (Files.exists(yaml)) return yaml;
|
||||||
Path yml = resolveInProfilesDir(validateName(name) + ".yml");
|
Path yml = resolveInProfilesDir(validateName(name), ".yml");
|
||||||
return Files.exists(yml) ? yml : yaml;
|
return Files.exists(yml) ? yml : yaml;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -268,4 +268,48 @@ class ConfigCommandsTest {
|
|||||||
assertTrue(json.contains("\"container\" : \"floci-az-b\""), json);
|
assertTrue(json.contains("\"container\" : \"floci-az-b\""), json);
|
||||||
assertTrue(json.contains("\"port\" : 14577"), 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);
|
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
|
@Test
|
||||||
void parsingRaisesTheDedicatedException() {
|
void parsingRaisesTheDedicatedException() {
|
||||||
assertThrows(ProfileNotFoundException.class,
|
assertThrows(ProfileNotFoundException.class,
|
||||||
|
|||||||
@@ -64,11 +64,11 @@ class ProfileStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
void missingProfileIsEmptyAndDeleteReportsIt() throws Exception {
|
void missingProfileIsEmptyAndDeleteReportsIt() throws Exception {
|
||||||
assertTrue(store().get("absent").isEmpty());
|
assertTrue(store().get("absent").isEmpty());
|
||||||
assertFalse(store().delete("absent"));
|
assertTrue(store().delete("absent").isEmpty());
|
||||||
|
|
||||||
store().save(new Profile(ProductProfile.AWS, "here"));
|
store().save(new Profile(ProductProfile.AWS, "here"));
|
||||||
assertTrue(store().delete("here"));
|
assertFalse(store().delete("here").isEmpty());
|
||||||
assertFalse(store().delete("here"));
|
assertTrue(store().delete("here").isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -76,7 +76,7 @@ class ProfileStoreTest {
|
|||||||
Files.writeString(tempDir.resolve("staging.yml"), "container: floci-staging\n");
|
Files.writeString(tempDir.resolve("staging.yml"), "container: floci-staging\n");
|
||||||
|
|
||||||
assertEquals("floci-staging", store().get("staging").orElseThrow().container);
|
assertEquals("floci-staging", store().get("staging").orElseThrow().container);
|
||||||
assertTrue(store().delete("staging"));
|
assertFalse(store().delete("staging").isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -89,6 +89,43 @@ class ProfileStoreTest {
|
|||||||
assertEquals("handwritten", profiles.get(0).name);
|
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
|
@ParameterizedTest
|
||||||
@ValueSource(strings = {"ok", "ok-1", "ok_1", "ok.1", "OK"})
|
@ValueSource(strings = {"ok", "ok-1", "ok_1", "ok.1", "OK"})
|
||||||
void acceptsOrdinaryNames(String name) {
|
void acceptsOrdinaryNames(String name) {
|
||||||
@@ -112,7 +149,7 @@ class ProfileStoreTest {
|
|||||||
Files.writeString(tempDir.resolve("team alpha.yaml"), "container: floci-team\n");
|
Files.writeString(tempDir.resolve("team alpha.yaml"), "container: floci-team\n");
|
||||||
|
|
||||||
assertEquals("floci-team", store().get("team alpha").orElseThrow().container);
|
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());
|
assertTrue(store().get("team alpha").isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +175,7 @@ class ProfileStoreTest {
|
|||||||
assertEquals("floci-team", store().get("team\\alpha").orElseThrow().container);
|
assertEquals("floci-team", store().get("team\\alpha").orElseThrow().container);
|
||||||
assertEquals(tempDir.toAbsolutePath().normalize(),
|
assertEquals(tempDir.toAbsolutePath().normalize(),
|
||||||
store().profileFile("team\\alpha").getParent());
|
store().profileFile("team\\alpha").getParent());
|
||||||
assertTrue(store().delete("team\\alpha"));
|
assertFalse(store().delete("team\\alpha").isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -160,4 +197,13 @@ class ProfileStoreTest {
|
|||||||
store().profileFile(name).getParent(), name);
|
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.ProductProfile;
|
||||||
import io.floci.cli.commands.RestartCommand;
|
import io.floci.cli.commands.RestartCommand;
|
||||||
|
import io.floci.cli.config.ProfileDefaultValueProvider;
|
||||||
import io.floci.cli.config.ProfileStore;
|
import io.floci.cli.config.ProfileStore;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.io.TempDir;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
@@ -125,4 +126,39 @@ class RestartCommandTest {
|
|||||||
System.setErr(err);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user