Merge pull request #129 from typesafehub/wip/havocp-config-load-nonexistent

Require config.file/resource/url to exist regardless of parse options
This commit is contained in:
Havoc Pennington 2014-01-06 06:59:10 -08:00
commit 172aa72ea3
2 changed files with 47 additions and 18 deletions

View File

@ -219,21 +219,22 @@ public final class ConfigFactory {
+ "', config.url='" + url + "', config.resource='" + resource
+ "'; don't know which one to use!");
} else {
// the override file/url/resource MUST be present or it's an error
ConfigParseOptions overrideOptions = parseOptions.setAllowMissing(false);
if (resource != null) {
if (resource.startsWith("/"))
resource = resource.substring(1);
// this deliberately does not parseResourcesAnySyntax; if
// people want that they can use an include statement.
return load(loader, parseResources(loader, resource, parseOptions), resolveOptions);
Config parsedResources = parseResources(loader, resource, overrideOptions);
return load(loader, parsedResources, resolveOptions);
} else if (file != null) {
return load(
loader,
parseFile(new File(file), parseOptions), resolveOptions);
Config parsedFile = parseFile(new File(file), overrideOptions);
return load(loader, parsedFile, resolveOptions);
} else {
try {
return load(
loader,
parseURL(new URL(url), parseOptions), resolveOptions);
Config parsedURL = parseURL(new URL(url), overrideOptions);
return load(loader, parsedURL, resolveOptions);
} catch (MalformedURLException e) {
throw new ConfigException.Generic("Bad URL in config.url system property: '"
+ url + "': " + e.getMessage(), e);

View File

@ -828,20 +828,48 @@ class PublicApiTest extends TestUtils {
}
@Test
def missingConfFails() {
def missingOverrideResourceFails() {
assertEquals("config.file is not set", null, System.getProperty("config.file"))
val old = System.getProperty("config.resource")
System.setProperty("config.resource", "donotexists.conf")
intercept[ConfigException.IO] {
ConfigFactory.load(ConfigParseOptions.defaults().setAllowMissing(false))
try {
System.setProperty("config.resource", "donotexists.conf")
intercept[ConfigException.IO] {
ConfigFactory.load()
}
} finally {
// cleanup properties
Option(old).map { v =>
System.setProperty("config.resource", v)
v
}.orElse {
System.clearProperty("config.resource")
None
}
assertEquals("config.resource restored", old, System.getProperty("config.resource"))
ConfigImpl.reloadSystemPropertiesConfig()
}
}
// cleanup properties
Option(old).map { v =>
System.setProperty("config.resource", v)
v
}.orElse {
System.clearProperty("config.resource")
None
@Test
def missingOverrideFileFails() {
assertEquals("config.resource is not set", null, System.getProperty("config.resource"))
val old = System.getProperty("config.file")
try {
System.setProperty("config.file", "donotexists.conf")
intercept[ConfigException.IO] {
ConfigFactory.load()
}
} finally {
// cleanup properties
Option(old).map { v =>
System.setProperty("config.file", v)
v
}.orElse {
System.clearProperty("config.file")
None
}
assertEquals("config.file restored", old, System.getProperty("config.file"))
ConfigImpl.reloadSystemPropertiesConfig()
}
}