@@ -20,8 +20,9 @@ import qualified Language.Haskell.LSP.Types.Lens as L
20
20
import qualified Language.Haskell.LSP.Types.Capabilities as C
21
21
import Test.Hls.Util
22
22
import Test.Tasty
23
- import Test.Tasty.ExpectedFailure (ignoreTestBecause )
23
+ import Test.Tasty.ExpectedFailure (ignoreTestBecause , expectFailBecause )
24
24
import Test.Tasty.HUnit
25
+ import System.FilePath ((</>) )
25
26
26
27
{-# ANN module ("HLint: ignore Reduce duplication"::String) #-}
27
28
@@ -41,7 +42,7 @@ tests = testGroup "code actions" [
41
42
42
43
hlintTests :: TestTree
43
44
hlintTests = testGroup " hlint suggestions" [
44
- testCase " provides 3.8 code actions including apply all" $ runSession hlsCommand fullCaps " test/testdata/hlint " $ do
45
+ testCase " provides 3.8 code actions including apply all" $ runHlintSession " " $ do
45
46
doc <- openDoc " ApplyRefact2.hs" " haskell"
46
47
diags@ (reduceDiag: _) <- waitForDiagnosticsFromSource doc " hlint"
47
48
@@ -73,55 +74,110 @@ hlintTests = testGroup "hlint suggestions" [
73
74
_ <- waitForDiagnosticsFromSource doc " hlint"
74
75
75
76
cars <- getAllCodeActions doc
76
- etaReduce <- liftIO $ inspectCommand cars [" Apply hint: Eta reduce" ]
77
+ etaReduce <- liftIO $ inspectCommand cars [" Eta reduce" ]
77
78
78
79
executeCommand etaReduce
79
80
80
81
contents <- skipManyTill anyMessage $ getDocumentEdit doc
81
82
liftIO $ contents @?= " main = undefined\n foo = id\n "
82
83
83
- , testCase " changing configuration enables or disables hlint diagnostics" $ runSession hlsCommand fullCaps " test/testdata/hlint " $ do
84
+ , testCase " changing configuration enables or disables hlint diagnostics" $ runHlintSession " " $ do
84
85
let config = def { hlintOn = True }
85
86
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
86
87
87
88
doc <- openDoc " ApplyRefact2.hs" " haskell"
88
- diags <- waitForDiagnosticsFromSource doc " hlint"
89
-
90
- liftIO $ length diags > 0 @? " There are hlint diagnostics"
89
+ testHlintDiagnostics doc
91
90
92
91
let config' = def { hlintOn = False }
93
92
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config'))
94
93
95
94
diags' <- waitForDiagnosticsFrom doc
96
95
97
- liftIO $ Just " hlint" `notElem` map (^. L. source) diags' @? " There are no hlint diagnostics"
98
-
99
- , testCase " changing document contents updates hlint diagnostics" $ runSession hlsCommand fullCaps " test/testdata/hlint" $ do
100
- doc <- openDoc " ApplyRefact2.hs" " haskell"
101
- diags <- waitForDiagnosticsSource " hlint"
102
-
103
- liftIO $ length diags @?= 2 -- "Eta Reduce" and "Redundant Id"
104
-
105
- let change = TextDocumentContentChangeEvent
106
- (Just (Range (Position 1 8 ) (Position 1 12 )))
107
- Nothing " x"
96
+ liftIO $ noHlintDiagnostics diags'
108
97
109
- changeDoc doc [change]
98
+ , knownBrokenForGhcVersions [GHC88 , GHC86 ] " hlint doesn't take in account cpp flag as ghc -D argument" $
99
+ testCase " hlint diagnostics works with CPP via ghc -XCPP argument (#554)" $ runHlintSession " cpp" $ do
100
+ doc <- openDoc " ApplyRefact3.hs" " haskell"
101
+ testHlintDiagnostics doc
110
102
111
- diags' <- waitForDiagnostics
103
+ , knownBrokenForGhcVersions [GHC88 , GHC86 ] " hlint doesn't take in account cpp flag as ghc -D argument" $
104
+ testCase " hlint diagnostics works with CPP via language pragma (#554)" $ runHlintSession " " $ do
105
+ doc <- openDoc " ApplyRefact3.hs" " haskell"
106
+ testHlintDiagnostics doc
112
107
113
- liftIO $ (not $ Just " hlint" `elem` map (^. L. source) diags') @? " There are no hlint diagnostics"
108
+ , testCase " hlint diagnostics works with CPP via -XCPP argument and flag via #include header (#554)" $ runHlintSession " cpp" $ do
109
+ doc <- openDoc " ApplyRefact2.hs" " haskell"
110
+ testHlintDiagnostics doc
111
+
112
+ , knownBrokenForGhcVersions [GHC88 , GHC86 ] " apply-refact doesn't take in account the -X argument" $
113
+ testCase " apply-refact works with LambdaCase via ghc -XLambdaCase argument (#590)" $ runHlintSession " lambdacase" $ do
114
+ testRefactor " ApplyRefact1.hs" " Redundant bracket"
115
+ expectedLambdaCase
116
+
117
+ , testCase " apply hints works with LambdaCase via language pragma" $ runHlintSession " " $ do
118
+ testRefactor " ApplyRefact1.hs" " Redundant bracket"
119
+ (" {-# LANGUAGE LambdaCase #-}" : expectedLambdaCase)
120
+
121
+ , expectFailBecause " apply-refact doesn't work with cpp" $
122
+ testCase " apply hints works with CPP via -XCPP argument" $ runHlintSession " cpp" $ do
123
+ testRefactor " ApplyRefact3.hs" " Redundant bracket"
124
+ expectedCPP
125
+
126
+ , expectFailBecause " apply-refact doesn't work with cpp" $
127
+ testCase " apply hints works with CPP via language pragma" $ runHlintSession " " $ do
128
+ testRefactor " ApplyRefact3.hs" " Redundant bracket"
129
+ (" {-# LANGUAGE CPP #-}" : expectedCPP)
130
+
131
+ , testCase " hlint diagnostics ignore hints honouring .hlint.yaml" $ runHlintSession " ignore" $ do
132
+ doc <- openDoc " ApplyRefact.hs" " haskell"
133
+ expectNoMoreDiagnostics 3 doc " hlint"
134
+
135
+ , testCase " hlint diagnostics ignore hints honouring ANN annotations" $ runHlintSession " " $ do
136
+ doc <- openDoc " ApplyRefact4.hs" " haskell"
137
+ expectNoMoreDiagnostics 3 doc " hlint"
138
+
139
+ , knownBrokenForGhcVersions [GHC810 ] " hlint plugin doesn't honour HLINT annotations (#838)" $
140
+ testCase " hlint diagnostics ignore hints honouring HLINT annotations" $ runHlintSession " " $ do
141
+ doc <- openDoc " ApplyRefact5.hs" " haskell"
142
+ expectNoMoreDiagnostics 3 doc " hlint"
143
+ ]
144
+ where
145
+ runHlintSession :: FilePath -> Session a -> IO a
146
+ runHlintSession subdir =
147
+ failIfSessionTimeout . runSession hlsCommand fullCaps (" test/testdata/hlint" </> subdir)
114
148
115
- let change' = TextDocumentContentChangeEvent
116
- ( Just ( Range ( Position 1 8 ) ( Position 1 12 )))
117
- Nothing " id x "
149
+ noHlintDiagnostics :: [ Diagnostic ] -> Assertion
150
+ noHlintDiagnostics diags =
151
+ Just " hlint " `notElem` map ( ^. L. source) diags @? " There are no hlint diagnostics "
118
152
119
- changeDoc doc [change']
153
+ testHlintDiagnostics doc = do
154
+ diags <- waitForDiagnosticsFromSource doc " hlint"
155
+ liftIO $ length diags > 0 @? " There are hlint diagnostics"
120
156
121
- diags'' <- waitForDiagnosticsFromSource doc " hlint"
157
+ testRefactor file caTitle expected = do
158
+ doc <- openDoc file " haskell"
159
+ testHlintDiagnostics doc
122
160
123
- liftIO $ length diags'' @?= 2
124
- ]
161
+ cas <- map fromAction <$> getAllCodeActions doc
162
+ let ca = find (\ ca -> caTitle `T.isSuffixOf` (ca ^. L. title)) cas
163
+ liftIO $ isJust ca @? (" There is '" ++ T. unpack caTitle ++ " ' code action" )
164
+
165
+ executeCodeAction (fromJust ca)
166
+
167
+ contents <- getDocumentEdit doc
168
+ liftIO $ contents @?= T. unlines expected
169
+
170
+ expectedLambdaCase = [ " module ApplyRefact1 where" , " "
171
+ , " f = \\ case \" true\" -> True"
172
+ , " _ -> False"
173
+ ]
174
+ expectedCPP = [ " module ApplyRefact3 where" , " "
175
+ , " #ifdef FLAG"
176
+ , " f = 1"
177
+ , " #else"
178
+ , " g = 2"
179
+ , " #endif" , " "
180
+ ]
125
181
126
182
renameTests :: TestTree
127
183
renameTests = testGroup " rename suggestions" [
0 commit comments