tester.nim 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Small program that runs the test cases
  2. import strutils, os, sequtils
  3. from std/private/gitutils import diffFiles
  4. const
  5. dir = "nimpretty/tests"
  6. outputdir = dir / "outputdir"
  7. var
  8. failures = 0
  9. when defined(develop):
  10. const nimp = "bin" / "nimpretty".addFileExt(ExeExt)
  11. if execShellCmd("nim c -o:$# nimpretty/nimpretty.nim" % [nimp]) != 0:
  12. quit("FAILURE: compilation of nimpretty failed")
  13. else:
  14. const nimp = "nimpretty"
  15. proc test(infile, ext: string) =
  16. if execShellCmd("$# -o:$# --backup:off $#" % [nimp, infile.changeFileExt(ext), infile]) != 0:
  17. echo "FAILURE: nimpretty cannot prettify ", infile
  18. failures += 1
  19. return
  20. let nimFile = splitFile(infile).name
  21. let expected = dir / "expected" / nimFile & ".nim"
  22. let produced = dir / nimFile.changeFileExt(ext)
  23. if readFile(expected) != readFile(produced):
  24. echo "FAILURE: files differ: ", nimFile
  25. echo diffFiles(expected, produced).output
  26. failures += 1
  27. else:
  28. echo "SUCCESS: files identical: ", nimFile
  29. proc testTogether(infiles: seq[string]) =
  30. if execShellCmd("$# --outDir:$# --backup:off $#" % [nimp, outputdir, infiles.join(" ")]) != 0:
  31. echo "FAILURE: nimpretty cannot prettify files: ", $infiles
  32. failures += 1
  33. return
  34. for infile in infiles:
  35. let nimFile = splitFile(infile).name
  36. let expected = dir / "expected" / nimFile & ".nim"
  37. let produced = dir / "outputdir" / infile
  38. if readFile(expected) != readFile(produced):
  39. echo "FAILURE: files differ: ", nimFile
  40. echo diffFiles(expected, produced).output
  41. failures += 1
  42. else:
  43. echo "SUCCESS: files identical: ", nimFile
  44. let allFiles = toSeq(walkFiles(dir / "*.nim"))
  45. for t in allFiles:
  46. test(t, "pretty")
  47. # also test that pretty(pretty(x)) == pretty(x)
  48. test(t.changeFileExt("pretty"), "pretty2")
  49. removeFile(t.changeFileExt("pretty"))
  50. removeFile(t.changeFileExt("pretty2"))
  51. testTogether(allFiles)
  52. removeDir(outputdir)
  53. if failures > 0: quit($failures & " failures occurred.")