feat(config): let profile edit open a profile that no longer parses

edit asked the store to parse the profile before opening it, so the one profile that needed repairing could not be opened. A file holding YAML's null is now reported as a profile without settings instead of failing with an exception.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-10-11 09:09:08 -05:00
1 parent 346ff62408
commit a460cbd7d7
3 files changed
+42 -10

No files matched your search

@@ -12,6 +12,7 @@ import picocli.CommandLine.*;
import picocli.CommandLine.Model.CommandSpec;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
@@ -187,16 +188,12 @@ public class ConfigProfileCommand implements Callable<Integer> {
private int editProfile(Printer printer, ProfileStore store) {
if (name == null) { printer.error(nameRequired()); return 1; }
String prefix = this.profile.commandPrefix() + " config profile ";
Path file;
try {
if (store.get(name).isEmpty()) {
printer.error("Profile '" + name + "' not found.\nRun '" + prefix + "create " + name + "' to create it first.");
return 1;
}
file = store.existingFile(name);
} catch (IOException e) {
// Unreadable as it stands: opening it is exactly how the user fixes that.
file = store.existingFile(name);
// Asked of the file, not of its contents: a profile that no longer parses is exactly the
// one that has to be opened to be repaired.
Path file = store.existingFile(name);
if (!Files.exists(file)) {
printer.error("Profile '" + name + "' not found.\nRun '" + prefix + "create " + name + "' to create it first.");
return 1;
}
List<String> editor = editorCommand(env.apply("VISUAL"), env.apply("EDITOR"));
@@ -98,6 +98,10 @@ public class ProfileStore {
// 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);
// 'null' and '~' are valid YAML for "nothing": a document, but not a profile.
if (profile == null) {
throw new IOException(file + " holds no settings: it has to be a mapping, like 'port: 4566'");
}
// Hand-written files often omit 'name:'; the file name is the name.
if (profile.name == null || profile.name.isBlank()) {
profile.name = nameOf(file);
@@ -252,6 +252,37 @@ class ErgonomicsTest {
assertTrue(r.err().contains("floci config profile edit dev"), r.err());
}
/** A profile that does not parse, or holds YAML's "nothing", can still be opened and repaired. */
@ParameterizedTest
@ValueSource(strings = {"port: [unterminated\n", "null\n", "~\n", "- a list\n"})
@DisabledOnOs(OS.WINDOWS)
void aBrokenProfileCanBeOpenedAndRepaired(String broken) throws Exception {
Path file = Files.createDirectories(tempDir.resolve("profiles")).resolve("dev.yaml");
Files.writeString(file, broken);
Path editor = editorThat("echo 'port: 14566' > \"$1\"");
Run r = edit(Map.of("EDITOR", editor.toString()), "dev");
assertEquals(0, r.exit(), r.err());
assertEquals(14566, store().get("dev").orElseThrow().port);
}
/** Saving YAML's "nothing" is reported like any other file that is no longer a profile. */
@ParameterizedTest
@ValueSource(strings = {"null", "~"})
@DisabledOnOs(OS.WINDOWS)
void anEditThatLeavesNothingIsReported(String nothing) throws Exception {
store().save(new Profile(ProductProfile.AWS, "dev"));
Path editor = editorThat("echo '" + nothing + "' > \"$1\"");
Run r = edit(Map.of("EDITOR", editor.toString()), "dev");
assertEquals(1, r.exit());
assertTrue(r.err().contains("Profile 'dev' is no longer valid"), r.err());
assertTrue(r.err().contains("holds no settings"), r.err());
assertFalse(r.err().contains("Exception"), r.err());
}
@Test
@DisabledOnOs(OS.WINDOWS)
void anEditorThatFailsIsReported() throws Exception {