Adds `IO.Process.getCurrentDir` and `IO.Process.setCurrentDir` for retrieving and setting, respectively, the current working directory of a process. The names of the functions are inspired by Rust (e.g., [`set_current_dir`](https://doc.rust-lang.org/std/env/fn.set_current_dir.html)).
18 lines
644 B
Text
18 lines
644 B
Text
def test : IO Unit := do
|
||
let cwd ← IO.Process.getCurrentDir
|
||
IO.println cwd
|
||
IO.Process.setCurrentDir ".."
|
||
let cwd' ← IO.Process.getCurrentDir
|
||
IO.println cwd'
|
||
let actual := cwd'.normalize
|
||
let expected := cwd.normalize.parent.getD
|
||
(cwd.components[0]!.push System.FilePath.pathSeparator)
|
||
unless expected == actual do
|
||
throw <| IO.userError s!"expected {expected}, got {actual}"
|
||
|
||
-- Ensures this test is idempotent in an interactive session.
|
||
def withoutModifyingCurrentDir (x : IO α) : IO α := do
|
||
let cwd ← IO.Process.getCurrentDir
|
||
try x finally IO.Process.setCurrentDir cwd
|
||
|
||
#eval withoutModifyingCurrentDir test
|