@@ -14,7 +14,7 @@ import Control.Applicative.Combinators
14
14
import Control.Exception (bracket_ , catch )
15
15
import qualified Control.Lens as Lens
16
16
import Control.Monad
17
- import Control.Monad.IO.Class (liftIO )
17
+ import Control.Monad.IO.Class (MonadIO , liftIO )
18
18
import Data.Aeson (FromJSON , Value , toJSON )
19
19
import qualified Data.Binary as Binary
20
20
import Data.Default
@@ -64,6 +64,7 @@ import Development.IDE.Plugin.Test (WaitForIdeRuleResult(..), TestRequest(BlockS
64
64
import Control.Monad.Extra (whenJust )
65
65
import qualified Language.Haskell.LSP.Types.Lens as L
66
66
import Control.Lens ((^.) )
67
+ import Data.Functor
67
68
68
69
main :: IO ()
69
70
main = do
@@ -676,6 +677,7 @@ codeActionTests = testGroup "code actions"
676
677
, removeImportTests
677
678
, extendImportTests
678
679
, suggestImportTests
680
+ , disableWarningTests
679
681
, fixConstructorImportTests
680
682
, importRenameActionTests
681
683
, fillTypedHoleTests
@@ -881,9 +883,8 @@ removeImportTests = testGroup "remove import actions"
881
883
]
882
884
docB <- createDoc " ModuleB.hs" " haskell" contentB
883
885
_ <- waitForDiagnostics
884
- [CACodeAction action@ CodeAction { _title = actionTitle }, _]
885
- <- getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
886
- liftIO $ " Remove import" @=? actionTitle
886
+ action <- assertJust " Code action not found" . firstJust (caWithTitle " Remove import" )
887
+ =<< getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
887
888
executeCodeAction action
888
889
contentAfterAction <- documentContents docB
889
890
let expectedContentAfterAction = T. unlines
@@ -907,9 +908,8 @@ removeImportTests = testGroup "remove import actions"
907
908
]
908
909
docB <- createDoc " ModuleB.hs" " haskell" contentB
909
910
_ <- waitForDiagnostics
910
- [CACodeAction action@ CodeAction { _title = actionTitle }, _]
911
- <- getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
912
- liftIO $ " Remove import" @=? actionTitle
911
+ action <- assertJust " Code action not found" . firstJust (caWithTitle " Remove import" )
912
+ =<< getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
913
913
executeCodeAction action
914
914
contentAfterAction <- documentContents docB
915
915
let expectedContentAfterAction = T. unlines
@@ -936,9 +936,8 @@ removeImportTests = testGroup "remove import actions"
936
936
]
937
937
docB <- createDoc " ModuleB.hs" " haskell" contentB
938
938
_ <- waitForDiagnostics
939
- [CACodeAction action@ CodeAction { _title = actionTitle }, _]
940
- <- getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
941
- liftIO $ " Remove stuffA, stuffC from import" @=? actionTitle
939
+ action <- assertJust " Code action not found" . firstJust (caWithTitle " Remove stuffA, stuffC from import" )
940
+ =<< getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
942
941
executeCodeAction action
943
942
contentAfterAction <- documentContents docB
944
943
let expectedContentAfterAction = T. unlines
@@ -965,9 +964,8 @@ removeImportTests = testGroup "remove import actions"
965
964
]
966
965
docB <- createDoc " ModuleB.hs" " haskell" contentB
967
966
_ <- waitForDiagnostics
968
- [CACodeAction action@ CodeAction { _title = actionTitle }, _]
969
- <- getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
970
- liftIO $ " Remove !!, <?> from import" @=? actionTitle
967
+ action <- assertJust " Code action not found" . firstJust (caWithTitle " Remove !!, <?> from import" )
968
+ =<< getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
971
969
executeCodeAction action
972
970
contentAfterAction <- documentContents docB
973
971
let expectedContentAfterAction = T. unlines
@@ -993,9 +991,8 @@ removeImportTests = testGroup "remove import actions"
993
991
]
994
992
docB <- createDoc " ModuleB.hs" " haskell" contentB
995
993
_ <- waitForDiagnostics
996
- [CACodeAction action@ CodeAction { _title = actionTitle }, _]
997
- <- getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
998
- liftIO $ " Remove A from import" @=? actionTitle
994
+ action <- assertJust " Code action not found" . firstJust (caWithTitle " Remove A from import" )
995
+ =<< getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
999
996
executeCodeAction action
1000
997
contentAfterAction <- documentContents docB
1001
998
let expectedContentAfterAction = T. unlines
@@ -1020,9 +1017,8 @@ removeImportTests = testGroup "remove import actions"
1020
1017
]
1021
1018
docB <- createDoc " ModuleB.hs" " haskell" contentB
1022
1019
_ <- waitForDiagnostics
1023
- [CACodeAction action@ CodeAction { _title = actionTitle }, _]
1024
- <- getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
1025
- liftIO $ " Remove A, E, F from import" @=? actionTitle
1020
+ action <- assertJust " Code action not found" . firstJust (caWithTitle " Remove A, E, F from import" )
1021
+ =<< getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
1026
1022
executeCodeAction action
1027
1023
contentAfterAction <- documentContents docB
1028
1024
let expectedContentAfterAction = T. unlines
@@ -1044,9 +1040,8 @@ removeImportTests = testGroup "remove import actions"
1044
1040
]
1045
1041
docB <- createDoc " ModuleB.hs" " haskell" contentB
1046
1042
_ <- waitForDiagnostics
1047
- [CACodeAction action@ CodeAction { _title = actionTitle }, _]
1048
- <- getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
1049
- liftIO $ " Remove import" @=? actionTitle
1043
+ action <- assertJust " Code action not found" . firstJust (caWithTitle " Remove import" )
1044
+ =<< getCodeActions docB (Range (Position 2 0 ) (Position 2 5 ))
1050
1045
executeCodeAction action
1051
1046
contentAfterAction <- documentContents docB
1052
1047
let expectedContentAfterAction = T. unlines
@@ -1069,9 +1064,8 @@ removeImportTests = testGroup "remove import actions"
1069
1064
]
1070
1065
doc <- createDoc " ModuleC.hs" " haskell" content
1071
1066
_ <- waitForDiagnostics
1072
- [_, _, _, _, CACodeAction action@ CodeAction { _title = actionTitle }]
1073
- <- getCodeActions doc (Range (Position 2 0 ) (Position 2 5 ))
1074
- liftIO $ " Remove all redundant imports" @=? actionTitle
1067
+ action <- assertJust " Code action not found" . firstJust (caWithTitle " Remove all redundant imports" )
1068
+ =<< getCodeActions doc (Range (Position 2 0 ) (Position 2 5 ))
1075
1069
executeCodeAction action
1076
1070
contentAfterAction <- documentContents doc
1077
1071
let expectedContentAfterAction = T. unlines
@@ -1087,6 +1081,10 @@ removeImportTests = testGroup "remove import actions"
1087
1081
]
1088
1082
liftIO $ expectedContentAfterAction @=? contentAfterAction
1089
1083
]
1084
+ where
1085
+ caWithTitle t = \ case
1086
+ CACodeAction a@ CodeAction {_title} -> guard (_title == t) >> Just a
1087
+ _ -> Nothing
1090
1088
1091
1089
extendImportTests :: TestTree
1092
1090
extendImportTests = testGroup " extend import actions"
@@ -1441,6 +1439,57 @@ suggestImportTests = testGroup "suggest import actions"
1441
1439
else
1442
1440
liftIO $ [_title | CACodeAction CodeAction {_title} <- actions, _title == newImp ] @?= []
1443
1441
1442
+ disableWarningTests :: TestTree
1443
+ disableWarningTests =
1444
+ testGroup " disable warnings" $
1445
+ [
1446
+ ( " missing-signatures"
1447
+ , T. unlines
1448
+ [ " {-# OPTIONS_GHC -Wall #-}"
1449
+ , " main = putStrLn \" hello\" "
1450
+ ]
1451
+ , T. unlines
1452
+ [ " {-# OPTIONS_GHC -Wall #-}"
1453
+ , " {-# OPTIONS_GHC -Wno-missing-signatures #-}"
1454
+ , " main = putStrLn \" hello\" "
1455
+ ]
1456
+ )
1457
+ ,
1458
+ ( " unused-imports"
1459
+ , T. unlines
1460
+ [ " {-# OPTIONS_GHC -Wall #-}"
1461
+ , " "
1462
+ , " "
1463
+ , " module M where"
1464
+ , " "
1465
+ , " import Data.Functor"
1466
+ ]
1467
+ , T. unlines
1468
+ [ " {-# OPTIONS_GHC -Wall #-}"
1469
+ , " {-# OPTIONS_GHC -Wno-unused-imports #-}"
1470
+ , " "
1471
+ , " "
1472
+ , " module M where"
1473
+ , " "
1474
+ , " import Data.Functor"
1475
+ ]
1476
+ )
1477
+ ]
1478
+ <&> \ (warning, initialContent, expectedContent) -> testSession (T. unpack warning) $ do
1479
+ doc <- createDoc " Module.hs" " haskell" initialContent
1480
+ _ <- waitForDiagnostics
1481
+ codeActs <- mapMaybe caResultToCodeAct <$> getCodeActions doc (Range (Position 0 0 ) (Position 0 0 ))
1482
+ case find (\ CodeAction {_title} -> _title == " Disable \" " <> warning <> " \" warnings" ) codeActs of
1483
+ Nothing -> liftIO $ assertFailure " No code action with expected title"
1484
+ Just action -> do
1485
+ executeCodeAction action
1486
+ contentAfterAction <- documentContents doc
1487
+ liftIO $ expectedContent @=? contentAfterAction
1488
+ where
1489
+ caResultToCodeAct = \ case
1490
+ CACommand _ -> Nothing
1491
+ CACodeAction c -> Just c
1492
+
1444
1493
insertNewDefinitionTests :: TestTree
1445
1494
insertNewDefinitionTests = testGroup " insert new definition actions"
1446
1495
[ testSession " insert new function definition" $ do
@@ -2192,7 +2241,12 @@ removeRedundantConstraintsTests = let
2192
2241
doc <- createDoc " Testing.hs" " haskell" code
2193
2242
_ <- waitForDiagnostics
2194
2243
actionsOrCommands <- getCodeActions doc (Range (Position 4 0 ) (Position 4 maxBound ))
2195
- liftIO $ assertBool " Found some actions" (null actionsOrCommands)
2244
+ liftIO $ assertBool " Found some actions (other than \" disable warnings\" )"
2245
+ $ all isDisableWarningAction actionsOrCommands
2246
+ where
2247
+ isDisableWarningAction = \ case
2248
+ CACodeAction CodeAction {_title} -> " Disable" `T.isPrefixOf` _title && " warnings" `T.isSuffixOf` _title
2249
+ _ -> False
2196
2250
2197
2251
in testGroup " remove redundant function constraints"
2198
2252
[ check
@@ -4037,7 +4091,10 @@ asyncTests = testGroup "async"
4037
4091
]
4038
4092
void waitForDiagnostics
4039
4093
actions <- getCodeActions doc (Range (Position 1 0 ) (Position 1 0 ))
4040
- liftIO $ [ _title | CACodeAction CodeAction {_title} <- actions] @=? [" add signature: foo :: a -> a" ]
4094
+ liftIO $ [ _title | CACodeAction CodeAction {_title} <- actions] @=?
4095
+ [ " add signature: foo :: a -> a"
4096
+ , " Disable \" missing-signatures\" warnings"
4097
+ ]
4041
4098
, testSession " request" $ do
4042
4099
-- Execute a custom request that will block for 1000 seconds
4043
4100
void $ sendRequest (CustomClientMethod " test" ) $ BlockSeconds 1000
@@ -4048,7 +4105,10 @@ asyncTests = testGroup "async"
4048
4105
]
4049
4106
void waitForDiagnostics
4050
4107
actions <- getCodeActions doc (Range (Position 0 0 ) (Position 0 0 ))
4051
- liftIO $ [ _title | CACodeAction CodeAction {_title} <- actions] @=? [" add signature: foo :: a -> a" ]
4108
+ liftIO $ [ _title | CACodeAction CodeAction {_title} <- actions] @=?
4109
+ [ " add signature: foo :: a -> a"
4110
+ , " Disable \" missing-signatures\" warnings"
4111
+ ]
4052
4112
]
4053
4113
4054
4114
@@ -4425,3 +4485,9 @@ withTempDir :: (FilePath -> IO a) -> IO a
4425
4485
withTempDir f = System.IO.Extra. withTempDir $ \ dir -> do
4426
4486
dir' <- canonicalizePath dir
4427
4487
f dir'
4488
+
4489
+ -- | Assert that a value is not 'Nothing', and extract the value.
4490
+ assertJust :: MonadIO m => String -> Maybe a -> m a
4491
+ assertJust s = \ case
4492
+ Nothing -> liftIO $ assertFailure s
4493
+ Just x -> pure x
0 commit comments