From 89dc02f1850546eb5147492d68592ecb670442b4 Mon Sep 17 00:00:00 2001 From: Kazu Yamamoto Date: Tue, 5 Mar 2013 10:22:33 +0900 Subject: [PATCH] test for Cradle. --- Types.hs | 2 +- test/CheckSpec.hs | 2 +- test/CradleSpec.hs | 27 +++++++++++++++++++++++++++ test/Expectation.hs | 9 +++++++-- test/InfoSpec.hs | 2 +- 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 test/CradleSpec.hs diff --git a/Types.hs b/Types.hs index 2df9317..d39a068 100644 --- a/Types.hs +++ b/Types.hs @@ -69,7 +69,7 @@ data Cradle = Cradle { , cradleCabalDir :: Maybe FilePath , cradleCabalFile :: Maybe FilePath , cradlePackageConf :: Maybe FilePath - } deriving Show + } deriving (Eq, Show) ---------------------------------------------------------------- diff --git a/test/CheckSpec.hs b/test/CheckSpec.hs index 20feb20..e3a0eba 100644 --- a/test/CheckSpec.hs +++ b/test/CheckSpec.hs @@ -11,7 +11,7 @@ spec :: Spec spec = do describe "checkSyntax" $ do it "can check even if an executable depends on its library" $ do - withDirectory "test/data/ghc-mod-check" $ do + withDirectory_ "test/data/ghc-mod-check" $ do (strVer,_) <- getGHCVersion cradle <- findCradle Nothing strVer res <- checkSyntax defaultOptions cradle "main.hs" diff --git a/test/CradleSpec.hs b/test/CradleSpec.hs new file mode 100644 index 0000000..100a4e1 --- /dev/null +++ b/test/CradleSpec.hs @@ -0,0 +1,27 @@ +module CradleSpec where + +import Cradle +import Expectation +import System.Directory +import Test.Hspec +import Types + +spec :: Spec +spec = do + describe "findCradle" $ do + it "returns the current directory" $ do + withDirectory_ "/" $ + findCradle Nothing "7.4.1" `shouldReturn` Cradle {cradleCurrentDir = "/", cradleCabalDir = Nothing, cradleCabalFile = Nothing, cradlePackageConf = Nothing} + + it "finds a cabal file" $ do + withDirectory "test/data/subdir1/subdir2" $ \dir -> + findCradle Nothing "7.4.1" `shouldReturn` Cradle {cradleCurrentDir = "/Users/kazu/work/ghc-mod/test/data", cradleCabalDir = Just "/Users/kazu/work/ghc-mod/test/data", cradleCabalFile = Just "/Users/kazu/work/ghc-mod/test/data/cabalapi.cabal", cradlePackageConf = Nothing} + + it "finds a sandbox" $ do + withDirectory "test/data/subdir1/subdir2" $ \dir -> + findCradle Nothing "7.6.2" `shouldReturn` Cradle {cradleCurrentDir = "/Users/kazu/work/ghc-mod/test/data", cradleCabalDir = Just "/Users/kazu/work/ghc-mod/test/data", cradleCabalFile = Just "/Users/kazu/work/ghc-mod/test/data/cabalapi.cabal", cradlePackageConf = Just "/Users/kazu/work/ghc-mod/test/data/cabal-dev/packages-7.6.2.conf"} + + it "finds a sandbox if exists" $ do + withDirectory "/" $ \dir -> + findCradle (Just "/Users/kazu/work/ghc-mod/test/data/cabal-dev") "7.6.2" `shouldReturn` Cradle {cradleCurrentDir = "/", cradleCabalDir = Nothing, cradleCabalFile = Nothing, cradlePackageConf = Just "/Users/kazu/work/ghc-mod/test/data/cabal-dev/packages-7.6.2.conf"} + diff --git a/test/Expectation.hs b/test/Expectation.hs index 3794af1..2032b9d 100644 --- a/test/Expectation.hs +++ b/test/Expectation.hs @@ -9,7 +9,12 @@ shouldContain containers element = do let res = element `elem` containers res `shouldBe` True -withDirectory :: FilePath -> IO a -> IO a -withDirectory dir action = bracket getCurrentDirectory +withDirectory_ :: FilePath -> IO a -> IO a +withDirectory_ dir action = bracket getCurrentDirectory setCurrentDirectory (\_ -> setCurrentDirectory dir >> action) + +withDirectory :: FilePath -> (FilePath -> IO a) -> IO a +withDirectory dir action = bracket getCurrentDirectory + setCurrentDirectory + (\d -> setCurrentDirectory dir >> action d) diff --git a/test/InfoSpec.hs b/test/InfoSpec.hs index cf8ba12..a6c52a4 100644 --- a/test/InfoSpec.hs +++ b/test/InfoSpec.hs @@ -11,7 +11,7 @@ spec :: Spec spec = do describe "typeExpr" $ do it "shows types of the expression and its outers" $ do - withDirectory "test/data/ghc-mod-check" $ do + withDirectory_ "test/data/ghc-mod-check" $ do (strVer,_) <- getGHCVersion cradle <- findCradle Nothing strVer res <- typeExpr defaultOptions cradle "Data.Foo" 9 5 "Data/Foo.hs"