Skip to content

Commit db52edb

Browse files
committed
Add a rename test that tests for compilation errors
1 parent 9bb1927 commit db52edb

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

haskell-language-server.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ test-suite hls-rename-plugin-tests
614614
, hls-test-utils == 2.7.0.0
615615
, lens
616616
, lsp-types
617+
, row-types
617618
, text
618619

619620
-----------------------------

plugins/hls-rename-plugin/test/Main.hs

+55-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
{-# LANGUAGE OverloadedLabels #-}
12
{-# LANGUAGE OverloadedStrings #-}
23

34
module Main (main) where
45

56
import Control.Lens ((^.))
67
import Data.Aeson
78
import qualified Data.Map as M
8-
import Data.Text (Text)
9+
import Data.Text (Text, pack)
910
import Ide.Plugin.Config
1011
import qualified Ide.Plugin.Rename as Rename
12+
import Data.Row ((.+), (.==))
1113
import qualified Language.LSP.Protocol.Lens as L
1214
import System.FilePath
1315
import Test.Hls
@@ -73,6 +75,40 @@ tests = testGroup "Rename"
7375
"rename: Invalid Params: No symbol to rename at given position"
7476
Nothing
7577
renameExpectError expectedError doc (Position 0 10) "ImpossibleRename"
78+
79+
, testCase "fails when module does not compile" $ runRenameSession "" $ do
80+
doc <- openDoc "FunctionArgument.hs" "haskell"
81+
expectNoMoreDiagnostics 3 doc "typecheck"
82+
83+
-- Update the document so it doesn't compile
84+
let change = TextDocumentContentChangeEvent $ InL $ #range .== Range (Position 2 13) (Position 2 17)
85+
.+ #rangeLength .== Nothing
86+
.+ #text .== "A"
87+
changeDoc doc [change]
88+
diags@(tcDiag : _) <- waitForDiagnosticsFrom doc
89+
90+
-- Make sure there's a typecheck error
91+
liftIO $ do
92+
length diags @?= 1
93+
tcDiag ^. L.range @?= Range (Position 2 13) (Position 2 14)
94+
tcDiag ^. L.severity @?= Just DiagnosticSeverity_Error
95+
tcDiag ^. L.source @?= Just "typecheck"
96+
97+
-- Make sure renaming fails
98+
renameErr <- expectRenameError doc (Position 3 0) "foo'"
99+
liftIO $ do
100+
renameErr ^. L.code @?= InL LSPErrorCodes_RequestFailed
101+
renameErr ^. L.message @?= "rename: Rule Failed: GetHieAst"
102+
103+
-- Update the document so it compiles
104+
let change' = TextDocumentContentChangeEvent $ InL $ #range .== Range (Position 2 13) (Position 2 14)
105+
.+ #rangeLength .== Nothing
106+
.+ #text .== "Int"
107+
changeDoc doc [change']
108+
expectNoMoreDiagnostics 3 doc "typecheck"
109+
110+
-- Make sure renaming succeeds
111+
rename doc (Position 3 0) "foo'"
76112
]
77113

78114
goldenWithRename :: TestName-> FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree
@@ -90,3 +126,21 @@ renameExpectError expectedError doc pos newName = do
90126

91127
testDataDir :: FilePath
92128
testDataDir = "plugins" </> "hls-rename-plugin" </> "test" </> "testdata"
129+
130+
-- | Attempts to renames the term at the specified position, expecting a failure
131+
expectRenameError ::
132+
TextDocumentIdentifier ->
133+
Position ->
134+
String ->
135+
Session ResponseError
136+
expectRenameError doc pos newName = do
137+
let params = RenameParams Nothing doc pos (pack newName)
138+
rsp <- request SMethod_TextDocumentRename params
139+
case rsp ^. L.result of
140+
Left err -> pure err
141+
Right x -> liftIO $ assertFailure $
142+
"Got unexpected successful rename response for " <> show (doc ^. L.uri)
143+
144+
runRenameSession :: FilePath -> Session a -> IO a
145+
runRenameSession subdir = failIfSessionTimeout
146+
. runSessionWithServerAndCaps def renamePlugin codeActionNoResolveCaps (testDataDir </> subdir)

0 commit comments

Comments
 (0)