From a94b1a3b5adf36ad4b43bf4945cb7c98a2a2d176 Mon Sep 17 00:00:00 2001 From: Kazu Yamamoto Date: Sat, 21 Sep 2013 18:37:33 +0900 Subject: [PATCH] fixing tests. --- Language/Haskell/GhcMod/Cradle.hs | 12 +++++++++++- test/CabalApiSpec.hs | 4 ++-- test/CheckSpec.hs | 9 +++++---- test/CradleSpec.hs | 4 ++-- test/InfoSpec.hs | 13 +++++++------ test/data/cabal.sandbox.config | 12 ++++++------ 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/Language/Haskell/GhcMod/Cradle.hs b/Language/Haskell/GhcMod/Cradle.hs index ecd298b..ed8c57b 100644 --- a/Language/Haskell/GhcMod/Cradle.hs +++ b/Language/Haskell/GhcMod/Cradle.hs @@ -1,6 +1,10 @@ {-# LANGUAGE BangPatterns #-} -module Language.Haskell.GhcMod.Cradle (findCradle, getPackageDbDir) where +module Language.Haskell.GhcMod.Cradle ( + findCradle + , findCradleWithoutSandbox + , getPackageDbDir + ) where import Data.Char (isSpace) import Control.Applicative ((<$>)) @@ -41,6 +45,12 @@ findCradle' wdir = do , cradlePackageDbOpts = pkgDbOpts } +-- Just for testing +findCradleWithoutSandbox :: IO Cradle +findCradleWithoutSandbox = do + cradle <- findCradle + return cradle { cradlePackageDbOpts = [] } + ---------------------------------------------------------------- cabalSuffix :: String diff --git a/test/CabalApiSpec.hs b/test/CabalApiSpec.hs index 63a3458..4174253 100644 --- a/test/CabalApiSpec.hs +++ b/test/CabalApiSpec.hs @@ -25,10 +25,10 @@ spec = do pkgDesc <- parseCabalFile $ fromJust $ cradleCabalFile cradle res <- getCompilerOptions [] cradle pkgDesc let res' = res { - ghcOptions = map (toRelativeDir dir) (ghcOptions res) + ghcOptions = ghcOptions res , includeDirs = map (toRelativeDir dir) (includeDirs res) } - res' `shouldBe` CompilerOptions {ghcOptions = ["-no-user-package-db","-package-db","test/data/.cabal-sandbox/i386-osx-ghc-7.6.3-packages.conf.d","-XHaskell98"], includeDirs = ["test/data","test/data/dist/build","test/data/dist/build/autogen","test/data/subdir1/subdir2","test/data/test"], depPackages = ["Cabal","base","template-haskell"]} + res' `shouldBe` CompilerOptions {ghcOptions = ["-no-user-package-db","-package-db","/home/me/work/ghc-mod/test/data/.cabal-sandbox/i386-osx-ghc-7.6.3-packages.conf.d","-XHaskell98"], includeDirs = ["test/data","test/data/dist/build","test/data/dist/build/autogen","test/data/subdir1/subdir2","test/data/test"], depPackages = ["Cabal","base","template-haskell"]} describe "cabalDependPackages" $ do it "extracts dependent packages" $ do diff --git a/test/CheckSpec.hs b/test/CheckSpec.hs index d289a08..5ecf072 100644 --- a/test/CheckSpec.hs +++ b/test/CheckSpec.hs @@ -2,6 +2,7 @@ module CheckSpec where import Data.List (isSuffixOf, isInfixOf, isPrefixOf) import Language.Haskell.GhcMod +import Language.Haskell.GhcMod.Cradle import System.FilePath import Test.Hspec @@ -12,24 +13,24 @@ spec = do describe "checkSyntax" $ do it "can check even if an executable depends on its library" $ do withDirectory_ "test/data/ghc-mod-check" $ do - cradle <- findCradle + cradle <- findCradleWithoutSandbox res <- checkSyntax defaultOptions cradle ["main.hs"] res `shouldBe` "main.hs:5:1:Warning: Top-level binding with no type signature: main :: IO ()\NUL\n" it "can check even if a test module imports another test module located at different directory" $ do withDirectory_ "test/data/check-test-subdir" $ do - cradle <- findCradle + cradle <- findCradleWithoutSandbox res <- checkSyntax defaultOptions cradle ["test/Bar/Baz.hs"] res `shouldSatisfy` (("test" "Foo.hs:3:1:Warning: Top-level binding with no type signature: foo :: [Char]\NUL\n") `isSuffixOf`) it "can detect mutually imported modules" $ do withDirectory_ "test/data" $ do - cradle <- findCradle + cradle <- findCradleWithoutSandbox res <- checkSyntax defaultOptions cradle ["Mutual1.hs"] res `shouldSatisfy` ("Module imports form a cycle" `isInfixOf`) it "can check a module using QuasiQuotes" $ do withDirectory_ "test/data" $ do - cradle <- findCradle + cradle <- findCradleWithoutSandbox res <- checkSyntax defaultOptions cradle ["Baz.hs"] res `shouldSatisfy` ("Baz.hs:5:1:Warning:" `isPrefixOf`) diff --git a/test/CradleSpec.hs b/test/CradleSpec.hs index bba4199..17b5934 100644 --- a/test/CradleSpec.hs +++ b/test/CradleSpec.hs @@ -29,7 +29,7 @@ spec = do cradleCurrentDir = "test" "data" "subdir1" "subdir2" , cradleCabalDir = Just ("test" "data") , cradleCabalFile = Just ("test" "data" "cabalapi.cabal") - , cradlePackageDbOpts = ["-no-user-package-db", "-package-db", "test" "data" ".cabal-sandbox" "i386-osx-ghc-7.6.3-packages.conf.d"] + , cradlePackageDbOpts = ["-no-user-package-db", "-package-db", "test" "data" ".cabal-sandbox" "/home/me/work/ghc-mod/test/data/.cabal-sandbox/i386-osx-ghc-7.6.3-packages.conf.d"] } it "works even if a sandbox config file is broken" $ do withDirectory "test/data/broken-sandbox" $ \dir -> do @@ -44,7 +44,7 @@ spec = do describe "getPackageDbDir" $ do it "parses a config file and extracts package db" $ do pkgDb <- getPackageDbDir "test/data/cabal.sandbox.config" - pkgDb `shouldBe` "/Users/kazu/work/ghc-mod/test/data/.cabal-sandbox/i386-osx-ghc-7.6.3-packages.conf.d" + pkgDb `shouldBe` "/home/me/work/ghc-mod/test/data/.cabal-sandbox/i386-osx-ghc-7.6.3-packages.conf.d" it "throws an error if a config file is broken" $ do getPackageDbDir "test/data/bad.config" `shouldThrow` anyException diff --git a/test/InfoSpec.hs b/test/InfoSpec.hs index 02d5a61..9052842 100644 --- a/test/InfoSpec.hs +++ b/test/InfoSpec.hs @@ -2,6 +2,7 @@ module InfoSpec where import Data.List (isPrefixOf) import Language.Haskell.GhcMod +import Language.Haskell.GhcMod.Cradle import System.Exit import System.Process import Test.Hspec @@ -13,38 +14,38 @@ spec = do describe "typeExpr" $ do it "shows types of the expression and its outers" $ do withDirectory_ "test/data/ghc-mod-check" $ do - cradle <- findCradle + cradle <- findCradleWithoutSandbox res <- typeExpr defaultOptions cradle "Data/Foo.hs" "Data.Foo" 9 5 res `shouldBe` "9 5 11 40 \"Int -> a -> a -> a\"\n7 1 11 40 \"Int -> Integer\"\n" it "works with a module using TemplateHaskell" $ do withDirectory_ "test/data" $ do - cradle <- findCradle + cradle <- findCradleWithoutSandbox res <- typeExpr defaultOptions cradle "Bar.hs" "Bar" 5 1 res `shouldBe` unlines ["5 1 5 20 \"[Char]\""] it "works with a module that imports another module using TemplateHaskell" $ do withDirectory_ "test/data" $ do - cradle <- findCradle + cradle <- findCradleWithoutSandbox res <- typeExpr defaultOptions cradle "Main.hs" "Main" 3 8 res `shouldBe` unlines ["3 8 3 16 \"String -> IO ()\"", "3 8 3 20 \"IO ()\"", "3 1 3 20 \"IO ()\""] describe "infoExpr" $ do it "works for non-export functions" $ do withDirectory_ "test/data" $ do - cradle <- findCradle + cradle <- findCradleWithoutSandbox res <- infoExpr defaultOptions cradle "Info.hs" "Info" "fib" res `shouldSatisfy` ("fib :: Int -> Int" `isPrefixOf`) it "works with a module using TemplateHaskell" $ do withDirectory_ "test/data" $ do - cradle <- findCradle + cradle <- findCradleWithoutSandbox res <- infoExpr defaultOptions cradle "Bar.hs" "Bar" "foo" res `shouldSatisfy` ("foo :: ExpQ" `isPrefixOf`) it "works with a module that imports another module using TemplateHaskell" $ do withDirectory_ "test/data" $ do - cradle <- findCradle + cradle <- findCradleWithoutSandbox res <- infoExpr defaultOptions cradle "Main.hs" "Main" "bar" res `shouldSatisfy` ("bar :: [Char]" `isPrefixOf`) diff --git a/test/data/cabal.sandbox.config b/test/data/cabal.sandbox.config index 3b7feaf..9697fff 100644 --- a/test/data/cabal.sandbox.config +++ b/test/data/cabal.sandbox.config @@ -4,15 +4,15 @@ -- if you want to change the default settings for this sandbox. -local-repo: /Users/kazu/work/ghc-mod/test/data/.cabal-sandbox/packages -logs-dir: /Users/kazu/work/ghc-mod/test/data/.cabal-sandbox/logs -world-file: /Users/kazu/work/ghc-mod/test/data/.cabal-sandbox/world +local-repo: /home/me/work/ghc-mod/test/data/.cabal-sandbox/packages +logs-dir: /home/me/work/ghc-mod/test/data/.cabal-sandbox/logs +world-file: /home/me/work/ghc-mod/test/data/.cabal-sandbox/world user-install: False -package-db: /Users/kazu/work/ghc-mod/test/data/.cabal-sandbox/i386-osx-ghc-7.6.3-packages.conf.d -build-summary: /Users/kazu/work/ghc-mod/test/data/.cabal-sandbox/logs/build.log +package-db: /home/me/work/ghc-mod/test/data/.cabal-sandbox/i386-osx-ghc-7.6.3-packages.conf.d +build-summary: /home/me/work/ghc-mod/test/data/.cabal-sandbox/logs/build.log install-dirs - prefix: /Users/kazu/work/ghc-mod/test/data/.cabal-sandbox + prefix: /home/me/work/ghc-mod/test/data/.cabal-sandbox bindir: $prefix/bin libdir: $prefix/lib libsubdir: $arch-$os-$compiler/$pkgid