lean4-htt/tests/lean/run/currentDir.lean
Mac Malone e6160d7d4a
feat: IO.Process.get/setCurrentDir (#4036)
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)).
2024-05-02 13:49:10 +00:00

18 lines
644 B
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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