Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/main/java/dev/jbang/source/RefTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public String toString() {
}

public static RefTarget create(String ref, Path dest, ResourceResolver siblingResolver) {
return new RefTarget(siblingResolver.resolve(ref), dest);
ResourceRef resolved = siblingResolver.resolve(ref);
if (resolved == null) {
resolved = ResourceRef.forUnresolvable(ref, "not resolvable from " + siblingResolver.description());
}
return new RefTarget(resolved, dest);
}

public static RefTarget create(ResourceRef ref, Path dest) {
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/dev/jbang/cli/TestInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,25 @@

}

@Test
void testInitWithMissingTemplateFile() throws IOException {
Path cwd = Util.getCwd();
Path out = cwd.resolve("result.java");

// Create template with reference to non-existent file
int addResult = JBang.getCommandLine()
.execute("template", "add", "-f", cwd.toString(), "--name=bad-template",
"{basename}/{basename}App.java=tpl/NonExistentFile.java");
assertThat(addResult, is(0));

Check failure on line 366 in src/test/java/dev/jbang/cli/TestInit.java

View workflow job for this annotation

GitHub Actions / ci-build / unit-test-jvm (ubuntu-latest)

TestInit.testInitWithMissingTemplateFile()

java.lang.AssertionError: Expected: is <0> but: was <2>

Check failure on line 366 in src/test/java/dev/jbang/cli/TestInit.java

View workflow job for this annotation

GitHub Actions / ci-build / unit-test-jvm (macos-latest)

TestInit.testInitWithMissingTemplateFile()

java.lang.AssertionError: Expected: is <0> but: was <2>

// Attempting to init with this template should fail gracefully with clear error
int result = JBang.getCommandLine()
.execute("init", "--template=bad-template", "test");

// Should fail with non-zero exit code
assertThat(result, not(0));
Comment on lines +369 to +373
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test comment says the init should fail “with clear error”, but the test only checks for a non-zero exit code. To ensure this change prevents the prior NPE regression, capture stderr/stdout and assert that it contains the new unresolvable-resource message (and/or does not contain the previous NullPointerException text). BaseTest already provides helpers like captureOutput(...)/checkedRun(...) that can be used here.

Suggested change
int result = JBang.getCommandLine()
.execute("init", "--template=bad-template", "test");
// Should fail with non-zero exit code
assertThat(result, not(0));
int[] result = new int[1];
String output = captureOutput(() -> result[0] = JBang.getCommandLine()
.execute("init", "--template=bad-template", "test"));
// Should fail with non-zero exit code
assertThat(result[0], not(0));
assertThat(output, containsString("unresolvable"));
assertThat(output, containsString("NonExistentFile.java"));
assertThat(output, not(containsString("NullPointerException")));

Copilot uses AI. Check for mistakes.
// Output file should not be created
assertThat(out.toFile().exists(), is(false));
Comment on lines +359 to +375
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test currently asserts result.java is not created, but jbang init --template=bad-template test won’t write to result.java anyway (it will generate files based on the template’s target pattern, likely under cwd/test/...). This means the “no output files are created” assertion is not actually validating the behavior. Consider asserting that the expected generated directory/file for the template target (eg cwd.resolve("test") or the specific {basename}/{basename}App.java output) does not exist after the failing init.

Copilot uses AI. Check for mistakes.
}

}
Loading