on master help sign in
Add tests again
88ffd3cJasper Van der Jeugt
hakyll.cabal+9−14
@@ -40,12 +40,19 @@ Category: Web
40Cabal-Version: >= 1.8
40Cabal-Version: >= 1.8
41Build-Type: Simple
41Build-Type: Simple
42Data-Dir: data
42Data-Dir: data
43+
43Data-Files:
44Data-Files:
44 templates/atom.xml
45 templates/atom.xml
45 templates/atom-item.xml
46 templates/atom-item.xml
46 templates/rss.xml
47 templates/rss.xml
47 templates/rss-item.xml
48 templates/rss-item.xml
48
49
50+Extra-source-files:
51+ tests/data/example.md
52+ tests/data/example.md.metadata
53+ tests/data/example.md.out
54+ tests/data/template.html
55+
49Source-Repository head
56Source-Repository head
50 Type: git
57 Type: git
51 Location: git://github.com/jaspervdj/hakyll.git
58 Location: git://github.com/jaspervdj/hakyll.git
@@ -200,19 +207,7 @@ Test-suite hakyll-tests
200 unix >= 2.4 && < 2.7
207 unix >= 2.4 && < 2.7
201
208
202 Other-modules:
209 Other-modules:
203− Hakyll.Web.Util.Html.Tests
210+ Hakyll.Core.Dependencies.Tests
204− Hakyll.Web.Urls.Relativize.Tests
205− Hakyll.Web.Urls.Tests
211+ Hakyll.Core.Provider.Tests
206 Hakyll.Web.Template.Tests
212 Hakyll.Web.Template.Tests
207− Hakyll.Web.Page.Metadata.Tests
208− Hakyll.Web.Page.Tests
209− Hakyll.Core.Compiler.Tests
210− Hakyll.Core.Identifier.Tests
211− Hakyll.Core.Util.Arrow.Tests
212− Hakyll.Core.Util.String.Tests
213− Hakyll.Core.UnixFilter.Tests
214− Hakyll.Core.Routes.Tests
215− Hakyll.Core.Store.Tests
216− Hakyll.Core.Rules.Tests
217− Hakyll.Core.DependencyAnalyzer.Tests
218 TestSuite.Util
213 TestSuite.Util
src/Hakyll/Core/Compiler.hs+2−2
@@ -81,13 +81,13 @@ getResourceBody = getResourceWith resourceBody
81--------------------------------------------------------------------------------
81--------------------------------------------------------------------------------
82-- | Get the resource we are compiling as a string
82-- | Get the resource we are compiling as a string
83getResourceString :: Compiler (Item String)
83getResourceString :: Compiler (Item String)
84−getResourceString = getResourceWith $ const resourceString
84+getResourceString = getResourceWith resourceString
85
85
86
86
87--------------------------------------------------------------------------------
87--------------------------------------------------------------------------------
88-- | Get the resource we are compiling as a lazy bytestring
88-- | Get the resource we are compiling as a lazy bytestring
89getResourceLBS :: Compiler (Item ByteString)
89getResourceLBS :: Compiler (Item ByteString)
90−getResourceLBS = getResourceWith $ const resourceLBS
90+getResourceLBS = getResourceWith resourceLBS
91
91
92
92
93--------------------------------------------------------------------------------
93--------------------------------------------------------------------------------
src/Hakyll/Core/Provider/Internal.hs+23−15
@@ -7,32 +7,35 @@ module Hakyll.Core.Provider.Internal
7 , resourceExists
7 , resourceExists
8 , resourceMetadataResource
8 , resourceMetadataResource
9
9
10+ , resourceFilePath
10 , resourceString
11 , resourceString
11 , resourceLBS
12 , resourceLBS
12 ) where
13 ) where
13
14
14
15
15--------------------------------------------------------------------------------
16--------------------------------------------------------------------------------
16−import Control.Applicative ((<$>))
17+import Control.Applicative ((<$>))
17−import qualified Data.ByteString.Lazy as BL
18+import qualified Data.ByteString.Lazy as BL
18import Data.IORef
19import Data.IORef
19−import Data.Map (Map)
20+import Data.Map (Map)
20−import qualified Data.Map as M
21+import qualified Data.Map as M
21−import Data.Set (Set)
22+import Data.Set (Set)
22−import qualified Data.Set as S
23+import qualified Data.Set as S
23−import System.FilePath (addExtension)
24+import System.FilePath (addExtension, (</>))
24
25
25
26
26--------------------------------------------------------------------------------
27--------------------------------------------------------------------------------
28+import Hakyll.Core.Identifier
27import Hakyll.Core.Store
29import Hakyll.Core.Store
28import Hakyll.Core.Util.File
30import Hakyll.Core.Util.File
29−import Hakyll.Core.Identifier
30
31
31
32
32--------------------------------------------------------------------------------
33--------------------------------------------------------------------------------
33-- | Responsible for retrieving and listing resources
34-- | Responsible for retrieving and listing resources
34data Provider = Provider
35data Provider = Provider
36+ { -- Top of the provided directory
37+ providerDirectory :: FilePath
35− { -- | A list of all files found
38+ , -- | A list of all files found
36 providerSet :: Set Identifier
39 providerSet :: Set Identifier
37 , -- | Cache keeping track of modified files
40 , -- | Cache keeping track of modified files
38 providerModifiedCache :: IORef (Map Identifier Bool)
41 providerModifiedCache :: IORef (Map Identifier Bool)
@@ -49,9 +52,9 @@ newProvider :: Store -- ^ Store to use
49 -> IO Provider -- ^ Resulting provider
52 -> IO Provider -- ^ Resulting provider
50newProvider store ignore directory = do
53newProvider store ignore directory = do
51 list <- map fromFilePath . filter (not . ignore) <$>
54 list <- map fromFilePath . filter (not . ignore) <$>
52− getRecursiveContents False directory
55+ getRecursiveContents directory
53 cache <- newIORef M.empty
56 cache <- newIORef M.empty
54− return $ Provider (S.fromList list) cache store
57+ return $ Provider directory (S.fromList list) cache store
55
58
56
59
57--------------------------------------------------------------------------------
60--------------------------------------------------------------------------------
@@ -74,13 +77,18 @@ resourceMetadataResource =
74 fromFilePath . flip addExtension "metadata" . toFilePath
77 fromFilePath . flip addExtension "metadata" . toFilePath
75
78
76
79
80+--------------------------------------------------------------------------------
81+resourceFilePath :: Provider -> Identifier -> FilePath
82+resourceFilePath p i = providerDirectory p </> toFilePath i
83+
84+
77--------------------------------------------------------------------------------
85--------------------------------------------------------------------------------
78-- | Get the raw body of a resource as string
86-- | Get the raw body of a resource as string
79−resourceString :: Identifier -> IO String
87+resourceString :: Provider -> Identifier -> IO String
80−resourceString = readFile . toFilePath
88+resourceString p i = readFile $ resourceFilePath p i
81
89
82
90
83--------------------------------------------------------------------------------
91--------------------------------------------------------------------------------
84-- | Get the raw body of a resource of a lazy bytestring
92-- | Get the raw body of a resource of a lazy bytestring
85−resourceLBS :: Identifier -> IO BL.ByteString
93+resourceLBS :: Provider -> Identifier -> IO BL.ByteString
86−resourceLBS = BL.readFile . toFilePath
94+resourceLBS p i = BL.readFile $ resourceFilePath p i
src/Hakyll/Core/Provider/Metadata.hs+4−4
@@ -25,19 +25,19 @@ import Hakyll.Core.Util.String
25
25
26--------------------------------------------------------------------------------
26--------------------------------------------------------------------------------
27loadMetadata :: Provider -> Identifier -> IO (Metadata, Maybe String)
27loadMetadata :: Provider -> Identifier -> IO (Metadata, Maybe String)
28−loadMetadata rp identifier = do
28+loadMetadata p identifier = do
29 hasHeader <- probablyHasMetadataHeader fp
29 hasHeader <- probablyHasMetadataHeader fp
30 (md, body) <- if hasHeader
30 (md, body) <- if hasHeader
31 then second Just <$> loadMetadataHeader fp
31 then second Just <$> loadMetadataHeader fp
32 else return (M.empty, Nothing)
32 else return (M.empty, Nothing)
33
33
34− emd <- if resourceExists rp mi then loadMetadataFile mfp else return M.empty
34+ emd <- if resourceExists p mi then loadMetadataFile mfp else return M.empty
35
35
36 return (M.union md emd, body)
36 return (M.union md emd, body)
37 where
37 where
38− fp = toFilePath identifier
38+ fp = resourceFilePath p identifier
39 mi = resourceMetadataResource identifier
39 mi = resourceMetadataResource identifier
40− mfp = toFilePath mi
40+ mfp = resourceFilePath p mi
41
41
42
42
43--------------------------------------------------------------------------------
43--------------------------------------------------------------------------------
src/Hakyll/Core/Provider/MetadataCache.hs+1−1
@@ -29,7 +29,7 @@ resourceBody p r = do
29 load p r
29 load p r
30 Store.Found bd <- Store.get (providerStore p)
30 Store.Found bd <- Store.get (providerStore p)
31 [name, toFilePath r, "body"]
31 [name, toFilePath r, "body"]
32− maybe (resourceString r) return bd
32+ maybe (resourceString p r) return bd
33
33
34
34
35--------------------------------------------------------------------------------
35--------------------------------------------------------------------------------
src/Hakyll/Core/Provider/Modified.hs+12−9
@@ -15,6 +15,7 @@ import Data.IORef
15import qualified Data.Map as M
15import qualified Data.Map as M
16import Data.Time (UTCTime)
16import Data.Time (UTCTime)
17import System.Directory (getModificationTime)
17import System.Directory (getModificationTime)
18+import System.FilePath ((</>))
18
19
19
20
20--------------------------------------------------------------------------------
21--------------------------------------------------------------------------------
@@ -28,7 +29,7 @@ import qualified Hakyll.Core.Store as Store
28--------------------------------------------------------------------------------
29--------------------------------------------------------------------------------
29-- | A resource is modified if it or its metadata has changed
30-- | A resource is modified if it or its metadata has changed
30resourceModified :: Provider -> Identifier -> IO Bool
31resourceModified :: Provider -> Identifier -> IO Bool
31−resourceModified rp r
32+resourceModified p r
32 | not exists = return False
33 | not exists = return False
33 | otherwise = do
34 | otherwise = do
34 cache <- readIORef cacheRef
35 cache <- readIORef cacheRef
@@ -38,19 +39,20 @@ resourceModified rp r
38 -- Check if the actual file was modified, and do a recursive
39 -- Check if the actual file was modified, and do a recursive
39 -- call to check if the metadata file was modified
40 -- call to check if the metadata file was modified
40 m <- (||)
41 m <- (||)
41− <$> fileDigestModified store (toFilePath r)
42+ <$> fileDigestModified store filePath
42− <*> resourceModified rp (resourceMetadataResource r)
43+ <*> resourceModified p (resourceMetadataResource r)
43 modifyIORef cacheRef (M.insert normalized m)
44 modifyIORef cacheRef (M.insert normalized m)
44
45
45 -- Important! (But ugly)
46 -- Important! (But ugly)
46− when m $ resourceInvalidateMetadataCache rp r
47+ when m $ resourceInvalidateMetadataCache p r
47
48
48 return m
49 return m
49 where
50 where
50 normalized = setVersion Nothing r
51 normalized = setVersion Nothing r
51− exists = resourceExists rp r
52+ exists = resourceExists p r
52− store = providerStore rp
53+ store = providerStore p
53− cacheRef = providerModifiedCache rp
54+ cacheRef = providerModifiedCache p
55+ filePath = resourceFilePath p r
54
56
55
57
56--------------------------------------------------------------------------------
58--------------------------------------------------------------------------------
@@ -79,5 +81,6 @@ fileDigest = fmap MD5.hashlazy . BL.readFile
79
81
80
82
81--------------------------------------------------------------------------------
83--------------------------------------------------------------------------------
82−resourceModificationTime :: Identifier -> IO UTCTime
84+resourceModificationTime :: Provider -> Identifier -> IO UTCTime
83−resourceModificationTime = getModificationTime . toFilePath
85+resourceModificationTime p i =
86+ getModificationTime $ providerDirectory p </> toFilePath i
src/Hakyll/Core/Util/File.hs+36−28
@@ -1,52 +1,60 @@
1+--------------------------------------------------------------------------------
1-- | A module containing various file utility functions
2-- | A module containing various file utility functions
2−--
3module Hakyll.Core.Util.File
3module Hakyll.Core.Util.File
4 ( makeDirectories
4 ( makeDirectories
5 , getRecursiveContents
5 , getRecursiveContents
6 , isFileInternal
6 , isFileInternal
7 ) where
7 ) where
8
8
9−import Control.Applicative ((<$>))
10−import Control.Monad (forM)
11−import Data.List (isPrefixOf)
12−import System.Directory ( createDirectoryIfMissing, doesDirectoryExist
13− , getDirectoryContents
14− )
15−import System.FilePath ( normalise, takeDirectory, splitPath
16− , dropTrailingPathSeparator, (</>)
17− )
18
9
19−import Hakyll.Core.Configuration
10+--------------------------------------------------------------------------------
11+import Control.Applicative ((<$>))
12+import Control.Monad (forM)
13+import Data.List (isPrefixOf)
14+import System.Directory (createDirectoryIfMissing,
15+ doesDirectoryExist,
16+ getDirectoryContents)
17+import System.FilePath (dropTrailingPathSeparator,
18+ splitPath, takeDirectory, (</>))
20
19
20+
21+--------------------------------------------------------------------------------
22+import Hakyll.Core.Configuration
23+
24+
25+--------------------------------------------------------------------------------
21-- | Given a path to a file, try to make the path writable by making
26-- | Given a path to a file, try to make the path writable by making
22-- all directories on the path.
27-- all directories on the path.
23−--
24makeDirectories :: FilePath -> IO ()
28makeDirectories :: FilePath -> IO ()
25makeDirectories = createDirectoryIfMissing True . takeDirectory
29makeDirectories = createDirectoryIfMissing True . takeDirectory
26
30
31+
32+--------------------------------------------------------------------------------
27-- | Get all contents of a directory.
33-- | Get all contents of a directory.
28−getRecursiveContents :: Bool -- ^ Include directories?
29− -> FilePath -- ^ Directory to search
34+getRecursiveContents :: FilePath -- ^ Directory to search
30 -> IO [FilePath] -- ^ List of files found
35 -> IO [FilePath] -- ^ List of files found
31−getRecursiveContents includeDirs topdir = do
36+getRecursiveContents top = go ""
32− topdirExists <- doesDirectoryExist topdir
33− if not topdirExists
34− then return []
35− else do
36− names <- filter isProper <$> getDirectoryContents topdir
37− paths <- forM names $ \name -> do
38− let path = normalise $ topdir </> name
39− isDirectory <- doesDirectoryExist path
40− if isDirectory then getRecursiveContents includeDirs path
41− else return [path]
42− return $ if includeDirs then topdir : concat paths
43− else concat paths
44 where
37 where
45 isProper = (`notElem` [".", ".."])
38 isProper = (`notElem` [".", ".."])
39+ go dir = do
40+ dirExists <- doesDirectoryExist (top </> dir)
41+ if not dirExists
42+ then return []
43+ else do
44+ names <- filter isProper <$> getDirectoryContents (top </> dir)
45+ paths <- forM names $ \name -> do
46+ let rel = dir </> name
47+ isDirectory <- doesDirectoryExist (top </> rel)
48+ if isDirectory
49+ then go rel
50+ else return [rel]
51+
52+ return $ concat paths
53+
46
54
55+--------------------------------------------------------------------------------
47-- | Check if a file is meant for Hakyll internal use, i.e. if it is located in
56-- | Check if a file is meant for Hakyll internal use, i.e. if it is located in
48-- the destination or store directory
57-- the destination or store directory
49−--
50isFileInternal :: Configuration -- ^ Configuration
58isFileInternal :: Configuration -- ^ Configuration
51 -> FilePath -- ^ File to check
59 -> FilePath -- ^ File to check
52 -> Bool -- ^ If the given file is internal
60 -> Bool -- ^ If the given file is internal
src/Hakyll/Web/Template/Context.hs+3−1
@@ -195,7 +195,9 @@ modificationTimeFieldWith :: TimeLocale -- ^ Time output locale
195 -> String -- ^ Format
195 -> String -- ^ Format
196 -> Context a -- ^ Resulting context
196 -> Context a -- ^ Resulting context
197modificationTimeFieldWith locale key fmt = field key $ \i -> do
197modificationTimeFieldWith locale key fmt = field key $ \i -> do
198+ provider <- compilerProvider <$> compilerAsk
199+ mtime <- compilerUnsafeIO $
198− mtime <- compilerUnsafeIO $ resourceModificationTime $ itemIdentifier i
200+ resourceModificationTime provider $ itemIdentifier i
199 return $ formatTime locale fmt mtime
201 return $ formatTime locale fmt mtime
200
202
201
203
tests/Hakyll/Core/Dependencies/Tests.hs+3−3
@@ -41,9 +41,9 @@ oldFacts = M.fromList
41 , ("posts/02.md",
41 , ("posts/02.md",
42 [])
42 [])
43 , ("index.md",
43 , ("index.md",
44− [ Pattern "posts/*" ["posts/01.md", "posts/02.md"]
44+ [ PatternDependency "posts/*" ["posts/01.md", "posts/02.md"]
45− , Identifier "posts/01.md"
45+ , IdentifierDependency "posts/01.md"
46− , Identifier "posts/02.md"
46+ , IdentifierDependency "posts/02.md"
47 ])
47 ])
48 ]
48 ]
49
49
tests/Hakyll/Core/Provider/Tests.hsnew file+35
@@ -0,0 +1,35 @@
1+--------------------------------------------------------------------------------
2+{-# LANGUAGE OverloadedStrings #-}
3+module Hakyll.Core.Provider.Tests
4+ ( tests
5+ ) where
6+
7+
8+--------------------------------------------------------------------------------
9+import qualified Data.Map as M
10+import Test.Framework (Test, testGroup)
11+import Test.Framework.Providers.HUnit (testCase)
12+import Test.HUnit (Assertion, assert, (@=?))
13+
14+
15+--------------------------------------------------------------------------------
16+import Hakyll.Core.Provider
17+import TestSuite.Util
18+
19+
20+--------------------------------------------------------------------------------
21+tests :: Test
22+tests = testGroup "Hakyll.Core.Provider.Tests"
23+ [ testCase "case01" case01
24+ ]
25+
26+
27+--------------------------------------------------------------------------------
28+case01 :: Assertion
29+case01 = withTestStore $ \store -> do
30+ provider <- newTestProvider store
31+ assert $ resourceExists provider "example.md"
32+
33+ metadata <- resourceMetadata provider "example.md"
34+ Just "An example" @=? M.lookup "title" metadata
35+ Just "External data" @=? M.lookup "external" metadata
tests/Hakyll/Web/Template/Tests.hs+33−48
@@ -1,55 +1,40 @@
1+--------------------------------------------------------------------------------
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
2module Hakyll.Web.Template.Tests
3module Hakyll.Web.Template.Tests
3 ( tests
4 ( tests
4 ) where
5 ) where
5
6
7+
8+--------------------------------------------------------------------------------
6−import Test.Framework
9+import Test.Framework (Test, testGroup)
7−import Test.HUnit hiding (Test)
10+import Test.Framework.Providers.HUnit (testCase)
8−
11+import Test.HUnit (Assertion, (@=?))
9−import qualified Data.Map as M
12+
10−
13+
14+--------------------------------------------------------------------------------
15+import Hakyll.Core.Item
16+import Hakyll.Core.Provider
11−import Hakyll.Web.Page
17+import Hakyll.Web.Page
12−import Hakyll.Web.Template
18+import Hakyll.Web.Template
13−import Hakyll.Web.Template.Read
19+import Hakyll.Web.Template.Context
14−import TestSuite.Util
20+import TestSuite.Util
15−
21+
22+
23+--------------------------------------------------------------------------------
16−tests :: [Test]
24+tests :: Test
17−tests = fromAssertions "applyTemplate"
25+tests = testGroup "Hakyll.Core.Template.Tests"
18− -- Hakyll templates
26+ [ testCase "case01" case01
19− [ applyTemplateAssertion readTemplate applyTemplate
20− ("bar" @=?) "$foo$" [("foo", "bar")]
21−
22− , applyTemplateAssertion readTemplate applyTemplate
23− ("$ barqux" @=?) "$$ $foo$$bar$" [("foo", "bar"), ("bar", "qux")]
24−
25− , applyTemplateAssertion readTemplate applyTemplate
26− ("$foo$" @=?) "$foo$" []
27−
28− -- Hamlet templates
29− , applyTemplateAssertion readHamletTemplate applyTemplate
30− (("<head><title>notice</title></head><body>A paragraph</body>" @=?) .
31− filter (/= '\n'))
32− "<head>\n\
33− \ <title>#{title}\n\
34− \<body>\n\
35− \ A paragraph\n"
36− [("title", "notice")]
37−
38− -- Missing keys
39− , let missing "foo" = "bar"
40− missing "bar" = "qux"
41− missing x = reverse x
42− in applyTemplateAssertion readTemplate (applyTemplateWith missing)
43− ("bar foo ver" @=?) "$foo$ $bar$ $rev$" [("bar", "foo")]
44 ]
27 ]
45
28
46−-- | Utility function to create quick template tests
29+
47−--
30+--------------------------------------------------------------------------------
48−applyTemplateAssertion :: (String -> Template)
49− -> (Template -> Page String -> Page String)
50− -> (String -> Assertion)
31+case01 :: Assertion
51− -> String
32+case01 = withTestStore $ \store -> do
52− -> [(String, String)]
33+ provider <- newTestProvider store
53− -> Assertion
34+
54−applyTemplateAssertion parser apply correct template page =
35+ out <- resourceString provider "example.md.out"
55− correct $ pageBody (apply (parser template) (fromMap $ M.fromList page))
36+ tpl <- testCompilerDone store provider "template.html" $ templateCompiler
37+ item <- testCompilerDone store provider "example.md" $
38+ pageCompiler >>= applyTemplate (itemBody tpl) defaultContext
39+
40+ out @=? itemBody item
tests/TestSuite.hs+16−46
@@ -1,53 +1,23 @@
1+--------------------------------------------------------------------------------
1−module Main where
2+module Main
3+ ( main
4+ ) where
2
5
3−import Test.Framework (defaultMain, testGroup)
4
6
5−import qualified Hakyll.Core.Compiler.Tests
7+--------------------------------------------------------------------------------
6−import qualified Hakyll.Core.DependencyAnalyzer.Tests
8+import Test.Framework (defaultMain)
7−import qualified Hakyll.Core.Identifier.Tests
9+
8−import qualified Hakyll.Core.Routes.Tests
10+
9−import qualified Hakyll.Core.Rules.Tests
11+--------------------------------------------------------------------------------
10−import qualified Hakyll.Core.Store.Tests
11−import qualified Hakyll.Core.UnixFilter.Tests
12−import qualified Hakyll.Core.Util.Arrow.Tests
12+import qualified Hakyll.Core.Dependencies.Tests
13−import qualified Hakyll.Core.Util.String.Tests
13+import qualified Hakyll.Core.Provider.Tests
14−import qualified Hakyll.Web.Page.Tests
15−import qualified Hakyll.Web.Page.Metadata.Tests
16import qualified Hakyll.Web.Template.Tests
14import qualified Hakyll.Web.Template.Tests
17−import qualified Hakyll.Web.Urls.Tests
18−import qualified Hakyll.Web.Urls.Relativize.Tests
19−import qualified Hakyll.Web.Util.Html.Tests
20
15
16+
17+--------------------------------------------------------------------------------
21main :: IO ()
18main :: IO ()
22main = defaultMain
19main = defaultMain
23− [ testGroup "Hakyll.Core.Compiler.Tests"
20+ [ Hakyll.Core.Dependencies.Tests.tests
24− Hakyll.Core.Compiler.Tests.tests
21+ , Hakyll.Core.Provider.Tests.tests
25− , testGroup "Hakyll.Core.DependencyAnalyzer.Tests"
22+ , Hakyll.Web.Template.Tests.tests
26− Hakyll.Core.DependencyAnalyzer.Tests.tests
27− , testGroup "Hakyll.Core.Identifier.Tests"
28− Hakyll.Core.Identifier.Tests.tests
29− , testGroup "Hakyll.Core.Routes.Tests"
30− Hakyll.Core.Routes.Tests.tests
31− , testGroup "Hakyll.Core.Rules.Tests"
32− Hakyll.Core.Rules.Tests.tests
33− , testGroup "Hakyll.Core.Store.Tests"
34− Hakyll.Core.Store.Tests.tests
35− , testGroup "Hakyll.Core.UnixFilter.Tests"
36− Hakyll.Core.UnixFilter.Tests.tests
37− , testGroup "Hakyll.Core.Util.Arrow.Tests"
38− Hakyll.Core.Util.Arrow.Tests.tests
39− , testGroup "Hakyll.Core.Util.String.Tests"
40− Hakyll.Core.Util.String.Tests.tests
41− , testGroup "Hakyll.Web.Page.Tests"
42− Hakyll.Web.Page.Tests.tests
43− , testGroup "Hakyll.Web.Page.Metadata.Tests"
44− Hakyll.Web.Page.Metadata.Tests.tests
45− , testGroup "Hakyll.Web.Template.Tests"
46− Hakyll.Web.Template.Tests.tests
47− , testGroup "Hakyll.Web.Urls.Tests"
48− Hakyll.Web.Urls.Tests.tests
49− , testGroup "Hakyll.Web.Urls.Relativize.Tests"
50− Hakyll.Web.Urls.Relativize.Tests.tests
51− , testGroup "Hakyll.Web.Util.Html.Tests"
52− Hakyll.Web.Util.Html.Tests.tests
53 ]
23 ]
tests/TestSuite/Util.hs+67−30
@@ -1,24 +1,32 @@
1+--------------------------------------------------------------------------------
1-- | Test utilities
2-- | Test utilities
2−--
3module TestSuite.Util
3module TestSuite.Util
4 ( fromAssertions
4 ( fromAssertions
5− , makeStoreTest
5+ , withTestStore
6− , runCompilerJobTest
6+ , newTestProvider
7+ , testCompiler
8+ , testCompilerDone
7 ) where
9 ) where
8
10
9−import Data.Monoid (mempty)
10
11
12+--------------------------------------------------------------------------------
13+import Data.Monoid (mempty)
14+import System.Directory (removeDirectoryRecursive)
11−import Test.Framework
15+import Test.Framework
12−import Test.Framework.Providers.HUnit
16+import Test.Framework.Providers.HUnit
13−import Test.HUnit hiding (Test)
17+import Test.HUnit hiding (Test)
14
18
15−import Hakyll.Core.Compiler.Internal
16−import Hakyll.Core.Identifier
17−import Hakyll.Core.Logger
18−import Hakyll.Core.Resource.Provider
19−import Hakyll.Core.Store (Store)
20−import qualified Hakyll.Core.Store as Store
21
19
20+--------------------------------------------------------------------------------
21+import Hakyll.Core.Compiler.Internal
22+import Hakyll.Core.Identifier
23+import qualified Hakyll.Core.Logger as Logger
24+import Hakyll.Core.Provider
25+import Hakyll.Core.Store (Store)
26+import qualified Hakyll.Core.Store as Store
27+
28+
29+--------------------------------------------------------------------------------
22fromAssertions :: String -- ^ Name
30fromAssertions :: String -- ^ Name
23 -> [Assertion] -- ^ Cases
31 -> [Assertion] -- ^ Cases
24 -> [Test] -- ^ Result tests
32 -> [Test] -- ^ Result tests
@@ -26,20 +34,49 @@ fromAssertions name = zipWith testCase names
26 where
34 where
27 names = map (\n -> name ++ " [" ++ show n ++ "]") [1 :: Int ..]
35 names = map (\n -> name ++ " [" ++ show n ++ "]") [1 :: Int ..]
28
36
29−-- | Create a store for testing
37+
30−--
38+--------------------------------------------------------------------------------
31−makeStoreTest :: IO Store
39+withTestStore :: (Store -> IO a) -> IO a
40+withTestStore f = do
32−makeStoreTest = Store.new True "_store"
41+ store <- Store.new True "_teststore"
33−
42+ result <- f store
34−-- | Testing for 'runCompilerJob'
43+ removeDirectoryRecursive "_teststore"
35−--
44+ return result
36−runCompilerJobTest :: Compiler () a
45+
37− -> Identifier ()
46+
38− -> ResourceProvider
47+--------------------------------------------------------------------------------
39− -> [Identifier ()]
48+newTestProvider :: Store -> IO Provider
40− -> IO a
49+newTestProvider store = newProvider store (const False) "tests/data"
41−runCompilerJobTest compiler id' provider uni = do
50+
42− store <- makeStoreTest
51+
43− logger <- makeLogger $ const $ return ()
52+--------------------------------------------------------------------------------
44− Right x <- runCompilerJob compiler id' provider uni mempty store True logger
53+testCompiler :: Store -> Provider -> Identifier -> Compiler a
54+ -> IO (CompilerResult a)
55+testCompiler store provider underlying compiler = do
56+ logger <- Logger.new Logger.Debug (\_ -> return ())
57+ let read' = CompilerRead
58+ { compilerUnderlying = underlying
59+ , compilerProvider = provider
60+ , compilerUniverse = []
61+ , compilerRoutes = mempty
62+ , compilerStore = store
63+ , compilerLogger = logger
64+ }
65+
66+ result <- runCompiler compiler read'
67+ Logger.flush logger
45− return x
68+ return result
69+
70+
71+--------------------------------------------------------------------------------
72+testCompilerDone :: Store -> Provider -> Identifier -> Compiler a -> IO a
73+testCompilerDone store provider underlying compiler = do
74+ result <- testCompiler store provider underlying compiler
75+ case result of
76+ CompilerDone x _ -> return x
77+ CompilerError e -> error $
78+ "TestSuite.Util.testCompilerDone: compiler " ++ show underlying ++
79+ " threw: " ++ e
80+ CompilerRequire i _ -> error $
81+ "TestSuite.Util.testCompilerDone: compiler " ++ show underlying ++
82+ " requires: " ++ show i
tests/data/example.mdnew file+5
@@ -0,0 +1,5 @@
1+---
2+title: An example
3+---
4+
5+This is an example.
tests/data/example.md.metadatanew file+1
@@ -0,0 +1 @@
1+external: External data
tests/data/example.md.outnew file+3
@@ -0,0 +1,3 @@
1+<div>
2+ <p>This is an example.</p>
3+</div>
tests/data/template.htmlnew file+3
@@ -0,0 +1,3 @@
1+<div>
2+ $body$
3+</div>