Skip to content

Commit

Permalink
Use direct cradle for tests when stack is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
expipiplus1 authored and mpickering committed Nov 14, 2019
1 parent 52691a6 commit 714f42a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion test/dispatcher/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Haskell.Ide.Engine.Plugin.Generic
main :: IO ()
main = do
hSetBuffering stderr LineBuffering
setupStackFiles
setupBuildToolFiles
config <- getHspecFormattedConfig "dispatcher"
withFileLogging "main-dispatcher.log" $ do
hspecWith config funcSpec
Expand Down
2 changes: 1 addition & 1 deletion test/functional/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import TestUtils

main :: IO ()
main = do
setupStackFiles
setupBuildToolFiles
-- run a test session to warm up the cache to prevent timeouts in other tests
putStrLn "Warming up HIE cache..."
runSessionWithConfig (defaultConfig { messageTimeout = 120 }) hieCommand fullCaps "test/testdata" $
Expand Down
2 changes: 1 addition & 1 deletion test/plugin-dispatcher/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Test.Hspec.Runner

main :: IO ()
main = do
setupStackFiles
setupBuildToolFiles
config <- getHspecFormattedConfig "plugin-dispatcher"
withFileLogging "plugin-dispatcher.log" $ hspecWith config newPluginSpec

Expand Down
2 changes: 1 addition & 1 deletion test/unit/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import qualified Spec

main :: IO ()
main = do
setupStackFiles
setupBuildToolFiles
config <- getHspecFormattedConfig "unit"
withFileLogging "main.log" $ hspecWith config $ Spec.spec

Expand Down
41 changes: 33 additions & 8 deletions test/utils/TestUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module TestUtils
(
withFileLogging
, setupStackFiles
, setupBuildToolFiles
, testCommand
, runSingle
, runSingleReq
Expand Down Expand Up @@ -91,14 +91,30 @@ withFileLogging logFile f = do

-- ---------------------------------------------------------------------

setupStackFiles :: IO ()
setupStackFiles =
-- If an executable @stack@ is present on the system then setup stack files,
-- otherwise specify a direct cradle with -isrc
setupBuildToolFiles :: IO ()
setupBuildToolFiles = do
stack <- findExecutable "stack"
let s = case stack of
Nothing -> setupDirectFilesIn
Just _ -> setupStackFilesIn
forM_ files $ \f -> do
resolver <- readResolver
writeFile (f ++ "stack.yaml") $ stackFileContents resolver
writeFile (f ++ "hie.yaml") hieYamlCradleStackContents
s f
-- Cleanup stack directory in case the presence of stack has changed since
-- the last run
removePathForcibly (f ++ ".stack-work")

setupStackFilesIn :: FilePath -> IO ()
setupStackFilesIn f = do
resolver <- readResolver
writeFile (f ++ "stack.yaml") $ stackFileContents resolver
writeFile (f ++ "hie.yaml") hieYamlCradleStackContents

setupDirectFilesIn :: FilePath -> IO ()
setupDirectFilesIn f =
writeFile (f ++ "hie.yaml") hieYamlCradleDirectContents

-- ---------------------------------------------------------------------

files :: [FilePath]
Expand Down Expand Up @@ -197,14 +213,23 @@ readResolverFrom yamlPath = do

hieYamlCradleStackContents :: String
hieYamlCradleStackContents = unlines
[ "# WARNING: THIS FILE IS AUTOGENERATED IN test/Main.hs. IT WILL BE OVERWRITTEN ON EVERY TEST RUN"
[ "# WARNING: THIS FILE IS AUTOGENERATED IN test/utils/TestUtils.hs. IT WILL BE OVERWRITTEN ON EVERY TEST RUN"
, "cradle:"
, " stack:"
]

hieYamlCradleDirectContents :: String
hieYamlCradleDirectContents = unlines
[ "# WARNING: THIS FILE IS AUTOGENERATED IN test/utils/TestUtils.hs. IT WILL BE OVERWRITTEN ON EVERY TEST RUN"
, "cradle:"
, " direct:"
, " arguments:"
, " - -isrc"
]

stackFileContents :: String -> String
stackFileContents resolver = unlines
[ "# WARNING: THIS FILE IS AUTOGENERATED IN test/Main.hs. IT WILL BE OVERWRITTEN ON EVERY TEST RUN"
[ "# WARNING: THIS FILE IS AUTOGENERATED IN test/utils/TestUtils. IT WILL BE OVERWRITTEN ON EVERY TEST RUN"
, "resolver: " ++ resolver
, "packages:"
, "- '.'"
Expand Down

0 comments on commit 714f42a

Please sign in to comment.