Skip to content

Commit

Permalink
Improve the test coverage to 91.45%
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Sep 6, 2016
1 parent 9a922cc commit aa48f91
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
29 changes: 16 additions & 13 deletions wvlet-config/src/main/scala/wvlet/config/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,14 @@ case class ConfigHolder(tpe: ObjectType, value: Any)

case class ConfigPaths(configPaths: Seq[String]) extends LogSupport {
info(s"Config file paths: [${configPaths.mkString(", ")}]")

def findConfigFile(name: String): String = {
configPaths
.map(p => new File(p, name))
.find(_.exists())
.map(_.getPath)
.getOrElse(throw new FileNotFoundException(s"${name} is not found"))
}
}

object Config extends LogSupport {
private def defaultConfigPath = Seq(
".", // current directory
sys.props.getOrElse("prog.home", "") // program home for wvlet-launcher
)
private def defaultConfigPath = cleanupConfigPaths(
Seq(
".", // current directory
sys.props.getOrElse("prog.home", "") // program home for wvlet-launcher
))

def apply(env: String, defaultEnv: String = "default", configPaths: Seq[String] = defaultConfigPath): Config = Config(ConfigEnv(env, defaultEnv, configPaths),
Map.empty[ObjectType, ConfigHolder])
Expand Down Expand Up @@ -124,6 +117,16 @@ case class Config private[config](env: ConfigEnv, holder: Map[ObjectType, Config
}
}

def getOrElse[ConfigType:ru.TypeTag](default: => ConfigType) : ConfigType = {
val t = ObjectType.ofTypeTag(implicitly[ru.TypeTag[ConfigType]])
find(t) match {
case Some(x) =>
x.asInstanceOf[ConfigType]
case None =>
default
}
}

def defaultValueOf[ConfigType: ru.TypeTag] : ConfigType = {
val tpe = ObjectType.ofTypeTag(implicitly[ru.TypeTag[ConfigType]])
getDefaultValueOf(tpe).asInstanceOf[ConfigType]
Expand Down Expand Up @@ -192,7 +195,7 @@ case class Config private[config](env: ConfigEnv, holder: Map[ObjectType, Config
def registerFromYamlOrElse[ConfigType: ru.TypeTag : ClassTag](yamlFile: String, defaultValue: => ConfigType): Config = {
val tpe = ObjectType.ofTypeTag(implicitly[ru.TypeTag[ConfigType]])
val config = loadFromYaml[ConfigType](yamlFile, onMissing = Some(defaultValue))
this + ConfigHolder(tpe, config)
this + ConfigHolder(tpe, config.get)
}

def overrideWithProperties(props: Properties, onUnusedProperties: Properties => Unit = REPORT_UNUSED_PROPERTIES): Config = {
Expand Down
49 changes: 49 additions & 0 deletions wvlet-config/src/test/scala/wvlet/config/ConfigTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,56 @@ class ConfigTest extends WvletSpec {
Config(env = env, configPaths = configPaths)
.registerFromYaml[SampleConfig]("myconfig.yml")

"ConfigEnv" should {
"set config paths" in {
val env = ConfigEnv("debug", "default", Seq.empty)
val newEnv = env.withConfigPaths(Seq("."))
newEnv.configPaths should contain (".")
}
}

"Config" should {
"use current directory for search path" in {
val c = Config(env = "debug")
c.env.configPaths should contain (".")
}

"customize env" in {
val config = Config(env = "staging", defaultEnv = "default")
val devConfig = config.withEnv("development", "test")
devConfig.env.env shouldBe "development"
devConfig.env.defaultEnv shouldBe "test"
}

"customize config paths" in {
val config = Config(env = "staging", defaultEnv = "default", configPaths = Seq.empty)
val newConfig = config.withConfigPaths(Seq("."))
newConfig.env.configPaths should contain (".")
}

"throw error on unknown config type" in {
val config = Config(env = "staging", defaultEnv = "default", configPaths = Seq.empty)
intercept[IllegalArgumentException] {
config.of[String]
}
}

"support getOrElse" in {
val config = Config(env = "staging", defaultEnv = "default", configPaths = Seq.empty)
.register[Int](10)
val s = config.getOrElse[String]("hello world")
s shouldBe "hello world"

config.getOrElse[Int](20) shouldBe 10
}

"support registerFromYamlOrElse" in {
val config = Config(env = "staging", defaultEnv = "default", configPaths = Seq.empty)
.registerFromYamlOrElse[String]("unknown-yaml-file.yml", "hello world")
val s = config.of[String]
s shouldBe "hello world"
}

"map yaml file into a case class" in {
val config = loadConfig("default")

Expand Down

0 comments on commit aa48f91

Please sign in to comment.