mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-23 22:11:37 -05:00
fix(config): write profiles and config atomically and report files that cannot be read
Profile files and ~/.floci/config.yaml are replaced through a temporary file, so an interrupted write leaves the old file intact; an unknown key in config.yaml no longer resets the default product to aws, and an unreadable config.yaml or profile file is reported on stderr instead of being ignored.
This commit is contained in:
5 files changed
+161
-5
No files matched your search
@@ -0,0 +1,73 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.config.GlobalConfigStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class GlobalConfigStoreTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private GlobalConfigStore store() {
|
||||
return new GlobalConfigStore(tempDir.resolve("config.yaml"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultsToAwsWithoutAFile() {
|
||||
assertEquals("aws", store().getDefaultProduct());
|
||||
}
|
||||
|
||||
@Test
|
||||
void remembersTheProductItWasGiven() throws Exception {
|
||||
store().setDefaultProduct("gcp");
|
||||
|
||||
assertEquals("gcp", store().getDefaultProduct());
|
||||
}
|
||||
|
||||
/** BL-042: the write goes through a temporary file, and none is left behind. */
|
||||
@Test
|
||||
void writesLeaveNoTemporaryFiles() throws Exception {
|
||||
store().setDefaultProduct("oci");
|
||||
store().setDefaultProduct("az");
|
||||
|
||||
try (var files = Files.list(tempDir)) {
|
||||
assertEquals(List.of("config.yaml"), files.map(p -> p.getFileName().toString()).toList());
|
||||
}
|
||||
}
|
||||
|
||||
/** BL-043: a key this version does not know must not reset the default product. */
|
||||
@Test
|
||||
void anUnknownKeyKeepsTheProduct() throws Exception {
|
||||
Files.writeString(tempDir.resolve("config.yaml"), "default-product: az\nsome-future-key: true\n");
|
||||
|
||||
assertEquals("az", store().getDefaultProduct());
|
||||
}
|
||||
|
||||
/** BL-043: an unreadable file still falls back to aws, but says so. */
|
||||
@Test
|
||||
void anUnreadableFileWarnsAndFallsBackToAws() throws Exception {
|
||||
Files.writeString(tempDir.resolve("config.yaml"), "default-product: [unterminated\n");
|
||||
PrintStream err = System.err;
|
||||
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
|
||||
System.setErr(new PrintStream(errBuf));
|
||||
String product;
|
||||
try {
|
||||
product = store().getDefaultProduct();
|
||||
} finally {
|
||||
System.setErr(err);
|
||||
}
|
||||
|
||||
assertEquals("aws", product);
|
||||
assertTrue(errBuf.toString().contains("Could not read"), errBuf.toString());
|
||||
assertTrue(errBuf.toString().contains("floci config default-product"), errBuf.toString());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user