1
1
mirror of https://github.com/sol/hpack.git synced 2024-10-04 03:38:00 +03:00

Rudimentary support for test sections

This commit is contained in:
Simon Hengel 2015-03-27 18:15:21 +08:00
parent e946c856cb
commit da71d173a3
7 changed files with 93 additions and 7 deletions

View File

@ -17,6 +17,7 @@ library
, directory
, filepath
, interpolate
, unordered-containers
, yaml
default-language: Haskell2010
@ -29,19 +30,18 @@ executable cb
default-language: Haskell2010
test-suite spec
type:
exitcode-stdio-1.0
type: exitcode-stdio-1.0
ghc-options:
-Wall
hs-source-dirs: test, src
main-is:
Spec.hs
main-is: Spec.hs
build-depends:
base == 4.*
, base-compat
, directory
, filepath
, interpolate
, unordered-containers
, yaml
, hspec == 2.*

View File

@ -1,7 +1,12 @@
dependencies:
- base == 4.*
- base-compat
- interpolate
- directory
- filepath
- interpolate
- unordered-containers
- yaml
tests:
spec:
main: test/Spec.hs

View File

@ -3,12 +3,12 @@ module Cabalize where
import Control.Applicative
import Data.Maybe
import Data.Char
import Data.List
import Data.String.Interpolate
import System.Directory
import System.FilePath
import System.Exit.Compat
import qualified Data.HashMap.Lazy as Map
import Util
import Config (Config)
@ -20,6 +20,7 @@ data Package = Package {
packageName :: String
, packageVersion :: [Int]
, packageLibrary :: Library
, packageTests :: [Test]
} deriving (Eq, Show)
data Library = Library {
@ -27,6 +28,29 @@ data Library = Library {
, libraryDependencies :: [Dependency]
} deriving (Eq, Show)
data Test = Test {
testName :: String
, testMain :: FilePath
, testDependencies :: [Dependency]
} deriving (Eq, Show)
renderTests :: [Test] -> String
renderTests = unlines . map renderTest
renderTest :: Test -> String
renderTest Test{..} = stripEmptyLines [i|
test-suite spec
type: exitcode-stdio-1.0
hs-source-dirs: #{takeDirectory testMain}
main-is: #{takeFileName testMain}
build-depends:
#{intercalate "\n , " $ sort testDependencies}
default-language: Haskell2010
|]
testConfigToTest :: [Dependency] -> String -> Config.Test -> Test
testConfigToTest dependencies name t = Test name (Config.main t) dependencies
configFile :: FilePath
configFile = "package.yaml"
@ -48,13 +72,14 @@ build-type: Simple
cabal-version: >= 1.10
#{renderLibrary packageLibrary}
#{renderTests packageTests}
|]
renderVersion :: [Int] -> String
renderVersion = intercalate "." . map show
renderLibrary :: Library -> String
renderLibrary Library{..} = dropWhile isSpace [i|
renderLibrary Library{..} = stripEmptyLines [i|
library
hs-source-dirs: src
exposed-modules:
@ -72,6 +97,7 @@ mkPackage Config.Config{..} = do
packageName = name
, packageVersion = [0,0,0]
, packageLibrary = library
, packageTests = (map (uncurry $ testConfigToTest dependencies) . Map.toList) tests
}
return package

View File

@ -3,12 +3,20 @@ module Config where
import Data.Yaml
import GHC.Generics
import Data.HashMap.Lazy (HashMap)
data Config = Config {
dependencies :: [String]
, tests :: HashMap String Test
} deriving (Eq, Show, Generic)
instance FromJSON Config
data Test = Test {
main :: FilePath
} deriving (Eq, Show, Generic)
instance FromJSON Test
readConfig :: FilePath -> IO (Maybe Config)
readConfig = decodeFile

View File

@ -33,6 +33,21 @@ spec = do
, directory
, filepath
, interpolate
, unordered-containers
, yaml
default-language: Haskell2010
test-suite spec
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
build-depends:
base == 4.*
, base-compat
, directory
, filepath
, interpolate
, unordered-containers
, yaml
default-language: Haskell2010
|])

24
test/ConfigSpec.hs Normal file
View File

@ -0,0 +1,24 @@
{-# LANGUAGE QuasiQuotes, OverloadedLists #-}
module ConfigSpec (main, spec) where
import Test.Hspec
import Helper
import Data.String.Interpolate
import Config hiding (main)
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "readConfig" $ do
it "reads package config" $ do
withFile [i|
dependencies:
- base
tests:
spec:
main: test/Spec.hs
|] $ \file -> readConfig file `shouldReturn` Just (Config ["base"] [("spec", Test "test/Spec.hs")])

8
test/Helper.hs Normal file
View File

@ -0,0 +1,8 @@
module Helper where
import Control.Exception
import System.Directory
withFile :: String -> (FilePath -> IO a) -> IO a
withFile c action = bracket_ (writeFile file c) (removeFile file) (action file)
where file = "foo.yaml"