@@ -10,13 +10,13 @@
1010module Main (main) where
1111
1212import Control.Applicative.Combinators
13−import Control.Exception (bracket, catch)
14−import qualified Control.Lens as Lens
13+import Control.Exception (catch)
1514import Control.Monad
1615import Control.Monad.IO.Class (liftIO)
1716import Data.Aeson (FromJSON, Value)
17+import Data.Char (toLower)
1818import Data.Foldable
19−import Data.List.Extra
19+import Data.List
2020import Data.Rope.UTF16 (Rope)
2121import qualified Data.Rope.UTF16 as Rope
2222import Development.IDE.Core.PositionMapping (fromCurrent, toCurrent)
@@ -28,14 +28,14 @@ import Development.IDE.Test
2828import Development.IDE.Test.Runfiles
2929import Development.IDE.Types.Location
3030import Development.Shake (getDirectoryFilesIO)
31−import Language.Haskell.LSP.Test
31+import qualified Language.Haskell.LSP.Test as LSPTest
32+import Language.Haskell.LSP.Test hiding (openDoc')
3233import Language.Haskell.LSP.Messages
3334import Language.Haskell.LSP.Types
3435import Language.Haskell.LSP.Types.Capabilities
35−import qualified Language.Haskell.LSP.Types.Lens as Lsp (diagnostics, params, message)
3636import Language.Haskell.LSP.VFS (applyChange)
3737import Network.URI
38−import System.Environment.Blank (getEnv, setEnv, unsetEnv)
38+import System.Environment.Blank (setEnv)
3939import System.FilePath
4040import System.IO.Extra
4141import System.Directory
@@ -49,35 +49,31 @@ import Test.Tasty.QuickCheck
4949import Data.Maybe
5050
5151main :: IO ()
52−main = do
53− -- We mess with env vars so run single-threaded.
54− setEnv "TASTY_NUM_THREADS" "1" True
55− defaultMainWithRerun $ testGroup "HIE"
56− [ testSession "open close" $ do
57− doc <- createDoc "Testing.hs" "haskell" ""
58− void (skipManyTill anyMessage message :: Session WorkDoneProgressCreateRequest)
59− void (skipManyTill anyMessage message :: Session WorkDoneProgressBeginNotification)
60− closeDoc doc
61− void (skipManyTill anyMessage message :: Session WorkDoneProgressEndNotification)
62− , initializeResponseTests
63− , completionTests
64− , cppTests
65− , diagnosticTests
66− , codeActionTests
67− , codeLensesTests
68− , outlineTests
69− , findDefinitionAndHoverTests
70− , pluginTests
71− , preprocessorTests
72− , thTests
73− , safeTests
74− , unitTests
75− , haddockTests
76− , positionMappingTests
77− , watchedFilesTests
78− , cradleTests
79− , dependentFileTest
80− ]
52+main = defaultMainWithRerun $ testGroup "HIE"
53+ [ testSession "open close" $ do
54+ doc <- openDoc' "Testing.hs" "haskell" ""
55+ void (skipManyTill anyMessage message :: Session WorkDoneProgressCreateRequest)
56+ void (skipManyTill anyMessage message :: Session WorkDoneProgressBeginNotification)
57+ closeDoc doc
58+ void (skipManyTill anyMessage message :: Session WorkDoneProgressEndNotification)
59+ , initializeResponseTests
60+ , completionTests
61+ , cppTests
62+ , diagnosticTests
63+ , codeActionTests
64+ , codeLensesTests
65+ , outlineTests
66+ , findDefinitionAndHoverTests
67+ , pluginTests
68+ , preprocessorTests
69+ , thTests
70+ , safeTests
71+ , unitTests
72+ , haddockTests
73+ , positionMappingTests
74+ , watchedFilesTests
75+ , cradleTests
76+ ]
8177
8278initializeResponseTests :: TestTree
8379initializeResponseTests = withResource acquire release tests where
@@ -133,12 +129,12 @@ initializeResponseTests = withResource acquire release tests where
133129 where
134130 doTest = do
135131 ir <- getInitializeResponse
136− let Just ExecuteCommandOptions {_commands = List [command]} = getActual $ innerCaps ir
137− True @=? T.isSuffixOf "typesignature.add" command
132+ let Just (ExecuteCommandOptions {_commands = List [command]}) = getActual $ innerCaps ir
133+ True @=? (T.isSuffixOf "typesignature.add" command)
138134
139135
140136 innerCaps :: InitializeResponse -> InitializeResponseCapabilitiesInner
141− innerCaps (ResponseMessage _ _ (Right (InitializeResponseCapabilities c))) = c
137+ innerCaps (ResponseMessage _ _ (Just (InitializeResponseCapabilities c)) _) = c
142138 innerCaps _ = error "this test only expects inner capabilities"
143139
144140 acquire :: IO InitializeResponse
@@ -152,7 +148,7 @@ diagnosticTests :: TestTree
152148diagnosticTests = testGroup "diagnostics"
153149 [ testSessionWait "fix syntax error" $ do
154150 let content = T.unlines [ "module Testing wher" ]
155− doc <- createDoc "Testing.hs" "haskell" content
151+ doc <- openDoc' "Testing.hs" "haskell" content
156152 expectDiagnostics [("Testing.hs", [(DsError, (0, 15), "parse error")])]
157153 let change = TextDocumentContentChangeEvent
158154 { _range = Just (Range (Position 0 15) (Position 0 19))
@@ -163,7 +159,7 @@ diagnosticTests = testGroup "diagnostics"
163159 expectDiagnostics [("Testing.hs", [])]
164160 , testSessionWait "introduce syntax error" $ do
165161 let content = T.unlines [ "module Testing where" ]
166− doc <- createDoc "Testing.hs" "haskell" content
162+ doc <- openDoc' "Testing.hs" "haskell" content
167163 void $ skipManyTill anyMessage (message :: Session WorkDoneProgressCreateRequest)
168164 void $ skipManyTill anyMessage (message :: Session WorkDoneProgressBeginNotification)
169165 let change = TextDocumentContentChangeEvent
@@ -181,7 +177,7 @@ diagnosticTests = testGroup "diagnostics"
181177 , "bar :: Int -> Int -> Int"
182178 , "bar a b = cd + b"
183179 ]
184− _ <- createDoc "Testing.hs" "haskell" content
180+ _ <- openDoc' "Testing.hs" "haskell" content
185181 expectDiagnostics
186182 [ ( "Testing.hs"
187183 , [ (DsError, (2, 14), "Variable not in scope: ab")
@@ -195,7 +191,7 @@ diagnosticTests = testGroup "diagnostics"
195191 , "foo :: Int -> String -> Int"
196192 , "foo a b = a + b"
197193 ]
198− _ <- createDoc "Testing.hs" "haskell" content
194+ _ <- openDoc' "Testing.hs" "haskell" content
199195 expectDiagnostics
200196 [ ( "Testing.hs"
201197 , [(DsError, (2, 14), "Couldn't match type '[Char]' with 'Int'")]
@@ -207,7 +203,7 @@ diagnosticTests = testGroup "diagnostics"
207203 , "foo :: Int -> String"
208204 , "foo a = _ a"
209205 ]
210− _ <- createDoc "Testing.hs" "haskell" content
206+ _ <- openDoc' "Testing.hs" "haskell" content
211207 expectDiagnostics
212208 [ ( "Testing.hs"
213209 , [(DsError, (2, 8), "Found hole: _ :: Int -> String")]
@@ -229,23 +225,24 @@ diagnosticTests = testGroup "diagnostics"
229225 [ ("A.hs", [(DsError, (2,4), aMessage)])
230226 , ("B.hs", [(DsError, (3,4), bMessage)])]
231227 deferralTest title binding msg = testSessionWait title $ do
232− _ <- createDoc "A.hs" "haskell" $ sourceA binding
233− _ <- createDoc "B.hs" "haskell" sourceB
228+ _ <- openDoc' "A.hs" "haskell" $ sourceA binding
229+ _ <- openDoc' "B.hs" "haskell" sourceB
234230 expectDiagnostics $ expectedDs msg
235231 in
236232 [ deferralTest "type error" "True" "Couldn't match expected type"
237233 , deferralTest "typed hole" "_" "Found hole"
238234 , deferralTest "out of scope var" "unbound" "Variable not in scope"
235+ , deferralTest "message shows error" "True" "A.hs:3:5: error:"
239236 ]
240237
241238 , testSessionWait "remove required module" $ do
242239 let contentA = T.unlines [ "module ModuleA where" ]
243− docA <- createDoc "ModuleA.hs" "haskell" contentA
240+ docA <- openDoc' "ModuleA.hs" "haskell" contentA
244241 let contentB = T.unlines
245242 [ "module ModuleB where"
246243 , "import ModuleA"
247244 ]
248− _ <- createDoc "ModuleB.hs" "haskell" contentB
245+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
249246 let change = TextDocumentContentChangeEvent
250247 { _range = Just (Range (Position 0 0) (Position 0 20))
251248 , _rangeLength = Nothing
@@ -258,20 +255,20 @@ diagnosticTests = testGroup "diagnostics"
258255 [ "module ModuleB where"
259256 , "import ModuleA"
260257 ]
261− _ <- createDoc "ModuleB.hs" "haskell" contentB
258+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
262259 expectDiagnostics [("ModuleB.hs", [(DsError, (1, 7), "Could not find module")])]
263260 let contentA = T.unlines [ "module ModuleA where" ]
264− _ <- createDoc "ModuleA.hs" "haskell" contentA
261+ _ <- openDoc' "ModuleA.hs" "haskell" contentA
265262 expectDiagnostics [("ModuleB.hs", [])]
266263 , testSessionWait "add missing module (non workspace)" $ do
267264 let contentB = T.unlines
268265 [ "module ModuleB where"
269266 , "import ModuleA"
270267 ]
271− _ <- createDoc "/tmp/ModuleB.hs" "haskell" contentB
268+ _ <- openDoc'' "/tmp/ModuleB.hs" "haskell" contentB
272269 expectDiagnostics [("/tmp/ModuleB.hs", [(DsError, (1, 7), "Could not find module")])]
273270 let contentA = T.unlines [ "module ModuleA where" ]
274− _ <- createDoc "/tmp/ModuleA.hs" "haskell" contentA
271+ _ <- openDoc'' "/tmp/ModuleA.hs" "haskell" contentA
275272 expectDiagnostics [("/tmp/ModuleB.hs", [])]
276273 , testSessionWait "cyclic module dependency" $ do
277274 let contentA = T.unlines
@@ -282,8 +279,8 @@ diagnosticTests = testGroup "diagnostics"
282279 [ "module ModuleB where"
283280 , "import ModuleA"
284281 ]
285− _ <- createDoc "ModuleA.hs" "haskell" contentA
286− _ <- createDoc "ModuleB.hs" "haskell" contentB
282+ _ <- openDoc' "ModuleA.hs" "haskell" contentA
283+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
287284 expectDiagnostics
288285 [ ( "ModuleA.hs"
289286 , [(DsError, (1, 7), "Cyclic module dependency between ModuleA, ModuleB")]
@@ -304,9 +301,9 @@ diagnosticTests = testGroup "diagnostics"
304301 let contentBboot = T.unlines
305302 [ "module ModuleB where"
306303 ]
307− _ <- createDoc "ModuleA.hs" "haskell" contentA
308− _ <- createDoc "ModuleB.hs" "haskell" contentB
309− _ <- createDoc "ModuleB.hs-boot" "haskell" contentBboot
304+ _ <- openDoc' "ModuleA.hs" "haskell" contentA
305+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
306+ _ <- openDoc' "ModuleB.hs-boot" "haskell" contentBboot
310307 expectDiagnostics []
311308 , testSessionWait "correct reference used with hs-boot" $ do
312309 let contentB = T.unlines
@@ -328,10 +325,10 @@ diagnosticTests = testGroup "diagnostics"
328325 -- resolved to the hs-boot file
329326 , "y = x"
330327 ]
331− _ <- createDoc "ModuleB.hs" "haskell" contentB
332− _ <- createDoc "ModuleA.hs" "haskell" contentA
333− _ <- createDoc "ModuleA.hs-boot" "haskell" contentAboot
334− _ <- createDoc "ModuleC.hs" "haskell" contentC
328+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
329+ _ <- openDoc' "ModuleA.hs" "haskell" contentA
330+ _ <- openDoc' "ModuleA.hs-boot" "haskell" contentAboot
331+ _ <- openDoc' "ModuleC.hs" "haskell" contentC
335332 expectDiagnostics []
336333 , testSessionWait "redundant import" $ do
337334 let contentA = T.unlines ["module ModuleA where"]
@@ -340,8 +337,8 @@ diagnosticTests = testGroup "diagnostics"
340337 , "module ModuleB where"
341338 , "import ModuleA"
342339 ]
343− _ <- createDoc "ModuleA.hs" "haskell" contentA
344− _ <- createDoc "ModuleB.hs" "haskell" contentB
340+ _ <- openDoc' "ModuleA.hs" "haskell" contentA
341+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
345342 expectDiagnostics
346343 [ ( "ModuleB.hs"
347344 , [(DsWarning, (2, 0), "The import of 'ModuleA' is redundant")]
@@ -363,8 +360,8 @@ diagnosticTests = testGroup "diagnostics"
363360 , "wrong1 = ThisList.map"
364361 , "wrong2 = BaseList.x"
365362 ]
366− _ <- createDoc "Data/List.hs" "haskell" thisDataListContent
367− _ <- createDoc "Main.hs" "haskell" mainContent
363+ _ <- openDoc' "Data/List.hs" "haskell" thisDataListContent
364+ _ <- openDoc' "Main.hs" "haskell" mainContent
368365 expectDiagnostics
369366 [ ( "Main.hs"
370367 , [(DsError, (6, 9), "Not in scope: \8216ThisList.map\8217")
@@ -379,7 +376,7 @@ diagnosticTests = testGroup "diagnostics"
379376 , "foo :: Ord a => a -> Int"
380377 , "foo a = 1"
381378 ]
382− _ <- createDoc "Foo.hs" "haskell" fooContent
379+ _ <- openDoc' "Foo.hs" "haskell" fooContent
383380 expectDiagnostics
384381 [ ( "Foo.hs"
385382 -- The test is to make sure that warnings contain unqualified names
@@ -404,14 +401,14 @@ diagnosticTests = testGroup "diagnostics"
404401 Just pathB <- pure $ uriToFilePath uriB
405402 uriB <- pure $
406403 let (drive, suffix) = splitDrive pathB
407− in filePathToUri (joinDrive (lower drive) suffix)
404+ in filePathToUri (joinDrive (map toLower drive ) suffix)
408405 liftIO $ createDirectoryIfMissing True (takeDirectory pathB)
409406 liftIO $ writeFileUTF8 pathB $ T.unpack bContent
410407 uriA <- getDocUri "A/A.hs"
411408 Just pathA <- pure $ uriToFilePath uriA
412409 uriA <- pure $
413410 let (drive, suffix) = splitDrive pathA
414− in filePathToUri (joinDrive (lower drive) suffix)
411+ in filePathToUri (joinDrive (map toLower drive ) suffix)
415412 let itemA = TextDocumentItem uriA "haskell" 0 aContent
416413 let a = TextDocumentIdentifier uriA
417414 sendNotification TextDocumentDidOpen (DidOpenTextDocumentParams itemA)
@@ -430,32 +427,13 @@ diagnosticTests = testGroup "diagnostics"
430427 , "foo :: Int"
431428 , "foo = 1 {-|-}"
432429 ]
433− _ <- createDoc "Foo.hs" "haskell" fooContent
430+ _ <- openDoc' "Foo.hs" "haskell" fooContent
434431 expectDiagnostics
435432 [ ( "Foo.hs"
436433 , [(DsError, (2, 8), "Parse error on input")
437434 ]
438435 )
439436 ]
440− , testSessionWait "strip file path" $ do
441− let
442− name = "Testing"
443− content = T.unlines
444− [ "module " <> name <> " where"
445− , "value :: Maybe ()"
446− , "value = [()]"
447− ]
448− _ <- createDoc (T.unpack name <> ".hs") "haskell" content
449− notification <- skipManyTill anyMessage diagnostic
450− let
451− offenders =
452− Lsp.params .
453− Lsp.diagnostics .
454− Lens.folded .
455− Lsp.message .
456− Lens.filtered (T.isInfixOf ("/" <> name <> ".hs:"))
457− failure msg = liftIO $ assertFailure $ "Expected file path to be stripped but got " <> T.unpack msg
458− Lens.mapMOf_ offenders failure notification
459437 ]
460438
461439codeActionTests :: TestTree
@@ -481,26 +459,30 @@ codeLensesTests = testGroup "code lenses"
481459watchedFilesTests :: TestTree
482460watchedFilesTests = testGroup "watched files"
483461 [ testSession' "workspace files" $ \sessionDir -> do
484− liftIO $ writeFile (sessionDir </> "hie.yaml") "cradle: {direct: {arguments: [\"-isrc\"]}}"
485− _doc <- createDoc "A.hs" "haskell" "{-#LANGUAGE NoImplicitPrelude #-}\nmodule A where\nimport WatchedFilesMissingModule"
462+ liftIO $ writeFile (sessionDir </> "hie.yaml") $ "cradle: {direct: {arguments: [\"-isrc\"]}}"
463+ _doc <- openDoc' "A.hs" "haskell" "{-#LANGUAGE NoImplicitPrelude #-}\nmodule A where\nimport WatchedFilesMissingModule"
486464 watchedFileRegs <- getWatchedFilesSubscriptionsUntil @PublishDiagnosticsNotification
487465
488− -- Expect 4 subscriptions (A does not get any because it's VFS):
466+ -- Expect 6 subscriptions (A does not get any because it's VFS):
489467 -- - /path-to-workspace/WatchedFilesMissingModule.hs
490468 -- - /path-to-workspace/WatchedFilesMissingModule.lhs
491− -- - /path-to-workspace/src/WatchedFilesMissingModule.hs
492− -- - /path-to-workspace/src/WatchedFilesMissingModule.lhs
493− liftIO $ length watchedFileRegs @?= 4
469+ -- - WatchedFilesMissingModule.hs
470+ -- - WatchedFilesMissingModule.lhs
471+ -- - src/WatchedFilesMissingModule.hs
472+ -- - src/WatchedFilesMissingModule.lhs
473+ liftIO $ length watchedFileRegs @?= 6
494474
495475 , testSession' "non workspace file" $ \sessionDir -> do
496− liftIO $ writeFile (sessionDir </> "hie.yaml") "cradle: {direct: {arguments: [\"-i/tmp\"]}}"
497− _doc <- createDoc "A.hs" "haskell" "{-# LANGUAGE NoImplicitPrelude#-}\nmodule A where\nimport WatchedFilesMissingModule"
476+ liftIO $ writeFile (sessionDir </> "hie.yaml") $ "cradle: {direct: {arguments: [\"-i/tmp\"]}}"
477+ _doc <- openDoc' "A.hs" "haskell" "{-# LANGUAGE NoImplicitPrelude#-}\nmodule A where\nimport WatchedFilesMissingModule"
498478 watchedFileRegs <- getWatchedFilesSubscriptionsUntil @PublishDiagnosticsNotification
499479
500− -- Expect 2 subscriptions (/tmp does not get any as it is out of the workspace):
480+ -- Expect 4 subscriptions (/tmp does not get any as it is out of the workspace):
501481 -- - /path-to-workspace/WatchedFilesMissingModule.hs
502482 -- - /path-to-workspace/WatchedFilesMissingModule.lhs
503− liftIO $ length watchedFileRegs @?= 2
483+ -- - WatchedFilesMissingModule.hs
484+ -- - WatchedFilesMissingModule.lhs
485+ liftIO $ length watchedFileRegs @?= 4
504486
505487 -- TODO add a test for didChangeWorkspaceFolder
506488 ]
@@ -513,7 +495,7 @@ renameActionTests = testGroup "rename actions"
513495 , "foo :: Int -> Int"
514496 , "foo argName = argNme"
515497 ]
516− doc <- createDoc "Testing.hs" "haskell" content
498+ doc <- openDoc' "Testing.hs" "haskell" content
517499 _ <- waitForDiagnostics
518500 action <- findCodeAction doc (Range (Position 2 14) (Position 2 20)) "Replace with ‘argName’"
519501 executeCodeAction action
@@ -531,7 +513,7 @@ renameActionTests = testGroup "rename actions"
531513 , "foo :: Maybe a -> [a]"
532514 , "foo = maybToList"
533515 ]
534− doc <- createDoc "Testing.hs" "haskell" content
516+ doc <- openDoc' "Testing.hs" "haskell" content
535517 _ <- waitForDiagnostics
536518 action <- findCodeAction doc (Range (Position 3 6) (Position 3 16)) "Replace with ‘maybeToList’"
537519 executeCodeAction action
@@ -549,7 +531,7 @@ renameActionTests = testGroup "rename actions"
549531 , "foo :: Char -> Char -> Char -> Char"
550532 , "foo argument1 argument2 argument3 = argumentX"
551533 ]
552− doc <- createDoc "Testing.hs" "haskell" content
534+ doc <- openDoc' "Testing.hs" "haskell" content
553535 _ <- waitForDiagnostics
554536 _ <- findCodeActions doc (Range (Position 2 36) (Position 2 45))
555537 ["Replace with ‘argument1’", "Replace with ‘argument2’", "Replace with ‘argument3’"]
@@ -561,7 +543,7 @@ renameActionTests = testGroup "rename actions"
561543 , "monus x y = max 0 (x - y)"
562544 , "foo x y = x `monnus` y"
563545 ]
564− doc <- createDoc "Testing.hs" "haskell" content
546+ doc <- openDoc' "Testing.hs" "haskell" content
565547 _ <- waitForDiagnostics
566548 actionsOrCommands <- getCodeActions doc (Range (Position 3 12) (Position 3 20))
567549 [fixTypo] <- pure [action | CACodeAction action@CodeAction{ _title = actionTitle } <- actionsOrCommands, "monus" `T.isInfixOf` actionTitle ]
@@ -584,7 +566,7 @@ typeWildCardActionTests = testGroup "type wildcard actions"
584566 , "func :: _"
585567 , "func x = x"
586568 ]
587− doc <- createDoc "Testing.hs" "haskell" content
569+ doc <- openDoc' "Testing.hs" "haskell" content
588570 _ <- waitForDiagnostics
589571 actionsOrCommands <- getCodeActions doc (Range (Position 2 1) (Position 2 10))
590572 let [addSignature] = [action | CACodeAction action@CodeAction { _title = actionTitle } <- actionsOrCommands
@@ -604,7 +586,7 @@ typeWildCardActionTests = testGroup "type wildcard actions"
604586 , "func :: _"
605587 , "func x y = x + y"
606588 ]
607− doc <- createDoc "Testing.hs" "haskell" content
589+ doc <- openDoc' "Testing.hs" "haskell" content
608590 _ <- waitForDiagnostics
609591 actionsOrCommands <- getCodeActions doc (Range (Position 2 1) (Position 2 10))
610592 let [addSignature] = [action | CACodeAction action@CodeAction { _title = actionTitle } <- actionsOrCommands
@@ -627,7 +609,7 @@ typeWildCardActionTests = testGroup "type wildcard actions"
627609 , " y = x * 2"
628610 , " in y"
629611 ]
630− doc <- createDoc "Testing.hs" "haskell" content
612+ doc <- openDoc' "Testing.hs" "haskell" content
631613 _ <- waitForDiagnostics
632614 actionsOrCommands <- getCodeActions doc (Range (Position 4 1) (Position 4 10))
633615 let [addSignature] = [action | CACodeAction action@CodeAction { _title = actionTitle } <- actionsOrCommands
@@ -652,7 +634,7 @@ removeImportTests = testGroup "remove import actions"
652634 let contentA = T.unlines
653635 [ "module ModuleA where"
654636 ]
655− _docA <- createDoc "ModuleA.hs" "haskell" contentA
637+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
656638 let contentB = T.unlines
657639 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
658640 , "module ModuleB where"
@@ -660,7 +642,7 @@ removeImportTests = testGroup "remove import actions"
660642 , "stuffB :: Integer"
661643 , "stuffB = 123"
662644 ]
663− docB <- createDoc "ModuleB.hs" "haskell" contentB
645+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
664646 _ <- waitForDiagnostics
665647 [CACodeAction action@CodeAction { _title = actionTitle }]
666648 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
@@ -678,7 +660,7 @@ removeImportTests = testGroup "remove import actions"
678660 let contentA = T.unlines
679661 [ "module ModuleA where"
680662 ]
681− _docA <- createDoc "ModuleA.hs" "haskell" contentA
663+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
682664 let contentB = T.unlines
683665 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
684666 , "module ModuleB where"
@@ -686,7 +668,7 @@ removeImportTests = testGroup "remove import actions"
686668 , "stuffB :: Integer"
687669 , "stuffB = 123"
688670 ]
689− docB <- createDoc "ModuleB.hs" "haskell" contentB
671+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
690672 _ <- waitForDiagnostics
691673 [CACodeAction action@CodeAction { _title = actionTitle }]
692674 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
@@ -708,14 +690,14 @@ removeImportTests = testGroup "remove import actions"
708690 , "stuffB = 123"
709691 , "stuffC = ()"
710692 ]
711− _docA <- createDoc "ModuleA.hs" "haskell" contentA
693+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
712694 let contentB = T.unlines
713695 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
714696 , "module ModuleB where"
715697 , "import ModuleA (stuffA, stuffB, stuffC, stuffA)"
716698 , "main = print stuffB"
717699 ]
718− docB <- createDoc "ModuleB.hs" "haskell" contentB
700+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
719701 _ <- waitForDiagnostics
720702 [CACodeAction action@CodeAction { _title = actionTitle }]
721703 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
@@ -737,18 +719,22 @@ removeImportTests = testGroup "remove import actions"
737719 , "stuffB :: Integer"
738720 , "stuffB = 123"
739721 ]
740− _docA <- createDoc "ModuleA.hs" "haskell" contentA
722+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
741723 let contentB = T.unlines
742724 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
743725 , "module ModuleB where"
744726 , "import qualified ModuleA as A ((<?>), stuffB, (!!))"
745727 , "main = print A.stuffB"
746728 ]
747− docB <- createDoc "ModuleB.hs" "haskell" contentB
729+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
748730 _ <- waitForDiagnostics
749731 [CACodeAction action@CodeAction { _title = actionTitle }]
750732 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
733+#if MIN_GHC_API_VERSION(8,6,0)
751734 liftIO $ "Remove !!, <?> from import" @=? actionTitle
735+#else
736+ liftIO $ "Remove A.!!, A.<?> from import" @=? actionTitle
737+#endif
752738 executeCodeAction action
753739 contentAfterAction <- documentContents docB
754740 let expectedContentAfterAction = T.unlines
@@ -765,14 +751,14 @@ removeImportTests = testGroup "remove import actions"
765751 , "stuffB :: Integer"
766752 , "stuffB = 123"
767753 ]
768− _docA <- createDoc "ModuleA.hs" "haskell" contentA
754+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
769755 let contentB = T.unlines
770756 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
771757 , "module ModuleB where"
772758 , "import ModuleA (A(..), stuffB)"
773759 , "main = print stuffB"
774760 ]
775− docB <- createDoc "ModuleB.hs" "haskell" contentB
761+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
776762 _ <- waitForDiagnostics
777763 [CACodeAction action@CodeAction { _title = actionTitle }]
778764 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
@@ -792,14 +778,14 @@ removeImportTests = testGroup "remove import actions"
792778 , "data D = A | B"
793779 , "data E = F"
794780 ]
795− _docA <- createDoc "ModuleA.hs" "haskell" contentA
781+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
796782 let contentB = T.unlines
797783 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
798784 , "module ModuleB where"
799785 , "import ModuleA (D(A,B), E(F))"
800786 , "main = B"
801787 ]
802− docB <- createDoc "ModuleB.hs" "haskell" contentB
788+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
803789 _ <- waitForDiagnostics
804790 [CACodeAction action@CodeAction { _title = actionTitle }]
805791 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
@@ -940,8 +926,8 @@ extendImportTests = testGroup "extend import actions"
940926 ]
941927 where
942928 template contentA contentB range expectedAction expectedContentB = do
943− _docA <- createDoc "ModuleA.hs" "haskell" contentA
944− docB <- createDoc "ModuleB.hs" "haskell" contentB
929+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
930+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
945931 _ <- waitForDiagnostics
946932 CACodeAction action@CodeAction { _title = actionTitle } : _
947933 <- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
@@ -981,8 +967,6 @@ suggestImportTests = testGroup "suggest import actions"
981967 , test True [] "f :: Typeable a => a" ["f = undefined"] "import Data.Typeable (Typeable)"
982968 , test True [] "f = pack" [] "import Data.Text (pack)"
983969 , test True [] "f :: Text" ["f = undefined"] "import Data.Text (Text)"
984− , test True [] "f = [] & id" [] "import Data.Function ((&))"
985− , test True [] "f = (&) [] id" [] "import Data.Function ((&))"
986970 ]
987971 ]
988972 where
@@ -991,20 +975,19 @@ suggestImportTests = testGroup "suggest import actions"
991975 after = T.unlines $ "module A where" : ["import " <> x | x <- imps] ++ [newImp] ++ def : other
992976 cradle = "cradle: {direct: {arguments: [-hide-all-packages, -package, base, -package, text, -package-env, -]}}"
993977 liftIO $ writeFileUTF8 (dir </> "hie.yaml") cradle
994− doc <- createDoc "Test.hs" "haskell" before
978+ doc <- openDoc' "Test.hs" "haskell" before
995979 _diags <- waitForDiagnostics
996980 let defLine = length imps + 1
997981 range = Range (Position defLine 0) (Position defLine maxBound)
998982 actions <- getCodeActions doc range
999− if wanted
1000− then do
1001− action <- liftIO $ pickActionWithTitle newImp actions
1002− executeCodeAction action
1003− contentAfterAction <- documentContents doc
1004− liftIO $ after @=? contentAfterAction
1005− else
1006− liftIO $ [_title | CACodeAction CodeAction{_title} <- actions, _title == newImp ] @?= []
1007−
983+ case wanted of
984+ False ->
985+ liftIO $ [_title | CACodeAction CodeAction{_title} <- actions, _title == newImp ] @?= []
986+ True -> do
987+ action <- liftIO $ pickActionWithTitle newImp actions
988+ executeCodeAction action
989+ contentAfterAction <- documentContents doc
990+ liftIO $ after @=? contentAfterAction
1008991
1009992addExtensionTests :: TestTree
1010993addExtensionTests = testGroup "add language extension actions"
@@ -1051,7 +1034,7 @@ addExtensionTests = testGroup "add language extension actions"
10511034 ]
10521035 where
10531036 template initialContent range expectedAction expectedContents = do
1054− doc <- createDoc "Module.hs" "haskell" initialContent
1037+ doc <- openDoc' "Module.hs" "haskell" initialContent
10551038 _ <- waitForDiagnostics
10561039 CACodeAction action@CodeAction { _title = actionTitle } : _
10571040 <- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
@@ -1074,7 +1057,7 @@ insertNewDefinitionTests = testGroup "insert new definition actions"
10741057 [""
10751058 ,"someOtherCode = ()"
10761059 ]
1077− docB <- createDoc "ModuleB.hs" "haskell" (T.unlines $ txtB ++ txtB')
1060+ docB <- openDoc' "ModuleB.hs" "haskell" (T.unlines $ txtB ++ txtB')
10781061 _ <- waitForDiagnostics
10791062 CACodeAction action@CodeAction { _title = actionTitle } : _
10801063 <- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
@@ -1098,7 +1081,7 @@ insertNewDefinitionTests = testGroup "insert new definition actions"
10981081 [""
10991082 ,"someOtherCode = ()"
11001083 ]
1101− docB <- createDoc "ModuleB.hs" "haskell" (T.unlines $ txtB ++ txtB')
1084+ docB <- openDoc' "ModuleB.hs" "haskell" (T.unlines $ txtB ++ txtB')
11021085 _ <- waitForDiagnostics
11031086 CACodeAction action@CodeAction { _title = actionTitle } : _
11041087 <- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
@@ -1137,8 +1120,8 @@ fixConstructorImportTests = testGroup "fix import actions"
11371120 ]
11381121 where
11391122 template contentA contentB range expectedAction expectedContentB = do
1140− _docA <- createDoc "ModuleA.hs" "haskell" contentA
1141− docB <- createDoc "ModuleB.hs" "haskell" contentB
1123+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
1124+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
11421125 _diags <- waitForDiagnostics
11431126 CACodeAction action@CodeAction { _title = actionTitle } : _
11441127 <- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
@@ -1157,7 +1140,7 @@ importRenameActionTests = testGroup "import rename actions"
11571140 [ "module Testing where"
11581141 , "import Data.Mape"
11591142 ]
1160− doc <- createDoc "Testing.hs" "haskell" content
1143+ doc <- openDoc' "Testing.hs" "haskell" content
11611144 _ <- waitForDiagnostics
11621145 actionsOrCommands <- getCodeActions doc (Range (Position 2 8) (Position 2 16))
11631146 let [changeToMap] = [action | CACodeAction action@CodeAction{ _title = actionTitle } <- actionsOrCommands, ("Data." <> modname) `T.isInfixOf` actionTitle ]
@@ -1194,7 +1177,7 @@ fillTypedHoleTests = let
11941177 newA newB newC = testSession (T.unpack actionTitle) $ do
11951178 let originalCode = sourceCode oldA oldB oldC
11961179 let expectedCode = sourceCode newA newB newC
1197− doc <- createDoc "Testing.hs" "haskell" originalCode
1180+ doc <- openDoc' "Testing.hs" "haskell" originalCode
11981181 _ <- waitForDiagnostics
11991182 actionsOrCommands <- getCodeActions doc (Range (Position 9 0) (Position 9 maxBound))
12001183 chosenAction <- liftIO $ pickActionWithTitle actionTitle actionsOrCommands
@@ -1242,7 +1225,7 @@ addSigActionTests = let
12421225 def >:: sig = testSession (T.unpack def) $ do
12431226 let originalCode = before def
12441227 let expectedCode = after' def sig
1245− doc <- createDoc "Sigs.hs" "haskell" originalCode
1228+ doc <- openDoc' "Sigs.hs" "haskell" originalCode
12461229 _ <- waitForDiagnostics
12471230 actionsOrCommands <- getCodeActions doc (Range (Position 3 1) (Position 3 maxBound))
12481231 chosenAction <- liftIO $ pickActionWithTitle ("add signature: " <> sig) actionsOrCommands
@@ -1274,7 +1257,7 @@ addSigLensesTests = let
12741257 sigSession withMissing def sig = testSession (T.unpack def) $ do
12751258 let originalCode = before withMissing def
12761259 let expectedCode = after' withMissing def sig
1277− doc <- createDoc "Sigs.hs" "haskell" originalCode
1260+ doc <- openDoc' "Sigs.hs" "haskell" originalCode
12781261 [CodeLens {_command = Just c}] <- getCodeLenses doc
12791262 executeCommand c
12801263 modifiedCode <- getDocumentEdit doc
@@ -1296,36 +1279,33 @@ addSigLensesTests = let
12961279 ]
12971280 ]
12981281
1299−checkDefs :: [Location] -> Session [Expect] -> Session ()
1300−checkDefs defs mkExpectations = traverse_ check =<< mkExpectations where
1301−
1302− check (ExpectRange expectedRange) = do
1303− assertNDefinitionsFound 1 defs
1304− assertRangeCorrect (head defs) expectedRange
1305− check (ExpectLocation expectedLocation) = do
1306− assertNDefinitionsFound 1 defs
1307− liftIO $ head defs @?= expectedLocation
1308− check ExpectNoDefinitions = do
1309− assertNDefinitionsFound 0 defs
1310− check ExpectExternFail = liftIO $ assertFailure "Expecting to fail to find in external file"
1311− check _ = pure () -- all other expectations not relevant to getDefinition
1312−
1313− assertNDefinitionsFound :: Int -> [a] -> Session ()
1314− assertNDefinitionsFound n defs = liftIO $ assertEqual "number of definitions" n (length defs)
1315−
1316− assertRangeCorrect Location{_range = foundRange} expectedRange =
1317− liftIO $ expectedRange @=? foundRange
1318−
1319−
13201282findDefinitionAndHoverTests :: TestTree
13211283findDefinitionAndHoverTests = let
13221284
1323− tst (get, check) pos targetRange title = testSessionWithExtraFiles "hover" title $ \dir -> do
1324− doc <- openTestDataDoc (dir </> sourceFilePath)
1285+ tst (get, check) pos targetRange title = testSession title $ do
1286+ doc <- openTestDataDoc sourceFilePath
13251287 found <- get doc pos
13261288 check found targetRange
13271289
1290+ checkDefs :: [Location] -> Session [Expect] -> Session ()
1291+ checkDefs defs mkExpectations = traverse_ check =<< mkExpectations where
13281292
1293+ check (ExpectRange expectedRange) = do
1294+ assertNDefinitionsFound 1 defs
1295+ assertRangeCorrect (head defs) expectedRange
1296+ check (ExpectLocation expectedLocation) = do
1297+ assertNDefinitionsFound 1 defs
1298+ liftIO $ head defs @?= expectedLocation
1299+ check ExpectNoDefinitions = do
1300+ assertNDefinitionsFound 0 defs
1301+ check ExpectExternFail = liftIO $ assertFailure "Expecting to fail to find in external file"
1302+ check _ = pure () -- all other expectations not relevant to getDefinition
1303+
1304+ assertNDefinitionsFound :: Int -> [a] -> Session ()
1305+ assertNDefinitionsFound n defs = liftIO $ assertEqual "number of definitions" n (length defs)
1306+
1307+ assertRangeCorrect Location{_range = foundRange} expectedRange =
1308+ liftIO $ expectedRange @=? foundRange
13291309
13301310 checkHover :: Maybe Hover -> Session [Expect] -> Session ()
13311311 checkHover hover expectations = traverse_ check =<< expectations where
@@ -1371,8 +1351,7 @@ findDefinitionAndHoverTests = let
13711351
13721352 mkFindTests tests = testGroup "get"
13731353 [ testGroup "definition" $ mapMaybe fst tests
1374− , testGroup "hover" $ mapMaybe snd tests
1375− , checkFileCompiles sourceFilePath ]
1354+ , testGroup "hover" $ mapMaybe snd tests ]
13761355
13771356 test runDef runHover look expect = testM runDef runHover look (return expect)
13781357
@@ -1467,17 +1446,8 @@ findDefinitionAndHoverTests = let
14671446 broken = Just . (`xfail` "known broken")
14681447 no = const Nothing -- don't run this test at all
14691448
1470−checkFileCompiles :: FilePath -> TestTree
1471−checkFileCompiles fp =
1472− testSessionWithExtraFiles "hover" ("Does " ++ fp ++ " compile") $ \dir -> do
1473− void (openTestDataDoc (dir </> fp))
1474− expectNoMoreDiagnostics 0.5
1475−
1476−
1477−
14781449pluginTests :: TestTree
1479−pluginTests = (`xfail8101` "known broken (#556)")
1480− $ testSessionWait "plugins" $ do
1450+pluginTests = testSessionWait "plugins" $ do
14811451 let content =
14821452 T.unlines
14831453 [ "{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}"
@@ -1491,7 +1461,7 @@ pluginTests = (`xfail8101` "known broken (#556)")
14911461 , "foo :: Int -> Int -> Int"
14921462 , "foo a b = a + c"
14931463 ]
1494− _ <- createDoc "Testing.hs" "haskell" content
1464+ _ <- openDoc' "Testing.hs" "haskell" content
14951465 expectDiagnostics
14961466 [ ( "Testing.hs",
14971467 [(DsError, (8, 14), "Variable not in scope: c")]
@@ -1518,7 +1488,7 @@ cppTests =
15181488 run $ expectError content (2, 1)
15191489 )
15201490 , testSessionWait "cpp-ghcide" $ do
1521− _ <- createDoc "A.hs" "haskell" $ T.unlines
1491+ _ <- openDoc' "A.hs" "haskell" $ T.unlines
15221492 ["{-# LANGUAGE CPP #-}"
15231493 ,"main ="
15241494 ,"#ifdef __GHCIDE__"
@@ -1532,7 +1502,7 @@ cppTests =
15321502 where
15331503 expectError :: T.Text -> Cursor -> Session ()
15341504 expectError content cursor = do
1535− _ <- createDoc "Testing.hs" "haskell" content
1505+ _ <- openDoc' "Testing.hs" "haskell" content
15361506 expectDiagnostics
15371507 [ ( "Testing.hs",
15381508 [(DsError, cursor, "error: unterminated")]
@@ -1548,7 +1518,7 @@ preprocessorTests = testSessionWait "preprocessor" $ do
15481518 , "module Testing where"
15491519 , "y = x + z" -- plugin replaces x with y, making this have only one diagnostic
15501520 ]
1551− _ <- createDoc "Testing.hs" "haskell" content
1521+ _ <- openDoc' "Testing.hs" "haskell" content
15521522 expectDiagnostics
15531523 [ ( "Testing.hs",
15541524 [(DsError, (2, 8), "Variable not in scope: z")]
@@ -1581,8 +1551,8 @@ safeTests =
15811551 ,"safeId = trustWorthyId"
15821552 ]
15831553
1584− _ <- createDoc "A.hs" "haskell" sourceA
1585− _ <- createDoc "B.hs" "haskell" sourceB
1554+ _ <- openDoc' "A.hs" "haskell" sourceA
1555+ _ <- openDoc' "B.hs" "haskell" sourceB
15861556 expectNoMoreDiagnostics 1 ]
15871557
15881558thTests :: TestTree
@@ -1610,8 +1580,8 @@ thTests =
16101580 "b :: Integer",
16111581 "b = $(litE $ IntegerL $ a) + n"
16121582 ]
1613− _ <- createDoc "A.hs" "haskell" sourceA
1614− _ <- createDoc "B.hs" "haskell" sourceB
1583+ _ <- openDoc' "A.hs" "haskell" sourceA
1584+ _ <- openDoc' "B.hs" "haskell" sourceB
16151585 expectDiagnostics [ ( "B.hs", [(DsError, (6, 29), "Variable not in scope: n")] ) ]
16161586 , testSessionWait "newtype-closure" $ do
16171587 let sourceA =
@@ -1631,8 +1601,8 @@ thTests =
16311601 ,"import A"
16321602 ,"b :: Int"
16331603 ,"b = $( a )" ]
1634− _ <- createDoc "A.hs" "haskell" sourceA
1635− _ <- createDoc "B.hs" "haskell" sourceB
1604+ _ <- openDoc' "A.hs" "haskell" sourceA
1605+ _ <- openDoc' "B.hs" "haskell" sourceB
16361606 return ()
16371607 ]
16381608
@@ -1641,7 +1611,7 @@ completionTests
16411611 = testGroup "completion"
16421612 [ testSessionWait "variable" $ do
16431613 let source = T.unlines ["module A where", "f = hea"]
1644− docId <- createDoc "A.hs" "haskell" source
1614+ docId <- openDoc' "A.hs" "haskell" source
16451615 compls <- getCompletions docId (Position 1 7)
16461616 liftIO $ map dropDocs compls @?=
16471617 [complItem "head" (Just CiFunction) (Just "[a] -> a")]
@@ -1653,7 +1623,7 @@ completionTests
16531623 ]
16541624 , testSessionWait "constructor" $ do
16551625 let source = T.unlines ["module A where", "f = Tru"]
1656− docId <- createDoc "A.hs" "haskell" source
1626+ docId <- openDoc' "A.hs" "haskell" source
16571627 compls <- getCompletions docId (Position 1 7)
16581628 liftIO $ map dropDocs compls @?=
16591629 [ complItem "True" (Just CiConstructor) (Just "Bool")
@@ -1665,7 +1635,7 @@ completionTests
16651635 ]
16661636 , testSessionWait "type" $ do
16671637 let source = T.unlines ["{-# OPTIONS_GHC -Wall #-}", "module A () where", "f :: ()", "f = ()"]
1668− docId <- createDoc "A.hs" "haskell" source
1638+ docId <- openDoc' "A.hs" "haskell" source
16691639 expectDiagnostics [ ("A.hs", [(DsWarning, (3,0), "not used")]) ]
16701640 changeDoc docId [TextDocumentContentChangeEvent Nothing Nothing $ T.unlines ["{-# OPTIONS_GHC -Wall #-}", "module A () where", "f :: Bo", "f = True"]]
16711641 compls <- getCompletions docId (Position 2 7)
@@ -1682,7 +1652,7 @@ completionTests
16821652 checkDocText "Bool" boolDocs [ "Defined in 'Prelude'" ]
16831653 , testSessionWait "qualified" $ do
16841654 let source = T.unlines ["{-# OPTIONS_GHC -Wunused-binds #-}", "module A () where", "f = ()"]
1685− docId <- createDoc "A.hs" "haskell" source
1655+ docId <- openDoc' "A.hs" "haskell" source
16861656 expectDiagnostics [ ("A.hs", [(DsWarning, (2, 0), "not used")]) ]
16871657 changeDoc docId [TextDocumentContentChangeEvent Nothing Nothing $ T.unlines ["{-# OPTIONS_GHC -Wunused-binds #-}", "module A () where", "f = Prelude.hea"]]
16881658 compls <- getCompletions docId (Position 2 15)
@@ -1696,7 +1666,7 @@ completionTests
16961666 ]
16971667 , testSessionWait "keyword" $ do
16981668 let source = T.unlines ["module A where", "f = newty"]
1699− docId <- createDoc "A.hs" "haskell" source
1669+ docId <- openDoc' "A.hs" "haskell" source
17001670 compls <- getCompletions docId (Position 1 9)
17011671 liftIO $ compls @?= [keywordItem "newtype"]
17021672 , testSessionWait "type context" $ do
@@ -1705,7 +1675,7 @@ completionTests
17051675 , "module A () where"
17061676 , "f = f"
17071677 ]
1708− docId <- createDoc "A.hs" "haskell" source
1678+ docId <- openDoc' "A.hs" "haskell" source
17091679 expectDiagnostics [("A.hs", [(DsWarning, (2, 0), "not used")])]
17101680 changeDoc docId
17111681 [ TextDocumentContentChangeEvent Nothing Nothing $ T.unlines
@@ -1773,7 +1743,7 @@ outlineTests = testGroup
17731743 "outline"
17741744 [ testSessionWait "type class" $ do
17751745 let source = T.unlines ["module A where", "class A a where a :: a -> Bool"]
1776− docId <- createDoc "A.hs" "haskell" source
1746+ docId <- openDoc' "A.hs" "haskell" source
17771747 symbols <- getDocumentSymbols docId
17781748 liftIO $ symbols @?= Left
17791749 [ moduleSymbol
@@ -1786,7 +1756,7 @@ outlineTests = testGroup
17861756 ]
17871757 , testSessionWait "type class instance " $ do
17881758 let source = T.unlines ["class A a where", "instance A () where"]
1789− docId <- createDoc "A.hs" "haskell" source
1759+ docId <- openDoc' "A.hs" "haskell" source
17901760 symbols <- getDocumentSymbols docId
17911761 liftIO $ symbols @?= Left
17921762 [ classSymbol "A a" (R 0 0 0 15) []
@@ -1794,7 +1764,7 @@ outlineTests = testGroup
17941764 ]
17951765 , testSessionWait "type family" $ do
17961766 let source = T.unlines ["{-# language TypeFamilies #-}", "type family A"]
1797− docId <- createDoc "A.hs" "haskell" source
1767+ docId <- openDoc' "A.hs" "haskell" source
17981768 symbols <- getDocumentSymbols docId
17991769 liftIO $ symbols @?= Left [docSymbolD "A" "type family" SkClass (R 1 0 1 13)]
18001770 , testSessionWait "type family instance " $ do
@@ -1803,7 +1773,7 @@ outlineTests = testGroup
18031773 , "type family A a"
18041774 , "type instance A () = ()"
18051775 ]
1806− docId <- createDoc "A.hs" "haskell" source
1776+ docId <- openDoc' "A.hs" "haskell" source
18071777 symbols <- getDocumentSymbols docId
18081778 liftIO $ symbols @?= Left
18091779 [ docSymbolD "A a" "type family" SkClass (R 1 0 1 15)
@@ -1811,7 +1781,7 @@ outlineTests = testGroup
18111781 ]
18121782 , testSessionWait "data family" $ do
18131783 let source = T.unlines ["{-# language TypeFamilies #-}", "data family A"]
1814− docId <- createDoc "A.hs" "haskell" source
1784+ docId <- openDoc' "A.hs" "haskell" source
18151785 symbols <- getDocumentSymbols docId
18161786 liftIO $ symbols @?= Left [docSymbolD "A" "data family" SkClass (R 1 0 1 11)]
18171787 , testSessionWait "data family instance " $ do
@@ -1820,7 +1790,7 @@ outlineTests = testGroup
18201790 , "data family A a"
18211791 , "data instance A () = A ()"
18221792 ]
1823− docId <- createDoc "A.hs" "haskell" source
1793+ docId <- openDoc' "A.hs" "haskell" source
18241794 symbols <- getDocumentSymbols docId
18251795 liftIO $ symbols @?= Left
18261796 [ docSymbolD "A a" "data family" SkClass (R 1 0 1 11)
@@ -1828,36 +1798,36 @@ outlineTests = testGroup
18281798 ]
18291799 , testSessionWait "constant" $ do
18301800 let source = T.unlines ["a = ()"]
1831− docId <- createDoc "A.hs" "haskell" source
1801+ docId <- openDoc' "A.hs" "haskell" source
18321802 symbols <- getDocumentSymbols docId
18331803 liftIO $ symbols @?= Left
18341804 [docSymbol "a" SkFunction (R 0 0 0 6)]
18351805 , testSessionWait "pattern" $ do
18361806 let source = T.unlines ["Just foo = Just 21"]
1837− docId <- createDoc "A.hs" "haskell" source
1807+ docId <- openDoc' "A.hs" "haskell" source
18381808 symbols <- getDocumentSymbols docId
18391809 liftIO $ symbols @?= Left
18401810 [docSymbol "Just foo" SkFunction (R 0 0 0 18)]
18411811 , testSessionWait "pattern with type signature" $ do
18421812 let source = T.unlines ["{-# language ScopedTypeVariables #-}", "a :: () = ()"]
1843− docId <- createDoc "A.hs" "haskell" source
1813+ docId <- openDoc' "A.hs" "haskell" source
18441814 symbols <- getDocumentSymbols docId
18451815 liftIO $ symbols @?= Left
18461816 [docSymbol "a :: ()" SkFunction (R 1 0 1 12)]
18471817 , testSessionWait "function" $ do
18481818 let source = T.unlines ["a x = ()"]
1849− docId <- createDoc "A.hs" "haskell" source
1819+ docId <- openDoc' "A.hs" "haskell" source
18501820 symbols <- getDocumentSymbols docId
18511821 liftIO $ symbols @?= Left [docSymbol "a" SkFunction (R 0 0 0 8)]
18521822 , testSessionWait "type synonym" $ do
18531823 let source = T.unlines ["type A = Bool"]
1854− docId <- createDoc "A.hs" "haskell" source
1824+ docId <- openDoc' "A.hs" "haskell" source
18551825 symbols <- getDocumentSymbols docId
18561826 liftIO $ symbols @?= Left
18571827 [docSymbol' "A" SkTypeParameter (R 0 0 0 13) (R 0 5 0 6)]
18581828 , testSessionWait "datatype" $ do
18591829 let source = T.unlines ["data A = C"]
1860− docId <- createDoc "A.hs" "haskell" source
1830+ docId <- openDoc' "A.hs" "haskell" source
18611831 symbols <- getDocumentSymbols docId
18621832 liftIO $ symbols @?= Left
18631833 [ docSymbolWithChildren "A"
@@ -1865,21 +1835,9 @@ outlineTests = testGroup
18651835 (R 0 0 0 10)
18661836 [docSymbol "C" SkConstructor (R 0 9 0 10)]
18671837 ]
1868− , testSessionWait "record fields" $ do
1869− let source = T.unlines ["data A = B {", " x :: Int", " , y :: Int}"]
1870− docId <- createDoc "A.hs" "haskell" source
1871− symbols <- getDocumentSymbols docId
1872− liftIO $ symbols @=? Left
1873− [ docSymbolWithChildren "A" SkStruct (R 0 0 2 13)
1874− [ docSymbolWithChildren' "B" SkConstructor (R 0 9 2 13) (R 0 9 0 10)
1875− [ docSymbol "x" SkField (R 1 2 1 3)
1876− , docSymbol "y" SkField (R 2 4 2 5)
1877− ]
1878− ]
1879− ]
18801838 , testSessionWait "import" $ do
18811839 let source = T.unlines ["import Data.Maybe"]
1882− docId <- createDoc "A.hs" "haskell" source
1840+ docId <- openDoc' "A.hs" "haskell" source
18831841 symbols <- getDocumentSymbols docId
18841842 liftIO $ symbols @?= Left
18851843 [docSymbolWithChildren "imports"
@@ -1890,7 +1848,7 @@ outlineTests = testGroup
18901848 ]
18911849 , testSessionWait "multiple import" $ do
18921850 let source = T.unlines ["", "import Data.Maybe", "", "import Control.Exception", ""]
1893− docId <- createDoc "A.hs" "haskell" source
1851+ docId <- openDoc' "A.hs" "haskell" source
18941852 symbols <- getDocumentSymbols docId
18951853 liftIO $ symbols @?= Left
18961854 [docSymbolWithChildren "imports"
@@ -1905,7 +1863,7 @@ outlineTests = testGroup
19051863 [ "{-# language ForeignFunctionInterface #-}"
19061864 , "foreign import ccall \"a\" a :: Int"
19071865 ]
1908− docId <- createDoc "A.hs" "haskell" source
1866+ docId <- openDoc' "A.hs" "haskell" source
19091867 symbols <- getDocumentSymbols docId
19101868 liftIO $ symbols @?= Left [docSymbolD "a" "import" SkObject (R 1 0 1 33)]
19111869 , testSessionWait "foreign export" $ do
@@ -1913,7 +1871,7 @@ outlineTests = testGroup
19131871 [ "{-# language ForeignFunctionInterface #-}"
19141872 , "foreign export ccall odd :: Int -> Bool"
19151873 ]
1916− docId <- createDoc "A.hs" "haskell" source
1874+ docId <- openDoc' "A.hs" "haskell" source
19171875 symbols <- getDocumentSymbols docId
19181876 liftIO $ symbols @?= Left [docSymbolD "odd" "export" SkObject (R 1 0 1 39)]
19191877 ]
@@ -1926,8 +1884,6 @@ outlineTests = testGroup
19261884 DocumentSymbol name (Just detail) kind Nothing loc loc Nothing
19271885 docSymbolWithChildren name kind loc cc =
19281886 DocumentSymbol name Nothing kind Nothing loc loc (Just $ List cc)
1929− docSymbolWithChildren' name kind loc selectionLoc cc =
1930− DocumentSymbol name Nothing kind Nothing loc selectionLoc (Just $ List cc)
19311887 moduleSymbol name loc cc = DocumentSymbol name
19321888 Nothing
19331889 SkFile
@@ -1949,13 +1905,6 @@ pattern R x y x' y' = Range (Position x y) (Position x' y')
19491905xfail :: TestTree -> String -> TestTree
19501906xfail = flip expectFailBecause
19511907
1952−xfail8101 :: TestTree -> String -> TestTree
1953−#if MIN_GHC_API_VERSION(8,10,0)
1954−xfail8101 = flip expectFailBecause
1955−#else
1956−xfail8101 t _ = t
1957−#endif
1958−
19591908data Expect
19601909 = ExpectRange Range -- Both gotoDef and hover should report this range
19611910 | ExpectLocation Location
@@ -2033,7 +1982,6 @@ cradleTests :: TestTree
20331982cradleTests = testGroup "cradle"
20341983 [testGroup "dependencies" [sessionDepsArePickedUp]
20351984 ,testGroup "loading" [loadCradleOnlyonce]
2036− ,testGroup "multi" [simpleMultiTest, simpleMultiTest2]
20371985 ]
20381986
20391987loadCradleOnlyonce :: TestTree
@@ -2048,53 +1996,17 @@ loadCradleOnlyonce = testGroup "load cradle only once"
20481996 test dir
20491997 implicit dir = test dir
20501998 test _dir = do
2051− doc <- createDoc "B.hs" "haskell" "module B where\nimport Data.Foo"
1999+ doc <- openDoc' "B.hs" "haskell" "module B where\nimport Data.Foo"
20522000 msgs <- someTill (skipManyTill anyMessage cradleLoadedMessage) (skipManyTill anyMessage (message @PublishDiagnosticsNotification))
20532001 liftIO $ length msgs @?= 1
20542002 changeDoc doc [TextDocumentContentChangeEvent Nothing Nothing "module B where\nimport Data.Maybe"]
20552003 msgs <- manyTill (skipManyTill anyMessage cradleLoadedMessage) (skipManyTill anyMessage (message @PublishDiagnosticsNotification))
20562004 liftIO $ length msgs @?= 0
2057− _ <- createDoc "A.hs" "haskell" "module A where\nimport LoadCradleBar"
2005+ _ <- openDoc' "A.hs" "haskell" "module A where\nimport LoadCradleBar"
20582006 msgs <- manyTill (skipManyTill anyMessage cradleLoadedMessage) (skipManyTill anyMessage (message @PublishDiagnosticsNotification))
20592007 liftIO $ length msgs @?= 0
20602008
20612009
2062−dependentFileTest :: TestTree
2063−dependentFileTest = testGroup "addDependentFile"
2064− [testGroup "file-changed" [testSession' "test" test]
2065− ]
2066− where
2067− test dir = do
2068− -- If the file contains B then no type error
2069− -- otherwise type error
2070− liftIO $ writeFile (dir </> "dep-file.txt") "A"
2071− let fooContent = T.unlines
2072− [ "{-# LANGUAGE TemplateHaskell #-}"
2073− , "module Foo where"
2074− , "import Language.Haskell.TH.Syntax"
2075− , "foo :: Int"
2076− , "foo = 1 + $(do"
2077− , " qAddDependentFile \"dep-file.txt\""
2078− , " f <- qRunIO (readFile \"dep-file.txt\")"
2079− , " if f == \"B\" then [| 1 |] else lift f)"
2080− ]
2081− let bazContent = T.unlines ["module Baz where", "import Foo"]
2082− _ <-createDoc "Foo.hs" "haskell" fooContent
2083− doc <- createDoc "Baz.hs" "haskell" bazContent
2084− expectDiagnostics
2085− [("Foo.hs", [(DsError, (4, 6), "Couldn't match expected type")])]
2086− -- Now modify the dependent file
2087− liftIO $ writeFile (dir </> "dep-file.txt") "B"
2088− let change = TextDocumentContentChangeEvent
2089− { _range = Just (Range (Position 2 0) (Position 2 6))
2090− , _rangeLength = Nothing
2091− , _text = "f = ()"
2092− }
2093− -- Modifying Baz will now trigger Foo to be rebuilt as well
2094− changeDoc doc [change]
2095− expectDiagnostics [("Foo.hs", [])]
2096−
2097−
20982010cradleLoadedMessage :: Session FromServerMessage
20992011cradleLoadedMessage = satisfy $ \case
21002012 NotCustomServer (NotificationMessage _ (CustomServerMethod m) _) -> m == cradleLoadedMethod
@@ -2103,56 +2015,6 @@ cradleLoadedMessage = satisfy $ \case
21032015cradleLoadedMethod :: T.Text
21042016cradleLoadedMethod = "ghcide/cradle/loaded"
21052017
2106−-- Stack sets this which trips up cabal in the multi-component tests.
2107−-- However, our plugin tests rely on those env vars so we unset it locally.
2108−withoutStackEnv :: IO a -> IO a
2109−withoutStackEnv s =
2110− bracket
2111− (mapM getEnv vars >>= \prevState -> mapM_ unsetEnv vars >> pure prevState)
2112− (\prevState -> mapM_ (\(var, value) -> restore var value) (zip vars prevState))
2113− (const s)
2114− where vars =
2115− [ "GHC_PACKAGE_PATH"
2116− , "GHC_ENVIRONMENT"
2117− , "HASKELL_DIST_DIR"
2118− , "HASKELL_PACKAGE_SANDBOX"
2119− , "HASKELL_PACKAGE_SANDBOXES"
2120− ]
2121− restore var Nothing = unsetEnv var
2122− restore var (Just val) = setEnv var val True
2123−
2124−simpleMultiTest :: TestTree
2125−simpleMultiTest = testCase "simple-multi-test" $ withoutStackEnv $ runWithExtraFiles "multi" $ \dir -> do
2126− let aPath = dir </> "a/A.hs"
2127− bPath = dir </> "b/B.hs"
2128− aSource <- liftIO $ readFileUtf8 aPath
2129− (TextDocumentIdentifier adoc) <- createDoc aPath "haskell" aSource
2130− expectNoMoreDiagnostics 0.5
2131− bSource <- liftIO $ readFileUtf8 bPath
2132− bdoc <- createDoc bPath "haskell" bSource
2133− expectNoMoreDiagnostics 0.5
2134− locs <- getDefinitions bdoc (Position 2 7)
2135− let fooL = mkL adoc 2 0 2 3
2136− checkDefs locs (pure [fooL])
2137− expectNoMoreDiagnostics 0.5
2138−
2139−-- Like simpleMultiTest but open the files in the other order
2140−simpleMultiTest2 :: TestTree
2141−simpleMultiTest2 = testCase "simple-multi-test2" $ withoutStackEnv $ runWithExtraFiles "multi" $ \dir -> do
2142− let aPath = dir </> "a/A.hs"
2143− bPath = dir </> "b/B.hs"
2144− bSource <- liftIO $ readFileUtf8 bPath
2145− bdoc <- createDoc bPath "haskell" bSource
2146− expectNoMoreDiagnostics 5
2147− aSource <- liftIO $ readFileUtf8 aPath
2148− (TextDocumentIdentifier adoc) <- createDoc aPath "haskell" aSource
2149− -- Need to have some delay here or the test fails
2150− expectNoMoreDiagnostics 5
2151− locs <- getDefinitions bdoc (Position 2 7)
2152− let fooL = mkL adoc 2 0 2 3
2153− checkDefs locs (pure [fooL])
2154− expectNoMoreDiagnostics 0.5
2155−
21562018sessionDepsArePickedUp :: TestTree
21572019sessionDepsArePickedUp = testSession'
21582020 "session-deps-are-picked-up"
@@ -2162,7 +2024,7 @@ sessionDepsArePickedUp = testSession'
21622024 (dir </> "hie.yaml")
21632025 "cradle: {direct: {arguments: []}}"
21642026 -- Open without OverloadedStrings and expect an error.
2165− doc <- createDoc "Foo.hs" "haskell" fooContent
2027+ doc <- openDoc' "Foo.hs" "haskell" fooContent
21662028 expectDiagnostics
21672029 [("Foo.hs", [(DsError, (3, 6), "Couldn't match expected type")])]
21682030 -- Update hie.yaml to enable OverloadedStrings.
@@ -2197,9 +2059,6 @@ sessionDepsArePickedUp = testSession'
21972059testSession :: String -> Session () -> TestTree
21982060testSession name = testCase name . run
21992061
2200−testSessionWithExtraFiles :: FilePath -> String -> (FilePath -> Session ()) -> TestTree
2201−testSessionWithExtraFiles prefix name = testCase name . runWithExtraFiles prefix
2202−
22032062testSession' :: String -> (FilePath -> Session ()) -> TestTree
22042063testSession' name = testCase name . run'
22052064
@@ -2234,19 +2093,6 @@ mkRange a b c d = Range (Position a b) (Position c d)
22342093run :: Session a -> IO a
22352094run s = withTempDir $ \dir -> runInDir dir s
22362095
2237−runWithExtraFiles :: FilePath -> (FilePath -> Session a) -> IO a
2238−runWithExtraFiles prefix s = withTempDir $ \dir -> do
2239− copyTestDataFiles dir prefix
2240− runInDir dir (s dir)
2241−
2242−copyTestDataFiles :: FilePath -> FilePath -> IO ()
2243−copyTestDataFiles dir prefix = do
2244− -- Copy all the test data files to the temporary workspace
2245− testDataFiles <- getDirectoryFilesIO ("test/data" </> prefix) ["//*"]
2246− for_ testDataFiles $ \f -> do
2247− createDirectoryIfMissing True $ dir </> takeDirectory f
2248− copyFile ("test/data" </> prefix </> f) (dir </> f)
2249−
22502096run' :: (FilePath -> Session a) -> IO a
22512097run' s = withTempDir $ \dir -> runInDir dir (s dir)
22522098
@@ -2258,6 +2104,11 @@ runInDir dir s = do
22582104 -- since the package import test creates "Data/List.hs", which otherwise has no physical home
22592105 createDirectoryIfMissing True $ dir ++ "/Data"
22602106
2107+ -- Copy all the test data files to the temporary workspace
2108+ testDataFiles <- getDirectoryFilesIO "test/data" ["//*"]
2109+ for_ testDataFiles $ \f -> do
2110+ createDirectoryIfMissing True $ dir </> takeDirectory f
2111+ copyFile ("test/data" </> f) (dir </> f)
22612112
22622113 let cmd = unwords [ghcideExe, "--lsp", "--test", "--cwd", dir]
22632114 -- HIE calls getXgdDirectory which assumes that HOME is set.
@@ -2276,7 +2127,7 @@ runInDir dir s = do
22762127openTestDataDoc :: FilePath -> Session TextDocumentIdentifier
22772128openTestDataDoc path = do
22782129 source <- liftIO $ readFileUtf8 $ "test/data" </> path
2279− createDoc path "haskell" source
2130+ openDoc' path "haskell" source
22802131
22812132findCodeActions :: TextDocumentIdentifier -> Range -> [T.Text] -> Session [CodeAction]
22822133findCodeActions doc range expectedTitles = do
@@ -2317,6 +2168,20 @@ unitTests = do
23172168 uriToFilePath' uri @?= Just ""
23182169 ]
23192170
2171+-- | Wrapper around 'LSPTest.openDoc'' that sends file creation events
2172+openDoc' :: FilePath -> String -> T.Text -> Session TextDocumentIdentifier
2173+openDoc' fp name contents = do
2174+ res@(TextDocumentIdentifier uri) <- LSPTest.openDoc' fp name contents
2175+ -- Needed as ghcide sets up and relies on WatchedFiles but lsp-test does not track them
2176+ sendNotification WorkspaceDidChangeWatchedFiles (DidChangeWatchedFilesParams $ List [FileEvent uri FcCreated])
2177+ return res
2178+
2179+-- | Version of 'LSPTest.openDoc'' that does not send WatchedFiles events for files outside the workspace
2180+openDoc'' :: FilePath -> String -> T.Text -> Session TextDocumentIdentifier
2181+-- At the moment this is just LSPTest.openDoc' but it may change in the future
2182+-- when/if lsp-test implements WatchedFiles
2183+openDoc'' = LSPTest.openDoc'
2184+
23202185positionMappingTests :: TestTree
23212186positionMappingTests =
23222187 testGroup "position mapping"
@@ -2491,6 +2356,6 @@ getWatchedFilesSubscriptionsUntil = do
24912356 msgs <- manyTill (Just <$> message @RegisterCapabilityRequest <|> Nothing <$ anyMessage) (message @end)
24922357 return
24932358 [ args
2494− | Just RequestMessage{_params = RegistrationParams (List regs)} <- msgs
2359+ | Just (RequestMessage{_params = RegistrationParams (List regs)}) <- msgs
24952360 , Registration _id WorkspaceDidChangeWatchedFiles args <- regs
24962361 ]