Skip to content

Commit 15a2320

Browse files
author
kokobd
committed
fix hls test utils
1 parent 56a00c9 commit 15a2320

File tree

3 files changed

+63
-80
lines changed

3 files changed

+63
-80
lines changed

ghcide/src/Development/IDE/Plugin/Test.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import Data.Text (Text, pack)
2727
import Development.IDE.Core.OfInterest (getFilesOfInterest)
2828
import Development.IDE.Core.Rules
2929
import Development.IDE.Core.RuleTypes
30-
import Development.IDE.Core.Service
3130
import Development.IDE.Core.Shake
3231
import Development.IDE.GHC.Compat
3332
import Development.IDE.Graph (Action)

hls-test-utils/src/Test/Hls.hs

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -189,57 +189,56 @@ runSessionWithServer' ::
189189
Session a ->
190190
IO a
191191
runSessionWithServer' plugins conf sconf caps root s = withLock lock $ keepCurrentDirectory $ do
192-
(inR, inW) <- createPipe
193-
(outR, outW) <- createPipe
194-
195-
docWithPriorityRecorder <- makeDefaultStderrRecorder Nothing Debug
196-
197-
logStdErr <- fromMaybe "0" <$> lookupEnv "LSP_TEST_LOG_STDERR"
198-
199-
let
200-
docWithFilteredPriorityRecorder@Recorder{ logger_ } =
201-
if logStdErr == "0" then mempty
202-
else cfilter (\WithPriority{ priority } -> priority >= Debug) docWithPriorityRecorder
203-
204-
-- exists until old logging style is phased out
205-
logger = Logger $ \p m -> logger_ (WithPriority p emptyCallStack (pretty m))
206-
207-
recorder = cmapWithPrio pretty docWithFilteredPriorityRecorder
208-
209-
arguments@Arguments{ argsHlsPlugins, argsIdeOptions, argsLogger } = defaultArguments (cmapWithPrio LogIDEMain recorder) logger
210-
211-
hlsPlugins =
212-
plugins
213-
++ [Test.blockCommandDescriptor "block-command", Test.plugin]
214-
++ idePluginsToPluginDesc argsHlsPlugins
215-
ideOptions = \config ghcSession ->
216-
let defIdeOptions = argsIdeOptions config ghcSession
217-
in defIdeOptions
218-
{ optTesting = IdeTesting True
219-
, optCheckProject = pure False
220-
}
221-
222-
server <-
223-
async $
224-
Ghcide.defaultMain
225-
(cmapWithPrio LogIDEMain recorder)
226-
arguments
227-
{ argsHandleIn = pure inR
228-
, argsHandleOut = pure outW
229-
, argsDefaultHlsConfig = conf
230-
, argsLogger = argsLogger
231-
, argsIdeOptions = ideOptions
232-
, argsHlsPlugins = pluginDescToIdePlugins hlsPlugins }
233-
234-
x <- runSessionWithHandles inW outR sconf caps root s
235-
hClose inW
236-
timeout 3 (wait server) >>= \case
237-
Just () -> pure ()
238-
Nothing -> do
239-
putStrLn "Server does not exit in 3s, canceling the async task..."
240-
(t, _) <- duration $ cancel server
241-
putStrLn $ "Finishing canceling (took " <> showDuration t <> "s)"
242-
pure x
192+
(inR, inW) <- createPipe
193+
(outR, outW) <- createPipe
194+
195+
docWithPriorityRecorder <- makeDefaultStderrRecorder Nothing Debug
196+
197+
logStdErr <- fromMaybe "0" <$> lookupEnv "LSP_TEST_LOG_STDERR"
198+
199+
let
200+
docWithFilteredPriorityRecorder@Recorder{ logger_ } =
201+
if logStdErr == "0" then mempty
202+
else cfilter (\WithPriority{ priority } -> priority >= Debug) docWithPriorityRecorder
203+
204+
-- exists until old logging style is phased out
205+
logger = Logger $ \p m -> logger_ (WithPriority p emptyCallStack (pretty m))
206+
207+
recorder = cmapWithPrio pretty docWithFilteredPriorityRecorder
208+
209+
arguments@Arguments{ argsHlsPlugins, argsIdeOptions, argsLogger } = defaultArguments (cmapWithPrio LogIDEMain recorder) logger
210+
211+
hlsPlugins =
212+
plugins
213+
++ [Test.blockCommandDescriptor "block-command", Test.plugin]
214+
++ idePluginsToPluginDesc argsHlsPlugins
215+
ideOptions config ghcSession =
216+
let defIdeOptions = argsIdeOptions config ghcSession
217+
in defIdeOptions
218+
{ optTesting = IdeTesting True
219+
, optCheckProject = pure False
220+
}
221+
222+
server <- async $
223+
Ghcide.defaultMain (cmapWithPrio LogIDEMain recorder)
224+
arguments
225+
{ argsHandleIn = pure inR
226+
, argsHandleOut = pure outW
227+
, argsDefaultHlsConfig = conf
228+
, argsLogger = argsLogger
229+
, argsIdeOptions = ideOptions
230+
, argsHlsPlugins = pluginDescToIdePlugins hlsPlugins
231+
}
232+
233+
x <- runSessionWithHandles inW outR sconf caps root s
234+
hClose inW
235+
timeout 3 (wait server) >>= \case
236+
Just () -> pure ()
237+
Nothing -> do
238+
putStrLn "Server does not exit in 3s, canceling the async task..."
239+
(t, _) <- duration $ cancel server
240+
putStrLn $ "Finishing canceling (took " <> showDuration t <> "s)"
241+
pure x
243242

244243
-- | Wait for the next progress end step
245244
waitForProgressDone :: Session ()

hls-test-utils/src/Test/Hls/Util.hs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import Data.List.Extra (find)
5555
import qualified Data.Set as Set
5656
import qualified Data.Text as T
5757
import Development.IDE (GhcVersion (..), ghcVersion)
58+
import Development.IDE.Plugin.Test (TestRequest (GetFilesOfInterest))
5859
import qualified Language.LSP.Test as Test
5960
import Language.LSP.Types hiding (Reason (..))
6061
import qualified Language.LSP.Types.Capabilities as C
@@ -297,16 +298,7 @@ waitForDiagnosticsFrom doc = do
297298
else return diags
298299

299300
waitForDiagnosticsFromSource :: TextDocumentIdentifier -> String -> Test.Session [Diagnostic]
300-
waitForDiagnosticsFromSource doc src = do
301-
diagsNot <- skipManyTill Test.anyMessage (Test.message STextDocumentPublishDiagnostics)
302-
let (List diags) = diagsNot ^. L.params . L.diagnostics
303-
let res = filter matches diags
304-
if doc ^. L.uri /= diagsNot ^. L.params . L.uri || null res
305-
then waitForDiagnosticsFromSource doc src
306-
else return res
307-
where
308-
matches :: Diagnostic -> Bool
309-
matches d = d ^. L.source == Just (T.pack src)
301+
waitForDiagnosticsFromSource = waitForDiagnosticsFromSourceWithTimeout 5
310302

311303
-- | wait for @timeout@ seconds and report an assertion failure
312304
-- if any diagnostic messages arrive in that period
@@ -322,38 +314,31 @@ expectNoMoreDiagnostics timeout doc src = do
322314
-- If timeout is 0 it will wait until the session timeout
323315
waitForDiagnosticsFromSourceWithTimeout :: Seconds -> TextDocumentIdentifier -> String -> Test.Session [Diagnostic]
324316
waitForDiagnosticsFromSourceWithTimeout timeout document source = do
325-
when (timeout > 0) $ do
317+
when (timeout > 0) $
326318
-- Give any further diagnostic messages time to arrive.
327319
liftIO $ sleep timeout
328320
-- Send a dummy message to provoke a response from the server.
329321
-- This guarantees that we have at least one message to
330322
-- process, so message won't block or timeout.
331-
void $ Test.sendNotification (SCustomMethod "non-existent-method") A.Null
332-
handleMessages
323+
testId <- Test.sendRequest (SCustomMethod "test") (A.toJSON GetFilesOfInterest)
324+
handleMessages testId
333325
where
334326
matches :: Diagnostic -> Bool
335327
matches d = d ^. L.source == Just (T.pack source)
336328

337-
handleMessages = handleDiagnostic <|> handleCustomMethodResponse <|> ignoreOthers
338-
handleDiagnostic = do
329+
handleMessages testId = handleDiagnostic testId <|> handleCustomMethodResponse testId <|> ignoreOthers testId
330+
handleDiagnostic testId = do
339331
diagsNot <- Test.message STextDocumentPublishDiagnostics
340332
let fileUri = diagsNot ^. L.params . L.uri
341333
(List diags) = diagsNot ^. L.params . L.diagnostics
342334
res = filter matches diags
343335
if fileUri == document ^. L.uri && not (null res)
344-
then return diags else handleMessages
345-
handleCustomMethodResponse =
346-
-- the CustomClientMethod triggers a RspCustomServer
347-
-- handle that and then exit
348-
void (Test.satisfyMaybe responseForNonExistentMethod) >> return []
349-
350-
responseForNonExistentMethod :: FromServerMessage -> Maybe FromServerMessage
351-
responseForNonExistentMethod notif
352-
| FromServerMess SWindowLogMessage logMsg <- notif,
353-
"non-existent-method" `T.isInfixOf` (logMsg ^. L.params . L.message) = Just notif
354-
| otherwise = Nothing
355-
356-
ignoreOthers = void Test.anyMessage >> handleMessages
336+
then return diags else handleMessages testId
337+
handleCustomMethodResponse testId = do
338+
_ <- Test.responseForId (SCustomMethod "test") testId
339+
pure []
340+
341+
ignoreOthers testId = void Test.anyMessage >> handleMessages testId
357342

358343
failIfSessionTimeout :: IO a -> IO a
359344
failIfSessionTimeout action = action `catch` errorHandler

0 commit comments

Comments
 (0)