Skip to content

Commit

Permalink
Merge pull request #415 from fendor/enhance/find-cradle-dir
Browse files Browse the repository at this point in the history
Accept directories in 'findCradle'
  • Loading branch information
michaelpj authored Oct 10, 2023
2 parents 7d086c3 + e5ae93a commit ce863db
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
11 changes: 9 additions & 2 deletions src/HIE/Bios/Cradle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ import Text.ParserCombinators.ReadP (readP_to_S)

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

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

-- | Given root\/hie.yaml load the Cradle.
Expand Down
34 changes: 22 additions & 12 deletions tests/BiosTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Control.Monad ( forM_ )
import Data.List ( sort, isPrefixOf )
import Data.Typeable
import System.Directory
import System.FilePath ((</>) )
import System.FilePath ((</>))
import System.Exit (ExitCode(ExitSuccess, ExitFailure))
import Control.Monad.Extra (unlessM)
import qualified HIE.Bios.Ghc.Gap as Gap
Expand Down Expand Up @@ -48,17 +48,7 @@ main = do

defaultMainWithIngredients (ignoreToolTests:defaultIngredients) $
testGroup "Bios-tests"
[ testGroup "Find cradle"
[ testCaseSteps "simple-cabal" $
runTestEnvLocal "./simple-cabal" $ do
findCradleForModuleM "B.hs" (Just "hie.yaml")

-- Checks if we can find a hie.yaml even when the given filepath
-- is unknown. This functionality is required by Haskell IDE Engine.
, testCaseSteps "simple-cabal-unknown-path" $
runTestEnvLocal "./simple-cabal" $ do
findCradleForModuleM "Foo.hs" (Just "hie.yaml")
]
[ testGroup "Find cradle" findCradleTests
, testGroup "Symlink" symbolicLinkTests
, testGroup "Loading tests"
[ testGroup "bios" biosTestCases
Expand Down Expand Up @@ -301,6 +291,26 @@ directTestCases =
testDirectoryM isMultiCradle "B.hs"
]

findCradleTests :: [TestTree]
findCradleTests =
[ cradleFileTest "Simple Existing File" "./simple-cabal" "B.hs" (Just "hie.yaml")
-- Checks if we can find a hie.yaml even when the given filepath
-- is unknown. This functionality is required by Haskell IDE Engine.
, cradleFileTest "Existing File" "cabal-with-ghc" "src/MyLib.hs" (Just "hie.yaml")
, cradleFileTest "Non-existing file" "cabal-with-ghc" "src/MyLib2.hs" (Just "hie.yaml")
, cradleFileTest "Non-existing file 2" "cabal-with-ghc" "MyLib2.hs" (Just "hie.yaml")
, cradleFileTest "Directory 1" "cabal-with-ghc" "src/" (Just "hie.yaml")
, cradleFileTest "Directory 2" "simple-cabal" "" (Just "hie.yaml")
-- Unknown directory in a project, ought to work as well.
, cradleFileTest "Directory 3" "simple-cabal" "src/" (Just "hie.yaml")
, cradleFileTest "Directory does not exist" "doesnotexist" "A.hs" Nothing
]
where
cradleFileTest :: String -> FilePath -> FilePath -> Maybe FilePath -> TestTree
cradleFileTest testName dir fpTarget result = testCaseSteps testName $ do
runTestEnv dir $ do
findCradleForModuleM fpTarget result

-- ------------------------------------------------------------------
-- Unit-test Helper functions
-- ------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion tests/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@ findCradleForModuleM fp expected' = do
withTempCopy :: FilePath -> (FilePath -> IO a) -> IO a
withTempCopy srcDir f =
withSystemTempDirectory "hie-bios-test" $ \newDir -> do
copyDir srcDir newDir
exists <- doesDirectoryExist srcDir
when exists $ do
copyDir srcDir newDir
f newDir

copyDir :: FilePath -> FilePath -> IO ()
Expand Down

0 comments on commit ce863db

Please sign in to comment.