Skip to content

Commit a1fe52f

Browse files
authored
[Migrate BootTests] part of #4173 Migrate ghcide tests to hls test utils (#4227)
* migrate boot test * add comment
1 parent 4985793 commit a1fe52f

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

Diff for: ghcide/test/exe/BootTests.hs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module BootTests (tests) where
22

3-
import Config (checkDefs, mkR)
3+
import Config (checkDefs, mkR, runInDir,
4+
runWithExtraFiles)
45
import Control.Applicative.Combinators
56
import Control.Monad
67
import Control.Monad.IO.Class (liftIO)
@@ -15,16 +16,15 @@ import Language.LSP.Protocol.Types hiding
1516
SemanticTokensEdit (..),
1617
mkRange)
1718
import Language.LSP.Test
18-
import System.FilePath
19+
import Test.Hls.FileSystem (toAbsFp)
1920
import Test.Tasty
2021
import Test.Tasty.HUnit
21-
import TestUtils
2222

2323

2424
tests :: TestTree
2525
tests = testGroup "boot"
2626
[ testCase "boot-def-test" $ runWithExtraFiles "boot" $ \dir -> do
27-
let cPath = dir </> "C.hs"
27+
let cPath = dir `toAbsFp` "C.hs"
2828
cSource <- liftIO $ readFileUtf8 cPath
2929
-- Dirty the cache
3030
liftIO $ runInDir dir $ do
@@ -51,6 +51,6 @@ tests = testGroup "boot"
5151
let floc = mkR 9 0 9 1
5252
checkDefs locs (pure [floc])
5353
, testCase "graph with boot modules" $ runWithExtraFiles "boot2" $ \dir -> do
54-
_ <- openDoc (dir </> "A.hs") "haskell"
54+
_ <- openDoc (dir `toAbsFp` "A.hs") "haskell"
5555
expectNoMoreDiagnostics 2
5656
]

Diff for: ghcide/test/exe/Config.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Config(
1313
, testWithDummyPluginEmpty'
1414
, testWithDummyPluginAndCap'
1515
, runWithExtraFiles
16+
, runInDir
1617
, testWithExtraFiles
1718

1819
-- * utilities for testing definition and hover
@@ -36,7 +37,7 @@ import Language.LSP.Protocol.Types (Null (..))
3637
import System.FilePath ((</>))
3738
import Test.Hls
3839
import qualified Test.Hls.FileSystem as FS
39-
import Test.Hls.FileSystem (FileSystem)
40+
import Test.Hls.FileSystem (FileSystem, fsRoot)
4041

4142
testDataDir :: FilePath
4243
testDataDir = "ghcide" </> "test" </> "data"
@@ -80,6 +81,9 @@ runWithExtraFiles dirName action = do
8081
testWithExtraFiles :: String -> String -> (FileSystem -> Session ()) -> TestTree
8182
testWithExtraFiles testName dirName action = testCase testName $ runWithExtraFiles dirName action
8283

84+
runInDir :: FileSystem -> Session a -> IO a
85+
runInDir fs = runSessionWithServerNoRootLock False dummyPlugin def def def (fsRoot fs)
86+
8387
pattern R :: UInt -> UInt -> UInt -> UInt -> Range
8488
pattern R x y x' y' = Range (Position x y) (Position x' y')
8589

Diff for: hls-test-utils/src/Test/Hls.hs

+25-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Test.Hls
3131
runSessionWithServerAndCaps,
3232
runSessionWithServerInTmpDir,
3333
runSessionWithServerAndCapsInTmpDir,
34+
runSessionWithServerNoRootLock,
3435
runSessionWithServer',
3536
runSessionWithServerInTmpDir',
3637
-- continuation version that take a FileSystem
@@ -618,7 +619,10 @@ lockForTempDirs = unsafePerformIO newLock
618619

619620
-- | Host a server, and run a test session on it
620621
-- Note: cwd will be shifted into @root@ in @Session a@
621-
runSessionWithServer' ::
622+
-- notice this function should only be used in tests that
623+
-- require to be nested in the same temporary directory
624+
-- use 'runSessionWithServerInTmpDir' for other cases
625+
runSessionWithServerNoRootLock ::
622626
(Pretty b) =>
623627
-- | whether we disable the kick action or not
624628
Bool ->
@@ -632,7 +636,7 @@ runSessionWithServer' ::
632636
FilePath ->
633637
Session a ->
634638
IO a
635-
runSessionWithServer' disableKick pluginsDp conf sconf caps root s = withLock lock $ keepCurrentDirectory $ do
639+
runSessionWithServerNoRootLock disableKick pluginsDp conf sconf caps root s = do
636640
(inR, inW) <- createPipe
637641
(outR, outW) <- createPipe
638642

@@ -676,6 +680,25 @@ runSessionWithServer' disableKick pluginsDp conf sconf caps root s = withLock l
676680
putStrLn $ "Finishing canceling (took " <> showDuration t <> "s)"
677681
pure x
678682

683+
-- | Host a server, and run a test session on it
684+
-- Note: cwd will be shifted into @root@ in @Session a@
685+
runSessionWithServer' ::
686+
(Pretty b) =>
687+
-- | whether we disable the kick action or not
688+
Bool ->
689+
-- | Plugin to load on the server.
690+
PluginTestDescriptor b ->
691+
-- | lsp config for the server
692+
Config ->
693+
-- | config for the test session
694+
SessionConfig ->
695+
ClientCapabilities ->
696+
FilePath ->
697+
Session a ->
698+
IO a
699+
runSessionWithServer' disableKick pluginsDp conf sconf caps root s =
700+
withLock lock $ keepCurrentDirectory $ runSessionWithServerNoRootLock disableKick pluginsDp conf sconf caps root s
701+
679702
-- | Wait for the next progress begin step
680703
waitForProgressBegin :: Session ()
681704
waitForProgressBegin = skipManyTill anyMessage $ satisfyMaybe $ \case

0 commit comments

Comments
 (0)