Skip to content

Commit

Permalink
Add goto dependency definition test
Browse files Browse the repository at this point in the history
  • Loading branch information
nlander committed Aug 8, 2023
1 parent c532025 commit 6daf799
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions ghcide/ghcide.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ test-suite ghcide-tests
ReferenceTests
GarbageCollectionTests
OpenCloseTest
Dependency
default-extensions:
BangPatterns
DeriveFunctor
Expand Down
6 changes: 6 additions & 0 deletions ghcide/test/data/dependency/Dependency.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Dependency where

import Control.Concurrent.Async (AsyncCancelled (..))

asyncCancelled :: AsyncCancelled
asyncCancelled = AsyncCancelled
11 changes: 11 additions & 0 deletions ghcide/test/data/dependency/dependency.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: dependency
version: 0.1.0.0
cabal-version: 2.0
build-type: Simple

library
exposed-modules: Dependency
default-language: Haskell2010
build-depends: base
, async == 2.2.4
ghc-options: -fwrite-ide-info
2 changes: 2 additions & 0 deletions ghcide/test/data/dependency/hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cradle:
cabal:
72 changes: 72 additions & 0 deletions ghcide/test/exe/Dependency.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE GADTs #-}
module Dependency where

import qualified Control.Applicative as Applicative
import Control.Applicative.Combinators (skipManyTill)
import Control.Monad.IO.Class (liftIO)
import qualified Data.Aeson as A
import Data.Bool (bool)
import Data.List (isSuffixOf)
import Data.Proxy (Proxy (..))
import Language.LSP.Protocol.Message (TCustomMessage (NotMess),
TNotificationMessage (..))
import Language.LSP.Protocol.Types (Definition (..),
Location (..), Position (..),
Range (..),
type (|?) (InL, InR),
uriToFilePath)
import Language.LSP.Test (Session, anyMessage,
customNotification,
getDefinitions, openDoc)
import System.FilePath (splitDirectories, (<.>),
(</>))
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (assertBool, assertFailure,
(@?=))
import TestUtils (testSessionWithExtraFiles)

tests :: TestTree
tests =
testGroup "gotoDefinition for dependencies"
[ dependencyTest
]
where
dependencyTest :: TestTree
dependencyTest = testSessionWithExtraFiles "dependency" "gotoDefinition in async" $
\dir -> do
doc <- openDoc (dir </> "Dependency" <.> "hs") "haskell"
_hieFile <- fileDoneIndexing ["Control", "Concurrent", "Async.hie"]
defs <- getDefinitions doc (Position 5 20)
let expRange = Range (Position 430 22) (Position 430 36)
case defs of
InL (Definition (InR [Location fp actualRange])) ->
liftIO $ do
let locationDirectories :: [String]
locationDirectories =
maybe [] splitDirectories $
uriToFilePath fp
assertBool "AsyncCancelled found in a module that is not Control.Concurrent Async"
$ ["Control", "Concurrent", "Async.hs"]
`isSuffixOf` locationDirectories
actualRange @?= expRange
wrongLocation ->
liftIO $
assertFailure $ "Wrong location for AsyncCancelled: "
++ show wrongLocation
fileDoneIndexing :: [String] -> Session FilePath
fileDoneIndexing fpSuffix =
skipManyTill anyMessage indexedFile
where
indexedFile :: Session FilePath
indexedFile = do
NotMess TNotificationMessage{_params} <-
customNotification (Proxy @"ghcide/reference/ready")
case A.fromJSON _params of
A.Success fp -> do
let fpDirs :: [String]
fpDirs = splitDirectories fp
bool Applicative.empty (pure fp) $
fpSuffix `isSuffixOf` fpDirs
other -> error $ "Failed to parse ghcide/reference/ready file: " <> show other
2 changes: 2 additions & 0 deletions ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import ClientSettingsTests
import ReferenceTests
import GarbageCollectionTests
import ExceptionTests
import Dependency

main :: IO ()
main = do
Expand Down Expand Up @@ -124,4 +125,5 @@ main = do
, GarbageCollectionTests.tests
, HieDbRetry.tests
, ExceptionTests.tests recorder logger
, Dependency.tests
]

0 comments on commit 6daf799

Please sign in to comment.