diff --git a/src/Config.hs b/src/Config.hs index 28ad2c6..9a55b7d 100644 --- a/src/Config.hs +++ b/src/Config.hs @@ -8,6 +8,7 @@ module Config ( #ifdef TEST , ConfigFile(..) , readConfigFilesFrom +, tryReadFile #endif ) where diff --git a/test/GHC/DiagnosticSpec.hs b/test/GHC/DiagnosticSpec.hs index f5c7977..abd88ec 100644 --- a/test/GHC/DiagnosticSpec.hs +++ b/test/GHC/DiagnosticSpec.hs @@ -13,16 +13,22 @@ import GHC.Diagnostic test :: HasCallStack => FilePath -> Spec test name = it name $ do err <- translate <$> ghc ["-fno-diagnostics-show-caret"] - Just diagnostic <- parse . encodeUtf8 <$> ghc ["-fdiagnostics-as-json"] + json <- encodeUtf8 <$> ghc ["-fdiagnostics-as-json"] + ensureFile (dir "err.out") (encodeUtf8 err) + ensureFile (dir "err.json") json + Just diagnostic <- return $ parse json decodeUtf8 (format diagnostic) `shouldBe` err where + dir :: FilePath + dir = "test" "assets" name + ghc :: [String] -> IO String ghc args = do requireGhc [9,10] bin <- lookupGhc <$> getEnvironment let process :: CreateProcess - process = proc bin (args ++ ["test/assets" name "Foo.hs"]) + process = proc bin (args ++ [dir "Foo.hs"]) (_, _, err) <- readCreateProcessWithExitCode process "" return err diff --git a/test/Helper.hs b/test/Helper.hs index 9a92bbb..61ff0a0 100644 --- a/test/Helper.hs +++ b/test/Helper.hs @@ -24,6 +24,7 @@ module Helper ( , to_json , requireGhc +, ensureFile ) where import Prelude hiding (span) @@ -39,10 +40,12 @@ import Test.Mockery.Directory as Imports (touch) import System.Environment import qualified System.Timeout +import qualified Data.ByteString as ByteString import Data.ByteString.Lazy (toStrict) import Data.Aeson (ToJSON, encode) import Run () +import Config (tryReadFile) import Util import Language.Haskell.GhciWrapper @@ -127,3 +130,9 @@ requireGhc (makeVersion -> required) = do env <- getEnvironment let Just ghcVersion = lookupGhcVersion env >>= parseVersion when (ghcVersion < required) pending + +ensureFile :: FilePath -> ByteString -> IO () +ensureFile name new = do + old <- tryReadFile name + unless (old == Just new) $ do + ByteString.writeFile name new diff --git a/test/assets/non-existing/err.json b/test/assets/non-existing/err.json new file mode 100644 index 0000000..910ffed --- /dev/null +++ b/test/assets/non-existing/err.json @@ -0,0 +1 @@ +{"version":"1.0","ghcVersion":"ghc-9.10.1","span":null,"severity":"Error","code":49196,"message":["Can't find test/assets/non-existing/Foo.hs"],"hints":[]} diff --git a/test/assets/non-existing/err.out b/test/assets/non-existing/err.out new file mode 100644 index 0000000..61e02cb --- /dev/null +++ b/test/assets/non-existing/err.out @@ -0,0 +1,3 @@ +: error: [GHC-49196] + Can't find test/assets/non-existing/Foo.hs + diff --git a/test/assets/use-BlockArguments/err.json b/test/assets/use-BlockArguments/err.json new file mode 100644 index 0000000..611d0cb --- /dev/null +++ b/test/assets/use-BlockArguments/err.json @@ -0,0 +1 @@ +{"version":"1.0","ghcVersion":"ghc-9.10.1","span":{"file":"test/assets/use-BlockArguments/Foo.hs","start":{"line":5,"column":10},"end":{"line":5,"column":22}},"severity":"Error","code":52095,"message":["Unexpected do block in function application:\n do return ()"],"hints":["Use parentheses.","Perhaps you intended to use BlockArguments"]} diff --git a/test/assets/use-BlockArguments/err.out b/test/assets/use-BlockArguments/err.out new file mode 100644 index 0000000..8670c8f --- /dev/null +++ b/test/assets/use-BlockArguments/err.out @@ -0,0 +1,7 @@ +test/assets/use-BlockArguments/Foo.hs:5:10: error: [GHC-52095] + Unexpected do block in function application: + do return () + Suggested fixes: + • Use parentheses. + • Perhaps you intended to use BlockArguments + diff --git a/test/assets/variable-not-in-scope-perhaps-use/err.json b/test/assets/variable-not-in-scope-perhaps-use/err.json new file mode 100644 index 0000000..e3c301f --- /dev/null +++ b/test/assets/variable-not-in-scope-perhaps-use/err.json @@ -0,0 +1 @@ +{"version":"1.0","ghcVersion":"ghc-9.10.1","span":{"file":"test/assets/variable-not-in-scope-perhaps-use/Foo.hs","start":{"line":2,"column":7},"end":{"line":2,"column":14}},"severity":"Error","code":88464,"message":["Variable not in scope: filter_"],"hints":["Perhaps use `filter' (imported from Prelude)"]} diff --git a/test/assets/variable-not-in-scope-perhaps-use/err.out b/test/assets/variable-not-in-scope-perhaps-use/err.out new file mode 100644 index 0000000..dba1a4f --- /dev/null +++ b/test/assets/variable-not-in-scope-perhaps-use/err.out @@ -0,0 +1,4 @@ +test/assets/variable-not-in-scope-perhaps-use/Foo.hs:2:7: error: [GHC-88464] + Variable not in scope: filter_ + Suggested fix: Perhaps use `filter' (imported from Prelude) + diff --git a/test/assets/variable-not-in-scope/Spec.hs b/test/assets/variable-not-in-scope/Spec.hs deleted file mode 100644 index 5f2becf..0000000 --- a/test/assets/variable-not-in-scope/Spec.hs +++ /dev/null @@ -1,2 +0,0 @@ -module Foo where -foo = bar diff --git a/test/assets/variable-not-in-scope/err.json b/test/assets/variable-not-in-scope/err.json new file mode 100644 index 0000000..7377670 --- /dev/null +++ b/test/assets/variable-not-in-scope/err.json @@ -0,0 +1 @@ +{"version":"1.0","ghcVersion":"ghc-9.10.1","span":{"file":"test/assets/variable-not-in-scope/Foo.hs","start":{"line":2,"column":7},"end":{"line":2,"column":10}},"severity":"Error","code":88464,"message":["Variable not in scope: bar"],"hints":[]} diff --git a/test/assets/variable-not-in-scope/err.out b/test/assets/variable-not-in-scope/err.out new file mode 100644 index 0000000..1f30f36 --- /dev/null +++ b/test/assets/variable-not-in-scope/err.out @@ -0,0 +1,3 @@ +test/assets/variable-not-in-scope/Foo.hs:2:7: error: [GHC-88464] + Variable not in scope: bar +