|
| 1 | +-- Copyright (c) 2019-2020 The DAML and HLS Authors. All rights reserved. |
| 2 | +-- SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +{-# LANGUAGE DuplicateRecordFields #-} |
| 5 | +{-# LANGUAGE OverloadedStrings #-} |
| 6 | +{-# LANGUAGE PatternSynonyms #-} |
| 7 | +{-# LANGUAGE CPP #-} |
| 8 | + |
| 9 | +module Main (main) where |
| 10 | + |
| 11 | +import Control.Applicative.Combinators |
| 12 | +import Control.Exception (catch) |
| 13 | +import Control.Monad |
| 14 | +import Control.Monad.IO.Class (liftIO) |
| 15 | +import Data.Char (toLower) |
| 16 | +import Data.Foldable |
| 17 | +import Data.List |
| 18 | +import Data.Rope.UTF16 (Rope) |
| 19 | +import qualified Data.Rope.UTF16 as Rope |
| 20 | +import Development.IDE.Core.PositionMapping (fromCurrent, toCurrent) |
| 21 | +import Development.IDE.GHC.Util |
| 22 | +import qualified Data.Text as T |
| 23 | +import Development.IDE.Spans.Common |
| 24 | +-- import Development.IDE.Test |
| 25 | +-- import Development.IDE.Test.Runfiles |
| 26 | +import Development.IDE.Types.Location |
| 27 | +import qualified Language.Haskell.LSP.Test as LSPTest |
| 28 | +import Language.Haskell.LSP.Test hiding (openDoc') |
| 29 | +import Language.Haskell.LSP.Types |
| 30 | +import Language.Haskell.LSP.Types.Capabilities |
| 31 | +import Language.Haskell.LSP.VFS (applyChange) |
| 32 | +import System.Environment.Blank (setEnv) |
| 33 | +import System.FilePath |
| 34 | +import System.IO.Extra |
| 35 | +import System.Directory |
| 36 | +import Test.QuickCheck |
| 37 | +import Test.QuickCheck.Instances () |
| 38 | +import Test.Tasty |
| 39 | +import Test.Tasty.ExpectedFailure |
| 40 | +import Test.Tasty.HUnit |
| 41 | +import Test.Tasty.QuickCheck |
| 42 | +import Data.Maybe |
| 43 | + |
| 44 | +import TestUtils |
| 45 | + |
| 46 | +-- --------------------------------------------------------------------- |
| 47 | + |
| 48 | +main :: IO () |
| 49 | +main = defaultMain $ testGroup "HLS" |
| 50 | + [ testSession "open close" $ do |
| 51 | + doc <- openDoc' "Testing.hs" "haskell" "" |
| 52 | + void (message :: Session WorkDoneProgressCreateRequest) |
| 53 | + void (message :: Session WorkDoneProgressBeginNotification) |
| 54 | + closeDoc doc |
| 55 | + void (message :: Session WorkDoneProgressEndNotification) |
| 56 | + ] |
| 57 | + |
| 58 | +---------------------------------------------------------------------- |
| 59 | +-- Utils |
| 60 | + |
| 61 | + |
| 62 | +testSession :: String -> Session () -> TestTree |
| 63 | +testSession name = testCase name . run |
| 64 | + |
| 65 | +{- |
| 66 | +testSessionWait :: String -> Session () -> TestTree |
| 67 | +testSessionWait name = testSession name . |
| 68 | + -- Check that any diagnostics produced were already consumed by the test case. |
| 69 | + -- |
| 70 | + -- If in future we add test cases where we don't care about checking the diagnostics, |
| 71 | + -- this could move elsewhere. |
| 72 | + -- |
| 73 | + -- Experimentally, 0.5s seems to be long enough to wait for any final diagnostics to appear. |
| 74 | + ( >> expectNoMoreDiagnostics 0.5) |
| 75 | +
|
| 76 | +pickActionWithTitle :: T.Text -> [CAResult] -> CodeAction |
| 77 | +pickActionWithTitle title actions = head |
| 78 | + [ action |
| 79 | + | CACodeAction action@CodeAction{ _title = actionTitle } <- actions |
| 80 | + , title == actionTitle ] |
| 81 | +-} |
| 82 | + |
| 83 | +mkRange :: Int -> Int -> Int -> Int -> Range |
| 84 | +mkRange a b c d = Range (Position a b) (Position c d) |
| 85 | + |
| 86 | +run :: Session a -> IO a |
| 87 | +run s = withTempDir $ \dir -> do |
| 88 | + let ghcideExe = hieCommand |
| 89 | + |
| 90 | + -- Temporarily hack around https://github.com/mpickering/hie-bios/pull/56 |
| 91 | + -- since the package import test creates "Data/List.hs", which otherwise has no physical home |
| 92 | + createDirectoryIfMissing True $ dir ++ "/Data" |
| 93 | + |
| 94 | + let cmd = unwords [ghcideExe, "--lsp", "--cwd", dir] |
| 95 | + -- HIE calls getXgdDirectory which assumes that HOME is set. |
| 96 | + -- Only sets HOME if it wasn't already set. |
| 97 | + setEnv "HOME" "/homeless-shelter" False |
| 98 | + let lspTestCaps = fullCaps { _window = Just $ WindowClientCapabilities $ Just True } |
| 99 | + runSessionWithConfig conf cmd lspTestCaps dir s |
| 100 | + where |
| 101 | + conf = defaultConfig |
| 102 | + -- If you uncomment this you can see all logging |
| 103 | + -- which can be quite useful for debugging. |
| 104 | + -- { logStdErr = True, logColor = False } |
| 105 | + -- If you really want to, you can also see all messages |
| 106 | + -- { logMessages = True, logColor = False } |
| 107 | + |
| 108 | +openTestDataDoc :: FilePath -> Session TextDocumentIdentifier |
| 109 | +openTestDataDoc path = do |
| 110 | + source <- liftIO $ readFileUtf8 $ "test/data" </> path |
| 111 | + openDoc' path "haskell" source |
| 112 | + |
| 113 | +findCodeActions :: TextDocumentIdentifier -> Range -> [T.Text] -> Session [CodeAction] |
| 114 | +findCodeActions doc range expectedTitles = do |
| 115 | + actions <- getCodeActions doc range |
| 116 | + let matches = sequence |
| 117 | + [ listToMaybe |
| 118 | + [ action |
| 119 | + | CACodeAction action@CodeAction { _title = actionTitle } <- actions |
| 120 | + , actionTitle == expectedTitle ] |
| 121 | + | expectedTitle <- expectedTitles] |
| 122 | + let msg = show |
| 123 | + [ actionTitle |
| 124 | + | CACodeAction CodeAction { _title = actionTitle } <- actions |
| 125 | + ] |
| 126 | + ++ "is not a superset of " |
| 127 | + ++ show expectedTitles |
| 128 | + liftIO $ case matches of |
| 129 | + Nothing -> assertFailure msg |
| 130 | + Just _ -> pure () |
| 131 | + return (fromJust matches) |
| 132 | + |
| 133 | +findCodeAction :: TextDocumentIdentifier -> Range -> T.Text -> Session CodeAction |
| 134 | +findCodeAction doc range t = head <$> findCodeActions doc range [t] |
| 135 | + |
| 136 | +-- | Wrapper around 'LSPTest.openDoc'' that sends file creation events |
| 137 | +openDoc' :: FilePath -> String -> T.Text -> Session TextDocumentIdentifier |
| 138 | +openDoc' fp name contents = do |
| 139 | + res@(TextDocumentIdentifier uri) <- LSPTest.openDoc' fp name contents |
| 140 | + sendNotification WorkspaceDidChangeWatchedFiles (DidChangeWatchedFilesParams $ List [FileEvent uri FcCreated]) |
| 141 | + return res |
0 commit comments