Skip to content

Commit

Permalink
fix: quarkusioGH-374 Default alias name when creating a new alias (qu…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrsblck authored and maxandersen committed Oct 10, 2020
1 parent 5fd1613 commit 5be01bc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/dev/jbang/cli/Alias.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.jbang.cli;

import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -29,6 +31,9 @@ protected Path getCatalog() {
if (global) {
return Settings.getAliasesFile();
} else {
if (Files.isDirectory(catalogFile)) {
return Paths.get(catalogFile.toString(), AliasUtil.JBANG_CATALOG_JSON);
}
return catalogFile;
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/dev/jbang/cli/TestAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;

import org.junit.Assert;
Expand All @@ -18,6 +19,8 @@

import dev.jbang.AliasUtil;

import picocli.CommandLine;

public class TestAlias {

static final String aliases = "{\n" +
Expand Down Expand Up @@ -84,6 +87,31 @@ void testReadFromFile() throws IOException {
assertThat(AliasUtil.getAlias(cwd, "one"), notNullValue());
}

@Test
void testAddWithDefaultCatalogFile() throws IOException {
TemporaryFolder tmp = new TemporaryFolder();
tmp.create();
Path tmpPath = tmp.getRoot().toPath();
assertThat(Files.isRegularFile(Paths.get(tmpPath.toString(), AliasUtil.JBANG_CATALOG_JSON)), is(false));
Jbang jbang = new Jbang();
new CommandLine(jbang).execute("alias", "add", "-f", tmpPath.toString(), "name", "scriptOrFile");
assertThat(Files.isRegularFile(Paths.get(tmp.getRoot().toPath().toString(), AliasUtil.JBANG_CATALOG_JSON)),
is(true));
AliasUtil.Alias name = AliasUtil.getAlias(tmpPath, "name");
assertThat(name.scriptRef, is("scriptOrFile"));
}

@Test
void testAddPreservesExistingCatalog() throws IOException {
final Path jbangPath = jbangTempDir.getRoot().toPath();
Jbang jbang = new Jbang();
new CommandLine(jbang).execute("alias", "add", "-f", jbangPath.toString(), "name", "scriptOrFile");
AliasUtil.Alias one = AliasUtil.getAlias(jbangPath, "one");
AliasUtil.Alias name = AliasUtil.getAlias(jbangPath, "name");
assertThat(one.scriptRef, is("http://dummy"));
assertThat(name.scriptRef, is("scriptOrFile"));
}

@Test
void testGetAliasNone() throws IOException {
AliasUtil.Alias alias = AliasUtil.getAlias(cwd, "dummy-alias!", null, null);
Expand Down

0 comments on commit 5be01bc

Please sign in to comment.