on master help sign in
Add inHakyllDirectory function and test cases
commit2066be06213cd70fdeae42a6194bc645a15d9835 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateMon 2 Aug 2010, 12:59 (16 years ago) parent1af0421
5 files changed+53−2
src/Text/Hakyll.hs+8 src/Text/Hakyll/File.hs+22 src/Text/Hakyll/HakyllMonad.hs+2 tests/File.hs+20 tests/Page.hs+1−2
src/Text/Hakyll.hs+8
@@ -11,6 +11,7 @@ module Text.Hakyll
1111 ( defaultHakyllConfiguration
1212 , hakyll
1313 , hakyllWithConfiguration
14+ , runDefaultHakyll
1415 ) where
1516
1617import Control.Concurrent (forkIO, threadDelay)
@@ -144,3 +145,10 @@ server port preRespond = do
144145 root <- askHakyll siteDirectory
145146 let preRespondIO = runReaderT preRespond configuration
146147 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
55 , toCache
66 , toUrl
77 , toRoot
8+ , inHakyllDirectory
89 , removeSpaces
910 , makeDirectories
1011 , getRecursiveContents
@@ -16,6 +17,7 @@ module Text.Hakyll.File
1617 ) where
1718
1819import System.Directory
20+import Control.Applicative ((<$>))
1921import System.FilePath
2022import System.Time (ClockTime)
2123import Control.Monad
@@ -85,6 +87,26 @@ toRoot = emptyException . joinPath . map parent . splitPath
8587 emptyException [] = "."
8688 emptyException x = x
8789
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+
88110-- | Swaps spaces for '-'.
89111removeSpaces :: FilePath -> FilePath
90112removeSpaces = map swap
src/Text/Hakyll/HakyllMonad.hs+2
@@ -59,6 +59,8 @@ data HakyllConfiguration = HakyllConfiguration
5959askHakyll :: (HakyllConfiguration -> a) -> Hakyll a
6060askHakyll = flip liftM ask
6161
62+-- | Obtain the globally available, additional context.
63+--
6264getAdditionalContext :: HakyllConfiguration -> Context
6365getAdditionalContext configuration =
6466 let (Context c) = additionalContext configuration
tests/File.hs+20
@@ -4,6 +4,7 @@ module File
44
55import qualified Data.Map as M
66
7+import Control.Applicative ((<$>))
78import Data.Binary
89import Test.Framework (testGroup)
910import Test.Framework.Providers.HUnit
@@ -11,6 +12,7 @@ import Test.Framework.Providers.QuickCheck2
1112import Test.HUnit
1213import Test.QuickCheck
1314
15+import Text.Hakyll (runDefaultHakyll)
1416import Text.Hakyll.File
1517
1618-- File test group.
@@ -18,6 +20,10 @@ fileGroup = testGroup "File"
1820 [ testCase "test_toRoot_1" test_toRoot_1
1921 , testCase "test_toRoot_2" test_toRoot_2
2022 , 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
2127 , testCase "test_removeSpaces_1" test_removeSpaces_1
2228 , testCase "test_removeSpaces_2" test_removeSpaces_2
2329 , testCase "test_havingExtension_1" test_havingExtension_1
@@ -30,6 +36,20 @@ test_toRoot_1 = toRoot "/posts/foo.html" @?= ".."
3036test_toRoot_2 = toRoot "posts/foo.html" @?= ".."
3137test_toRoot_3 = toRoot "foo.html" @?= "."
3238
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+
3353-- removeSpaces test cases
3454test_removeSpaces_1 = removeSpaces "$root/tags/random crap.html"
3555 @?= "$root/tags/random-crap.html"
tests/Page.hs+1−2
@@ -35,8 +35,7 @@ test_readPage fileName content assertion = do
3535 temporaryDir <- getTemporaryDirectory
3636 let temporaryFile = temporaryDir </> fileName
3737 writeFile temporaryFile content
38− page <- runReaderT (readPage temporaryFile)
39− (defaultHakyllConfiguration "http://examples.com")
38+ page <- runDefaultHakyll (readPage temporaryFile)
4039 removeFile temporaryFile
4140 return $ assertion page
4241