Tests.hs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. {-# LANGUAGE MultiWayIf #-}
  2. module Bugs.Tests where
  3. import Data.Function (on)
  4. import Data.Monoid ((<>))
  5. import qualified Data.Text as T
  6. import Data.Text.Encoding
  7. import Test.Tasty
  8. import Test.Tasty.Silver
  9. import Test.Tasty.Silver.Advanced
  10. (readFileMaybe, GDiff(..), GShow(..), goldenTest1)
  11. import System.Directory
  12. import System.Exit
  13. import System.FilePath
  14. import Utils
  15. testDir :: FilePath
  16. testDir = "test" </> "Bugs"
  17. tests :: IO TestTree
  18. tests = do
  19. inpFiles <- getAgdaFilesInDir NonRec testDir
  20. return $ testGroup "Bugs" $ map mkTest inpFiles
  21. data TestResult
  22. = TestFailure T.Text
  23. | TestWarning T.Text
  24. | TestSuccess
  25. mkTest :: FilePath -- ^ Agda file to test.
  26. -> TestTree
  27. mkTest agdaFile =
  28. goldenTest1
  29. testName
  30. readGolden
  31. doRun
  32. (textDiff `on` printTestResult)
  33. (ShowText . printTestResult)
  34. resUpdate
  35. where
  36. testName = asTestName testDir agdaFile
  37. flagFile = dropAgdaExtension agdaFile <.> ".flags"
  38. errFile = dropAgdaExtension agdaFile <.> ".err"
  39. warnFile = dropAgdaExtension agdaFile <.> ".warn"
  40. readGolden = Just <$> do
  41. hasWarn <- doesFileExist warnFile
  42. hasErr <- doesFileExist errFile
  43. if | hasWarn && hasErr -> error "Cannot have both .warn and .err file"
  44. | hasWarn -> TestWarning <$> readTextFile warnFile
  45. | hasErr -> TestFailure <$> readTextFile errFile
  46. | otherwise -> pure TestSuccess
  47. doRun = do
  48. let agdaArgs = ["-v0", "-i" ++ testDir, "-itest/"
  49. , agdaFile, "--ignore-interfaces", "--no-libraries"
  50. ]
  51. (res, ret) <- runAgdaWithOptions testName agdaArgs (Just flagFile) Nothing
  52. pure $ case ret of
  53. AgdaSuccess Nothing -> TestSuccess
  54. AgdaSuccess (Just w) -> TestWarning $ "AGDA_WARNING\n\n" <> w
  55. AgdaFailure{} -> TestFailure $ "AGDA_FAILURE\n\n" <> printProgramResult res
  56. resUpdate :: TestResult -> IO ()
  57. resUpdate = \case
  58. TestSuccess -> pure ()
  59. TestWarning w -> writeTextFile warnFile w
  60. TestFailure e -> writeTextFile errFile e
  61. printTestResult :: TestResult -> T.Text
  62. printTestResult = \case
  63. TestSuccess -> "AGDA_SUCCESS\n\n"
  64. TestWarning w -> w
  65. TestFailure p -> p