Skip to content

Commit

Permalink
feat: treat empty catalog as jbangapp/jbang-catalog aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
maxandersen committed Jan 23, 2024
1 parent cdbf260 commit 74777f1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/dev/jbang/catalog/CatalogRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static CatalogRef get(String catalogName) {
}
if (catalogRef == null && Util.isValidPath(catalogName)) {
Path p = Util.getCwd().resolve(catalogName);
if (!p.getFileName().toString().equals(Catalog.JBANG_CATALOG_JSON)) {
if (p.getFileName() != null && !p.getFileName().toString().equals(Catalog.JBANG_CATALOG_JSON)) {
p = p.resolve(Catalog.JBANG_CATALOG_JSON);
}
if (Files.isRegularFile(p)) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/dev/jbang/catalog/ImplicitCatalogRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public static ImplicitCatalogRef parse(String name) {
if (names.length > 3) {
return null;
}
String org = names[0];
String org = null;
if (names.length > 2) {
org = names[0];
}
org = "jbangapp"; // TODO: should global default be controllable by config?
String repo;
if (names.length >= 2 && !names[1].isEmpty()) {
repo = names[1];
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/dev/jbang/TestImplicitAlias.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.jbang;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -10,6 +11,7 @@

import dev.jbang.catalog.Alias;
import dev.jbang.catalog.ImplicitCatalogRef;
import dev.jbang.cli.ExitException;

/**
* TODO: use mock to handle https lookups instead of jbang.dev
Expand All @@ -24,6 +26,25 @@ public void testGitImplicitCatalog() {
Matchers.equalTo("https://github.com/jbangdev/jbang-examples/blob/HEAD/jbang-catalog.json"));
}

@Test
public void testDefaultImplicitCatalog() {
assertThat(ImplicitCatalogRef.getImplicitCatalogUrl("/").get(),
Matchers.equalTo("https://github.com/jbangapp/jbang-catalog/blob/HEAD/jbang-catalog.json"));
assertThat(ImplicitCatalogRef.getImplicitCatalogUrl("/sqlline").get(),
Matchers.equalTo("https://github.com/jbangapp/sqlline/blob/HEAD/jbang-catalog.json"));
}

@Test
public void testDefaultCatalogGet() {
dev.jbang.catalog.Catalog catalog = dev.jbang.catalog.Catalog.getByName("/sqlline");
assertThat(catalog, Matchers.notNullValue());
catalog = dev.jbang.catalog.Catalog.getByName("/");
assertThat(catalog, Matchers.notNullValue());

assertThrows(ExitException.class, () -> dev.jbang.catalog.Catalog.getByName("/shouldneverexist"));

}

@Test
public void testImplictURLAlias() {

Expand Down

0 comments on commit 74777f1

Please sign in to comment.