Skip to content

Commit

Permalink
allow environment variable overrides for configuration. (#13)
Browse files Browse the repository at this point in the history
* allow environment variable overrides for configuration. avoid using deprecated scalatest traits

* additional doc
  • Loading branch information
tomnis committed Feb 15, 2019
1 parent faab3f7 commit 5fc93c3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/main/scala/com/workday/warp/common/PropertyEntry.scala
Expand Up @@ -7,6 +7,9 @@ package com.workday.warp.common
*/
case class PropertyEntry(propertyName: String, isRequired: Boolean, defaultValue: String) {

// used as a name for looking up a corresponding environment variable
val envVarName: String = this.propertyName.map(_.toUpper).replace(".", "_")

def this(propertyName: String, isRequired: Boolean) = this(propertyName, isRequired, None.orNull)
def this(propertyName: String) = this(propertyName, isRequired = false)

Expand Down
24 changes: 15 additions & 9 deletions src/main/scala/com/workday/warp/common/WarpPropertyManager.scala
Expand Up @@ -174,15 +174,16 @@ object WarpPropertyManager {


/**
* Computes a map containing the assigned values of each WarpProperty according to the following sources in order of
* decreasing precedence:
*
* 1: jvm system properties
* 2: properties from the warp configuration file (warp.properties)
* 3: default property values provided in the enum
*
* @return an immutable Map containing the values for each WarpProperty
*/
* Computes a map containing the assigned values of each WarpProperty according to the following sources in order of
* decreasing precedence:
*
* 1: environment variables
* 2: jvm system properties
* 3: properties from the warp configuration file (warp.properties)
* 4: default property values provided in the enum
*
* @return an immutable [[Map]] containing the values for each WarpProperty
*/
def overlayProperties: Map[String, String] = {
val properties: mutable.Map[String, String] = mutable.Map[String, String]()

Expand All @@ -206,6 +207,11 @@ object WarpPropertyManager {
this.systemProps.get(key) foreach { value =>
properties += (key -> value)
}

// if we find an environment variable override, add it to our map
sys.env.get(entry.envVarName) foreach { value =>
properties += (key -> value)
}
}

properties.toMap
Expand Down
Expand Up @@ -3,7 +3,7 @@ package com.workday.warp.common.spec
import com.workday.telemetron.spec.HasTelemetron
import com.workday.warp.common.utils.TryMatchers
import org.scalatest._
import org.scalatest.junit.{AssertionsForJUnit, JUnitSuite}
import org.scalatestplus.junit.{AssertionsForJUnit, JUnitSuite}

/**
* Base class for WARP framework tests written in scalatest.
Expand Down
Expand Up @@ -3,7 +3,7 @@ package com.workday.telemetron.junit
import com.workday.telemetron.annotation.{AfterOnce, BeforeOnce, Schedule}
import org.junit.{Rule, Test}
import org.scalatest.Matchers
import org.scalatest.junit.JUnitSuite
import org.scalatestplus.junit.JUnitSuite

/**
* Created by vignesh.kalidas on 2/8/17.
Expand Down
Expand Up @@ -15,5 +15,6 @@ class PropertyEntrySpec extends WarpJUnitSpec {
def constructor(): Unit = {
new PropertyEntry("com.workday.warp.foo").propertyName should be ("com.workday.warp.foo")
PropertyEntry("com.workday.warp.foo").propertyName should be ("com.workday.warp.foo")
PropertyEntry("com.workday.warp.foo").envVarName should be ("COM_WORKDAY_WARP_FOO")
}
}

0 comments on commit 5fc93c3

Please sign in to comment.