Skip to content

Commit

Permalink
Add test-cases for cradle load failures
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor committed Jun 8, 2020
1 parent 1c17dfc commit fdcc2af
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 9 deletions.
12 changes: 12 additions & 0 deletions hie-bios.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ Extra-Source-Files: ChangeLog
tests/projects/multi-stack/hie.yaml
tests/projects/multi-stack/multi-stack.cabal
tests/projects/multi-stack/src/Lib.hs
tests/projects/failing-bios/A.hs
tests/projects/failing-bios/B.cabal
tests/projects/failing-bios/hie.yaml
tests/projects/failing-cabal/failing-cabal.cabal
tests/projects/failing-cabal/hie.yaml
tests/projects/failing-cabal/MyLib.hs
tests/projects/failing-cabal/Setup.hs
tests/projects/failing-stack/failing-stack.cabal
tests/projects/failing-stack/hie.yaml
tests/projects/failing-stack/src/Lib.hs
tests/projects/failing-stack/Setup.hs
tests/projects/simple-bios/A.hs
tests/projects/simple-bios/B.hs
tests/projects/simple-bios/hie-bios.sh
Expand Down Expand Up @@ -183,6 +194,7 @@ test-suite bios-tests
extra,
tasty,
tasty-hunit,
tasty-expected-failure,
hspec-expectations,
hie-bios,
filepath,
Expand Down
13 changes: 11 additions & 2 deletions src/HIE/Bios/Cradle.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE BangPatterns #-}
Expand Down Expand Up @@ -118,8 +119,16 @@ addCradleDeps deps c =
addActionDeps :: CradleAction a -> CradleAction a
addActionDeps ca =
ca { runCradle = \l fp ->
(fmap (\(ComponentOptions os' dir ds) -> ComponentOptions os' dir (ds `union` deps)))
<$> runCradle ca l fp }
runCradle ca l fp
>>= \case
CradleSuccess (ComponentOptions os' dir ds) ->
pure $ CradleSuccess (ComponentOptions os' dir (ds `union` deps))
CradleFail err ->
pure $ CradleFail
(err { cradleErrorDependencies = cradleErrorDependencies err `union` deps })
CradleNone -> pure CradleNone
}


implicitConfig :: FilePath -> MaybeT IO (CradleConfig a, FilePath)
implicitConfig fp = do
Expand Down
44 changes: 37 additions & 7 deletions tests/BiosTests.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE CPP #-}
module Main where

import Test.Tasty
import Test.Tasty.ExpectedFailure
import Test.Tasty.HUnit
import Test.Hspec.Expectations
import HIE.Bios
Expand All @@ -16,6 +18,7 @@ import Data.Void
import System.Directory
import System.FilePath ( makeRelative, (</>) )
import System.Info.Extra ( isWindows )
import System.Exit (ExitCode(ExitFailure))

main :: IO ()
main = do
Expand All @@ -40,7 +43,15 @@ main = do
, testGroup "Loading tests"
$ linuxExlusiveTestCases
++
[ testCaseSteps "simple-bios-shell" $ testDirectory isBiosCradle "./tests/projects/simple-bios-shell/B.hs"
[ testCaseSteps "failing-cabal" $ testDirectoryFail isCabalCradle "./tests/projects/failing-cabal/MyLib.hs"
(\CradleError {..} -> do
cradleErrorExitCode `shouldBe` ExitFailure 1
cradleErrorDependencies `shouldMatchList` ["failing-cabal.cabal", "cabal.project", "cabal.project.local"])
, testCaseSteps "failing-bios" $ testDirectoryFail isBiosCradle "./tests/projects/failing-bios/B.hs"
(\CradleError {..} -> do
cradleErrorExitCode `shouldBe` ExitFailure 1
cradleErrorDependencies `shouldMatchList` ["hie.yaml"])
, testCaseSteps "simple-bios-shell" $ testDirectory isBiosCradle "./tests/projects/simple-bios-shell/B.hs"
, testCaseSteps "simple-cabal" $ testDirectory isCabalCradle "./tests/projects/simple-cabal/B.hs"
, testCaseSteps "simple-direct" $ testDirectory isDirectCradle "./tests/projects/simple-direct/B.hs"
, testCaseSteps "multi-direct" {- tests if both components can be loaded -}
Expand All @@ -52,7 +63,12 @@ main = do
]
-- TODO: Remove once there's a stackage snapshot for ghc 8.10
#if __GLASGOW_HASKELL__ < 810
++ [ testCaseSteps "simple-stack" $ testDirectory isStackCradle "./tests/projects/simple-stack/B.hs"
++ [ expectFailBecause "stack repl does not fail on an invalid cabal file" $
testCaseSteps "failing-stack" $ testDirectoryFail isStackCradle "./tests/projects/failing-stack/src/Lib.hs"
(\CradleError {..} -> do
cradleErrorExitCode `shouldBe` ExitFailure 1
cradleErrorDependencies `shouldMatchList` ["failing-stack.cabal", "stack.yaml", "package.yaml"])
, testCaseSteps "simple-stack" $ testDirectory isStackCradle "./tests/projects/simple-stack/B.hs"
, testCaseSteps "multi-stack" {- tests if both components can be loaded -}
$ testDirectory isStackCradle "./tests/projects/multi-stack/app/Main.hs"
>> testDirectory isStackCradle "./tests/projects/multi-stack/src/Lib.hs"
Expand Down Expand Up @@ -83,15 +99,27 @@ linuxExlusiveTestCases =
testDirectory :: (Cradle Void -> Bool) -> FilePath -> (String -> IO ()) -> IO ()
testDirectory cradlePred fp step = do
a_fp <- canonicalizePath fp
crd <- initialiseCradle cradlePred a_fp step
step "Initialise Flags"
testLoadFile crd a_fp step

testDirectoryFail :: (Cradle Void -> Bool) -> FilePath -> (CradleError -> Expectation) -> (String -> IO ()) -> IO ()
testDirectoryFail cradlePred fp cradleFailPred step = do
a_fp <- canonicalizePath fp
crd <- initialiseCradle cradlePred a_fp step
step "Initialise Flags"
testLoadFileCradleFail crd a_fp cradleFailPred step

initialiseCradle :: (Cradle Void -> Bool) -> FilePath -> (String -> IO ()) -> IO (Cradle Void)
initialiseCradle cradlePred a_fp step = do
step $ "Finding Cradle for: " ++ a_fp
mcfg <- findCradle a_fp
step $ "Loading Cradle: " ++ show mcfg
crd <- case mcfg of
Just cfg -> loadCradle cfg
Nothing -> loadImplicitCradle a_fp
crd `shouldSatisfy` cradlePred
step "Initialise Flags"
testLoadFile crd a_fp step
pure crd

testLoadFile :: Cradle a -> FilePath -> (String -> IO ()) -> IO ()
testLoadFile crd fp step = do
Expand All @@ -111,16 +139,17 @@ testLoadFile crd fp step = do
CradleNone -> liftIO $ expectationFailure "None"
CradleFail (CradleError _deps _ex stde) -> liftIO $ expectationFailure (unlines stde)

testLoadFileCradleFail :: Cradle a -> FilePath -> (CradleError -> Bool) -> (String -> IO ()) -> IO ()
testLoadFileCradleFail crd fp cradleFailPred step = do
testLoadFileCradleFail :: Cradle a -> FilePath -> (CradleError -> Expectation) -> (String -> IO ()) -> IO ()
testLoadFileCradleFail crd fp cradleErrorExpectation step = do
a_fp <- canonicalizePath fp
withCurrentDirectory (cradleRootDir crd) $
withGHC' $ do
let relFp = makeRelative (cradleRootDir crd) a_fp
res <- initializeFlagsWithCradleWithMessage (Just (\_ n _ _ -> step (show n))) relFp crd
case res of
CradleSuccess _ -> liftIO $ expectationFailure "Cradle loaded successfully"
CradleFail crdlFail -> liftIO $ crdlFail `shouldSatisfy` cradleFailPred
CradleNone -> liftIO $ expectationFailure "Unexpected none-Cradle"
CradleFail crdlFail -> liftIO $ cradleErrorExpectation crdlFail

findCradleForModule :: FilePath -> Maybe FilePath -> (String -> IO ()) -> IO ()
findCradleForModule fp expected' step = do
Expand All @@ -147,6 +176,7 @@ writeStackYamlFiles = do
stackProjects :: [FilePath]
stackProjects =
[ "tests" </> "projects" </> "multi-stack"
, "tests" </> "projects" </> "failing-stack"
, "tests" </> "projects" </> "simple-stack"
, "tests" </> "projects" </> "space stack"
, "tests" </> "projects" </> "implicit-stack"
Expand Down
1 change: 1 addition & 0 deletions tests/projects/failing-bios/A.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module A where
3 changes: 3 additions & 0 deletions tests/projects/failing-bios/B.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module B where

import A
5 changes: 5 additions & 0 deletions tests/projects/failing-bios/hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cradle:
bios:
shell: "exit 1"
dependencies:
- hie.yaml
4 changes: 4 additions & 0 deletions tests/projects/failing-cabal/MyLib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module MyLib (someFunc) where

someFunc :: IO ()
someFunc = putStrLn "someFunc"
2 changes: 2 additions & 0 deletions tests/projects/failing-cabal/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
13 changes: 13 additions & 0 deletions tests/projects/failing-cabal/failing-cabal.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cabal-version: >=1.10
name: failing-cabal
version: 0.1.0.0
license-file: LICENSE
author: fendor
build-type: Simple

library
exposed-modules: MyLib
build-depends: base >=4.13 && <4.14,
containers < 1 && > 1
-- ^^^^^^^^^^ <<< Invalid constraint
default-language: Haskell2010
2 changes: 2 additions & 0 deletions tests/projects/failing-cabal/hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cradle:
cabal:
2 changes: 2 additions & 0 deletions tests/projects/failing-stack/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
16 changes: 16 additions & 0 deletions tests/projects/failing-stack/failing-stack.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cabal-version: 1.12
name: failing-stack
version: 0.1.0.0
description: None
build-type: Simple

library
exposed-modules:
Lib
hs-source-dirs:
src
build-depends:
base >=4.7 && <5,
containes < 1 && > 1
-- ^^^^^^^^^^ <<< Invalid constraint
default-language: Haskell2010
2 changes: 2 additions & 0 deletions tests/projects/failing-stack/hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cradle:
stack:
6 changes: 6 additions & 0 deletions tests/projects/failing-stack/src/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Lib
( someFunc
) where

someFunc :: IO ()
someFunc = putStrLn "someFunc"

0 comments on commit fdcc2af

Please sign in to comment.