@@ -17,11 +17,9 @@ import Language.Haskell.GhciWrapper (lookupGhc)
1717import GHC.Diagnostic
1818import GHC.Diagnostic.Annotated
1919
20- data Requirement = NoRequirement | RequireGhc912
21-
2220test , ftest , xtest :: HasCallStack => FilePath -> [String ] -> String -> Maybe Annotation -> [Solution ] -> Spec
2321
24- test name = testWith name NoRequirement
22+ test name = testWith name minBound
2523
2624ftest name args code annotation = focus . test name args code annotation
2725
@@ -33,20 +31,22 @@ _ignore = let _ = (ftest, xtest) in ()
3331normalizeGhcVersion :: String -> String
3432normalizeGhcVersion = T. unpack . T. replace __GLASGOW_HASKELL_FULL_VERSION__ " 9.10.0" . T. pack
3533
36- testWith :: HasCallStack => FilePath -> Requirement -> [String ] -> String -> Maybe Annotation -> [Solution ] -> Spec
37- testWith name requirement extraArgs (unindent -> code) annotation solutions = it name do
34+ testWith :: HasCallStack => FilePath -> GHC -> [String ] -> String -> Maybe Annotation -> [Solution ] -> Spec
35+ testWith name requiredVersion extraArgs (unindent -> code) annotation solutions = it name do
3836 unless (T. null code) do
3937 ensureFile src $ T. encodeUtf8 code
4038 err <- translate <$> ghc [" -fno-diagnostics-show-caret" ]
4139 json <- ghc [" -fdiagnostics-as-json" , " --interactive" , " -ignore-dot-ghci" ]
4240 ensureFile (dir </> " err.out" ) (encodeUtf8 err)
4341 ensureFile (dir </> " err.json" ) (encodeUtf8 $ normalizeGhcVersion json)
44- Hspec. annotate (separator <> err <> separator) do
45- Just annotated <- return . parseAnnotated availableImports $ encodeUtf8 json
46- when shouldRun do
47- format annotated. diagnostic `shouldBe` err
48- annotated. annotation `shouldBe` annotation
49- annotated. solutions `shouldBe` solutions
42+ case parseAnnotated availableImports $ encodeUtf8 json of
43+ Nothing -> do
44+ expectationFailure $ " Parsing JSON failed:\n\n " <> json
45+ Just annotated -> Hspec. annotate (separator <> err <> separator) do
46+ whenGhc requiredVersion do
47+ format annotated. diagnostic `shouldBe` err
48+ annotated. annotation `shouldBe` annotation
49+ annotated. solutions `shouldBe` solutions
5050 where
5151 separator :: String
5252 separator = replicate 30 ' *' <> " \n "
@@ -59,7 +59,7 @@ testWith name requirement extraArgs (unindent -> code) annotation solutions = it
5959
6060 ghc :: [String ] -> IO String
6161 ghc args = do
62- requireGhc [ 9 , 10 ]
62+ require GHC_910
6363 bin <- lookupGhc <$> getEnvironment
6464 let
6565 process :: CreateProcess
@@ -73,16 +73,6 @@ testWith name requirement extraArgs (unindent -> code) annotation solutions = it
7373 ' ’' -> ' \' '
7474 c -> c
7575
76- shouldRun :: Bool
77- shouldRun = case requirement of
78- NoRequirement -> True
79- RequireGhc912 ->
80- #if __GLASGOW_HASKELL__ < 912
81- False
82- #else
83- True
84- #endif
85-
8676availableImports :: AvailableImports
8777availableImports = Map. fromList [
8878 (" c2w" , [" Data.ByteString.Internal" ])
@@ -147,11 +137,26 @@ spec = do
147137 foo = [|23|~]
148138 |] Nothing [EnableExtension " TemplateHaskellQuotes" , EnableExtension " TemplateHaskell" ]
149139
150- testWith " redundant-import" RequireGhc912 [" -Wall" , " -Werror " ] [r |
140+ testWith " redundant-import" GHC_912 [" -Wall" ] [r |
151141 module Foo where
152142 import Data.Maybe
153143 |] redundantImport [RemoveImport ]
154144
145+ testWith " redundant-import-error" GHC_912 [" -Wall" , " -Werror" ] [r |
146+ module Foo where
147+ import Data.Maybe
148+ |] redundantImport [RemoveImport ]
149+
150+ testWith " x-partial" GHC_912 [] [r |
151+ module Foo where
152+ foo = head
153+ |] Nothing []
154+
155+ testWith " x-partial-error" GHC_912 [" -Werror" ] [r |
156+ module Foo where
157+ foo = head
158+ |] Nothing []
159+
155160 test " non-existing" [] [r |
156161 |] Nothing []
157162
@@ -173,6 +178,30 @@ spec = do
173178 foo = "foo" + 23
174179 |] Nothing []
175180
181+ describe " analyzeHint" do
182+ it " detects missing extension" do
183+ let
184+ inputs :: [Text ]
185+ inputs = [
186+ requiredFor GHC_910 " Perhaps you intended to use BlockArguments"
187+ , requiredFor GHC_912 " Perhaps you intended to use the `BlockArguments' extension"
188+ ]
189+ for_ inputs \ input -> analyzeHint input `shouldBe` Just [
190+ EnableExtension " BlockArguments"
191+ ]
192+
193+ it " detects missing extensions" do
194+ let
195+ inputs :: [Text ]
196+ inputs = [
197+ requiredFor GHC_910 " Enable any of the following extensions: TemplateHaskell, TemplateHaskellQuotes"
198+ , requiredFor GHC_912 " Perhaps you intended to use the `TemplateHaskellQuotes' extension (implied by `TemplateHaskell')"
199+ ]
200+ for_ inputs \ input -> analyzeHint input `shouldBe` Just [
201+ EnableExtension " TemplateHaskellQuotes"
202+ , EnableExtension " TemplateHaskell"
203+ ]
204+
176205 describe " extractIdentifiers" do
177206 it " extracts identifiers" do
178207 extractIdentifiers " .. `foldl' ..., `foldr' .." `shouldBe` [UseName " foldl" , UseName " foldr" ]
0 commit comments