Skip to content

Commit

Permalink
Use name in env exception
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Dec 7, 2019
1 parent 8573092 commit 2d3d3d9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/library/scala/sys/package.scala
Expand Up @@ -59,10 +59,17 @@ package object sys {
// def prop(p: String) = Option(System.getProperty(p))

/** An immutable Map representing the current system environment.
*
* If lookup fails, use `System.getenv(_)` for case-insensitive lookup
* on a certain platform. If that also fails, throw `NoSuchElementException`.
*
* @return a Map containing the system environment variables.
*/
def env: Map[String, String] = Map.from(System.getenv().asScala).withDefault(v => Option(System.getenv(v)).getOrElse(throw new NoSuchElementException))
def env: Map[String, String] = Map.from(System.getenv().asScala).withDefault { v =>
val s = System.getenv(v)
if (s == null) throw new NoSuchElementException(v)
s
}

/** Register a shutdown hook to be run when the VM exits.
* The hook is automatically registered: the returned value can be ignored,
Expand Down
2 changes: 1 addition & 1 deletion test/junit/scala/sys/env.scala
Expand Up @@ -14,5 +14,5 @@ class EnvTest {
if (isWin) assertTrue(sys.env("path") != null)
}
@Test def `env throws if not found`(): Unit =
assertThrows[NoSuchElementException](sys.env("junk"))
assertThrows[NoSuchElementException](sys.env("junk"), _ == "junk")
}

0 comments on commit 2d3d3d9

Please sign in to comment.