on master help sign in
Add TmpFile utilities
bbc2631Jasper Van der Jeugt
hakyll.cabal+3−1
@@ -106,6 +106,7 @@ Library
106 pandoc >= 1.9.3 && < 1.10,
106 pandoc >= 1.9.3 && < 1.10,
107 parsec >= 3.0 && < 3.2,
107 parsec >= 3.0 && < 3.2,
108 process >= 1.0 && < 1.2,
108 process >= 1.0 && < 1.2,
109+ random >= 1.0 && < 1.1,
109 regex-base >= 0.93 && < 0.94,
110 regex-base >= 0.93 && < 0.94,
110 regex-tdfa >= 1.1 && < 1.2,
111 regex-tdfa >= 1.1 && < 1.2,
111 tagsoup >= 0.12.6 && < 0.13,
112 tagsoup >= 0.12.6 && < 0.13,
@@ -117,6 +118,7 @@ Library
117 Hakyll.Core.Compiler
118 Hakyll.Core.Compiler
118 Hakyll.Core.Configuration
119 Hakyll.Core.Configuration
119 Hakyll.Core.Dependencies
120 Hakyll.Core.Dependencies
121+ Hakyll.Core.File
120 Hakyll.Core.Identifier
122 Hakyll.Core.Identifier
121 Hakyll.Core.Identifier.Pattern
123 Hakyll.Core.Identifier.Pattern
122 Hakyll.Core.Item
124 Hakyll.Core.Item
@@ -125,7 +127,6 @@ Library
125 Hakyll.Core.Rules
127 Hakyll.Core.Rules
126 Hakyll.Core.Util.String
128 Hakyll.Core.Util.String
127 Hakyll.Core.Writable
129 Hakyll.Core.Writable
128− Hakyll.Core.Writable.CopyFile
129 Hakyll.Main
130 Hakyll.Main
130 Hakyll.Web.CompressCss
131 Hakyll.Web.CompressCss
131 Hakyll.Web.Feed
132 Hakyll.Web.Feed
@@ -211,6 +212,7 @@ Test-suite hakyll-tests
211 pandoc >= 1.9.3 && < 1.10,
212 pandoc >= 1.9.3 && < 1.10,
212 parsec >= 3.0 && < 3.2,
213 parsec >= 3.0 && < 3.2,
213 process >= 1.0 && < 1.2,
214 process >= 1.0 && < 1.2,
215+ random >= 1.0 && < 1.1,
214 regex-base >= 0.93 && < 0.94,
216 regex-base >= 0.93 && < 0.94,
215 regex-tdfa >= 1.1 && < 1.2,
217 regex-tdfa >= 1.1 && < 1.2,
216 tagsoup >= 0.12.6 && < 0.13,
218 tagsoup >= 0.12.6 && < 0.13,
src/Hakyll.hs+2−2
@@ -4,6 +4,7 @@
4module Hakyll
4module Hakyll
5 ( module Hakyll.Core.Compiler
5 ( module Hakyll.Core.Compiler
6 , module Hakyll.Core.Configuration
6 , module Hakyll.Core.Configuration
7+ , module Hakyll.Core.File
7 , module Hakyll.Core.Identifier
8 , module Hakyll.Core.Identifier
8 , module Hakyll.Core.Identifier.Pattern
9 , module Hakyll.Core.Identifier.Pattern
9 , module Hakyll.Core.Item
10 , module Hakyll.Core.Item
@@ -16,7 +17,6 @@ module Hakyll
16 , module Hakyll.Core.Util.File
17 , module Hakyll.Core.Util.File
17 , module Hakyll.Core.Util.String
18 , module Hakyll.Core.Util.String
18 , module Hakyll.Core.Writable
19 , module Hakyll.Core.Writable
19− , module Hakyll.Core.Writable.CopyFile
20 , module Hakyll.Main
20 , module Hakyll.Main
21 , module Hakyll.Web.CompressCss
21 , module Hakyll.Web.CompressCss
22 , module Hakyll.Web.Feed
22 , module Hakyll.Web.Feed
@@ -36,6 +36,7 @@ module Hakyll
36--------------------------------------------------------------------------------
36--------------------------------------------------------------------------------
37import Hakyll.Core.Compiler
37import Hakyll.Core.Compiler
38import Hakyll.Core.Configuration
38import Hakyll.Core.Configuration
39+import Hakyll.Core.File
39import Hakyll.Core.Identifier
40import Hakyll.Core.Identifier
40import Hakyll.Core.Identifier.Pattern
41import Hakyll.Core.Identifier.Pattern
41import Hakyll.Core.Item
42import Hakyll.Core.Item
@@ -48,7 +49,6 @@ import Hakyll.Core.UnixFilter
48import Hakyll.Core.Util.File
49import Hakyll.Core.Util.File
49import Hakyll.Core.Util.String
50import Hakyll.Core.Util.String
50import Hakyll.Core.Writable
51import Hakyll.Core.Writable
51−import Hakyll.Core.Writable.CopyFile
52import Hakyll.Main
52import Hakyll.Main
53import Hakyll.Web.CompressCss
53import Hakyll.Web.CompressCss
54import Hakyll.Web.Feed
54import Hakyll.Web.Feed
src/Hakyll/Commands.hs+3−5
@@ -13,9 +13,6 @@ module Hakyll.Commands
13
13
14
14
15--------------------------------------------------------------------------------
15--------------------------------------------------------------------------------
16−import Control.Monad (when)
17−import System.Directory (doesDirectoryExist,
18− removeDirectoryRecursive)
19import System.Exit (exitWith)
16import System.Exit (exitWith)
20import System.Process (system)
17import System.Process (system)
21
18
@@ -26,6 +23,7 @@ import Hakyll.Core.Configuration
26import Hakyll.Core.Logger (Verbosity)
23import Hakyll.Core.Logger (Verbosity)
27import Hakyll.Core.Rules
24import Hakyll.Core.Rules
28import Hakyll.Core.Runtime
25import Hakyll.Core.Runtime
26+import Hakyll.Core.Util.File
29
27
30
28
31--------------------------------------------------------------------------------
29--------------------------------------------------------------------------------
@@ -59,11 +57,11 @@ clean :: Configuration -> IO ()
59clean conf = do
57clean conf = do
60 remove $ destinationDirectory conf
58 remove $ destinationDirectory conf
61 remove $ storeDirectory conf
59 remove $ storeDirectory conf
60+ remove $ tmpDirectory conf
62 where
61 where
63 remove dir = do
62 remove dir = do
64 putStrLn $ "Removing " ++ dir ++ "..."
63 putStrLn $ "Removing " ++ dir ++ "..."
65− exists <- doesDirectoryExist dir
64+ removeDirectory dir
66− when exists $ removeDirectoryRecursive dir
67
65
68
66
69--------------------------------------------------------------------------------
67--------------------------------------------------------------------------------
src/Hakyll/Core/Compiler/Internal.hs+4−1
@@ -34,6 +34,7 @@ import qualified Data.Set as S
34
34
35
35
36--------------------------------------------------------------------------------
36--------------------------------------------------------------------------------
37+import Hakyll.Core.Configuration
37import Hakyll.Core.Dependencies
38import Hakyll.Core.Dependencies
38import Hakyll.Core.Identifier
39import Hakyll.Core.Identifier
39import Hakyll.Core.Identifier.Pattern
40import Hakyll.Core.Identifier.Pattern
@@ -47,7 +48,9 @@ import Hakyll.Core.Store
47--------------------------------------------------------------------------------
48--------------------------------------------------------------------------------
48-- | Environment in which a compiler runs
49-- | Environment in which a compiler runs
49data CompilerRead = CompilerRead
50data CompilerRead = CompilerRead
51+ { -- | Main configuration
52+ compilerConfig :: Configuration
50− { -- | Underlying identifier
53+ , -- | Underlying identifier
51 compilerUnderlying :: Identifier
54 compilerUnderlying :: Identifier
52 , -- | Resource provider
55 , -- | Resource provider
53 compilerProvider :: Provider
56 compilerProvider :: Provider
src/Hakyll/Core/Configuration.hs+4
@@ -18,6 +18,8 @@ data Configuration = Configuration
18 destinationDirectory :: FilePath
18 destinationDirectory :: FilePath
19 , -- | Directory where hakyll's internal store is kept
19 , -- | Directory where hakyll's internal store is kept
20 storeDirectory :: FilePath
20 storeDirectory :: FilePath
21+ , -- | Directory in which some temporary files will be kept
22+ tmpDirectory :: FilePath
21 , -- | Directory where hakyll finds the files to compile. This is @.@ by
23 , -- | Directory where hakyll finds the files to compile. This is @.@ by
22 -- default.
24 -- default.
23 providerDirectory :: FilePath
25 providerDirectory :: FilePath
@@ -61,6 +63,7 @@ defaultConfiguration :: Configuration
61defaultConfiguration = Configuration
63defaultConfiguration = Configuration
62 { destinationDirectory = "_site"
64 { destinationDirectory = "_site"
63 , storeDirectory = "_cache"
65 , storeDirectory = "_cache"
66+ , tmpDirectory = "_cache/tmp"
64 , providerDirectory = "."
67 , providerDirectory = "."
65 , ignoreFile = ignoreFile'
68 , ignoreFile = ignoreFile'
66 , deployCommand = "echo 'No deploy command specified'"
69 , deployCommand = "echo 'No deploy command specified'"
@@ -83,6 +86,7 @@ shouldIgnoreFile :: Configuration -> FilePath -> Bool
83shouldIgnoreFile conf path =
86shouldIgnoreFile conf path =
84 destinationDirectory conf `isPrefixOf` path' ||
87 destinationDirectory conf `isPrefixOf` path' ||
85 storeDirectory conf `isPrefixOf` path' ||
88 storeDirectory conf `isPrefixOf` path' ||
89+ tmpDirectory conf `isPrefixOf` path' ||
86 ignoreFile conf path'
90 ignoreFile conf path'
87 where
91 where
88 path' = normalise path
92 path' = normalise path
src/Hakyll/Core/File.hsnew file+89
@@ -0,0 +1,89 @@
1+--------------------------------------------------------------------------------
2+-- | Exports simple compilers to just copy files
3+{-# LANGUAGE DeriveDataTypeable #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+module Hakyll.Core.File
6+ ( CopyFile (..)
7+ , copyFileCompiler
8+ , TmpFile (..)
9+ , newTmpFile
10+ ) where
11+
12+
13+--------------------------------------------------------------------------------
14+import Control.Applicative ((<$>))
15+import Data.Binary (Binary (..))
16+import Data.Typeable (Typeable)
17+import System.Directory (copyFile, doesFileExist,
18+ renameFile)
19+import System.FilePath ((</>))
20+import System.Random (randomIO)
21+
22+
23+--------------------------------------------------------------------------------
24+import Hakyll.Core.Compiler
25+import Hakyll.Core.Compiler.Internal
26+import Hakyll.Core.Configuration
27+import Hakyll.Core.Identifier
28+import Hakyll.Core.Item
29+import qualified Hakyll.Core.Store as Store
30+import Hakyll.Core.Util.File
31+import Hakyll.Core.Writable
32+
33+
34+--------------------------------------------------------------------------------
35+-- | This will copy any file directly by using a system call
36+data CopyFile = CopyFile
37+ deriving (Show, Eq, Ord, Typeable)
38+
39+
40+--------------------------------------------------------------------------------
41+instance Binary CopyFile where
42+ put CopyFile = return ()
43+ get = return CopyFile
44+
45+
46+--------------------------------------------------------------------------------
47+instance Writable CopyFile where
48+ write dst item = copyFile (toFilePath $ itemIdentifier item) dst
49+
50+
51+--------------------------------------------------------------------------------
52+copyFileCompiler :: Compiler (Item CopyFile)
53+copyFileCompiler = makeItem CopyFile
54+
55+
56+--------------------------------------------------------------------------------
57+newtype TmpFile = TmpFile FilePath
58+ deriving (Typeable)
59+
60+
61+--------------------------------------------------------------------------------
62+instance Binary TmpFile where
63+ put _ = return ()
64+ get = error $
65+ "Hakyll.Core.File.TmpFile: You tried to load a TmpFile, however, " ++
66+ "this is not possible since these are deleted as soon as possible."
67+
68+
69+--------------------------------------------------------------------------------
70+instance Writable TmpFile where
71+ write dst (Item _ (TmpFile fp)) = renameFile fp dst
72+
73+
74+--------------------------------------------------------------------------------
75+-- | Create a tmp file
76+newTmpFile :: String -- ^ Suffix and extension
77+ -> Compiler TmpFile -- ^ Resulting tmp path
78+newTmpFile suffix = do
79+ path <- mkPath
80+ compilerUnsafeIO $ makeDirectories path
81+ debugCompiler $ "newTmpFile " ++ path
82+ return $ TmpFile path
83+ where
84+ mkPath = do
85+ rand <- compilerUnsafeIO $ randomIO :: Compiler Int
86+ tmp <- tmpDirectory . compilerConfig <$> compilerAsk
87+ let path = tmp </> Store.hash [show rand] ++ "-" ++ suffix
88+ exists <- compilerUnsafeIO $ doesFileExist path
89+ if exists then mkPath else return path
src/Hakyll/Core/Runtime.hs+6−1
@@ -85,6 +85,10 @@ run config verbosity rules = do
85
85
86 Right (_, s, _) -> do
86 Right (_, s, _) -> do
87 Store.set store factsKey $ runtimeFacts s
87 Store.set store factsKey $ runtimeFacts s
88+
89+ Logger.debug logger "Removing tmp directory..."
90+ removeDirectory $ tmpDirectory config
91+
88 Logger.flush logger
92 Logger.flush logger
89 return ruleSet
93 return ruleSet
90 where
94 where
@@ -180,7 +184,8 @@ chase trail id'
180
184
181 let compiler = todo M.! id'
185 let compiler = todo M.! id'
182 read' = CompilerRead
186 read' = CompilerRead
187+ { compilerConfig = config
183− { compilerUnderlying = id'
188+ , compilerUnderlying = id'
184 , compilerProvider = provider
189 , compilerProvider = provider
185 , compilerUniverse = M.keysSet universe
190 , compilerUniverse = M.keysSet universe
186 , compilerRoutes = routes
191 , compilerRoutes = routes
src/Hakyll/Core/Store.hs+8−6
@@ -9,6 +9,7 @@ module Hakyll.Core.Store
9 , set
9 , set
10 , get
10 , get
11 , delete
11 , delete
12+ , hash
12 ) where
13 ) where
13
14
14
15
@@ -143,13 +144,14 @@ delete store identifier = do
143 key = hash identifier
144 key = hash identifier
144
145
145
146
146−--------------------------------------------------------------------------------
147−hash :: [String] -> String
148−hash = concatMap (printf "%02x") . B.unpack .
149− MD5.hash . T.encodeUtf8 . T.pack . intercalate "/"
150−
151−
152--------------------------------------------------------------------------------
147--------------------------------------------------------------------------------
153-- | Delete a file unless it doesn't exist...
148-- | Delete a file unless it doesn't exist...
154deleteFile :: FilePath -> IO ()
149deleteFile :: FilePath -> IO ()
155deleteFile = handle (\(_ :: IOException) -> return ()) . removeFile
150deleteFile = handle (\(_ :: IOException) -> return ()) . removeFile
151+
152+
153+--------------------------------------------------------------------------------
154+-- | Mostly meant for internal usage
155+hash :: [String] -> String
156+hash = concatMap (printf "%02x") . B.unpack .
157+ MD5.hash . T.encodeUtf8 . T.pack . intercalate "/"
src/Hakyll/Core/Util/File.hs+11−2
@@ -3,14 +3,16 @@
3module Hakyll.Core.Util.File
3module Hakyll.Core.Util.File
4 ( makeDirectories
4 ( makeDirectories
5 , getRecursiveContents
5 , getRecursiveContents
6+ , removeDirectory
6 ) where
7 ) where
7
8
8
9
9--------------------------------------------------------------------------------
10--------------------------------------------------------------------------------
10import Control.Applicative ((<$>))
11import Control.Applicative ((<$>))
11−import Control.Monad (forM)
12+import Control.Monad (forM, when)
12import System.Directory (createDirectoryIfMissing,
13import System.Directory (createDirectoryIfMissing,
13− doesDirectoryExist, getDirectoryContents)
14+ doesDirectoryExist, getDirectoryContents,
15+ removeDirectoryRecursive)
14import System.FilePath (takeDirectory, (</>))
16import System.FilePath (takeDirectory, (</>))
15
17
16
18
@@ -42,3 +44,10 @@ getRecursiveContents top = go ""
42 else return [rel]
44 else return [rel]
43
45
44 return $ concat paths
46 return $ concat paths
47+
48+
49+--------------------------------------------------------------------------------
50+removeDirectory :: FilePath -> IO ()
51+removeDirectory fp = do
52+ e <- doesDirectoryExist fp
53+ when e $ removeDirectoryRecursive fp
src/Hakyll/Core/Writable/CopyFile.hsdeleted−43
@@ -1,43 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | Exports simple compilers to just copy files
3−{-# LANGUAGE DeriveDataTypeable #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5−module Hakyll.Core.Writable.CopyFile
6− ( CopyFile (..)
7− , copyFileCompiler
8− ) where
9−
10−
11−--------------------------------------------------------------------------------
12−import Data.Binary (Binary (..))
13−import Data.Typeable (Typeable)
14−import System.Directory (copyFile)
15−
16−
17−--------------------------------------------------------------------------------
18−import Hakyll.Core.Compiler
19−import Hakyll.Core.Identifier
20−import Hakyll.Core.Item
21−import Hakyll.Core.Writable
22−
23−
24−--------------------------------------------------------------------------------
25−-- | This will copy any file directly by using a system call
26−data CopyFile = CopyFile
27− deriving (Show, Eq, Ord, Typeable)
28−
29−
30−--------------------------------------------------------------------------------
31−instance Binary CopyFile where
32− put CopyFile = return ()
33− get = return CopyFile
34−
35−
36−--------------------------------------------------------------------------------
37−instance Writable CopyFile where
38− write dst item = copyFile (toFilePath $ itemIdentifier item) dst
39−
40−
41−--------------------------------------------------------------------------------
42−copyFileCompiler :: Compiler (Item CopyFile)
43−copyFileCompiler = makeItem CopyFile
tests/Hakyll/Core/Provider/Tests.hs+2−1
@@ -26,7 +26,8 @@ tests = testGroup "Hakyll.Core.Provider.Tests"
26
26
27--------------------------------------------------------------------------------
27--------------------------------------------------------------------------------
28case01 :: Assertion
28case01 :: Assertion
29−case01 = withTestStore $ \store -> do
29+case01 = do
30+ store <- newTestStore
30 provider <- newTestProvider store
31 provider <- newTestProvider store
31 assert $ resourceExists provider "example.md"
32 assert $ resourceExists provider "example.md"
32
33
tests/Hakyll/Core/Rules/Tests.hs+3−2
@@ -14,12 +14,12 @@ import Test.HUnit (Assertion, assert, (@=?))
14
14
15--------------------------------------------------------------------------------
15--------------------------------------------------------------------------------
16import Hakyll.Core.Compiler
16import Hakyll.Core.Compiler
17+import Hakyll.Core.File
17import Hakyll.Core.Identifier
18import Hakyll.Core.Identifier
18import Hakyll.Core.Identifier.Pattern
19import Hakyll.Core.Identifier.Pattern
19import Hakyll.Core.Routes
20import Hakyll.Core.Routes
20import Hakyll.Core.Rules
21import Hakyll.Core.Rules
21import Hakyll.Core.Rules.Internal
22import Hakyll.Core.Rules.Internal
22−import Hakyll.Core.Writable.CopyFile
23import Hakyll.Web.Pandoc
23import Hakyll.Web.Pandoc
24import TestSuite.Util
24import TestSuite.Util
25
25
@@ -33,7 +33,8 @@ tests = testGroup "Hakyll.Core.Rules.Tests"
33
33
34--------------------------------------------------------------------------------
34--------------------------------------------------------------------------------
35rulesTest :: Assertion
35rulesTest :: Assertion
36−rulesTest = withTestStore $ \store -> do
36+rulesTest = do
37+ store <- newTestStore
37 provider <- newTestProvider store
38 provider <- newTestProvider store
38 ruleSet <- runRules rules provider
39 ruleSet <- runRules rules provider
39 let identifiers = S.fromList $ map fst $ rulesCompilers ruleSet
40 let identifiers = S.fromList $ map fst $ rulesCompilers ruleSet
tests/Hakyll/Core/Runtime/Tests.hs+7−4
@@ -25,8 +25,8 @@ tests = testGroup "Hakyll.Core.Runtime.Tests" $ fromAssertions "run" [case01]
25
25
26--------------------------------------------------------------------------------
26--------------------------------------------------------------------------------
27case01 :: Assertion
27case01 :: Assertion
28−case01 = withTestConfiguration $ \config -> do
28+case01 = do
29− _ <- run config Logger.Error $ do
29+ _ <- run testConfiguration Logger.Error $ do
30 match "*.md" $ do
30 match "*.md" $ do
31 route $ setExtension "html"
31 route $ setExtension "html"
32 compile $ do
32 compile $ do
@@ -40,8 +40,11 @@ case01 = withTestConfiguration $ \config -> do
40 items <- loadAllSnapshots "*.md" "raw"
40 items <- loadAllSnapshots "*.md" "raw"
41 makeItem $ concat $ map itemBody (items :: [Item String])
41 makeItem $ concat $ map itemBody (items :: [Item String])
42
42
43+ example <- readFile $
43− example <- readFile $ destinationDirectory config </> "example.html"
44+ destinationDirectory testConfiguration </> "example.html"
44 lines example @?= ["<p>This is an example.</p>"]
45 lines example @?= ["<p>This is an example.</p>"]
45
46
46− bodies <- readFile $ destinationDirectory config </> "bodies.txt"
47+ bodies <- readFile $ destinationDirectory testConfiguration </> "bodies.txt"
47 head (lines bodies) @?= "This is an example."
48 head (lines bodies) @?= "This is an example."
49+
50+ cleanTestEnv
tests/Hakyll/Core/Store/Tests.hs+4−3
@@ -38,7 +38,7 @@ simpleSetGet = Q.monadicIO $ do
38 Q.run $ Store.set store key (value :: String)
38 Q.run $ Store.set store key (value :: String)
39 value' <- Q.run $ Store.get store key
39 value' <- Q.run $ Store.get store key
40 Q.assert $ Store.Found value == value'
40 Q.assert $ Store.Found value == value'
41− Q.run cleanTestStore
41+ Q.run cleanTestEnv
42
42
43
43
44--------------------------------------------------------------------------------
44--------------------------------------------------------------------------------
@@ -52,12 +52,13 @@ persistentSetGet = Q.monadicIO $ do
52 store2 <- Q.run newTestStore
52 store2 <- Q.run newTestStore
53 value' <- Q.run $ Store.get store2 key
53 value' <- Q.run $ Store.get store2 key
54 Q.assert $ Store.Found value == value'
54 Q.assert $ Store.Found value == value'
55− Q.run cleanTestStore
55+ Q.run cleanTestEnv
56
56
57
57
58--------------------------------------------------------------------------------
58--------------------------------------------------------------------------------
59wrongType :: H.Assertion
59wrongType :: H.Assertion
60−wrongType = withTestStore $ \store -> do
60+wrongType = do
61+ store <- newTestStore
61 -- Store a string and try to fetch an int
62 -- Store a string and try to fetch an int
62 Store.set store ["foo", "bar"] ("qux" :: String)
63 Store.set store ["foo", "bar"] ("qux" :: String)
63 value <- Store.get store ["foo", "bar"] :: IO (Store.Result Int)
64 value <- Store.get store ["foo", "bar"] :: IO (Store.Result Int)
tests/Hakyll/Core/UnixFilter/Tests.hs+3−1
@@ -27,11 +27,13 @@ tests = testGroup "Hakyll.Core.UnixFilter.Tests"
27
27
28--------------------------------------------------------------------------------
28--------------------------------------------------------------------------------
29unixFilterRev :: H.Assertion
29unixFilterRev :: H.Assertion
30−unixFilterRev = withTestStore $ \store -> do
30+unixFilterRev = do
31+ store <- newTestStore
31 provider <- newTestProvider store
32 provider <- newTestProvider store
32 output <- testCompilerDone store provider "russian.md" compiler
33 output <- testCompilerDone store provider "russian.md" compiler
33 expected <- testCompilerDone store provider "russian.md" getResourceString
34 expected <- testCompilerDone store provider "russian.md" getResourceString
34 H.assert $ rev (itemBody expected) == lines (itemBody output)
35 H.assert $ rev (itemBody expected) == lines (itemBody output)
36+ cleanTestEnv
35 where
37 where
36 compiler = getResourceString >>= withItemBody (unixFilter "rev" [])
38 compiler = getResourceString >>= withItemBody (unixFilter "rev" [])
37 rev = map reverse . lines
39 rev = map reverse . lines
tests/Hakyll/Web/Template/Context/Tests.hs+4−1
@@ -29,7 +29,8 @@ tests = testGroup "Hakyll.Core.Template.Context.Tests"
29
29
30--------------------------------------------------------------------------------
30--------------------------------------------------------------------------------
31testDateField :: Assertion
31testDateField :: Assertion
32−testDateField = withTestStore $ \store -> do
32+testDateField = do
33+ store <- newTestStore
33 provider <- newTestProvider store
34 provider <- newTestProvider store
34
35
35 date1 <- testContextDone store provider "example.md" "date" $
36 date1 <- testContextDone store provider "example.md" "date" $
@@ -41,6 +42,8 @@ testDateField = withTestStore $ \store -> do
41 dateField "date" "%B %e, %Y"
42 dateField "date" "%B %e, %Y"
42 date2 @=? "August 26, 2010"
43 date2 @=? "August 26, 2010"
43
44
45+ cleanTestEnv
46+
44
47
45--------------------------------------------------------------------------------
48--------------------------------------------------------------------------------
46testContextDone :: Store -> Provider -> Identifier -> String
49testContextDone :: Store -> Provider -> Identifier -> String
tests/Hakyll/Web/Template/Tests.hs+4−2
@@ -30,11 +30,13 @@ tests = testGroup "Hakyll.Core.Template.Tests"
30
30
31--------------------------------------------------------------------------------
31--------------------------------------------------------------------------------
32case01 :: Assertion
32case01 :: Assertion
33−case01 = withTestStore $ \store -> do
33+case01 = do
34+ store <- newTestStore
34 provider <- newTestProvider store
35 provider <- newTestProvider store
35
36
36 out <- resourceString provider "template.html.out"
37 out <- resourceString provider "template.html.out"
37− tpl <- testCompilerDone store provider "template.html" $ templateCompiler
38+ tpl <- testCompilerDone store provider "template.html" $
39+ templateCompiler
38 item <- testCompilerDone store provider "example.md" $
40 item <- testCompilerDone store provider "example.md" $
39 pandocCompiler >>= applyTemplate (itemBody tpl) testContext
41 pandocCompiler >>= applyTemplate (itemBody tpl) testContext
40
42
tests/TestSuite/Util.hs+23−33
@@ -3,19 +3,17 @@
3module TestSuite.Util
3module TestSuite.Util
4 ( fromAssertions
4 ( fromAssertions
5 , newTestStore
5 , newTestStore
6− , cleanTestStore
7− , withTestStore
8 , newTestProvider
6 , newTestProvider
9 , testCompiler
7 , testCompiler
10 , testCompilerDone
8 , testCompilerDone
11− , withTestConfiguration
9+ , testConfiguration
10+ , cleanTestEnv
12 ) where
11 ) where
13
12
14
13
15--------------------------------------------------------------------------------
14--------------------------------------------------------------------------------
16import Data.Monoid (mempty)
15import Data.Monoid (mempty)
17import qualified Data.Set as S
16import qualified Data.Set as S
18−import System.Directory (removeDirectoryRecursive)
19import Test.Framework
17import Test.Framework
20import Test.Framework.Providers.HUnit
18import Test.Framework.Providers.HUnit
21import Test.HUnit hiding (Test)
19import Test.HUnit hiding (Test)
@@ -30,6 +28,7 @@ import qualified Hakyll.Core.Logger as Logger
30import Hakyll.Core.Provider
28import Hakyll.Core.Provider
31import Hakyll.Core.Store (Store)
29import Hakyll.Core.Store (Store)
32import qualified Hakyll.Core.Store as Store
30import qualified Hakyll.Core.Store as Store
31+import Hakyll.Core.Util.File
33
32
34
33
35--------------------------------------------------------------------------------
34--------------------------------------------------------------------------------
@@ -42,26 +41,13 @@ fromAssertions name =
42
41
43--------------------------------------------------------------------------------
42--------------------------------------------------------------------------------
44newTestStore :: IO Store
43newTestStore :: IO Store
45−newTestStore = Store.new True "_teststore"
44+newTestStore = Store.new True $ storeDirectory testConfiguration
46−
47−
48−--------------------------------------------------------------------------------
49−cleanTestStore :: IO ()
50−cleanTestStore = removeDirectoryRecursive "_teststore"
51−
52−
53−--------------------------------------------------------------------------------
54−withTestStore :: (Store -> IO a) -> IO a
55−withTestStore f = do
56− store <- newTestStore
57− result <- f store
58− cleanTestStore
59− return result
60
45
61
46
62--------------------------------------------------------------------------------
47--------------------------------------------------------------------------------
63newTestProvider :: Store -> IO Provider
48newTestProvider :: Store -> IO Provider
64−newTestProvider store = newProvider store (const False) "tests/data"
49+newTestProvider store = newProvider store (const False) $
50+ providerDirectory testConfiguration
65
51
66
52
67--------------------------------------------------------------------------------
53--------------------------------------------------------------------------------
@@ -70,7 +56,8 @@ testCompiler :: Store -> Provider -> Identifier -> Compiler a
70testCompiler store provider underlying compiler = do
56testCompiler store provider underlying compiler = do
71 logger <- Logger.new Logger.Error
57 logger <- Logger.new Logger.Error
72 let read' = CompilerRead
58 let read' = CompilerRead
59+ { compilerConfig = testConfiguration
73− { compilerUnderlying = underlying
60+ , compilerUnderlying = underlying
74 , compilerProvider = provider
61 , compilerProvider = provider
75 , compilerUniverse = S.empty
62 , compilerUniverse = S.empty
76 , compilerRoutes = mempty
63 , compilerRoutes = mempty
@@ -99,15 +86,18 @@ testCompilerDone store provider underlying compiler = do
99
86
100
87
101--------------------------------------------------------------------------------
88--------------------------------------------------------------------------------
102−withTestConfiguration :: (Configuration -> IO a) -> IO a
89+testConfiguration :: Configuration
103−withTestConfiguration f = do
104− x <- f config
105− removeDirectoryRecursive $ destinationDirectory config
106− removeDirectoryRecursive $ storeDirectory config
107− return x
108− where
109− config = defaultConfiguration
90+testConfiguration = defaultConfiguration
110− { destinationDirectory = "_testsite"
91+ { destinationDirectory = "_testsite"
111− , storeDirectory = "_teststore"
92+ , storeDirectory = "_teststore"
93+ , tmpDirectory = "_testtmp"
112− , providerDirectory = "tests/data"
94+ , providerDirectory = "tests/data"
113− }
95+ }
96+
97+
98+--------------------------------------------------------------------------------
99+cleanTestEnv :: IO ()
100+cleanTestEnv = do
101+ removeDirectory $ destinationDirectory testConfiguration
102+ removeDirectory $ storeDirectory testConfiguration
103+ removeDirectory $ tmpDirectory testConfiguration