Skip to content

Commit eeeb283

Browse files
committed
Make signature helps reproducible before comparing them
This makes tests less flaky.
1 parent c0f7844 commit eeeb283

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

haskell-language-server.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ test-suite hls-signature-help-plugin-tests
882882
, hls-test-utils == 2.11.0.0
883883
, lens
884884
, lsp-types
885+
, regex-tdfa
885886
, text
886887
default-extensions:
887888
OverloadedStrings

plugins/hls-signature-help-plugin/test/Main.hs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-# LANGUAGE QuasiQuotes #-}
22

33
import Control.Exception (throw)
4-
import Control.Lens ((^.))
4+
import Control.Lens ((%~), (^.))
55
import Data.Maybe (fromJust)
66
import Data.Text (Text)
77
import qualified Data.Text as T
@@ -13,6 +13,7 @@ import Test.Hls.FileSystem (VirtualFileTree,
1313
directCradle, file,
1414
mkVirtualFileTree,
1515
text)
16+
import Text.Regex.TDFA ((=~))
1617

1718

1819
main :: IO ()
@@ -154,7 +155,7 @@ main =
154155
^ ^
155156
|]
156157
[ Nothing,
157-
Just $ SignatureHelp [SignatureInformation "pure :: forall (f :: Type -> Type) a. Applicative f => a -> f a" Nothing (Just [ParameterInformation (InR (55,56)) Nothing]) (Just (InL 0)), SignatureInformation "pure :: Bool -> IO Bool" Nothing (Just [ParameterInformation (InR (8,12)) Nothing]) (Just (InL 0)), SignatureInformation "pure :: forall a. a -> IO a" Nothing (Just [ParameterInformation (InR (18,19)) Nothing]) (Just (InL 0))] (Just 0) (Just (InL 0))
158+
Just $ SignatureHelp [SignatureInformation "pure :: forall (f :: Type -> Type) a. Applicative f => a -> f a" (Just $ InR $ MarkupContent MarkupKind_Markdown "\n\nLift a value.\n\n\\[Documentation\\]\\(file://.*\\)\n\n\\[Source\\]\\(file://.*\\)\n\n") (Just [ParameterInformation (InR (55,56)) Nothing]) (Just (InL 0)), SignatureInformation "pure :: Bool -> IO Bool" (Just $ InR $ MarkupContent MarkupKind_Markdown "\n\nLift a value.\n\n\\[Documentation\\]\\(file://.*\\)\n\n\\[Source\\]\\(file://.*\\)\n\n") (Just [ParameterInformation (InR (8,12)) Nothing]) (Just (InL 0)), SignatureInformation "pure :: forall a. a -> IO a" (Just $ InR $ MarkupContent MarkupKind_Markdown "\n\nLift a value.\n\n\\[Documentation\\]\\(file://.*\\)\n\n\\[Source\\]\\(file://.*\\)\n\n") (Just [ParameterInformation (InR (18,19)) Nothing]) (Just (InL 0))] (Just 0) (Just (InL 0))
158159
],
159160
mkTest
160161
"2 type constraints"
@@ -334,7 +335,28 @@ getSignatureHelpFromSession sourceCode (PosPrefixInfo _ _ _ position) =
334335
virtualFileTree = mkVirtualFileTreeWithSingleFile fileName sourceCode
335336
in runSessionWithServerInTmpDir def plugin virtualFileTree $ do
336337
doc <- openDoc fileName "haskell"
337-
getSignatureHelp doc position
338+
(fmap . fmap) mkReproducibleSignatureHelp (getSignatureHelp doc position)
339+
340+
mkReproducibleSignatureHelp :: SignatureHelp -> SignatureHelp
341+
mkReproducibleSignatureHelp = L.signatures . traverse . L.documentation %~ unifyLocalFilePath
342+
where
343+
unifyLocalFilePath (Just (InR (MarkupContent MarkupKind_Markdown doc))) =
344+
let (prefix, match, suffix) = doc =~ documentationRegex :: (Text, Text, Text)
345+
(prefix', match', suffix') = suffix =~ sourceRegex :: (Text, Text, Text)
346+
reproducibleDoc =
347+
if T.null match
348+
then prefix
349+
else
350+
prefix
351+
<> documentationRegex
352+
<> ( if T.null match'
353+
then prefix'
354+
else prefix' <> sourceRegex <> suffix'
355+
)
356+
in Just $ InR $ MarkupContent MarkupKind_Markdown reproducibleDoc
357+
unifyLocalFilePath mDoc = mDoc
358+
documentationRegex = "\\[Documentation\\]\\(file://.*\\)\n\n"
359+
sourceRegex = "\\[Source\\]\\(file://.*\\)\n\n"
338360

339361
mkVirtualFileTreeWithSingleFile :: FilePath -> Text -> VirtualFileTree
340362
mkVirtualFileTreeWithSingleFile fileName sourceCode =

0 commit comments

Comments
 (0)