on master help sign in
Add inHakyllDirectory function and test cases
2066be0Jasper Van der Jeugt
src/Text/Hakyll.hs+8
@@ -11,6 +11,7 @@ module Text.Hakyll
11 ( defaultHakyllConfiguration
11 ( defaultHakyllConfiguration
12 , hakyll
12 , hakyll
13 , hakyllWithConfiguration
13 , hakyllWithConfiguration
14+ , runDefaultHakyll
14 ) where
15 ) where
15
16
16import Control.Concurrent (forkIO, threadDelay)
17import Control.Concurrent (forkIO, threadDelay)
@@ -144,3 +145,10 @@ server port preRespond = do
144 root <- askHakyll siteDirectory
145 root <- askHakyll siteDirectory
145 let preRespondIO = runReaderT preRespond configuration
146 let preRespondIO = runReaderT preRespond configuration
146 liftIO $ simpleServer (fromIntegral port) root preRespondIO
147 liftIO $ simpleServer (fromIntegral port) root preRespondIO
148+
149+-- | Run a Hakyll action with default settings. This is mostly aimed at testing
150+-- code.
151+--
152+runDefaultHakyll :: Hakyll a -> IO a
153+runDefaultHakyll f =
154+ runReaderT f $ defaultHakyllConfiguration "http://example.com"
src/Text/Hakyll/File.hs+22
@@ -5,6 +5,7 @@ module Text.Hakyll.File
5 , toCache
5 , toCache
6 , toUrl
6 , toUrl
7 , toRoot
7 , toRoot
8+ , inHakyllDirectory
8 , removeSpaces
9 , removeSpaces
9 , makeDirectories
10 , makeDirectories
10 , getRecursiveContents
11 , getRecursiveContents
@@ -16,6 +17,7 @@ module Text.Hakyll.File
16 ) where
17 ) where
17
18
18import System.Directory
19import System.Directory
20+import Control.Applicative ((<$>))
19import System.FilePath
21import System.FilePath
20import System.Time (ClockTime)
22import System.Time (ClockTime)
21import Control.Monad
23import Control.Monad
@@ -85,6 +87,26 @@ toRoot = emptyException . joinPath . map parent . splitPath
85 emptyException [] = "."
87 emptyException [] = "."
86 emptyException x = x
88 emptyException x = x
87
89
90+-- | Check if a file is in a Hakyll directory. With a Hakyll directory, we mean
91+-- a directory that should be "ignored" such as the @_site@ or @_cache@
92+-- directory.
93+--
94+-- Example:
95+--
96+-- > inHakyllDirectory "_cache/pages/index.html"
97+--
98+-- Result:
99+--
100+-- > True
101+--
102+inHakyllDirectory :: FilePath -> Hakyll Bool
103+inHakyllDirectory path =
104+ or <$> mapM (liftM inDirectory . askHakyll) [siteDirectory, cacheDirectory]
105+ where
106+ inDirectory dir = case splitDirectories path of
107+ [] -> False
108+ (x : _) -> x == dir
109+
88-- | Swaps spaces for '-'.
110-- | Swaps spaces for '-'.
89removeSpaces :: FilePath -> FilePath
111removeSpaces :: FilePath -> FilePath
90removeSpaces = map swap
112removeSpaces = map swap
src/Text/Hakyll/HakyllMonad.hs+2
@@ -59,6 +59,8 @@ data HakyllConfiguration = HakyllConfiguration
59askHakyll :: (HakyllConfiguration -> a) -> Hakyll a
59askHakyll :: (HakyllConfiguration -> a) -> Hakyll a
60askHakyll = flip liftM ask
60askHakyll = flip liftM ask
61
61
62+-- | Obtain the globally available, additional context.
63+--
62getAdditionalContext :: HakyllConfiguration -> Context
64getAdditionalContext :: HakyllConfiguration -> Context
63getAdditionalContext configuration =
65getAdditionalContext configuration =
64 let (Context c) = additionalContext configuration
66 let (Context c) = additionalContext configuration
tests/File.hs+20
@@ -4,6 +4,7 @@ module File
4
4
5import qualified Data.Map as M
5import qualified Data.Map as M
6
6
7+import Control.Applicative ((<$>))
7import Data.Binary
8import Data.Binary
8import Test.Framework (testGroup)
9import Test.Framework (testGroup)
9import Test.Framework.Providers.HUnit
10import Test.Framework.Providers.HUnit
@@ -11,6 +12,7 @@ import Test.Framework.Providers.QuickCheck2
11import Test.HUnit
12import Test.HUnit
12import Test.QuickCheck
13import Test.QuickCheck
13
14
15+import Text.Hakyll (runDefaultHakyll)
14import Text.Hakyll.File
16import Text.Hakyll.File
15
17
16-- File test group.
18-- File test group.
@@ -18,6 +20,10 @@ fileGroup = testGroup "File"
18 [ testCase "test_toRoot_1" test_toRoot_1
20 [ testCase "test_toRoot_1" test_toRoot_1
19 , testCase "test_toRoot_2" test_toRoot_2
21 , testCase "test_toRoot_2" test_toRoot_2
20 , testCase "test_toRoot_3" test_toRoot_3
22 , testCase "test_toRoot_3" test_toRoot_3
23+ , testCase "test_inHakyllDirectory_1" test_inHakyllDirectory_1
24+ , testCase "test_inHakyllDirectory_2" test_inHakyllDirectory_2
25+ , testCase "test_inHakyllDirectory_3" test_inHakyllDirectory_3
26+ , testCase "test_inHakyllDirectory_4" test_inHakyllDirectory_4
21 , testCase "test_removeSpaces_1" test_removeSpaces_1
27 , testCase "test_removeSpaces_1" test_removeSpaces_1
22 , testCase "test_removeSpaces_2" test_removeSpaces_2
28 , testCase "test_removeSpaces_2" test_removeSpaces_2
23 , testCase "test_havingExtension_1" test_havingExtension_1
29 , testCase "test_havingExtension_1" test_havingExtension_1
@@ -30,6 +36,20 @@ test_toRoot_1 = toRoot "/posts/foo.html" @?= ".."
30test_toRoot_2 = toRoot "posts/foo.html" @?= ".."
36test_toRoot_2 = toRoot "posts/foo.html" @?= ".."
31test_toRoot_3 = toRoot "foo.html" @?= "."
37test_toRoot_3 = toRoot "foo.html" @?= "."
32
38
39+-- inHakyllDirectory test cases
40+test_inHakyllDirectory_1 =
41+ (runDefaultHakyll $ inHakyllDirectory "_site/foo.html")
42+ @? "test_inHakyllDirectory_1"
43+test_inHakyllDirectory_2 =
44+ (not <$> (runDefaultHakyll $ inHakyllDirectory "posts/foo.html"))
45+ @? "test_inHakyllDirectory_2"
46+test_inHakyllDirectory_3 =
47+ (not <$> (runDefaultHakyll $ inHakyllDirectory "index.html"))
48+ @? "test_inHakyllDirectory_3"
49+test_inHakyllDirectory_4 =
50+ (runDefaultHakyll $ inHakyllDirectory "_cache/index.html")
51+ @? "test_inHakyllDirectory_4"
52+
33-- removeSpaces test cases
53-- removeSpaces test cases
34test_removeSpaces_1 = removeSpaces "$root/tags/random crap.html"
54test_removeSpaces_1 = removeSpaces "$root/tags/random crap.html"
35 @?= "$root/tags/random-crap.html"
55 @?= "$root/tags/random-crap.html"
tests/Page.hs+1−2
@@ -35,8 +35,7 @@ test_readPage fileName content assertion = do
35 temporaryDir <- getTemporaryDirectory
35 temporaryDir <- getTemporaryDirectory
36 let temporaryFile = temporaryDir </> fileName
36 let temporaryFile = temporaryDir </> fileName
37 writeFile temporaryFile content
37 writeFile temporaryFile content
38− page <- runReaderT (readPage temporaryFile)
38+ page <- runDefaultHakyll (readPage temporaryFile)
39− (defaultHakyllConfiguration "http://examples.com")
40 removeFile temporaryFile
39 removeFile temporaryFile
41 return $ assertion page
40 return $ assertion page
42
41