From 24ada2abeb3047b1ec3d8b11814187debd1ad7ef Mon Sep 17 00:00:00 2001 From: eagletmt Date: Fri, 15 Mar 2013 17:30:21 +0900 Subject: [PATCH] Gather all hs-source-dirs to check test modules in sub-directories --- CabalApi.hs | 8 +++++++- test/CabalApiSpec.hs | 5 +++++ test/CheckSpec.hs | 7 +++++++ .../check-test-subdir/check-test-subdir.cabal | 15 +++++++++++++++ .../check-test-subdir/src/Check/Test/Subdir.hs | 4 ++++ test/data/check-test-subdir/test/Bar/Baz.hs | 5 +++++ test/data/check-test-subdir/test/Foo.hs | 3 +++ test/data/check-test-subdir/test/Main.hs | 5 +++++ 8 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/data/check-test-subdir/check-test-subdir.cabal create mode 100644 test/data/check-test-subdir/src/Check/Test/Subdir.hs create mode 100644 test/data/check-test-subdir/test/Bar/Baz.hs create mode 100644 test/data/check-test-subdir/test/Foo.hs create mode 100644 test/data/check-test-subdir/test/Main.hs diff --git a/CabalApi.hs b/CabalApi.hs index c622238..49ad9d9 100644 --- a/CabalApi.hs +++ b/CabalApi.hs @@ -5,6 +5,7 @@ module CabalApi ( , cabalParseFile , cabalBuildInfo , cabalAllDependPackages + , cabalAllSourceDirs , getGHCVersion ) where @@ -43,7 +44,7 @@ cookInfo ghcOptions cradle cabal = (gopts,idirs,depPkgs) Just cfile = cradleCabalFile cradle binfo = cabalBuildInfo cabal gopts = getGHCOptions ghcOptions binfo - idirs = includeDirectroies cdir owdir $ hsSourceDirs binfo + idirs = includeDirectroies cdir owdir $ cabalAllSourceDirs cabal depPkgs = removeMe cfile $ cabalAllDependPackages cabal removeMe :: FilePath -> [String] -> [String] @@ -79,6 +80,11 @@ cabalBuildInfo pd = fromJust $ fromLibrary pd <|> fromExecutable pd ---------------------------------------------------------------- +cabalAllSourceDirs :: GenericPackageDescription -> [FilePath] +cabalAllSourceDirs = fromPackageDescription (f libBuildInfo) (f buildInfo) (f testBuildInfo) (f benchmarkBuildInfo) + where + f getBuildInfo = concatMap (hsSourceDirs . getBuildInfo . condTreeData) + cabalAllDependPackages :: GenericPackageDescription -> [Package] cabalAllDependPackages pd = uniqueAndSort pkgs where diff --git a/test/CabalApiSpec.hs b/test/CabalApiSpec.hs index 3a2c67a..f90d1cc 100644 --- a/test/CabalApiSpec.hs +++ b/test/CabalApiSpec.hs @@ -11,6 +11,11 @@ spec = do pkgs <- cabalAllDependPackages <$> cabalParseFile "test/data/cabalapi.cabal" pkgs `shouldBe` ["Cabal","base","template-haskell"] + describe "cabalAllSourceDirs" $ do + it "extracts all hs-source-dirs" $ do + dirs <- cabalAllSourceDirs <$> cabalParseFile "test/data/check-test-subdir/check-test-subdir.cabal" + dirs `shouldBe` ["src", "test"] + describe "cabalBuildInfo" $ do it "extracts build info" $ do info <- cabalBuildInfo <$> cabalParseFile "test/data/cabalapi.cabal" diff --git a/test/CheckSpec.hs b/test/CheckSpec.hs index e3a0eba..a818b87 100644 --- a/test/CheckSpec.hs +++ b/test/CheckSpec.hs @@ -3,6 +3,7 @@ module CheckSpec where import CabalApi import Check import Cradle +import Data.List (isSuffixOf) import Expectation import Test.Hspec import Types @@ -16,3 +17,9 @@ spec = do cradle <- findCradle Nothing strVer 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 <- getGHCVersion >>= findCradle Nothing . fst + 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`) diff --git a/test/data/check-test-subdir/check-test-subdir.cabal b/test/data/check-test-subdir/check-test-subdir.cabal new file mode 100644 index 0000000..75d6ee1 --- /dev/null +++ b/test/data/check-test-subdir/check-test-subdir.cabal @@ -0,0 +1,15 @@ +name: check-test-subdir +version: 0.1.0 +build-type: Simple +cabal-version: >= 1.8 + +library + build-depends: base == 4.* + hs-source-dirs: src + exposed-modules: Check.Test.Subdir + +test-suite test + type: exitcode-stdio-1.0 + build-depends: base == 4.* + hs-source-dirs: test + main-is: Main.hs diff --git a/test/data/check-test-subdir/src/Check/Test/Subdir.hs b/test/data/check-test-subdir/src/Check/Test/Subdir.hs new file mode 100644 index 0000000..c3da147 --- /dev/null +++ b/test/data/check-test-subdir/src/Check/Test/Subdir.hs @@ -0,0 +1,4 @@ +module Check.Test.Subdir (subdir) where + +subdir :: String +subdir = "subdir" diff --git a/test/data/check-test-subdir/test/Bar/Baz.hs b/test/data/check-test-subdir/test/Bar/Baz.hs new file mode 100644 index 0000000..f195c71 --- /dev/null +++ b/test/data/check-test-subdir/test/Bar/Baz.hs @@ -0,0 +1,5 @@ +module Bar.Baz (baz) where +import Foo (foo) + +baz :: String +baz = unwords [foo, "baz"] diff --git a/test/data/check-test-subdir/test/Foo.hs b/test/data/check-test-subdir/test/Foo.hs new file mode 100644 index 0000000..6de2eb7 --- /dev/null +++ b/test/data/check-test-subdir/test/Foo.hs @@ -0,0 +1,3 @@ +module Foo (foo) where + +foo = "foo" diff --git a/test/data/check-test-subdir/test/Main.hs b/test/data/check-test-subdir/test/Main.hs new file mode 100644 index 0000000..11d061d --- /dev/null +++ b/test/data/check-test-subdir/test/Main.hs @@ -0,0 +1,5 @@ +module Main where +import Bar.Baz (baz) + +main :: IO () +main = putStrLn baz