Skip to content

Commit

Permalink
fix: no longer throws NPE for config list (#1276)
Browse files Browse the repository at this point in the history
The `Configuration` wasn't storing the originating `ResourceRef` in the
case of single file configurations (which is what `--global` is).
To fix this the code has been made more similar to `Catalog`.

Fixes #1275
  • Loading branch information
quintesse authored Mar 7, 2022
1 parent 238ef90 commit 4ffc15f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/main/java/dev/jbang/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public static Configuration instance() {
if (cfgFileName != null) {
Path cfgFile = Util.getCwd().resolve(cfgFileName);
if (Files.isReadable(cfgFile)) {
global = read(cfgFile);
global = get(cfgFile);
} else {
global = defaults();
}
Expand Down Expand Up @@ -220,6 +220,17 @@ public static Configuration defaults() {
return defaults;
}

public static Configuration get(Path catalogPath) {
return get(ResourceRef.forNamedFile(catalogPath.toString(), catalogPath.toFile()));
}

private static Configuration get(ResourceRef ref) {
Path configPath = ref.getFile().toPath();
Configuration cfg = read(configPath);
cfg.storeRef = ref;
return cfg;
}

/**
* Returns a Config containing all the settings from local config files merged
* into one. This follows the system where settings that are "nearest" have
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/jbang/cli/BaseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class BaseCommand implements Callable<Integer> {
@CommandLine.Option(names = { "--config" }, description = "Path to config file to be used instead of the default")
void setConfig(Path config) {
if (Files.isReadable(config)) {
Configuration.instance(Configuration.read(config));
Configuration.instance(Configuration.get(config));
} else {
warn("Configuration file does not exist or could not be read: " + config);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/jbang/cli/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class BaseConfigCommand extends BaseCommand {
protected Configuration getConfig(Path cwd, boolean strict) {
Path cfgFile = getConfigFile(strict);
if (cfgFile != null) {
return Configuration.read(cfgFile);
return Configuration.get(cfgFile);
} else {
return Configuration.getMerged();
}
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/dev/jbang/TestConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package dev.jbang;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.*;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -16,6 +14,7 @@
import dev.jbang.cli.Init;
import dev.jbang.cli.JBang;
import dev.jbang.cli.Run;
import dev.jbang.source.ResourceRef;
import dev.jbang.util.Util;

import picocli.CommandLine;
Expand Down Expand Up @@ -82,6 +81,16 @@ public void testWriteReadConfig() throws IOException {
assertThat(cfg2.get("baz"), equalTo("jkl"));
}

@Test
public void testReadAndGetConfig() throws IOException {
Path cfgFile = jbangTempDir.resolve(Configuration.JBANG_CONFIG_PROPS);
Configuration cfgRead = Configuration.read(cfgFile);
assertThat(cfgRead.getStoreRef(), nullValue());
Configuration cfgGet = Configuration.get(cfgFile);
assertThat(cfgGet.getStoreRef(), notNullValue());
assertThat(cfgGet.getStoreRef(), equalTo(ResourceRef.forResource(cfgFile.toString())));
}

@Test
public void testDefaults() {
Configuration cfg = Configuration.defaults();
Expand Down

0 comments on commit 4ffc15f

Please sign in to comment.