From 2f3d63848601912a56980892a3a0cd242fab2107 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sat, 7 Sep 2024 11:11:49 +0800 Subject: [PATCH 1/5] . --- Readme.adoc | 4 ++-- os/src-jvm/package.scala | 5 ++++- os/src-native/package.scala | 5 ++++- os/test/src/PathTests.scala | 8 ++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Readme.adoc b/Readme.adoc index ac5b7dca..4d12f8f2 100644 --- a/Readme.adoc +++ b/Readme.adoc @@ -1815,8 +1815,8 @@ wd / os.up wd / os.up / os.up ---- -Note that there are no in-built operations to change the `os.pwd`. In general, -you should not need to: simply defining a new path, e.g. +`os.pwd` can be modified in certain scopes via the `os.pwd0` dynamic variable, but +best practice is not to change it. Instead simply define a new path, e.g. [source,scala] ---- diff --git a/os/src-jvm/package.scala b/os/src-jvm/package.scala index e5053657..7877f910 100644 --- a/os/src-jvm/package.scala +++ b/os/src-jvm/package.scala @@ -2,6 +2,7 @@ import scala.language.implicitConversions import java.nio.file.FileSystem import java.nio.file.FileSystems import java.nio.file.Paths +import scala.util.DynamicVariable package object os { type Generator[+T] = geny.Generator[T] @@ -38,7 +39,9 @@ package object os { /** * The current working directory for this process. */ - val pwd: Path = os.Path(java.nio.file.Paths.get(".").toAbsolutePath) + val pwd: Path = pwd0.value + val pwd0: DynamicVariable[Path] = + DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath)) val up: RelPath = RelPath.up diff --git a/os/src-native/package.scala b/os/src-native/package.scala index ea021c90..f0fdff87 100644 --- a/os/src-native/package.scala +++ b/os/src-native/package.scala @@ -1,5 +1,6 @@ import java.nio.file.FileSystem import java.nio.file.FileSystems +import scala.util.DynamicVariable package object os { type Generator[+T] = geny.Generator[T] val Generator = geny.Generator @@ -31,7 +32,9 @@ package object os { /** * The current working directory for this process. */ - val pwd: Path = os.Path(java.nio.file.Paths.get(".").toAbsolutePath) + val pwd: Path = pwd0.value + val pwd0: DynamicVariable[Path] = + DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath)) val up: RelPath = RelPath.up diff --git a/os/test/src/PathTests.scala b/os/test/src/PathTests.scala index 98aa6ae4..67f005d2 100644 --- a/os/test/src/PathTests.scala +++ b/os/test/src/PathTests.scala @@ -434,6 +434,14 @@ object PathTests extends TestSuite { System.err.printf("p[%s]\n", posix(p)) assert(posix(p) contains "/omg") } + test("pwd0") { + val x = os.pwd + val y = os.pwd0.withValue(os.pwd / "hello") { + os.pwd + } + + assert(x / "hello" == y) + } } // compare absolute paths def sameFile(a: java.nio.file.Path, b: java.nio.file.Path): Boolean = { From 97a675d4439d44b9cf91242db0b649e2c40cb70e Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sat, 7 Sep 2024 11:28:17 +0800 Subject: [PATCH 2/5] . --- os/src-jvm/package.scala | 4 ++-- os/src-native/package.scala | 4 ++-- os/test/src/SubprocessTests.scala | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/os/src-jvm/package.scala b/os/src-jvm/package.scala index 7877f910..0dc7623b 100644 --- a/os/src-jvm/package.scala +++ b/os/src-jvm/package.scala @@ -39,9 +39,9 @@ package object os { /** * The current working directory for this process. */ - val pwd: Path = pwd0.value + def pwd: Path = pwd0.value val pwd0: DynamicVariable[Path] = - DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath)) + new DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath)) val up: RelPath = RelPath.up diff --git a/os/src-native/package.scala b/os/src-native/package.scala index f0fdff87..89057268 100644 --- a/os/src-native/package.scala +++ b/os/src-native/package.scala @@ -32,9 +32,9 @@ package object os { /** * The current working directory for this process. */ - val pwd: Path = pwd0.value + def pwd: Path = pwd0.value val pwd0: DynamicVariable[Path] = - DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath)) + new DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath)) val up: RelPath = RelPath.up diff --git a/os/test/src/SubprocessTests.scala b/os/test/src/SubprocessTests.scala index 16eefe93..87f083d2 100644 --- a/os/test/src/SubprocessTests.scala +++ b/os/test/src/SubprocessTests.scala @@ -203,5 +203,21 @@ object SubprocessTests extends TestSuite { assert(output.out.lines() == Seq("HELLO /usr")) } } + test("pwd0") { + + val outsidePwd = os.pwd + val tmp0 = os.temp.dir() + val tmp = os.followLink(tmp0).getOrElse(tmp0) + val x = proc("bash", "-c", "pwd").call() + val y = os.pwd0.withValue(tmp) { + proc("bash", "-c", "pwd").call() + } + + val z = proc("bash", "-c", "pwd").call() + assert(outsidePwd.toString != tmp.toString) + assert(x.out.trim() == outsidePwd.toString) + assert(y.out.trim() == tmp.toString) + assert(z.out.trim() == outsidePwd.toString) + } } } From f36011b544640da18099cefec550eb3d6b610b2b Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sat, 7 Sep 2024 11:51:18 +0800 Subject: [PATCH 3/5] . --- os/test/src/SubprocessTests.scala | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/os/test/src/SubprocessTests.scala b/os/test/src/SubprocessTests.scala index 87f083d2..af3524cd 100644 --- a/os/test/src/SubprocessTests.scala +++ b/os/test/src/SubprocessTests.scala @@ -204,20 +204,23 @@ object SubprocessTests extends TestSuite { } } test("pwd0") { + // Windows doesnt have bash installed so a bit inconvenient + // to run these subprocesses for testing + if (!scala.util.Properties.isWin) { + val outsidePwd = os.pwd + val tmp0 = os.temp.dir() + val tmp = os.followLink(tmp0).getOrElse(tmp0) + val x = proc("bash", "-c", "pwd").call() + val y = os.pwd0.withValue(tmp) { + proc("bash", "-c", "pwd").call() + } - val outsidePwd = os.pwd - val tmp0 = os.temp.dir() - val tmp = os.followLink(tmp0).getOrElse(tmp0) - val x = proc("bash", "-c", "pwd").call() - val y = os.pwd0.withValue(tmp) { - proc("bash", "-c", "pwd").call() + val z = proc("bash", "-c", "pwd").call() + assert(outsidePwd.toString != tmp.toString) + assert(x.out.trim() == outsidePwd.toString) + assert(y.out.trim() == tmp.toString) + assert(z.out.trim() == outsidePwd.toString) } - - val z = proc("bash", "-c", "pwd").call() - assert(outsidePwd.toString != tmp.toString) - assert(x.out.trim() == outsidePwd.toString) - assert(y.out.trim() == tmp.toString) - assert(z.out.trim() == outsidePwd.toString) } } } From 740618fdd5a83ff79fc1fac31bb3ecc67e72d319 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sat, 7 Sep 2024 12:24:17 +0800 Subject: [PATCH 4/5] . --- os/test/src/SubprocessTests.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/test/src/SubprocessTests.scala b/os/test/src/SubprocessTests.scala index af3524cd..f154de6a 100644 --- a/os/test/src/SubprocessTests.scala +++ b/os/test/src/SubprocessTests.scala @@ -204,7 +204,7 @@ object SubprocessTests extends TestSuite { } } test("pwd0") { - // Windows doesnt have bash installed so a bit inconvenient + // Windows doesnt have bash installed so a bit inconvenient // to run these subprocesses for testing if (!scala.util.Properties.isWin) { val outsidePwd = os.pwd From 0f907b7bd85bd94226bb148ffd046750e12da773 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sat, 7 Sep 2024 17:49:13 +0800 Subject: [PATCH 5/5] . --- Readme.adoc | 2 +- os/src-jvm/package.scala | 4 ++-- os/src-native/package.scala | 4 ++-- os/test/src/PathTests.scala | 4 ++-- os/test/src/SubprocessTests.scala | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Readme.adoc b/Readme.adoc index 4d12f8f2..ce2b2723 100644 --- a/Readme.adoc +++ b/Readme.adoc @@ -1815,7 +1815,7 @@ wd / os.up wd / os.up / os.up ---- -`os.pwd` can be modified in certain scopes via the `os.pwd0` dynamic variable, but +`os.pwd` can be modified in certain scopes via the `os.dynamicPwd` dynamic variable, but best practice is not to change it. Instead simply define a new path, e.g. [source,scala] diff --git a/os/src-jvm/package.scala b/os/src-jvm/package.scala index 0dc7623b..d11a54d6 100644 --- a/os/src-jvm/package.scala +++ b/os/src-jvm/package.scala @@ -39,8 +39,8 @@ package object os { /** * The current working directory for this process. */ - def pwd: Path = pwd0.value - val pwd0: DynamicVariable[Path] = + def pwd: Path = dynamicPwd.value + val dynamicPwd: DynamicVariable[Path] = new DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath)) val up: RelPath = RelPath.up diff --git a/os/src-native/package.scala b/os/src-native/package.scala index 89057268..e71aac1a 100644 --- a/os/src-native/package.scala +++ b/os/src-native/package.scala @@ -32,8 +32,8 @@ package object os { /** * The current working directory for this process. */ - def pwd: Path = pwd0.value - val pwd0: DynamicVariable[Path] = + def pwd: Path = dynamicPwd.value + val dynamicPwd: DynamicVariable[Path] = new DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath)) val up: RelPath = RelPath.up diff --git a/os/test/src/PathTests.scala b/os/test/src/PathTests.scala index 67f005d2..365b1647 100644 --- a/os/test/src/PathTests.scala +++ b/os/test/src/PathTests.scala @@ -434,9 +434,9 @@ object PathTests extends TestSuite { System.err.printf("p[%s]\n", posix(p)) assert(posix(p) contains "/omg") } - test("pwd0") { + test("dynamicPwd") { val x = os.pwd - val y = os.pwd0.withValue(os.pwd / "hello") { + val y = os.dynamicPwd.withValue(os.pwd / "hello") { os.pwd } diff --git a/os/test/src/SubprocessTests.scala b/os/test/src/SubprocessTests.scala index f154de6a..a3bf96d1 100644 --- a/os/test/src/SubprocessTests.scala +++ b/os/test/src/SubprocessTests.scala @@ -203,7 +203,7 @@ object SubprocessTests extends TestSuite { assert(output.out.lines() == Seq("HELLO /usr")) } } - test("pwd0") { + test("dynamicPwd") { // Windows doesnt have bash installed so a bit inconvenient // to run these subprocesses for testing if (!scala.util.Properties.isWin) { @@ -211,7 +211,7 @@ object SubprocessTests extends TestSuite { val tmp0 = os.temp.dir() val tmp = os.followLink(tmp0).getOrElse(tmp0) val x = proc("bash", "-c", "pwd").call() - val y = os.pwd0.withValue(tmp) { + val y = os.dynamicPwd.withValue(tmp) { proc("bash", "-c", "pwd").call() }