Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make os.pwd modifiable via the os.pwd0 dynamic variable #298

Merged
merged 5 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.dynamicPwd` dynamic variable, but
best practice is not to change it. Instead simply define a new path, e.g.

[source,scala]
----
Expand Down
5 changes: 4 additions & 1 deletion os/src-jvm/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
def pwd: Path = dynamicPwd.value
val dynamicPwd: DynamicVariable[Path] =
new DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath))

val up: RelPath = RelPath.up

Expand Down
5 changes: 4 additions & 1 deletion os/src-native/package.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
def pwd: Path = dynamicPwd.value
val dynamicPwd: DynamicVariable[Path] =
new DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath))

val up: RelPath = RelPath.up

Expand Down
8 changes: 8 additions & 0 deletions os/test/src/PathTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ object PathTests extends TestSuite {
System.err.printf("p[%s]\n", posix(p))
assert(posix(p) contains "/omg")
}
test("dynamicPwd") {
val x = os.pwd
val y = os.dynamicPwd.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 = {
Expand Down
19 changes: 19 additions & 0 deletions os/test/src/SubprocessTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,24 @@ object SubprocessTests extends TestSuite {
assert(output.out.lines() == Seq("HELLO /usr"))
}
}
test("dynamicPwd") {
// 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.dynamicPwd.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)
}
}
}
}