Skip to content

Commit

Permalink
Make os.pwd modifiable via the os.pwd0 dynamic variable (#298)
Browse files Browse the repository at this point in the history
Basically the same thing we did in
#295 and
#283, except rather than being
`os.SubProcess`-specific this applies to all usage of `os.pwd`. And with
similar limitations: this works for code paths that go through OS-Lib,
but not for code that uses `java.io` or `java.nio` directly.

AFAIK this is the last of the "global" things we need to redirect in
Mill tasks, after env variables and default subprocess streams. I'd like
it to go into Mill 0.12.0 together with the other sandboxing-related
changes

Added unit tests and updated the documentation

Pull Request: #298
  • Loading branch information
lihaoyi authored Sep 7, 2024
1 parent 29266d7 commit 5dc0aa5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1828,8 +1828,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 @@ -225,5 +225,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)
}
}
}
}

0 comments on commit 5dc0aa5

Please sign in to comment.