Skip to content

Commit

Permalink
Fix wrapper tests by copying to temporary directory
Browse files Browse the repository at this point in the history
  • Loading branch information
lukel97 committed Jul 16, 2020
1 parent cf05aa9 commit 773ec86
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ stack*.yaml.lock
shake.yaml.lock

# ignore hie.yaml's for testdata
test/**/*.yaml
test/testdata/**/hie.yaml

# metadata files on macOS
.DS_Store
1 change: 1 addition & 0 deletions haskell-language-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ common hls-test-utils
, lsp-test
, stm
, tasty-hunit
, temporary
, text
, transformers
, unordered-containers
Expand Down
31 changes: 30 additions & 1 deletion test/utils/Test/Hls/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Test.Hls.Util
, setupBuildToolFiles
, withFileLogging
, findExe
, withCurrentDirectoryInTmp
-- , makeRequest
-- , runIGM
-- , runIGM'
Expand Down Expand Up @@ -46,6 +47,7 @@ import System.Directory
import System.Environment
import System.FilePath
import qualified System.Log.Logger as L
import System.IO.Temp
-- import Test.Hspec
import Test.Hspec.Runner
import Test.Hspec.Core.Formatters
Expand Down Expand Up @@ -332,6 +334,33 @@ findExeRecursive exe dir = do
findExe :: String -> IO FilePath
findExe name = do
fp <- fmap fromJust $ runMaybeT $
MaybeT (findExecutable name) <|>
MaybeT (findExecutable name) <|>
MaybeT (findExeRecursive name "dist-newstyle")
makeAbsolute fp

-- | Like 'withCurrentDirectory', but will copy the directory over to the system
-- temporary directory first to avoid haskell-language-server's source tree from
-- interfering with the cradle
withCurrentDirectoryInTmp :: FilePath -> IO a -> IO a
withCurrentDirectoryInTmp dir f =
withTempCopy dir $ \newDir ->
withCurrentDirectory newDir f

withTempCopy :: FilePath -> (FilePath -> IO a) -> IO a
withTempCopy srcDir f = do
withSystemTempDirectory "hls-test" $ \newDir -> do
copyDir srcDir newDir
f newDir

copyDir :: FilePath -> FilePath -> IO ()
copyDir src dst = do
cnts <- listDirectory src
forM_ cnts $ \file -> do
unless (file `elem` ignored) $ do
let srcFp = src </> file
dstFp = dst </> file
isDir <- doesDirectoryExist srcFp
if isDir
then createDirectory dstFp >> copyDir srcFp dstFp
else copyFile srcFp dstFp
where ignored = ["dist", "dist-newstyle", ".stack-work"]
11 changes: 5 additions & 6 deletions test/wrapper/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import Data.Char
import Test.Hls.Util
import Test.Tasty
import Test.Tasty.HUnit
import System.Directory
import System.Process

main :: IO ()
main = defaultMain $
testGroup "haskell-language-server-wrapper" [projectGhcVersionTests]
main = do
flushStackEnvironment
defaultMain $
testGroup "haskell-language-server-wrapper" [projectGhcVersionTests]

--TODO: WAIT ON HIE-BIOS STOP FILES
projectGhcVersionTests :: TestTree
projectGhcVersionTests = testGroup "--project-ghc-version"
[ testCase "stack with ghc 8.10.1" $
Expand All @@ -25,10 +25,9 @@ projectGhcVersionTests = testGroup "--project-ghc-version"
testDir :: FilePath -> String -> Assertion
testDir dir expectedVer = do
wrapper <- findExe "haskell-language-server-wrapper"
withCurrentDirectory dir $ do
withCurrentDirectoryInTmp dir $ do
actualVer <- trim <$> readProcess wrapper ["--project-ghc-version"] ""
actualVer @?= expectedVer

trim :: String -> String
trim = dropWhileEnd isSpace

1 change: 1 addition & 0 deletions test/wrapper/testdata/stack-8.10.1/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resolver: ghc-8.10.1
1 change: 1 addition & 0 deletions test/wrapper/testdata/stack-8.8.3/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resolver: ghc-8.8.3

0 comments on commit 773ec86

Please sign in to comment.