Skip to content

Commit ce863db

Browse files
authored
Merge pull request #415 from fendor/enhance/find-cradle-dir
Accept directories in 'findCradle'
2 parents 7d086c3 + e5ae93a commit ce863db

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/HIE/Bios/Cradle.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,17 @@ import Text.ParserCombinators.ReadP (readP_to_S)
7777

7878
----------------------------------------------------------------
7979

80-
-- | Given root\/foo\/bar.hs, return root\/hie.yaml, or wherever the yaml file was found.
80+
-- | Given @root\/foo\/bar.hs@, return @root\/hie.yaml@, or wherever the yaml file was found.
81+
--
82+
-- Note, 'findCradle' used to **not** work for directories and required a Haskell file.
83+
-- This has been fixed since @0.14.0@.
84+
-- However, 'loadCradle' and 'loadImplicitCradle' still require a Haskell
85+
-- source file and won't work properly with a directory parameter.
8186
findCradle :: FilePath -> IO (Maybe FilePath)
8287
findCradle wfile = do
83-
let wdir = takeDirectory wfile
88+
wdir <- doesDirectoryExist wfile >>= \case
89+
True -> pure wfile
90+
False -> pure (takeDirectory wfile)
8491
runMaybeT (yamlConfig wdir)
8592

8693
-- | Given root\/hie.yaml load the Cradle.

tests/BiosTests.hs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Control.Monad ( forM_ )
1919
import Data.List ( sort, isPrefixOf )
2020
import Data.Typeable
2121
import System.Directory
22-
import System.FilePath ((</>) )
22+
import System.FilePath ((</>))
2323
import System.Exit (ExitCode(ExitSuccess, ExitFailure))
2424
import Control.Monad.Extra (unlessM)
2525
import qualified HIE.Bios.Ghc.Gap as Gap
@@ -48,17 +48,7 @@ main = do
4848

4949
defaultMainWithIngredients (ignoreToolTests:defaultIngredients) $
5050
testGroup "Bios-tests"
51-
[ testGroup "Find cradle"
52-
[ testCaseSteps "simple-cabal" $
53-
runTestEnvLocal "./simple-cabal" $ do
54-
findCradleForModuleM "B.hs" (Just "hie.yaml")
55-
56-
-- Checks if we can find a hie.yaml even when the given filepath
57-
-- is unknown. This functionality is required by Haskell IDE Engine.
58-
, testCaseSteps "simple-cabal-unknown-path" $
59-
runTestEnvLocal "./simple-cabal" $ do
60-
findCradleForModuleM "Foo.hs" (Just "hie.yaml")
61-
]
51+
[ testGroup "Find cradle" findCradleTests
6252
, testGroup "Symlink" symbolicLinkTests
6353
, testGroup "Loading tests"
6454
[ testGroup "bios" biosTestCases
@@ -301,6 +291,26 @@ directTestCases =
301291
testDirectoryM isMultiCradle "B.hs"
302292
]
303293

294+
findCradleTests :: [TestTree]
295+
findCradleTests =
296+
[ cradleFileTest "Simple Existing File" "./simple-cabal" "B.hs" (Just "hie.yaml")
297+
-- Checks if we can find a hie.yaml even when the given filepath
298+
-- is unknown. This functionality is required by Haskell IDE Engine.
299+
, cradleFileTest "Existing File" "cabal-with-ghc" "src/MyLib.hs" (Just "hie.yaml")
300+
, cradleFileTest "Non-existing file" "cabal-with-ghc" "src/MyLib2.hs" (Just "hie.yaml")
301+
, cradleFileTest "Non-existing file 2" "cabal-with-ghc" "MyLib2.hs" (Just "hie.yaml")
302+
, cradleFileTest "Directory 1" "cabal-with-ghc" "src/" (Just "hie.yaml")
303+
, cradleFileTest "Directory 2" "simple-cabal" "" (Just "hie.yaml")
304+
-- Unknown directory in a project, ought to work as well.
305+
, cradleFileTest "Directory 3" "simple-cabal" "src/" (Just "hie.yaml")
306+
, cradleFileTest "Directory does not exist" "doesnotexist" "A.hs" Nothing
307+
]
308+
where
309+
cradleFileTest :: String -> FilePath -> FilePath -> Maybe FilePath -> TestTree
310+
cradleFileTest testName dir fpTarget result = testCaseSteps testName $ do
311+
runTestEnv dir $ do
312+
findCradleForModuleM fpTarget result
313+
304314
-- ------------------------------------------------------------------
305315
-- Unit-test Helper functions
306316
-- ------------------------------------------------------------------

tests/Utils.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,9 @@ findCradleForModuleM fp expected' = do
427427
withTempCopy :: FilePath -> (FilePath -> IO a) -> IO a
428428
withTempCopy srcDir f =
429429
withSystemTempDirectory "hie-bios-test" $ \newDir -> do
430-
copyDir srcDir newDir
430+
exists <- doesDirectoryExist srcDir
431+
when exists $ do
432+
copyDir srcDir newDir
431433
f newDir
432434

433435
copyDir :: FilePath -> FilePath -> IO ()

0 commit comments

Comments
 (0)