2014-05-09 18:37:15 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
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
|
2014-04-15 03:13:10 +00:00
|
|
|
import System.Directory (canonicalizePath,getCurrentDirectory)
|
2014-01-14 06:24:10 +00:00
|
|
|
import System.FilePath ((</>), pathSeparator)
|
2013-03-05 01:22:33 +00:00
|
|
|
import Test.Hspec
|
|
|
|
|
2013-09-03 02:49:35 +00:00
|
|
|
import Dir
|
|
|
|
|
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 "/"
|
2013-09-20 06:48:50 +00:00
|
|
|
res <- findCradle
|
2013-03-05 01:44:17 +00:00
|
|
|
res `shouldBe` Cradle {
|
2014-03-28 03:05:11 +00:00
|
|
|
cradleCurrentDir = curDir
|
2014-03-30 08:28:57 +00:00
|
|
|
, cradleRootDir = curDir
|
2014-03-28 03:05:11 +00:00
|
|
|
, cradleCabalFile = Nothing
|
2014-04-15 03:13:10 +00:00
|
|
|
, cradlePkgDbStack = [GlobalDb]
|
2013-03-05 01:44:17 +00:00
|
|
|
}
|
2013-09-20 06:57:26 +00:00
|
|
|
it "finds a cabal file and a sandbox" $ do
|
2014-04-15 03:13:10 +00:00
|
|
|
cwd <- getCurrentDirectory
|
2013-03-05 01:44:17 +00:00
|
|
|
withDirectory "test/data/subdir1/subdir2" $ \dir -> do
|
2013-09-20 06:48:50 +00:00
|
|
|
res <- relativeCradle dir <$> findCradle
|
2013-03-05 01:44:17 +00:00
|
|
|
res `shouldBe` Cradle {
|
2014-03-28 03:05:11 +00:00
|
|
|
cradleCurrentDir = "test" </> "data" </> "subdir1" </> "subdir2"
|
2014-03-30 08:28:57 +00:00
|
|
|
, cradleRootDir = "test" </> "data"
|
2014-03-28 03:05:11 +00:00
|
|
|
, cradleCabalFile = Just ("test" </> "data" </> "cabalapi.cabal")
|
2014-05-09 18:37:15 +00:00
|
|
|
#if MIN_VERSION_Cabal(1,18,0)
|
2014-04-15 03:13:10 +00:00
|
|
|
, cradlePkgDbStack = [GlobalDb, PackageDb (cwd </> "test/data/.cabal-sandbox/i386-osx-ghc-7.6.3-packages.conf.d")]
|
2014-05-09 18:37:15 +00:00
|
|
|
#else
|
|
|
|
, cradlePkgDbStack = [GlobalDb, UserDb]
|
|
|
|
#endif
|
2013-03-05 01:44:17 +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
|
|
|
|
res <- relativeCradle dir <$> findCradle
|
|
|
|
res `shouldBe` Cradle {
|
2014-03-28 03:05:11 +00:00
|
|
|
cradleCurrentDir = "test" </> "data" </> "broken-sandbox"
|
2014-03-30 08:28:57 +00:00
|
|
|
, cradleRootDir = "test" </> "data" </> "broken-sandbox"
|
2014-03-28 03:05:11 +00:00
|
|
|
, cradleCabalFile = Just ("test" </> "data" </> "broken-sandbox" </> "dummy.cabal")
|
2014-04-15 03:13:10 +00:00
|
|
|
, cradlePkgDbStack = [GlobalDb, UserDb]
|
2013-09-21 06:32:22 +00:00
|
|
|
}
|
|
|
|
|
2013-03-05 01:44:17 +00:00
|
|
|
relativeCradle :: FilePath -> Cradle -> Cradle
|
2014-01-30 11:42:25 +00:00
|
|
|
relativeCradle dir cradle = cradle {
|
2014-03-30 08:28:57 +00:00
|
|
|
cradleCurrentDir = toRelativeDir dir $ cradleCurrentDir cradle
|
|
|
|
, cradleRootDir = toRelativeDir dir $ cradleRootDir cradle
|
|
|
|
, cradleCabalFile = toRelativeDir dir <$> cradleCabalFile cradle
|
2013-03-05 01:44:17 +00:00
|
|
|
}
|
2014-01-14 06:24:10 +00:00
|
|
|
|
|
|
|
-- Work around GHC 7.2.2 where `canonicalizePath "/"` returns "/.".
|
|
|
|
stripLastDot :: FilePath -> FilePath
|
|
|
|
stripLastDot path
|
|
|
|
| (pathSeparator:'.':"") `isSuffixOf` path = init path
|
|
|
|
| otherwise = path
|