Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,13 @@ built for Camel 4.24:
* A test sets the options of a command with `CommandLineTestHelper.parse(command, args...)` (in the test jar of
`camel-jbang-core`) instead of `CommandLine.populateCommand`.

=== camel-jbang - camel validate exits with 66 for a missing file or nothing to validate

`camel validate yaml` of a file that does not exist now exits with code `66` (file not found), as `validate source` and
`validate properties` do; before, it reported a validation error with code `1`. `camel validate source` and
`camel validate normalize` given no file they can handle (such as `notes.txt`) now exit with code `66`; before, they
reported success with code `0`.

=== camel-jbang - plugins declare which commands they contribute to

A CLI plugin can now declare which built-in commands it participates in beyond its own command, using the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.camel.dsl.jbang.core.commands.CamelCommand;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.commands.ai.SourceValidator;
import org.apache.camel.dsl.jbang.core.common.CliException;
import org.apache.camel.dsl.jbang.core.common.JsonOutput;
import org.apache.camel.dsl.yaml.validator.YamlValidator;

Expand Down Expand Up @@ -76,6 +77,10 @@ public Integer doCall() throws Exception {
reports.put(n, SourceValidator.validate(f.getName(), content, catalog, null,
parent != null ? parent.toPath() : null, schemaValidator));
}
if (reports.isEmpty()) {
// before, "Validation success (files:0)" with exit code 0
throw CliException.fileNotFound("No files to validate (YAML, XML, Java and properties files)");
}
int count = reports.values().stream().mapToInt(List::size).sum();
if (jsonOutput) {
JsonOutput.printList(printer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.commands.MavenResolverMixin;
import org.apache.camel.dsl.jbang.core.commands.Run;
import org.apache.camel.dsl.jbang.core.common.CliException;
import org.apache.camel.dsl.jbang.run.common.CamelJBangConstants;
import org.apache.camel.main.BaseMainSupport;
import org.apache.camel.main.KameletMain;
Expand Down Expand Up @@ -91,7 +92,8 @@ public Integer doCall() throws Exception {
}
}
if (matched.isEmpty()) {
return 0;
// before, exit code 0 with no output
throw CliException.fileNotFound("No YAML files to normalize");
}
// a Kamelet loads as a route template: its template is normalized and put back into the Kamelet file
Map<String, String> kamelets = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public Integer doCall() throws Exception {
Map<String, List<Error>> reports = new LinkedHashMap<>();
for (String n : files) {
if (matchFile(n)) {
if (!new File(n).isFile()) {
// as validate source and validate properties: before, a validation error with exit code 1
throw CliException.fileNotFound("File not found: " + n);
}
var report = new ArrayList<>(validator.validate(new File(n)));
if (camelCatalog != null && report.isEmpty()) {
// the schema is fine: check what the schema cannot, endpoint URIs and simple expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ void nothingToValidateIsNotFound() throws Exception {
assertThat(e.getMessage()).isEqualTo("No properties files to validate");
}

@Test
void theCommandsAgreeOnMissingFilesAndNothingToDo() throws Exception {
// validate yaml reported a validation error with exit code 1 for a missing file, validate source said
// "Validation success (files:0)" and validate normalize exited 0 for nothing to do (CAMEL-26933)
String missing = dir.resolve("nope.yaml").toString();
CliException e = assertThrows(CliException.class, () -> run(new YamlValidateCommand(main()), missing));
assertThat(e.getExitCode()).isEqualTo(CliExitCode.FILE_NOT_FOUND);
assertThat(e.getMessage()).isEqualTo("File not found: " + missing);

Path text = Files.writeString(dir.resolve("notes.txt"), "not a route");
e = assertThrows(CliException.class, () -> run(new SourceValidateCommand(main()), text.toString()));
assertThat(e.getExitCode()).isEqualTo(CliExitCode.FILE_NOT_FOUND);
assertThat(e.getMessage()).startsWith("No files to validate");

e = assertThrows(CliException.class, () -> run(new YamlNormalizeCommand(main()), text.toString()));
assertThat(e.getExitCode()).isEqualTo(CliExitCode.FILE_NOT_FOUND);
assertThat(e.getMessage()).isEqualTo("No YAML files to normalize");
}

private CamelJBangMain main() {
printer = new StringPrinter();
return new CamelJBangMain().withPrinter(printer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void quit(int exitCode) {
main.setOut(new PrintStreamPrinter(ps));

// Run the validate command
main.execute("validate", "yaml", "src/test/resources/cheese.yaml");
main.execute("validate", "yaml", "src/test/resources/bad.yaml");

Assertions.assertEquals(1, code.get());

Expand Down
Loading