2013-03-05 01:22:33 +00:00
|
|
|
module CradleSpec where
|
|
|
|
|
2013-03-05 01:44:17 +00:00
|
|
|
import Control.Applicative
|
2014-01-14 06:24:10 +00:00
|
|
|
import Data.List (isSuffixOf)
|
2013-09-21 06:10:43 +00:00
|
|
|
import Language.Haskell.GhcMod.Cradle
|
|
|
|
import Language.Haskell.GhcMod.Types
|
2015-03-04 20:48:21 +00:00
|
|
|
import System.Directory (canonicalizePath)
|
|
|
|
import System.FilePath (pathSeparator)
|
2013-03-05 01:22:33 +00:00
|
|
|
import Test.Hspec
|
2015-09-01 08:27:12 +00:00
|
|
|
import TestUtils
|
2016-01-09 14:19:00 +00:00
|
|
|
import Prelude
|
2013-03-05 01:22:33 +00:00
|
|
|
|
2013-09-03 02:49:35 +00:00
|
|
|
import Dir
|
|
|
|
|
2015-03-04 20:48:21 +00:00
|
|
|
clean_ :: IO Cradle -> IO Cradle
|
|
|
|
clean_ f = do
|
|
|
|
crdl <- f
|
|
|
|
cleanupCradle crdl
|
|
|
|
return crdl
|
|
|
|
|
|
|
|
relativeCradle :: FilePath -> Cradle -> Cradle
|
|
|
|
relativeCradle dir crdl = crdl {
|
|
|
|
cradleCurrentDir = toRelativeDir dir $ cradleCurrentDir crdl
|
|
|
|
, cradleRootDir = toRelativeDir dir $ cradleRootDir crdl
|
|
|
|
, cradleCabalFile = toRelativeDir dir <$> cradleCabalFile crdl
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Work around GHC 7.2.2 where `canonicalizePath "/"` returns "/.".
|
|
|
|
stripLastDot :: FilePath -> FilePath
|
|
|
|
stripLastDot path
|
|
|
|
| (pathSeparator:'.':"") `isSuffixOf` path = init path
|
|
|
|
| otherwise = path
|
|
|
|
|
2013-03-05 01:22:33 +00:00
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
|
|
|
describe "findCradle" $ do
|
|
|
|
it "returns the current directory" $ do
|
2013-03-05 01:44:17 +00:00
|
|
|
withDirectory_ "/" $ do
|
2014-01-14 06:24:10 +00:00
|
|
|
curDir <- stripLastDot <$> canonicalizePath "/"
|
2016-05-14 18:18:06 +00:00
|
|
|
res <- clean_ $ runGmOutDef $ findCradleNoLog $ optPrograms defaultOptions
|
2014-10-14 17:52:58 +00:00
|
|
|
cradleCurrentDir res `shouldBe` curDir
|
|
|
|
cradleRootDir res `shouldBe` curDir
|
|
|
|
cradleCabalFile res `shouldBe` Nothing
|
|
|
|
|
2013-09-20 06:57:26 +00:00
|
|
|
it "finds a cabal file and a sandbox" $ do
|
2015-03-04 20:48:21 +00:00
|
|
|
withDirectory "test/data/cabal-project/subdir1/subdir2" $ \dir -> do
|
2016-05-14 18:18:06 +00:00
|
|
|
res <- relativeCradle dir <$> clean_ (runGmOutDef $ findCradleNoLog $ optPrograms defaultOptions)
|
2015-02-07 15:41:15 +00:00
|
|
|
|
|
|
|
cradleCurrentDir res `shouldBe`
|
2015-03-04 20:48:21 +00:00
|
|
|
"test/data/cabal-project/subdir1/subdir2"
|
2015-02-07 15:41:15 +00:00
|
|
|
|
2015-03-04 20:48:21 +00:00
|
|
|
cradleRootDir res `shouldBe` "test/data/cabal-project"
|
2015-02-07 15:41:15 +00:00
|
|
|
|
|
|
|
cradleCabalFile res `shouldBe`
|
2015-03-04 20:48:21 +00:00
|
|
|
Just ("test/data/cabal-project/cabalapi.cabal")
|
2015-02-07 15:41:15 +00:00
|
|
|
|
2013-09-21 06:32:22 +00:00
|
|
|
it "works even if a sandbox config file is broken" $ do
|
|
|
|
withDirectory "test/data/broken-sandbox" $ \dir -> do
|
2016-05-14 18:18:06 +00:00
|
|
|
res <- relativeCradle dir <$> clean_ (runGmOutDef $ findCradleNoLog $ optPrograms defaultOptions)
|
2015-02-07 15:41:15 +00:00
|
|
|
cradleCurrentDir res `shouldBe`
|
|
|
|
"test" </> "data" </> "broken-sandbox"
|
|
|
|
|
|
|
|
cradleRootDir res `shouldBe`
|
|
|
|
"test" </> "data" </> "broken-sandbox"
|
|
|
|
|
|
|
|
cradleCabalFile res `shouldBe`
|
|
|
|
Just ("test" </> "data" </> "broken-sandbox" </> "dummy.cabal")
|