ghc-mod/test/CradleSpec.hs

77 lines
3.1 KiB
Haskell
Raw Normal View History

2013-03-05 01:22:33 +00:00
module CradleSpec where
2013-03-05 01:44:17 +00:00
import Control.Applicative
2013-03-05 01:22:33 +00:00
import Cradle
2013-03-05 01:44:17 +00:00
import Data.List (isPrefixOf)
2013-03-05 01:22:33 +00:00
import Expectation
2013-03-31 14:12:34 +00:00
import System.FilePath (addTrailingPathSeparator, (</>))
2013-03-05 01:22:33 +00:00
import Test.Hspec
import Types
2013-03-31 14:12:34 +00:00
import System.Directory (canonicalizePath)
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
2013-03-31 14:12:34 +00:00
curDir <- canonicalizePath "/"
2013-03-05 01:44:17 +00:00
res <- findCradle Nothing "7.4.1"
res `shouldBe` Cradle {
2013-03-31 14:12:34 +00:00
cradleCurrentDir = curDir
2013-03-05 01:44:17 +00:00
, cradleCabalDir = Nothing
, cradleCabalFile = Nothing
, cradlePackageConf = Nothing
}
2013-03-05 01:22:33 +00:00
it "finds a cabal file" $ do
2013-03-05 01:44:17 +00:00
withDirectory "test/data/subdir1/subdir2" $ \dir -> do
res <- relativeCradle dir <$> findCradle Nothing "7.4.1"
res `shouldBe` Cradle {
2013-03-31 14:12:34 +00:00
cradleCurrentDir = "test" </> "data" </> "subdir1" </> "subdir2"
, cradleCabalDir = Just ("test" </> "data")
, cradleCabalFile = Just ("test" </> "data" </> "cabalapi.cabal")
2013-03-05 01:44:17 +00:00
, cradlePackageConf = Nothing
}
2013-03-05 01:22:33 +00:00
it "finds a sandbox" $ do
2013-03-05 01:44:17 +00:00
withDirectory "test/data/subdir1/subdir2" $ \dir -> do
res <- relativeCradle dir <$> findCradle Nothing "7.6.2"
res `shouldBe` Cradle {
2013-03-31 14:12:34 +00:00
cradleCurrentDir = "test" </> "data" </> "subdir1" </> "subdir2"
, cradleCabalDir = Just ("test" </> "data")
, cradleCabalFile = Just ("test" </> "data" </> "cabalapi.cabal")
, cradlePackageConf = Just ("test" </> "data" </> "cabal-dev" </> "packages-7.6.2.conf")
2013-03-05 01:44:17 +00:00
}
2013-03-05 01:22:33 +00:00
it "finds a sandbox if exists" $ do
2013-03-05 01:49:32 +00:00
withDirectory "/" $ \dir -> do
2013-03-31 14:12:34 +00:00
curDir <- canonicalizePath "/"
res <- relativeCradle dir <$> findCradle (Just $ addTrailingPathSeparator dir ++ ("test" </> "data" </> "cabal-dev")) "7.6.2"
2013-03-05 01:44:17 +00:00
res `shouldBe` Cradle {
2013-03-31 14:12:34 +00:00
cradleCurrentDir = curDir
2013-03-05 01:44:17 +00:00
, cradleCabalDir = Nothing
, cradleCabalFile = Nothing
2013-03-31 14:12:34 +00:00
, cradlePackageConf = Just ("test" </> "data" </> "cabal-dev" </> "packages-7.6.2.conf")
2013-03-05 01:44:17 +00:00
}
2013-03-05 01:22:33 +00:00
2013-03-05 01:49:32 +00:00
it "throws an error if the sandbox does not exist" $ do
withDirectory_ "/" $
findCradle (Just "/") "7.4.1" `shouldThrow` anyIOException
2013-03-05 01:44:17 +00:00
relativeCradle :: FilePath -> Cradle -> Cradle
relativeCradle dir cradle = Cradle {
cradleCurrentDir = toRelativeDir dir $ cradleCurrentDir cradle
, cradleCabalDir = toRelativeDir dir <$> cradleCabalDir cradle
, cradleCabalFile = toRelativeDir dir <$> cradleCabalFile cradle
, cradlePackageConf = toRelativeDir dir <$> cradlePackageConf cradle
}
toRelativeDir :: FilePath -> FilePath -> FilePath
toRelativeDir dir file
| dir' `isPrefixOf` file = drop len file
| otherwise = file
where
dir' = addTrailingPathSeparator dir
len = length dir'