diff --git a/build.sc b/build.sc index bff2b3d9..0eeee910 100644 --- a/build.sc +++ b/build.sc @@ -126,6 +126,7 @@ object os extends Module { object jvm extends Cross[OsJvmModule](scalaVersions) trait OsJvmModule extends OsModule with MiMaChecks { object test extends ScalaTests with OsLibTestModule + object nohometest extends ScalaTests with OsLibTestModule } object native extends Cross[OsNativeModule](scalaVersions) @@ -134,6 +135,7 @@ object os extends Module { object test extends ScalaNativeTests with OsLibTestModule { def nativeLinkStubs = true } + object nohometest extends ScalaNativeTests with OsLibTestModule } object watch extends Module { diff --git a/os/nohometest/src/NoHomeTests.scala b/os/nohometest/src/NoHomeTests.scala new file mode 100644 index 00000000..d91c976c --- /dev/null +++ b/os/nohometest/src/NoHomeTests.scala @@ -0,0 +1,25 @@ +package test.os + +import utest._ + +object NoHomeTests extends TestSuite { + private lazy val isWindows = sys.props("os.name").toLowerCase().contains("windows") + + val tests = Tests { + test("pwd when home is not available") { + System.setProperty("user.home", "?") + val homeException = intercept[IllegalArgumentException] { os.home } + .getMessage() + + val expectedException = + if (isWindows) + "Illegal char at index 0: ?" + else + "requirement failed: ? is not an absolute path" + + assert(homeException == expectedException) + os.pwd + () + } + } +} diff --git a/os/src-jvm/package.scala b/os/src-jvm/package.scala index b0414bdf..e5053657 100644 --- a/os/src-jvm/package.scala +++ b/os/src-jvm/package.scala @@ -23,10 +23,17 @@ package object os { os.ResourcePath.resource(resRoot) } + // See https://github.com/com-lihaoyi/os-lib/pull/239 + // and https://github.com/lightbend/mima/issues/794 + // why the need the inner object to preserve binary compatibility + private object _home { + lazy val value = Path(System.getProperty("user.home")) + } + /** * The user's home directory */ - val home: Path = Path(System.getProperty("user.home")) + def home: Path = _home.value /** * The current working directory for this process. diff --git a/os/src-native/package.scala b/os/src-native/package.scala index dcaaa4e7..ea021c90 100644 --- a/os/src-native/package.scala +++ b/os/src-native/package.scala @@ -16,10 +16,17 @@ package object os { path } + // See https://github.com/com-lihaoyi/os-lib/pull/239 + // and https://github.com/lightbend/mima/issues/794 + // why the need the inner object to preserve binary compatibility + private object _home { + lazy val value = Path(System.getProperty("user.home")) + } + /** * The user's home directory */ - val home: Path = Path(System.getProperty("user.home")) + def home: Path = _home.value /** * The current working directory for this process.