Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
fix: code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lovettchris committed Sep 1, 2022
1 parent 7712fb3 commit f548a15
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ jobs:
path: build

- name: Run tests
run: lake script run runTests
run: lake script run tests
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ There are some aspects you might want to take note of when attempting to develop

LeanInk uses simple diffing tests to make sure the core functionality works as expected. These tests are located in the `./test` folder.

You can run these tests using `lake script run runTests`. This will run LeanInk for every `.lean`, that's not a `lakefile` or part of an `lean_package`. It will compare the output of LeanInk to the expected output within the `.lean.leanInk.expected` file.
You can run these tests using `lake script run tests`. This will run LeanInk for every `.lean`, that's not a `lakefile` or part of an `lean_package`. It will compare the output of LeanInk to the expected output within the `.lean.leanInk.expected` file.

To capture a new expected output file you can either run `lake script run capture` to capture the output for all files or use leanInk itself to generate an output for a single file and rename it afterwards. Be sure to carefully examine the git diff before committing the new expected baselines.

Expand Down
36 changes: 15 additions & 21 deletions lakefile.lean
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ lean_exe leanInk {
supportInterpreter := true
}

def fileExists (p : FilePath) : IO Bool :=
return (← p.metadata.toBaseIO).toBool

/-! Run the leanInk that is built locally to analyze the given test file.
If there is a lakefile.lean present then pass the additional `--lake` option -/
def runLeanInk (test : FilePath) : IO UInt32 := do
let buildDir : FilePath := "build" / "bin"
let leanInkExe := if Platform.isWindows then buildDir / "leanInk.exe" else buildDir / "leanInk"
let leanInk : FilePath := "build" / "bin" / "leanInk"
let leanInkExe := leanInk.withExtension FilePath.exeExtension
let realPath ← IO.FS.realPath leanInkExe

if ! (← fileExists leanInkExe) then
if ! (← leanInkExe.pathExists) then
IO.println s!"Could not find leanInk executable at {leanInkExe}"
IO.println s!"Please run `lake build` in the LeanInk directory"
return 1
Expand All @@ -32,7 +29,7 @@ def runLeanInk (test : FilePath) : IO UInt32 := do
let mut args := #["analyze", fileName, "--x-enable-type-info", "--x-enable-docStrings", "--x-enable-semantic-token", "--prettify-output"]
if let some dir := test.parent then
let lakefile := dir / "lakefile.lean"
if (← fileExists lakefile) then
if (← lakefile.pathExists) then
IO.println s!"Running test {test} using lake..."
args := args ++ #["--lake", "lakefile.lean"]
else
Expand All @@ -47,20 +44,18 @@ def runLeanInk (test : FilePath) : IO UInt32 := do
return 1

/-! Compare the text contents of two files -/
def runDiff (actual : FilePath) (expected : FilePath) : IO UInt32 := do
def runDiff (actual : FilePath) (expected : FilePath) : IO Bool := do
let actualStr ← IO.FS.readFile actual
let expectedStr ← IO.FS.readFile expected
if actualStr.trim = expectedStr.trim then
return 0
else
return 1
return actualStr.trim = expectedStr.trim

def copyFile (src : FilePath) (dst : FilePath) : IO Unit := do
IO.FS.writeFile dst (← IO.FS.readFile src)

/-! Walk the 'test' folder looking for every `.lean` file that's not a `lakefile` or part of an `lean_package`.
If `capture` is true then update the `.lean.leanInk.expected` file, otherwise compare the new output
to the expected output and return an error if they are different. -/
/-! Walk the `test` folder looking for every `.lean` file that's not a `lakefile` or part of an
`lean_package` and run `leanInk` on it. If `capture` is true then update the `.lean.leanInk.expected`
file, otherwise compare the new output to the expected output and return an error if they are
different. -/
def execute (capture : Bool) : IO UInt32 := do
let root : FilePath := "." / "test"
let dirs ← walkDir root
Expand All @@ -69,26 +64,25 @@ def execute (capture : Bool) : IO UInt32 := do
if let some fileName := test.fileName then
let actual := test.withFileName (fileName ++ ".leanInk")
let expected := test.withFileName (fileName ++ ".leanInk.expected")
if (← fileExists expected) then
if (← expected.pathExists) then
let rc ← runLeanInk test
if rc ≠ 0 then
return 1
else if (capture) then
IO.println s!" UPDATING {expected}"
copyFile actual expected
else
let d ← runDiff actual expected
if d = 0 then
if (← runDiff actual expected) then
IO.println s!" SUCCESS"
else
IO.println s!" FAILED: diff {expected} {actual}"
return 1
return 0

script runTests (args) do
script tests (args) do
IO.println "Running diff tests for leanInk"
returnexecute False
execute False

script capture (args) do
IO.println "Updating .leanInk.expected output files"
returnexecute True
execute True

0 comments on commit f548a15

Please sign in to comment.