davsclaus commented on code in PR #27995:
URL: https://github.com/apache/camel/pull/27995#discussion_r4301728922
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java:
##########
@@ -232,10 +236,42 @@ private int execute() throws Exception {
Files.createDirectories(parentPath);
}
}
+ if (existsAlready(targetPath)) {
Review Comment:
Partly: `--force` is an option of the same command, so `camel init --force`
in a terminal opens the picker and overwrites. But a user who picks a file that
exists found out only at the end of the picks, so the picker now asks there:
`File already exists: <file>, overwrite it? [y/N]: `, no by default (then exit
`1` with the same error). The picker takes its input as a parameter, so
`InitTest` drives it: `initPickerAsksBeforeOverwritingTheFile` and
`initPickerKeepsTheFileWhenTheAnswerIsNo`. In 650f38253c8c.
_Claude Code on behalf of davsclaus_
##########
dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/InitTest.java:
##########
@@ -196,6 +196,32 @@ void initPipe() throws Exception {
assertTrue(Files.readString(workingDir.resolve("my.yaml")).contains("kind:
Pipe"));
}
+ @Test
+ void initFailsWhenTheFileExists() throws Exception {
+ Path existing = Files.writeString(workingDir.resolve("my.camel.yaml"),
"- route: {}");
+ StringPrinter printer = new StringPrinter();
+ Init initCommand = new Init(new CamelJBangMain().withPrinter(printer));
+ CommandLine.populateCommand(initCommand, "my.camel.yaml", "--dir=" +
workingDir);
+
+ int exit = initCommand.doCall();
+
+ assertEquals(1, exit);
+ assertEquals("ERROR: File already exists: " + existing + " (use
--force to overwrite it)", printer.getStderr());
+ assertEquals("- route: {}", Files.readString(existing), "the file is
kept as it was");
+ }
+
+ @Test
+ void initForceOverwritesTheFile() throws Exception {
Review Comment:
Done: `initForceOverwritesTheFile` first runs the same command without
`--force` and checks the exit `1` and the kept file, then with `--force` and
checks the template, so the test itself shows that `--force` is what
overwrites. In 650f38253c8c.
_Claude Code on behalf of davsclaus_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]