lean4-htt/tests/lean/run/tempfile.lean
Paul Reichert 98e4b2882f
refactor: migrate to new ranges (#8841)
This PR migrates usages of `Std.Range` to the new polymorphic ranges.

This PR unfortunately increases the transitive imports for
frequently-used parts of `Init` because the ranges now rely on iterators
in order to provide their functionality for types other than `Nat`.
However, iteration over ranges in compiled code is as efficient as
before in the examples I checked. This is because of a special
`IteratorLoop` implementation provided in the PR for this purpose.

There were two issues that were uncovered during migration:

* In `IndPredBelow.lean`, migrating the last remaining range causes
`compilerTest1.lean` to break. I have minimized the issue and came to
the conclusion it's a compiler bug. Therefore, I have not replaced said
old range usage yet (see #9186).
* In `BRecOn.lean`, we are publicly importing the ranges. Making this
import private should theoretically work, but there seems to be a
problem with the module system, causing the build to panic later in
`Init.Data.Grind.Poly` (see #9185).
* In `FuzzyMatching.lean`, inlining fails with the new ranges, which
would have led to significant slowdown. Therefore, I have not migrated
this file either.
2025-07-07 12:41:53 +00:00

100 lines
2.6 KiB
Text

/-!
# Temporary Files
These tests check that temporary files and directories can be created and used.
-/
/--
Tests temporary file creation.
-/
def test : IO Unit := do
let (handle, path) ← IO.FS.createTempFile
try
let toWrite := "Hello World"
handle.putStr toWrite
handle.flush
let handle2 ← IO.FS.Handle.mk path .read
let content ← handle2.getLine
assert! (content == toWrite)
finally
IO.FS.removeFile path
#eval test
/--
Tests temporary file helper.
-/
def testWithFile : IO Unit := do
let pathRef ← IO.mkRef none
IO.FS.withTempFile fun handle path => do
pathRef.set (some path)
assert! (← path.pathExists)
let toWrite := "Hello World"
handle.putStr toWrite
handle.flush
let handle2 ← IO.FS.Handle.mk path .read
let content ← handle2.getLine
assert! (content == toWrite)
match (← pathRef.get) with
| none => assert! false
| some p => assert! (! (← p.pathExists))
#eval testWithFile
/--
Tests temporary directory creation and ensures that files can be created in it.
-/
def testDir : IO Unit := do
let path ← IO.FS.createTempDir
try
assert! (← path.isDir)
let fileList ← path.readDir
assert! (fileList.isEmpty)
let toWrite := "Hello World"
for i in *...(3 : Nat) do
IO.FS.withFile (path / s!"{i}.txt") .write fun h => do
h.putStr toWrite
h.putStr (toString i)
for i in *...(3 : Nat) do
IO.FS.withFile (path / s!"{i}.txt") .read fun h => do
let content ← h.getLine
assert! (content == toWrite ++ toString i)
let fileList := ((← path.readDir).map (·.fileName)).qsortOrd
assert! (fileList == #["0.txt", "1.txt", "2.txt"])
finally
IO.FS.removeDirAll path
#eval testDir
/--
Tests temporary directory helper.
-/
def testWithDir : IO Unit := do
let pathRef ← IO.mkRef none
IO.FS.withTempDir fun path => do
pathRef.set (some path)
assert! (← path.isDir)
let fileList ← path.readDir
assert! (fileList.isEmpty)
let toWrite := "Hello World"
for i in *...(3 : Nat) do
IO.FS.withFile (path / s!"{i}.txt") .write fun h => do
h.putStr toWrite
h.putStr (toString i)
for i in *...(3 : Nat) do
IO.FS.withFile (path / s!"{i}.txt") .read fun h => do
let content ← h.getLine
assert! (content == toWrite ++ toString i)
let fileList := ((← path.readDir).map (·.fileName)).qsortOrd
assert! (fileList == #["0.txt", "1.txt", "2.txt"])
match (← pathRef.get) with
| none => assert! false
| some p => assert! (! (← p.pathExists))
#eval testWithDir