@@ -10,8 +10,7 @@
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)
@@ -28,14 +27,14 @@ import Development.IDE.Test
2827import Development.IDE.Test.Runfiles
2928import Development.IDE.Types.Location
3029import Development.Shake (getDirectoryFilesIO)
31−import Language.Haskell.LSP.Test
30+import qualified Language.Haskell.LSP.Test as LSPTest
31+import Language.Haskell.LSP.Test hiding (openDoc')
3232import Language.Haskell.LSP.Messages
3333import Language.Haskell.LSP.Types
3434import Language.Haskell.LSP.Types.Capabilities
35−import qualified Language.Haskell.LSP.Types.Lens as Lsp (diagnostics, params, message)
3635import Language.Haskell.LSP.VFS (applyChange)
3736import Network.URI
38−import System.Environment.Blank (getEnv, setEnv, unsetEnv)
37+import System.Environment.Blank (setEnv)
3938import System.FilePath
4039import System.IO.Extra
4140import System.Directory
@@ -49,35 +48,31 @@ import Test.Tasty.QuickCheck
4948import Data.Maybe
5049
5150main :: 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− ]
51+main = defaultMainWithRerun $ testGroup "HIE"
52+ [ testSession "open close" $ do
53+ doc <- openDoc' "Testing.hs" "haskell" ""
54+ void (skipManyTill anyMessage message :: Session WorkDoneProgressCreateRequest)
55+ void (skipManyTill anyMessage message :: Session WorkDoneProgressBeginNotification)
56+ closeDoc doc
57+ void (skipManyTill anyMessage message :: Session WorkDoneProgressEndNotification)
58+ , initializeResponseTests
59+ , completionTests
60+ , cppTests
61+ , diagnosticTests
62+ , codeActionTests
63+ , codeLensesTests
64+ , outlineTests
65+ , findDefinitionAndHoverTests
66+ , pluginTests
67+ , preprocessorTests
68+ , thTests
69+ , safeTests
70+ , unitTests
71+ , haddockTests
72+ , positionMappingTests
73+ , watchedFilesTests
74+ , cradleTests
75+ ]
8176
8277initializeResponseTests :: TestTree
8378initializeResponseTests = withResource acquire release tests where
@@ -138,7 +133,7 @@ initializeResponseTests = withResource acquire release tests where
138133
139134
140135 innerCaps :: InitializeResponse -> InitializeResponseCapabilitiesInner
141− innerCaps (ResponseMessage _ _ (Right (InitializeResponseCapabilities c))) = c
136+ innerCaps (ResponseMessage _ _ (Just (InitializeResponseCapabilities c)) _) = c
142137 innerCaps _ = error "this test only expects inner capabilities"
143138
144139 acquire :: IO InitializeResponse
@@ -152,7 +147,7 @@ diagnosticTests :: TestTree
152147diagnosticTests = testGroup "diagnostics"
153148 [ testSessionWait "fix syntax error" $ do
154149 let content = T.unlines [ "module Testing wher" ]
155− doc <- createDoc "Testing.hs" "haskell" content
150+ doc <- openDoc' "Testing.hs" "haskell" content
156151 expectDiagnostics [("Testing.hs", [(DsError, (0, 15), "parse error")])]
157152 let change = TextDocumentContentChangeEvent
158153 { _range = Just (Range (Position 0 15) (Position 0 19))
@@ -163,7 +158,7 @@ diagnosticTests = testGroup "diagnostics"
163158 expectDiagnostics [("Testing.hs", [])]
164159 , testSessionWait "introduce syntax error" $ do
165160 let content = T.unlines [ "module Testing where" ]
166− doc <- createDoc "Testing.hs" "haskell" content
161+ doc <- openDoc' "Testing.hs" "haskell" content
167162 void $ skipManyTill anyMessage (message :: Session WorkDoneProgressCreateRequest)
168163 void $ skipManyTill anyMessage (message :: Session WorkDoneProgressBeginNotification)
169164 let change = TextDocumentContentChangeEvent
@@ -181,7 +176,7 @@ diagnosticTests = testGroup "diagnostics"
181176 , "bar :: Int -> Int -> Int"
182177 , "bar a b = cd + b"
183178 ]
184− _ <- createDoc "Testing.hs" "haskell" content
179+ _ <- openDoc' "Testing.hs" "haskell" content
185180 expectDiagnostics
186181 [ ( "Testing.hs"
187182 , [ (DsError, (2, 14), "Variable not in scope: ab")
@@ -195,7 +190,7 @@ diagnosticTests = testGroup "diagnostics"
195190 , "foo :: Int -> String -> Int"
196191 , "foo a b = a + b"
197192 ]
198− _ <- createDoc "Testing.hs" "haskell" content
193+ _ <- openDoc' "Testing.hs" "haskell" content
199194 expectDiagnostics
200195 [ ( "Testing.hs"
201196 , [(DsError, (2, 14), "Couldn't match type '[Char]' with 'Int'")]
@@ -207,7 +202,7 @@ diagnosticTests = testGroup "diagnostics"
207202 , "foo :: Int -> String"
208203 , "foo a = _ a"
209204 ]
210− _ <- createDoc "Testing.hs" "haskell" content
205+ _ <- openDoc' "Testing.hs" "haskell" content
211206 expectDiagnostics
212207 [ ( "Testing.hs"
213208 , [(DsError, (2, 8), "Found hole: _ :: Int -> String")]
@@ -229,23 +224,24 @@ diagnosticTests = testGroup "diagnostics"
229224 [ ("A.hs", [(DsError, (2,4), aMessage)])
230225 , ("B.hs", [(DsError, (3,4), bMessage)])]
231226 deferralTest title binding msg = testSessionWait title $ do
232− _ <- createDoc "A.hs" "haskell" $ sourceA binding
233− _ <- createDoc "B.hs" "haskell" sourceB
227+ _ <- openDoc' "A.hs" "haskell" $ sourceA binding
228+ _ <- openDoc' "B.hs" "haskell" sourceB
234229 expectDiagnostics $ expectedDs msg
235230 in
236231 [ deferralTest "type error" "True" "Couldn't match expected type"
237232 , deferralTest "typed hole" "_" "Found hole"
238233 , deferralTest "out of scope var" "unbound" "Variable not in scope"
234+ , deferralTest "message shows error" "True" "A.hs:3:5: error:"
239235 ]
240236
241237 , testSessionWait "remove required module" $ do
242238 let contentA = T.unlines [ "module ModuleA where" ]
243− docA <- createDoc "ModuleA.hs" "haskell" contentA
239+ docA <- openDoc' "ModuleA.hs" "haskell" contentA
244240 let contentB = T.unlines
245241 [ "module ModuleB where"
246242 , "import ModuleA"
247243 ]
248− _ <- createDoc "ModuleB.hs" "haskell" contentB
244+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
249245 let change = TextDocumentContentChangeEvent
250246 { _range = Just (Range (Position 0 0) (Position 0 20))
251247 , _rangeLength = Nothing
@@ -258,20 +254,20 @@ diagnosticTests = testGroup "diagnostics"
258254 [ "module ModuleB where"
259255 , "import ModuleA"
260256 ]
261− _ <- createDoc "ModuleB.hs" "haskell" contentB
257+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
262258 expectDiagnostics [("ModuleB.hs", [(DsError, (1, 7), "Could not find module")])]
263259 let contentA = T.unlines [ "module ModuleA where" ]
264− _ <- createDoc "ModuleA.hs" "haskell" contentA
260+ _ <- openDoc' "ModuleA.hs" "haskell" contentA
265261 expectDiagnostics [("ModuleB.hs", [])]
266262 , testSessionWait "add missing module (non workspace)" $ do
267263 let contentB = T.unlines
268264 [ "module ModuleB where"
269265 , "import ModuleA"
270266 ]
271− _ <- createDoc "/tmp/ModuleB.hs" "haskell" contentB
267+ _ <- openDoc'' "/tmp/ModuleB.hs" "haskell" contentB
272268 expectDiagnostics [("/tmp/ModuleB.hs", [(DsError, (1, 7), "Could not find module")])]
273269 let contentA = T.unlines [ "module ModuleA where" ]
274− _ <- createDoc "/tmp/ModuleA.hs" "haskell" contentA
270+ _ <- openDoc'' "/tmp/ModuleA.hs" "haskell" contentA
275271 expectDiagnostics [("/tmp/ModuleB.hs", [])]
276272 , testSessionWait "cyclic module dependency" $ do
277273 let contentA = T.unlines
@@ -282,8 +278,8 @@ diagnosticTests = testGroup "diagnostics"
282278 [ "module ModuleB where"
283279 , "import ModuleA"
284280 ]
285− _ <- createDoc "ModuleA.hs" "haskell" contentA
286− _ <- createDoc "ModuleB.hs" "haskell" contentB
281+ _ <- openDoc' "ModuleA.hs" "haskell" contentA
282+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
287283 expectDiagnostics
288284 [ ( "ModuleA.hs"
289285 , [(DsError, (1, 7), "Cyclic module dependency between ModuleA, ModuleB")]
@@ -304,9 +300,9 @@ diagnosticTests = testGroup "diagnostics"
304300 let contentBboot = T.unlines
305301 [ "module ModuleB where"
306302 ]
307− _ <- createDoc "ModuleA.hs" "haskell" contentA
308− _ <- createDoc "ModuleB.hs" "haskell" contentB
309− _ <- createDoc "ModuleB.hs-boot" "haskell" contentBboot
303+ _ <- openDoc' "ModuleA.hs" "haskell" contentA
304+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
305+ _ <- openDoc' "ModuleB.hs-boot" "haskell" contentBboot
310306 expectDiagnostics []
311307 , testSessionWait "correct reference used with hs-boot" $ do
312308 let contentB = T.unlines
@@ -328,10 +324,10 @@ diagnosticTests = testGroup "diagnostics"
328324 -- resolved to the hs-boot file
329325 , "y = x"
330326 ]
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
327+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
328+ _ <- openDoc' "ModuleA.hs" "haskell" contentA
329+ _ <- openDoc' "ModuleA.hs-boot" "haskell" contentAboot
330+ _ <- openDoc' "ModuleC.hs" "haskell" contentC
335331 expectDiagnostics []
336332 , testSessionWait "redundant import" $ do
337333 let contentA = T.unlines ["module ModuleA where"]
@@ -340,8 +336,8 @@ diagnosticTests = testGroup "diagnostics"
340336 , "module ModuleB where"
341337 , "import ModuleA"
342338 ]
343− _ <- createDoc "ModuleA.hs" "haskell" contentA
344− _ <- createDoc "ModuleB.hs" "haskell" contentB
339+ _ <- openDoc' "ModuleA.hs" "haskell" contentA
340+ _ <- openDoc' "ModuleB.hs" "haskell" contentB
345341 expectDiagnostics
346342 [ ( "ModuleB.hs"
347343 , [(DsWarning, (2, 0), "The import of 'ModuleA' is redundant")]
@@ -363,8 +359,8 @@ diagnosticTests = testGroup "diagnostics"
363359 , "wrong1 = ThisList.map"
364360 , "wrong2 = BaseList.x"
365361 ]
366− _ <- createDoc "Data/List.hs" "haskell" thisDataListContent
367− _ <- createDoc "Main.hs" "haskell" mainContent
362+ _ <- openDoc' "Data/List.hs" "haskell" thisDataListContent
363+ _ <- openDoc' "Main.hs" "haskell" mainContent
368364 expectDiagnostics
369365 [ ( "Main.hs"
370366 , [(DsError, (6, 9), "Not in scope: \8216ThisList.map\8217")
@@ -379,7 +375,7 @@ diagnosticTests = testGroup "diagnostics"
379375 , "foo :: Ord a => a -> Int"
380376 , "foo a = 1"
381377 ]
382− _ <- createDoc "Foo.hs" "haskell" fooContent
378+ _ <- openDoc' "Foo.hs" "haskell" fooContent
383379 expectDiagnostics
384380 [ ( "Foo.hs"
385381 -- The test is to make sure that warnings contain unqualified names
@@ -430,32 +426,13 @@ diagnosticTests = testGroup "diagnostics"
430426 , "foo :: Int"
431427 , "foo = 1 {-|-}"
432428 ]
433− _ <- createDoc "Foo.hs" "haskell" fooContent
429+ _ <- openDoc' "Foo.hs" "haskell" fooContent
434430 expectDiagnostics
435431 [ ( "Foo.hs"
436432 , [(DsError, (2, 8), "Parse error on input")
437433 ]
438434 )
439435 ]
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
459436 ]
460437
461438codeActionTests :: TestTree
@@ -482,25 +459,29 @@ watchedFilesTests :: TestTree
482459watchedFilesTests = testGroup "watched files"
483460 [ testSession' "workspace files" $ \sessionDir -> do
484461 liftIO $ writeFile (sessionDir </> "hie.yaml") "cradle: {direct: {arguments: [\"-isrc\"]}}"
485− _doc <- createDoc "A.hs" "haskell" "{-#LANGUAGE NoImplicitPrelude #-}\nmodule A where\nimport WatchedFilesMissingModule"
462+ _doc <- openDoc' "A.hs" "haskell" "{-#LANGUAGE NoImplicitPrelude #-}\nmodule A where\nimport WatchedFilesMissingModule"
486463 watchedFileRegs <- getWatchedFilesSubscriptionsUntil @PublishDiagnosticsNotification
487464
488− -- Expect 4 subscriptions (A does not get any because it's VFS):
465+ -- Expect 6 subscriptions (A does not get any because it's VFS):
489466 -- - /path-to-workspace/WatchedFilesMissingModule.hs
490467 -- - /path-to-workspace/WatchedFilesMissingModule.lhs
491− -- - /path-to-workspace/src/WatchedFilesMissingModule.hs
492− -- - /path-to-workspace/src/WatchedFilesMissingModule.lhs
493− liftIO $ length watchedFileRegs @?= 4
468+ -- - WatchedFilesMissingModule.hs
469+ -- - WatchedFilesMissingModule.lhs
470+ -- - src/WatchedFilesMissingModule.hs
471+ -- - src/WatchedFilesMissingModule.lhs
472+ liftIO $ length watchedFileRegs @?= 6
494473
495474 , testSession' "non workspace file" $ \sessionDir -> do
496475 liftIO $ writeFile (sessionDir </> "hie.yaml") "cradle: {direct: {arguments: [\"-i/tmp\"]}}"
497− _doc <- createDoc "A.hs" "haskell" "{-# LANGUAGE NoImplicitPrelude#-}\nmodule A where\nimport WatchedFilesMissingModule"
476+ _doc <- openDoc' "A.hs" "haskell" "{-# LANGUAGE NoImplicitPrelude#-}\nmodule A where\nimport WatchedFilesMissingModule"
498477 watchedFileRegs <- getWatchedFilesSubscriptionsUntil @PublishDiagnosticsNotification
499478
500− -- Expect 2 subscriptions (/tmp does not get any as it is out of the workspace):
479+ -- Expect 4 subscriptions (/tmp does not get any as it is out of the workspace):
501480 -- - /path-to-workspace/WatchedFilesMissingModule.hs
502481 -- - /path-to-workspace/WatchedFilesMissingModule.lhs
503− liftIO $ length watchedFileRegs @?= 2
482+ -- - WatchedFilesMissingModule.hs
483+ -- - WatchedFilesMissingModule.lhs
484+ liftIO $ length watchedFileRegs @?= 4
504485
505486 -- TODO add a test for didChangeWorkspaceFolder
506487 ]
@@ -513,7 +494,7 @@ renameActionTests = testGroup "rename actions"
513494 , "foo :: Int -> Int"
514495 , "foo argName = argNme"
515496 ]
516− doc <- createDoc "Testing.hs" "haskell" content
497+ doc <- openDoc' "Testing.hs" "haskell" content
517498 _ <- waitForDiagnostics
518499 action <- findCodeAction doc (Range (Position 2 14) (Position 2 20)) "Replace with ‘argName’"
519500 executeCodeAction action
@@ -531,7 +512,7 @@ renameActionTests = testGroup "rename actions"
531512 , "foo :: Maybe a -> [a]"
532513 , "foo = maybToList"
533514 ]
534− doc <- createDoc "Testing.hs" "haskell" content
515+ doc <- openDoc' "Testing.hs" "haskell" content
535516 _ <- waitForDiagnostics
536517 action <- findCodeAction doc (Range (Position 3 6) (Position 3 16)) "Replace with ‘maybeToList’"
537518 executeCodeAction action
@@ -549,7 +530,7 @@ renameActionTests = testGroup "rename actions"
549530 , "foo :: Char -> Char -> Char -> Char"
550531 , "foo argument1 argument2 argument3 = argumentX"
551532 ]
552− doc <- createDoc "Testing.hs" "haskell" content
533+ doc <- openDoc' "Testing.hs" "haskell" content
553534 _ <- waitForDiagnostics
554535 _ <- findCodeActions doc (Range (Position 2 36) (Position 2 45))
555536 ["Replace with ‘argument1’", "Replace with ‘argument2’", "Replace with ‘argument3’"]
@@ -561,7 +542,7 @@ renameActionTests = testGroup "rename actions"
561542 , "monus x y = max 0 (x - y)"
562543 , "foo x y = x `monnus` y"
563544 ]
564− doc <- createDoc "Testing.hs" "haskell" content
545+ doc <- openDoc' "Testing.hs" "haskell" content
565546 _ <- waitForDiagnostics
566547 actionsOrCommands <- getCodeActions doc (Range (Position 3 12) (Position 3 20))
567548 [fixTypo] <- pure [action | CACodeAction action@CodeAction{ _title = actionTitle } <- actionsOrCommands, "monus" `T.isInfixOf` actionTitle ]
@@ -584,7 +565,7 @@ typeWildCardActionTests = testGroup "type wildcard actions"
584565 , "func :: _"
585566 , "func x = x"
586567 ]
587− doc <- createDoc "Testing.hs" "haskell" content
568+ doc <- openDoc' "Testing.hs" "haskell" content
588569 _ <- waitForDiagnostics
589570 actionsOrCommands <- getCodeActions doc (Range (Position 2 1) (Position 2 10))
590571 let [addSignature] = [action | CACodeAction action@CodeAction { _title = actionTitle } <- actionsOrCommands
@@ -604,7 +585,7 @@ typeWildCardActionTests = testGroup "type wildcard actions"
604585 , "func :: _"
605586 , "func x y = x + y"
606587 ]
607− doc <- createDoc "Testing.hs" "haskell" content
588+ doc <- openDoc' "Testing.hs" "haskell" content
608589 _ <- waitForDiagnostics
609590 actionsOrCommands <- getCodeActions doc (Range (Position 2 1) (Position 2 10))
610591 let [addSignature] = [action | CACodeAction action@CodeAction { _title = actionTitle } <- actionsOrCommands
@@ -627,7 +608,7 @@ typeWildCardActionTests = testGroup "type wildcard actions"
627608 , " y = x * 2"
628609 , " in y"
629610 ]
630− doc <- createDoc "Testing.hs" "haskell" content
611+ doc <- openDoc' "Testing.hs" "haskell" content
631612 _ <- waitForDiagnostics
632613 actionsOrCommands <- getCodeActions doc (Range (Position 4 1) (Position 4 10))
633614 let [addSignature] = [action | CACodeAction action@CodeAction { _title = actionTitle } <- actionsOrCommands
@@ -652,7 +633,7 @@ removeImportTests = testGroup "remove import actions"
652633 let contentA = T.unlines
653634 [ "module ModuleA where"
654635 ]
655− _docA <- createDoc "ModuleA.hs" "haskell" contentA
636+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
656637 let contentB = T.unlines
657638 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
658639 , "module ModuleB where"
@@ -660,7 +641,7 @@ removeImportTests = testGroup "remove import actions"
660641 , "stuffB :: Integer"
661642 , "stuffB = 123"
662643 ]
663− docB <- createDoc "ModuleB.hs" "haskell" contentB
644+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
664645 _ <- waitForDiagnostics
665646 [CACodeAction action@CodeAction { _title = actionTitle }]
666647 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
@@ -678,7 +659,7 @@ removeImportTests = testGroup "remove import actions"
678659 let contentA = T.unlines
679660 [ "module ModuleA where"
680661 ]
681− _docA <- createDoc "ModuleA.hs" "haskell" contentA
662+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
682663 let contentB = T.unlines
683664 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
684665 , "module ModuleB where"
@@ -686,7 +667,7 @@ removeImportTests = testGroup "remove import actions"
686667 , "stuffB :: Integer"
687668 , "stuffB = 123"
688669 ]
689− docB <- createDoc "ModuleB.hs" "haskell" contentB
670+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
690671 _ <- waitForDiagnostics
691672 [CACodeAction action@CodeAction { _title = actionTitle }]
692673 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
@@ -708,14 +689,14 @@ removeImportTests = testGroup "remove import actions"
708689 , "stuffB = 123"
709690 , "stuffC = ()"
710691 ]
711− _docA <- createDoc "ModuleA.hs" "haskell" contentA
692+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
712693 let contentB = T.unlines
713694 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
714695 , "module ModuleB where"
715696 , "import ModuleA (stuffA, stuffB, stuffC, stuffA)"
716697 , "main = print stuffB"
717698 ]
718− docB <- createDoc "ModuleB.hs" "haskell" contentB
699+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
719700 _ <- waitForDiagnostics
720701 [CACodeAction action@CodeAction { _title = actionTitle }]
721702 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
@@ -737,18 +718,22 @@ removeImportTests = testGroup "remove import actions"
737718 , "stuffB :: Integer"
738719 , "stuffB = 123"
739720 ]
740− _docA <- createDoc "ModuleA.hs" "haskell" contentA
721+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
741722 let contentB = T.unlines
742723 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
743724 , "module ModuleB where"
744725 , "import qualified ModuleA as A ((<?>), stuffB, (!!))"
745726 , "main = print A.stuffB"
746727 ]
747− docB <- createDoc "ModuleB.hs" "haskell" contentB
728+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
748729 _ <- waitForDiagnostics
749730 [CACodeAction action@CodeAction { _title = actionTitle }]
750731 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
732+#if MIN_GHC_API_VERSION(8,6,0)
751733 liftIO $ "Remove !!, <?> from import" @=? actionTitle
734+#else
735+ liftIO $ "Remove A.!!, A.<?> from import" @=? actionTitle
736+#endif
752737 executeCodeAction action
753738 contentAfterAction <- documentContents docB
754739 let expectedContentAfterAction = T.unlines
@@ -765,14 +750,14 @@ removeImportTests = testGroup "remove import actions"
765750 , "stuffB :: Integer"
766751 , "stuffB = 123"
767752 ]
768− _docA <- createDoc "ModuleA.hs" "haskell" contentA
753+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
769754 let contentB = T.unlines
770755 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
771756 , "module ModuleB where"
772757 , "import ModuleA (A(..), stuffB)"
773758 , "main = print stuffB"
774759 ]
775− docB <- createDoc "ModuleB.hs" "haskell" contentB
760+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
776761 _ <- waitForDiagnostics
777762 [CACodeAction action@CodeAction { _title = actionTitle }]
778763 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
@@ -792,14 +777,14 @@ removeImportTests = testGroup "remove import actions"
792777 , "data D = A | B"
793778 , "data E = F"
794779 ]
795− _docA <- createDoc "ModuleA.hs" "haskell" contentA
780+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
796781 let contentB = T.unlines
797782 [ "{-# OPTIONS_GHC -Wunused-imports #-}"
798783 , "module ModuleB where"
799784 , "import ModuleA (D(A,B), E(F))"
800785 , "main = B"
801786 ]
802− docB <- createDoc "ModuleB.hs" "haskell" contentB
787+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
803788 _ <- waitForDiagnostics
804789 [CACodeAction action@CodeAction { _title = actionTitle }]
805790 <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
@@ -940,8 +925,8 @@ extendImportTests = testGroup "extend import actions"
940925 ]
941926 where
942927 template contentA contentB range expectedAction expectedContentB = do
943− _docA <- createDoc "ModuleA.hs" "haskell" contentA
944− docB <- createDoc "ModuleB.hs" "haskell" contentB
928+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
929+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
945930 _ <- waitForDiagnostics
946931 CACodeAction action@CodeAction { _title = actionTitle } : _
947932 <- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
@@ -991,7 +976,7 @@ suggestImportTests = testGroup "suggest import actions"
991976 after = T.unlines $ "module A where" : ["import " <> x | x <- imps] ++ [newImp] ++ def : other
992977 cradle = "cradle: {direct: {arguments: [-hide-all-packages, -package, base, -package, text, -package-env, -]}}"
993978 liftIO $ writeFileUTF8 (dir </> "hie.yaml") cradle
994− doc <- createDoc "Test.hs" "haskell" before
979+ doc <- openDoc' "Test.hs" "haskell" before
995980 _diags <- waitForDiagnostics
996981 let defLine = length imps + 1
997982 range = Range (Position defLine 0) (Position defLine maxBound)
@@ -1051,7 +1036,7 @@ addExtensionTests = testGroup "add language extension actions"
10511036 ]
10521037 where
10531038 template initialContent range expectedAction expectedContents = do
1054− doc <- createDoc "Module.hs" "haskell" initialContent
1039+ doc <- openDoc' "Module.hs" "haskell" initialContent
10551040 _ <- waitForDiagnostics
10561041 CACodeAction action@CodeAction { _title = actionTitle } : _
10571042 <- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
@@ -1074,7 +1059,7 @@ insertNewDefinitionTests = testGroup "insert new definition actions"
10741059 [""
10751060 ,"someOtherCode = ()"
10761061 ]
1077− docB <- createDoc "ModuleB.hs" "haskell" (T.unlines $ txtB ++ txtB')
1062+ docB <- openDoc' "ModuleB.hs" "haskell" (T.unlines $ txtB ++ txtB')
10781063 _ <- waitForDiagnostics
10791064 CACodeAction action@CodeAction { _title = actionTitle } : _
10801065 <- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
@@ -1098,7 +1083,7 @@ insertNewDefinitionTests = testGroup "insert new definition actions"
10981083 [""
10991084 ,"someOtherCode = ()"
11001085 ]
1101− docB <- createDoc "ModuleB.hs" "haskell" (T.unlines $ txtB ++ txtB')
1086+ docB <- openDoc' "ModuleB.hs" "haskell" (T.unlines $ txtB ++ txtB')
11021087 _ <- waitForDiagnostics
11031088 CACodeAction action@CodeAction { _title = actionTitle } : _
11041089 <- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
@@ -1137,8 +1122,8 @@ fixConstructorImportTests = testGroup "fix import actions"
11371122 ]
11381123 where
11391124 template contentA contentB range expectedAction expectedContentB = do
1140− _docA <- createDoc "ModuleA.hs" "haskell" contentA
1141− docB <- createDoc "ModuleB.hs" "haskell" contentB
1125+ _docA <- openDoc' "ModuleA.hs" "haskell" contentA
1126+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
11421127 _diags <- waitForDiagnostics
11431128 CACodeAction action@CodeAction { _title = actionTitle } : _
11441129 <- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
@@ -1157,7 +1142,7 @@ importRenameActionTests = testGroup "import rename actions"
11571142 [ "module Testing where"
11581143 , "import Data.Mape"
11591144 ]
1160− doc <- createDoc "Testing.hs" "haskell" content
1145+ doc <- openDoc' "Testing.hs" "haskell" content
11611146 _ <- waitForDiagnostics
11621147 actionsOrCommands <- getCodeActions doc (Range (Position 2 8) (Position 2 16))
11631148 let [changeToMap] = [action | CACodeAction action@CodeAction{ _title = actionTitle } <- actionsOrCommands, ("Data." <> modname) `T.isInfixOf` actionTitle ]
@@ -1194,7 +1179,7 @@ fillTypedHoleTests = let
11941179 newA newB newC = testSession (T.unpack actionTitle) $ do
11951180 let originalCode = sourceCode oldA oldB oldC
11961181 let expectedCode = sourceCode newA newB newC
1197− doc <- createDoc "Testing.hs" "haskell" originalCode
1182+ doc <- openDoc' "Testing.hs" "haskell" originalCode
11981183 _ <- waitForDiagnostics
11991184 actionsOrCommands <- getCodeActions doc (Range (Position 9 0) (Position 9 maxBound))
12001185 chosenAction <- liftIO $ pickActionWithTitle actionTitle actionsOrCommands
@@ -1242,7 +1227,7 @@ addSigActionTests = let
12421227 def >:: sig = testSession (T.unpack def) $ do
12431228 let originalCode = before def
12441229 let expectedCode = after' def sig
1245− doc <- createDoc "Sigs.hs" "haskell" originalCode
1230+ doc <- openDoc' "Sigs.hs" "haskell" originalCode
12461231 _ <- waitForDiagnostics
12471232 actionsOrCommands <- getCodeActions doc (Range (Position 3 1) (Position 3 maxBound))
12481233 chosenAction <- liftIO $ pickActionWithTitle ("add signature: " <> sig) actionsOrCommands
@@ -1274,7 +1259,7 @@ addSigLensesTests = let
12741259 sigSession withMissing def sig = testSession (T.unpack def) $ do
12751260 let originalCode = before withMissing def
12761261 let expectedCode = after' withMissing def sig
1277− doc <- createDoc "Sigs.hs" "haskell" originalCode
1262+ doc <- openDoc' "Sigs.hs" "haskell" originalCode
12781263 [CodeLens {_command = Just c}] <- getCodeLenses doc
12791264 executeCommand c
12801265 modifiedCode <- getDocumentEdit doc
@@ -1296,36 +1281,33 @@ addSigLensesTests = let
12961281 ]
12971282 ]
12981283
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−
13201284findDefinitionAndHoverTests :: TestTree
13211285findDefinitionAndHoverTests = let
13221286
1323− tst (get, check) pos targetRange title = testSessionWithExtraFiles "hover" title $ \dir -> do
1324− doc <- openTestDataDoc (dir </> sourceFilePath)
1287+ tst (get, check) pos targetRange title = testSession title $ do
1288+ doc <- openTestDataDoc sourceFilePath
13251289 found <- get doc pos
13261290 check found targetRange
13271291
1292+ checkDefs :: [Location] -> Session [Expect] -> Session ()
1293+ checkDefs defs mkExpectations = traverse_ check =<< mkExpectations where
1294+
1295+ check (ExpectRange expectedRange) = do
1296+ assertNDefinitionsFound 1 defs
1297+ assertRangeCorrect (head defs) expectedRange
1298+ check (ExpectLocation expectedLocation) = do
1299+ assertNDefinitionsFound 1 defs
1300+ liftIO $ head defs @?= expectedLocation
1301+ check ExpectNoDefinitions = do
1302+ assertNDefinitionsFound 0 defs
1303+ check ExpectExternFail = liftIO $ assertFailure "Expecting to fail to find in external file"
1304+ check _ = pure () -- all other expectations not relevant to getDefinition
13281305
1306+ assertNDefinitionsFound :: Int -> [a] -> Session ()
1307+ assertNDefinitionsFound n defs = liftIO $ assertEqual "number of definitions" n (length defs)
1308+
1309+ assertRangeCorrect Location{_range = foundRange} expectedRange =
1310+ liftIO $ expectedRange @=? foundRange
13291311
13301312 checkHover :: Maybe Hover -> Session [Expect] -> Session ()
13311313 checkHover hover expectations = traverse_ check =<< expectations where
@@ -1371,8 +1353,7 @@ findDefinitionAndHoverTests = let
13711353
13721354 mkFindTests tests = testGroup "get"
13731355 [ testGroup "definition" $ mapMaybe fst tests
1374− , testGroup "hover" $ mapMaybe snd tests
1375− , checkFileCompiles sourceFilePath ]
1356+ , testGroup "hover" $ mapMaybe snd tests ]
13761357
13771358 test runDef runHover look expect = testM runDef runHover look (return expect)
13781359
@@ -1467,14 +1448,6 @@ findDefinitionAndHoverTests = let
14671448 broken = Just . (`xfail` "known broken")
14681449 no = const Nothing -- don't run this test at all
14691450
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−
14781451pluginTests :: TestTree
14791452pluginTests = (`xfail8101` "known broken (#556)")
14801453 $ testSessionWait "plugins" $ do
@@ -1491,7 +1464,7 @@ pluginTests = (`xfail8101` "known broken (#556)")
14911464 , "foo :: Int -> Int -> Int"
14921465 , "foo a b = a + c"
14931466 ]
1494− _ <- createDoc "Testing.hs" "haskell" content
1467+ _ <- openDoc' "Testing.hs" "haskell" content
14951468 expectDiagnostics
14961469 [ ( "Testing.hs",
14971470 [(DsError, (8, 14), "Variable not in scope: c")]
@@ -1518,7 +1491,7 @@ cppTests =
15181491 run $ expectError content (2, 1)
15191492 )
15201493 , testSessionWait "cpp-ghcide" $ do
1521− _ <- createDoc "A.hs" "haskell" $ T.unlines
1494+ _ <- openDoc' "A.hs" "haskell" $ T.unlines
15221495 ["{-# LANGUAGE CPP #-}"
15231496 ,"main ="
15241497 ,"#ifdef __GHCIDE__"
@@ -1532,7 +1505,7 @@ cppTests =
15321505 where
15331506 expectError :: T.Text -> Cursor -> Session ()
15341507 expectError content cursor = do
1535− _ <- createDoc "Testing.hs" "haskell" content
1508+ _ <- openDoc' "Testing.hs" "haskell" content
15361509 expectDiagnostics
15371510 [ ( "Testing.hs",
15381511 [(DsError, cursor, "error: unterminated")]
@@ -1548,7 +1521,7 @@ preprocessorTests = testSessionWait "preprocessor" $ do
15481521 , "module Testing where"
15491522 , "y = x + z" -- plugin replaces x with y, making this have only one diagnostic
15501523 ]
1551− _ <- createDoc "Testing.hs" "haskell" content
1524+ _ <- openDoc' "Testing.hs" "haskell" content
15521525 expectDiagnostics
15531526 [ ( "Testing.hs",
15541527 [(DsError, (2, 8), "Variable not in scope: z")]
@@ -1581,8 +1554,8 @@ safeTests =
15811554 ,"safeId = trustWorthyId"
15821555 ]
15831556
1584− _ <- createDoc "A.hs" "haskell" sourceA
1585− _ <- createDoc "B.hs" "haskell" sourceB
1557+ _ <- openDoc' "A.hs" "haskell" sourceA
1558+ _ <- openDoc' "B.hs" "haskell" sourceB
15861559 expectNoMoreDiagnostics 1 ]
15871560
15881561thTests :: TestTree
@@ -1610,8 +1583,8 @@ thTests =
16101583 "b :: Integer",
16111584 "b = $(litE $ IntegerL $ a) + n"
16121585 ]
1613− _ <- createDoc "A.hs" "haskell" sourceA
1614− _ <- createDoc "B.hs" "haskell" sourceB
1586+ _ <- openDoc' "A.hs" "haskell" sourceA
1587+ _ <- openDoc' "B.hs" "haskell" sourceB
16151588 expectDiagnostics [ ( "B.hs", [(DsError, (6, 29), "Variable not in scope: n")] ) ]
16161589 , testSessionWait "newtype-closure" $ do
16171590 let sourceA =
@@ -1631,8 +1604,8 @@ thTests =
16311604 ,"import A"
16321605 ,"b :: Int"
16331606 ,"b = $( a )" ]
1634− _ <- createDoc "A.hs" "haskell" sourceA
1635− _ <- createDoc "B.hs" "haskell" sourceB
1607+ _ <- openDoc' "A.hs" "haskell" sourceA
1608+ _ <- openDoc' "B.hs" "haskell" sourceB
16361609 return ()
16371610 ]
16381611
@@ -1641,7 +1614,7 @@ completionTests
16411614 = testGroup "completion"
16421615 [ testSessionWait "variable" $ do
16431616 let source = T.unlines ["module A where", "f = hea"]
1644− docId <- createDoc "A.hs" "haskell" source
1617+ docId <- openDoc' "A.hs" "haskell" source
16451618 compls <- getCompletions docId (Position 1 7)
16461619 liftIO $ map dropDocs compls @?=
16471620 [complItem "head" (Just CiFunction) (Just "[a] -> a")]
@@ -1653,7 +1626,7 @@ completionTests
16531626 ]
16541627 , testSessionWait "constructor" $ do
16551628 let source = T.unlines ["module A where", "f = Tru"]
1656− docId <- createDoc "A.hs" "haskell" source
1629+ docId <- openDoc' "A.hs" "haskell" source
16571630 compls <- getCompletions docId (Position 1 7)
16581631 liftIO $ map dropDocs compls @?=
16591632 [ complItem "True" (Just CiConstructor) (Just "Bool")
@@ -1665,7 +1638,7 @@ completionTests
16651638 ]
16661639 , testSessionWait "type" $ do
16671640 let source = T.unlines ["{-# OPTIONS_GHC -Wall #-}", "module A () where", "f :: ()", "f = ()"]
1668− docId <- createDoc "A.hs" "haskell" source
1641+ docId <- openDoc' "A.hs" "haskell" source
16691642 expectDiagnostics [ ("A.hs", [(DsWarning, (3,0), "not used")]) ]
16701643 changeDoc docId [TextDocumentContentChangeEvent Nothing Nothing $ T.unlines ["{-# OPTIONS_GHC -Wall #-}", "module A () where", "f :: Bo", "f = True"]]
16711644 compls <- getCompletions docId (Position 2 7)
@@ -1682,7 +1655,7 @@ completionTests
16821655 checkDocText "Bool" boolDocs [ "Defined in 'Prelude'" ]
16831656 , testSessionWait "qualified" $ do
16841657 let source = T.unlines ["{-# OPTIONS_GHC -Wunused-binds #-}", "module A () where", "f = ()"]
1685− docId <- createDoc "A.hs" "haskell" source
1658+ docId <- openDoc' "A.hs" "haskell" source
16861659 expectDiagnostics [ ("A.hs", [(DsWarning, (2, 0), "not used")]) ]
16871660 changeDoc docId [TextDocumentContentChangeEvent Nothing Nothing $ T.unlines ["{-# OPTIONS_GHC -Wunused-binds #-}", "module A () where", "f = Prelude.hea"]]
16881661 compls <- getCompletions docId (Position 2 15)
@@ -1696,7 +1669,7 @@ completionTests
16961669 ]
16971670 , testSessionWait "keyword" $ do
16981671 let source = T.unlines ["module A where", "f = newty"]
1699− docId <- createDoc "A.hs" "haskell" source
1672+ docId <- openDoc' "A.hs" "haskell" source
17001673 compls <- getCompletions docId (Position 1 9)
17011674 liftIO $ compls @?= [keywordItem "newtype"]
17021675 , testSessionWait "type context" $ do
@@ -1705,7 +1678,7 @@ completionTests
17051678 , "module A () where"
17061679 , "f = f"
17071680 ]
1708− docId <- createDoc "A.hs" "haskell" source
1681+ docId <- openDoc' "A.hs" "haskell" source
17091682 expectDiagnostics [("A.hs", [(DsWarning, (2, 0), "not used")])]
17101683 changeDoc docId
17111684 [ TextDocumentContentChangeEvent Nothing Nothing $ T.unlines
@@ -1773,7 +1746,7 @@ outlineTests = testGroup
17731746 "outline"
17741747 [ testSessionWait "type class" $ do
17751748 let source = T.unlines ["module A where", "class A a where a :: a -> Bool"]
1776− docId <- createDoc "A.hs" "haskell" source
1749+ docId <- openDoc' "A.hs" "haskell" source
17771750 symbols <- getDocumentSymbols docId
17781751 liftIO $ symbols @?= Left
17791752 [ moduleSymbol
@@ -1786,7 +1759,7 @@ outlineTests = testGroup
17861759 ]
17871760 , testSessionWait "type class instance " $ do
17881761 let source = T.unlines ["class A a where", "instance A () where"]
1789− docId <- createDoc "A.hs" "haskell" source
1762+ docId <- openDoc' "A.hs" "haskell" source
17901763 symbols <- getDocumentSymbols docId
17911764 liftIO $ symbols @?= Left
17921765 [ classSymbol "A a" (R 0 0 0 15) []
@@ -1794,7 +1767,7 @@ outlineTests = testGroup
17941767 ]
17951768 , testSessionWait "type family" $ do
17961769 let source = T.unlines ["{-# language TypeFamilies #-}", "type family A"]
1797− docId <- createDoc "A.hs" "haskell" source
1770+ docId <- openDoc' "A.hs" "haskell" source
17981771 symbols <- getDocumentSymbols docId
17991772 liftIO $ symbols @?= Left [docSymbolD "A" "type family" SkClass (R 1 0 1 13)]
18001773 , testSessionWait "type family instance " $ do
@@ -1803,7 +1776,7 @@ outlineTests = testGroup
18031776 , "type family A a"
18041777 , "type instance A () = ()"
18051778 ]
1806− docId <- createDoc "A.hs" "haskell" source
1779+ docId <- openDoc' "A.hs" "haskell" source
18071780 symbols <- getDocumentSymbols docId
18081781 liftIO $ symbols @?= Left
18091782 [ docSymbolD "A a" "type family" SkClass (R 1 0 1 15)
@@ -1811,7 +1784,7 @@ outlineTests = testGroup
18111784 ]
18121785 , testSessionWait "data family" $ do
18131786 let source = T.unlines ["{-# language TypeFamilies #-}", "data family A"]
1814− docId <- createDoc "A.hs" "haskell" source
1787+ docId <- openDoc' "A.hs" "haskell" source
18151788 symbols <- getDocumentSymbols docId
18161789 liftIO $ symbols @?= Left [docSymbolD "A" "data family" SkClass (R 1 0 1 11)]
18171790 , testSessionWait "data family instance " $ do
@@ -1820,7 +1793,7 @@ outlineTests = testGroup
18201793 , "data family A a"
18211794 , "data instance A () = A ()"
18221795 ]
1823− docId <- createDoc "A.hs" "haskell" source
1796+ docId <- openDoc' "A.hs" "haskell" source
18241797 symbols <- getDocumentSymbols docId
18251798 liftIO $ symbols @?= Left
18261799 [ docSymbolD "A a" "data family" SkClass (R 1 0 1 11)
@@ -1828,36 +1801,36 @@ outlineTests = testGroup
18281801 ]
18291802 , testSessionWait "constant" $ do
18301803 let source = T.unlines ["a = ()"]
1831− docId <- createDoc "A.hs" "haskell" source
1804+ docId <- openDoc' "A.hs" "haskell" source
18321805 symbols <- getDocumentSymbols docId
18331806 liftIO $ symbols @?= Left
18341807 [docSymbol "a" SkFunction (R 0 0 0 6)]
18351808 , testSessionWait "pattern" $ do
18361809 let source = T.unlines ["Just foo = Just 21"]
1837− docId <- createDoc "A.hs" "haskell" source
1810+ docId <- openDoc' "A.hs" "haskell" source
18381811 symbols <- getDocumentSymbols docId
18391812 liftIO $ symbols @?= Left
18401813 [docSymbol "Just foo" SkFunction (R 0 0 0 18)]
18411814 , testSessionWait "pattern with type signature" $ do
18421815 let source = T.unlines ["{-# language ScopedTypeVariables #-}", "a :: () = ()"]
1843− docId <- createDoc "A.hs" "haskell" source
1816+ docId <- openDoc' "A.hs" "haskell" source
18441817 symbols <- getDocumentSymbols docId
18451818 liftIO $ symbols @?= Left
18461819 [docSymbol "a :: ()" SkFunction (R 1 0 1 12)]
18471820 , testSessionWait "function" $ do
18481821 let source = T.unlines ["a x = ()"]
1849− docId <- createDoc "A.hs" "haskell" source
1822+ docId <- openDoc' "A.hs" "haskell" source
18501823 symbols <- getDocumentSymbols docId
18511824 liftIO $ symbols @?= Left [docSymbol "a" SkFunction (R 0 0 0 8)]
18521825 , testSessionWait "type synonym" $ do
18531826 let source = T.unlines ["type A = Bool"]
1854− docId <- createDoc "A.hs" "haskell" source
1827+ docId <- openDoc' "A.hs" "haskell" source
18551828 symbols <- getDocumentSymbols docId
18561829 liftIO $ symbols @?= Left
18571830 [docSymbol' "A" SkTypeParameter (R 0 0 0 13) (R 0 5 0 6)]
18581831 , testSessionWait "datatype" $ do
18591832 let source = T.unlines ["data A = C"]
1860− docId <- createDoc "A.hs" "haskell" source
1833+ docId <- openDoc' "A.hs" "haskell" source
18611834 symbols <- getDocumentSymbols docId
18621835 liftIO $ symbols @?= Left
18631836 [ docSymbolWithChildren "A"
@@ -1865,21 +1838,9 @@ outlineTests = testGroup
18651838 (R 0 0 0 10)
18661839 [docSymbol "C" SkConstructor (R 0 9 0 10)]
18671840 ]
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− ]
18801841 , testSessionWait "import" $ do
18811842 let source = T.unlines ["import Data.Maybe"]
1882− docId <- createDoc "A.hs" "haskell" source
1843+ docId <- openDoc' "A.hs" "haskell" source
18831844 symbols <- getDocumentSymbols docId
18841845 liftIO $ symbols @?= Left
18851846 [docSymbolWithChildren "imports"
@@ -1890,7 +1851,7 @@ outlineTests = testGroup
18901851 ]
18911852 , testSessionWait "multiple import" $ do
18921853 let source = T.unlines ["", "import Data.Maybe", "", "import Control.Exception", ""]
1893− docId <- createDoc "A.hs" "haskell" source
1854+ docId <- openDoc' "A.hs" "haskell" source
18941855 symbols <- getDocumentSymbols docId
18951856 liftIO $ symbols @?= Left
18961857 [docSymbolWithChildren "imports"
@@ -1905,7 +1866,7 @@ outlineTests = testGroup
19051866 [ "{-# language ForeignFunctionInterface #-}"
19061867 , "foreign import ccall \"a\" a :: Int"
19071868 ]
1908− docId <- createDoc "A.hs" "haskell" source
1869+ docId <- openDoc' "A.hs" "haskell" source
19091870 symbols <- getDocumentSymbols docId
19101871 liftIO $ symbols @?= Left [docSymbolD "a" "import" SkObject (R 1 0 1 33)]
19111872 , testSessionWait "foreign export" $ do
@@ -1913,7 +1874,7 @@ outlineTests = testGroup
19131874 [ "{-# language ForeignFunctionInterface #-}"
19141875 , "foreign export ccall odd :: Int -> Bool"
19151876 ]
1916− docId <- createDoc "A.hs" "haskell" source
1877+ docId <- openDoc' "A.hs" "haskell" source
19171878 symbols <- getDocumentSymbols docId
19181879 liftIO $ symbols @?= Left [docSymbolD "odd" "export" SkObject (R 1 0 1 39)]
19191880 ]
@@ -1926,8 +1887,6 @@ outlineTests = testGroup
19261887 DocumentSymbol name (Just detail) kind Nothing loc loc Nothing
19271888 docSymbolWithChildren name kind loc cc =
19281889 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)
19311890 moduleSymbol name loc cc = DocumentSymbol name
19321891 Nothing
19331892 SkFile
@@ -2033,7 +1992,6 @@ cradleTests :: TestTree
20331992cradleTests = testGroup "cradle"
20341993 [testGroup "dependencies" [sessionDepsArePickedUp]
20351994 ,testGroup "loading" [loadCradleOnlyonce]
2036− ,testGroup "multi" [simpleMultiTest, simpleMultiTest2]
20371995 ]
20381996
20391997loadCradleOnlyonce :: TestTree
@@ -2048,53 +2006,17 @@ loadCradleOnlyonce = testGroup "load cradle only once"
20482006 test dir
20492007 implicit dir = test dir
20502008 test _dir = do
2051− doc <- createDoc "B.hs" "haskell" "module B where\nimport Data.Foo"
2009+ doc <- openDoc' "B.hs" "haskell" "module B where\nimport Data.Foo"
20522010 msgs <- someTill (skipManyTill anyMessage cradleLoadedMessage) (skipManyTill anyMessage (message @PublishDiagnosticsNotification))
20532011 liftIO $ length msgs @?= 1
20542012 changeDoc doc [TextDocumentContentChangeEvent Nothing Nothing "module B where\nimport Data.Maybe"]
20552013 msgs <- manyTill (skipManyTill anyMessage cradleLoadedMessage) (skipManyTill anyMessage (message @PublishDiagnosticsNotification))
20562014 liftIO $ length msgs @?= 0
2057− _ <- createDoc "A.hs" "haskell" "module A where\nimport LoadCradleBar"
2015+ _ <- openDoc' "A.hs" "haskell" "module A where\nimport LoadCradleBar"
20582016 msgs <- manyTill (skipManyTill anyMessage cradleLoadedMessage) (skipManyTill anyMessage (message @PublishDiagnosticsNotification))
20592017 liftIO $ length msgs @?= 0
20602018
20612019
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−
20982020cradleLoadedMessage :: Session FromServerMessage
20992021cradleLoadedMessage = satisfy $ \case
21002022 NotCustomServer (NotificationMessage _ (CustomServerMethod m) _) -> m == cradleLoadedMethod
@@ -2103,56 +2025,6 @@ cradleLoadedMessage = satisfy $ \case
21032025cradleLoadedMethod :: T.Text
21042026cradleLoadedMethod = "ghcide/cradle/loaded"
21052027
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−
21562028sessionDepsArePickedUp :: TestTree
21572029sessionDepsArePickedUp = testSession'
21582030 "session-deps-are-picked-up"
@@ -2162,7 +2034,7 @@ sessionDepsArePickedUp = testSession'
21622034 (dir </> "hie.yaml")
21632035 "cradle: {direct: {arguments: []}}"
21642036 -- Open without OverloadedStrings and expect an error.
2165− doc <- createDoc "Foo.hs" "haskell" fooContent
2037+ doc <- openDoc' "Foo.hs" "haskell" fooContent
21662038 expectDiagnostics
21672039 [("Foo.hs", [(DsError, (3, 6), "Couldn't match expected type")])]
21682040 -- Update hie.yaml to enable OverloadedStrings.
@@ -2197,9 +2069,6 @@ sessionDepsArePickedUp = testSession'
21972069testSession :: String -> Session () -> TestTree
21982070testSession name = testCase name . run
21992071
2200−testSessionWithExtraFiles :: FilePath -> String -> (FilePath -> Session ()) -> TestTree
2201−testSessionWithExtraFiles prefix name = testCase name . runWithExtraFiles prefix
2202−
22032072testSession' :: String -> (FilePath -> Session ()) -> TestTree
22042073testSession' name = testCase name . run'
22052074
@@ -2234,19 +2103,6 @@ mkRange a b c d = Range (Position a b) (Position c d)
22342103run :: Session a -> IO a
22352104run s = withTempDir $ \dir -> runInDir dir s
22362105
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−
22502106run' :: (FilePath -> Session a) -> IO a
22512107run' s = withTempDir $ \dir -> runInDir dir (s dir)
22522108
@@ -2258,6 +2114,11 @@ runInDir dir s = do
22582114 -- since the package import test creates "Data/List.hs", which otherwise has no physical home
22592115 createDirectoryIfMissing True $ dir ++ "/Data"
22602116
2117+ -- Copy all the test data files to the temporary workspace
2118+ testDataFiles <- getDirectoryFilesIO "test/data" ["//*"]
2119+ for_ testDataFiles $ \f -> do
2120+ createDirectoryIfMissing True $ dir </> takeDirectory f
2121+ copyFile ("test/data" </> f) (dir </> f)
22612122
22622123 let cmd = unwords [ghcideExe, "--lsp", "--test", "--cwd", dir]
22632124 -- HIE calls getXgdDirectory which assumes that HOME is set.
@@ -2276,7 +2137,7 @@ runInDir dir s = do
22762137openTestDataDoc :: FilePath -> Session TextDocumentIdentifier
22772138openTestDataDoc path = do
22782139 source <- liftIO $ readFileUtf8 $ "test/data" </> path
2279− createDoc path "haskell" source
2140+ openDoc' path "haskell" source
22802141
22812142findCodeActions :: TextDocumentIdentifier -> Range -> [T.Text] -> Session [CodeAction]
22822143findCodeActions doc range expectedTitles = do
@@ -2317,6 +2178,20 @@ unitTests = do
23172178 uriToFilePath' uri @?= Just ""
23182179 ]
23192180
2181+-- | Wrapper around 'LSPTest.openDoc'' that sends file creation events
2182+openDoc' :: FilePath -> String -> T.Text -> Session TextDocumentIdentifier
2183+openDoc' fp name contents = do
2184+ res@(TextDocumentIdentifier uri) <- LSPTest.openDoc' fp name contents
2185+ -- Needed as ghcide sets up and relies on WatchedFiles but lsp-test does not track them
2186+ sendNotification WorkspaceDidChangeWatchedFiles (DidChangeWatchedFilesParams $ List [FileEvent uri FcCreated])
2187+ return res
2188+
2189+-- | Version of 'LSPTest.openDoc'' that does not send WatchedFiles events for files outside the workspace
2190+openDoc'' :: FilePath -> String -> T.Text -> Session TextDocumentIdentifier
2191+-- At the moment this is just LSPTest.openDoc' but it may change in the future
2192+-- when/if lsp-test implements WatchedFiles
2193+openDoc'' = LSPTest.openDoc'
2194+
23202195positionMappingTests :: TestTree
23212196positionMappingTests =
23222197 testGroup "position mapping"