Skip to content

Commit 409ca9c

Browse files
committed
Fix wrapper tests by copying to temporary directory
1 parent cf05aa9 commit 409ca9c

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

haskell-language-server.cabal

+2
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ common hls-test-utils
217217
, lsp-test
218218
, stm
219219
, tasty-hunit
220+
, temporary
221+
, process
220222
, text
221223
, transformers
222224
, unordered-containers

test/utils/Test/Hls/Util.hs

+34-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Test.Hls.Util
1515
, setupBuildToolFiles
1616
, withFileLogging
1717
, findExe
18+
, withCurrentDirectoryInTmp
1819
-- , makeRequest
1920
-- , runIGM
2021
-- , runIGM'
@@ -46,13 +47,15 @@ import System.Directory
4647
import System.Environment
4748
import System.FilePath
4849
import qualified System.Log.Logger as L
50+
import System.IO.Temp
4951
-- import Test.Hspec
5052
import Test.Hspec.Runner
5153
import Test.Hspec.Core.Formatters
5254
import Text.Blaze.Renderer.String (renderMarkup)
5355
import Text.Blaze.Internal
5456
-- import qualified Haskell.Ide.Engine.PluginApi as HIE (BiosOptions, defaultOptions)
5557
-- import HIE.Bios.Types
58+
import System.Process
5659

5760
-- testOptions :: HIE.BiosOptions
5861
-- testOptions = HIE.defaultOptions { cradleOptsVerbosity = Verbose }
@@ -332,6 +335,36 @@ findExeRecursive exe dir = do
332335
findExe :: String -> IO FilePath
333336
findExe name = do
334337
fp <- fmap fromJust $ runMaybeT $
335-
MaybeT (findExecutable name) <|>
338+
MaybeT (findExecutable name) <|>
336339
MaybeT (findExeRecursive name "dist-newstyle")
337340
makeAbsolute fp
341+
342+
-- | Like 'withCurrentDirectory', but will copy the directory over to the system
343+
-- temporary directory first to avoid haskell-language-server's source tree from
344+
-- interfering with the cradle
345+
withCurrentDirectoryInTmp :: FilePath -> IO a -> IO a
346+
withCurrentDirectoryInTmp dir f =
347+
withTempCopy dir $ \newDir ->
348+
withCurrentDirectory newDir $ do
349+
_ <- system "stack path"
350+
_ <- system "stack exec env"
351+
f
352+
353+
withTempCopy :: FilePath -> (FilePath -> IO a) -> IO a
354+
withTempCopy srcDir f = do
355+
withSystemTempDirectory "hls-test" $ \newDir -> do
356+
copyDir srcDir newDir
357+
f newDir
358+
359+
copyDir :: FilePath -> FilePath -> IO ()
360+
copyDir src dst = do
361+
cnts <- listDirectory src
362+
forM_ cnts $ \file -> do
363+
unless (file `elem` ignored) $ do
364+
let srcFp = src </> file
365+
dstFp = dst </> file
366+
isDir <- doesDirectoryExist srcFp
367+
if isDir
368+
then createDirectory dstFp >> copyDir srcFp dstFp
369+
else copyFile srcFp dstFp
370+
where ignored = ["dist", "dist-newstyle", ".stack-work"]

test/wrapper/Main.hs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import Data.Char
33
import Test.Hls.Util
44
import Test.Tasty
55
import Test.Tasty.HUnit
6-
import System.Directory
76
import System.Process
87

98
main :: IO ()
10-
main = defaultMain $
11-
testGroup "haskell-language-server-wrapper" [projectGhcVersionTests]
9+
main = do
10+
flushStackEnvironment
11+
defaultMain $
12+
testGroup "haskell-language-server-wrapper" [projectGhcVersionTests]
1213

13-
--TODO: WAIT ON HIE-BIOS STOP FILES
1414
projectGhcVersionTests :: TestTree
1515
projectGhcVersionTests = testGroup "--project-ghc-version"
1616
[ testCase "stack with ghc 8.10.1" $
@@ -25,10 +25,9 @@ projectGhcVersionTests = testGroup "--project-ghc-version"
2525
testDir :: FilePath -> String -> Assertion
2626
testDir dir expectedVer = do
2727
wrapper <- findExe "haskell-language-server-wrapper"
28-
withCurrentDirectory dir $ do
28+
withCurrentDirectoryInTmp dir $ do
2929
actualVer <- trim <$> readProcess wrapper ["--project-ghc-version"] ""
3030
actualVer @?= expectedVer
3131

3232
trim :: String -> String
3333
trim = dropWhileEnd isSpace
34-

0 commit comments

Comments
 (0)