Skip to content

Commit

Permalink
Update test/GHC/DiagnosticSpec.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Oct 23, 2024
1 parent 9e9bfe8 commit 5852876
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Config (
#ifdef TEST
, ConfigFile(..)
, readConfigFilesFrom
, tryReadFile
#endif
) where

Expand Down
10 changes: 8 additions & 2 deletions test/GHC/DiagnosticSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions test/Helper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Helper (
, to_json

, requireGhc
, ensureFile
) where

import Prelude hiding (span)
Expand All @@ -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

Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions test/assets/non-existing/err.json
Original file line number Diff line number Diff line change
@@ -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":[]}
3 changes: 3 additions & 0 deletions test/assets/non-existing/err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<no location info>: error: [GHC-49196]
Can't find test/assets/non-existing/Foo.hs

1 change: 1 addition & 0 deletions test/assets/use-BlockArguments/err.json
Original file line number Diff line number Diff line change
@@ -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"]}
7 changes: 7 additions & 0 deletions test/assets/use-BlockArguments/err.out
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions test/assets/variable-not-in-scope-perhaps-use/err.json
Original file line number Diff line number Diff line change
@@ -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)"]}
4 changes: 4 additions & 0 deletions test/assets/variable-not-in-scope-perhaps-use/err.out
Original file line number Diff line number Diff line change
@@ -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)

2 changes: 0 additions & 2 deletions test/assets/variable-not-in-scope/Spec.hs

This file was deleted.

1 change: 1 addition & 0 deletions test/assets/variable-not-in-scope/err.json
Original file line number Diff line number Diff line change
@@ -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":[]}
3 changes: 3 additions & 0 deletions test/assets/variable-not-in-scope/err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test/assets/variable-not-in-scope/Foo.hs:2:7: error: [GHC-88464]
Variable not in scope: bar

0 comments on commit 5852876

Please sign in to comment.