Skip to content

Commit 4c9de9e

Browse files
In tests, filter out diagnostics from other files.
In a unit test, when we tell the server to open "SomeFile.hs" it might also open "SomeOtherFile.hs" because they both use the same cradle. Then we get diagnostics for both.
1 parent e9638a5 commit 4c9de9e

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

test/functional/FunctionalCodeAction.hs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ hlintTests :: TestTree
4343
hlintTests = testGroup "hlint suggestions" [
4444
testCase "provides 3.8 code actions including apply all" $ runSession hlsCommand fullCaps "test/testdata/hlint" $ do
4545
doc <- openDoc "ApplyRefact2.hs" "haskell"
46-
diags@(reduceDiag:_) <- waitForDiagnosticsSource "hlint"
46+
diags@(reduceDiag:_) <- waitForDiagnosticsFromSource doc "hlint"
4747

4848
liftIO $ do
4949
length diags @?= 2 -- "Eta Reduce" and "Redundant Id"
@@ -70,7 +70,7 @@ hlintTests = testGroup "hlint suggestions" [
7070
, testCase "falls back to pre 3.8 code actions" $ runSession hlsCommand noLiteralCaps "test/testdata/hlint" $ do
7171
doc <- openDoc "ApplyRefact2.hs" "haskell"
7272

73-
_ <- waitForDiagnosticsSource "hlint"
73+
_ <- waitForDiagnosticsFromSource doc "hlint"
7474

7575
cars <- getAllCodeActions doc
7676
etaReduce <- liftIO $ inspectCommand cars ["Apply hint: Eta reduce"]
@@ -84,15 +84,15 @@ hlintTests = testGroup "hlint suggestions" [
8484
let config = def { hlintOn = True }
8585
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
8686

87-
_ <- openDoc "ApplyRefact2.hs" "haskell"
88-
diags <- waitForDiagnosticsSource "hlint"
87+
doc <- openDoc "ApplyRefact2.hs" "haskell"
88+
diags <- waitForDiagnosticsFromSource doc "hlint"
8989

9090
liftIO $ length diags > 0 @? "There are hlint diagnostics"
9191

9292
let config' = def { hlintOn = False }
9393
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config'))
9494

95-
diags' <- waitForDiagnostics
95+
diags' <- waitForDiagnosticsFrom doc
9696

9797
liftIO $ Just "hlint" `notElem` map (^. L.source) diags' @? "There are no hlint diagnostics"
9898

@@ -118,7 +118,7 @@ hlintTests = testGroup "hlint suggestions" [
118118

119119
changeDoc doc [change']
120120

121-
diags'' <- waitForDiagnosticsSource "hlint"
121+
diags'' <- waitForDiagnosticsFromSource doc "hlint"
122122

123123
liftIO $ length diags'' @?= 2
124124
]
@@ -128,7 +128,7 @@ renameTests = testGroup "rename suggestions" [
128128
testCase "works" $ runSession hlsCommand noLiteralCaps "test/testdata" $ do
129129
doc <- openDoc "CodeActionRename.hs" "haskell"
130130

131-
_ <- waitForDiagnosticsSource "typecheck"
131+
_ <- waitForDiagnosticsFromSource doc "typecheck"
132132

133133
cars <- getAllCodeActions doc
134134
replaceButStrLn <- liftIO $ inspectCommand cars ["Replace with", "putStrLn"]
@@ -141,7 +141,7 @@ renameTests = testGroup "rename suggestions" [
141141
$ runSession hlsCommand noLiteralCaps "test/testdata" $ do
142142
doc <- openDoc "CodeActionRename.hs" "haskell"
143143

144-
_ <- waitForDiagnosticsSource "typecheck"
144+
_ <- waitForDiagnosticsFromSource doc "typecheck"
145145

146146
cars <- getAllCodeActions doc
147147
cmd <- liftIO $ inspectCommand cars ["Replace with", "putStrLn"]
@@ -168,7 +168,7 @@ importTests = testGroup "import suggestions" [
168168
let config = def { formattingProvider = "none" }
169169
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config))
170170

171-
diag:_ <- waitForDiagnostics
171+
(diag:_) <- waitForDiagnosticsFrom doc
172172
liftIO $ diag ^. L.message @?= "Variable not in scope: when :: Bool -> IO () -> IO ()"
173173

174174
actionsOrCommands <- getAllCodeActions doc
@@ -195,7 +195,7 @@ packageTests = testGroup "add package suggestions" [
195195
doc <- openDoc "AddPackage.hs" "haskell"
196196

197197
-- ignore the first empty hlint diagnostic publish
198-
[_,diag:_] <- count 2 waitForDiagnostics
198+
[_,diag:_] <- count 2 $ waitForDiagnosticsFrom doc
199199

200200
let prefixes = [ "Could not load module `Data.Text'" -- Windows && GHC >= 8.6
201201
, "Could not find module `Data.Text'" -- Windows
@@ -223,7 +223,7 @@ packageTests = testGroup "add package suggestions" [
223223
doc <- openDoc "app/Asdf.hs" "haskell"
224224

225225
-- ignore the first empty hlint diagnostic publish
226-
[_,_:diag:_] <- count 2 waitForDiagnostics
226+
[_,_:diag:_] <- count 2 $ waitForDiagnosticsFrom doc
227227

228228
let prefixes = [ "Could not load module `Codec.Compression.GZip'" -- Windows && GHC >= 8.6
229229
, "Could not find module `Codec.Compression.GZip'" -- Windows
@@ -255,7 +255,7 @@ redundantImportTests = testGroup "redundant import code actions" [
255255
runSession hlsCommand fullCaps "test/testdata/redundantImportTest/" $ do
256256
doc <- openDoc "src/CodeActionRedundant.hs" "haskell"
257257

258-
diags <- waitForDiagnostics
258+
diags <- waitForDiagnosticsFrom doc
259259
liftIO $ expectDiagnostic diags ["The import of", "Data.List", "is redundant"]
260260

261261
mActions <- getAllCodeActions doc
@@ -280,7 +280,7 @@ redundantImportTests = testGroup "redundant import code actions" [
280280

281281
, testCase "doesn't touch other imports" $ runSession hlsCommand noLiteralCaps "test/testdata/redundantImportTest/" $ do
282282
doc <- openDoc "src/MultipleImports.hs" "haskell"
283-
_ <- waitForDiagnostics
283+
_ <- waitForDiagnosticsFrom doc
284284
CACommand cmd : _ <- getAllCodeActions doc
285285
executeCommand cmd
286286
contents <- documentContents doc
@@ -297,7 +297,7 @@ typedHoleTests = testGroup "typed hole code actions" [
297297
testCase "works" $
298298
runSession hlsCommand fullCaps "test/testdata" $ do
299299
doc <- openDoc "TypedHoles.hs" "haskell"
300-
_ <- waitForDiagnosticsSource "typecheck"
300+
_ <- waitForDiagnosticsFromSource doc "typecheck"
301301
cas <- getAllCodeActions doc
302302
liftIO $ do
303303
expectCodeAction cas ["replace _ with minBound"]
@@ -317,7 +317,7 @@ typedHoleTests = testGroup "typed hole code actions" [
317317
, testCase "shows more suggestions" $
318318
runSession hlsCommand fullCaps "test/testdata" $ do
319319
doc <- openDoc "TypedHoles2.hs" "haskell"
320-
_ <- waitForDiagnosticsSource "typecheck"
320+
_ <- waitForDiagnosticsFromSource doc "typecheck"
321321
cas <- getAllCodeActions doc
322322

323323
liftIO $ do
@@ -345,7 +345,7 @@ signatureTests = testGroup "missing top level signature code actions" [
345345
runSession hlsCommand fullCaps "test/testdata/" $ do
346346
doc <- openDoc "TopLevelSignature.hs" "haskell"
347347

348-
_ <- waitForDiagnosticsSource "typecheck"
348+
_ <- waitForDiagnosticsFromSource doc "typecheck"
349349
cas <- map fromAction <$> getAllCodeActions doc
350350

351351
liftIO $ "add signature: main :: IO ()" `elem` (map (^. L.title) cas) @? "Contains code action"
@@ -371,7 +371,7 @@ missingPragmaTests = testGroup "missing pragma warning code actions" [
371371
runSession hlsCommand fullCaps "test/testdata/addPragmas" $ do
372372
doc <- openDoc "NeedsPragmas.hs" "haskell"
373373

374-
_ <- waitForDiagnosticsSource "typecheck"
374+
_ <- waitForDiagnosticsFromSource doc "typecheck"
375375
cas <- map fromAction <$> getAllCodeActions doc
376376

377377
liftIO $ "Add \"TypeSynonymInstances\"" `elem` map (^. L.title) cas @? "Contains TypeSynonymInstances code action"
@@ -408,7 +408,7 @@ unusedTermTests = testGroup "unused term code actions" [
408408
runSession hlsCommand fullCaps "test/testdata/" $ do
409409
doc <- openDoc "UnusedTerm.hs" "haskell"
410410

411-
_ <- waitForDiagnosticsSource "typecheck"
411+
_ <- waitForDiagnosticsFromSource doc "typecheck"
412412
cars <- getAllCodeActions doc
413413
prefixImUnused <- liftIO $ inspectCodeAction cars ["Prefix imUnused with _"]
414414

@@ -430,7 +430,7 @@ unusedTermTests = testGroup "unused term code actions" [
430430
-- `CodeActionContext`
431431
, testCase "respect 'only' parameter" $ runSession hlsCommand fullCaps "test/testdata" $ do
432432
doc <- openDoc "CodeActionOnly.hs" "haskell"
433-
_ <- waitForDiagnostics
433+
_ <- waitForDiagnosticsFrom doc
434434
diags <- getCurrentDiagnostics doc
435435
let params = CodeActionParams doc (Range (Position 2 10) (Position 4 0)) caContext Nothing
436436
caContext = CodeActionContext (List diags) (Just (List [CodeActionRefactorInline]))
@@ -482,3 +482,23 @@ inspectCommand cars s = fromCommand <$> onMatch cars pred err
482482
where pred (CACommand command) = all (`T.isInfixOf` (command ^. L.title)) s
483483
pred _ = False
484484
err = "expected code action matching '" ++ show s ++ "' but did not find one"
485+
486+
waitForDiagnosticsFrom :: TextDocumentIdentifier -> Session [Diagnostic]
487+
waitForDiagnosticsFrom doc = do
488+
diagsNot <- skipManyTill anyMessage message :: Session PublishDiagnosticsNotification
489+
let (List diags) = diagsNot ^. L.params . L.diagnostics
490+
if doc ^. L.uri /= diagsNot ^. L.params . L.uri
491+
then waitForDiagnosticsFrom doc
492+
else return diags
493+
494+
waitForDiagnosticsFromSource :: TextDocumentIdentifier -> String -> Session [Diagnostic]
495+
waitForDiagnosticsFromSource doc src = do
496+
diagsNot <- skipManyTill anyMessage message :: Session PublishDiagnosticsNotification
497+
let (List diags) = diagsNot ^. L.params . L.diagnostics
498+
let res = filter matches diags
499+
if doc ^. L.uri /= diagsNot ^. L.params . L.uri || null res
500+
then waitForDiagnosticsFromSource doc src
501+
else return res
502+
where
503+
matches :: Diagnostic -> Bool
504+
matches d = d ^. L.source == Just (T.pack src)

0 commit comments

Comments
 (0)