on master help sign in
Add a test for compiler/route ordering
commit1c2804287a099b4ac2f6c2d9b3db452f7ef7bee1 authorJasper Van der Jeugt <m@jaspervdj.be> dateSat 16 Feb 2013, 12:59 (14 years ago) parent02a92d5
5 files changed+38−27
hakyll.cabal+2−1 src/Hakyll/Core/Rules/Internal.hs+8−16 tests/Hakyll/Core/Rules/Tests.hs+7−9 tests/Hakyll/Core/Runtime/Tests.hs+21−1 tests/data/images/favicon.ico
hakyll.cabal+2−1
@@ -64,10 +64,11 @@ Data-files:
6464Extra-source-files:
6565 tests/data/example.md
6666 tests/data/example.md.metadata
67+ tests/data/images/favicon.ico
68+ tests/data/posts/2010-08-26-birthday.md
6769 tests/data/russian.md
6870 tests/data/template.html
6971 tests/data/template.html.out
70− tests/data/posts/2010-08-26-birthday.md
7172
7273Source-Repository head
7374 Type: git
src/Hakyll/Core/Rules/Internal.hs+8−16
@@ -16,9 +16,9 @@ import Control.Applicative (Applicative, (<$>))
1616import Control.Monad.Reader (ask)
1717import Control.Monad.RWS (RWST, runRWST)
1818import Control.Monad.Trans (liftIO)
19+import qualified Data.Map as M
1920import Data.Monoid (Monoid, mappend, mempty)
2021import Data.Set (Set)
21−import qualified Data.Set as S
2222
2323
2424--------------------------------------------------------------------------------
@@ -92,25 +92,17 @@ instance MonadMetadata Rules where
9292runRules :: Rules a -> Provider -> IO RuleSet
9393runRules rules provider = do
9494 (_, _, ruleSet) <- runRWST (unRules rules) env emptyRulesState
95− case findDuplicate (map fst $ rulesCompilers ruleSet) of
96− Nothing -> return ruleSet
97− Just id' -> error $
98− "Hakyll.Core.Rules.Internal: two different rules for " ++
99− show id' ++ " exist, bailing out"
10095
96+ -- Ensure compiler uniqueness
97+ let ruleSet' = ruleSet
98+ { rulesCompilers = M.toList $
99+ M.fromListWith (flip const) (rulesCompilers ruleSet)
100+ }
101+
102+ return ruleSet'
101103 where
102104 env = RulesRead
103105 { rulesProvider = provider
104106 , rulesMatches = []
105107 , rulesVersion = Nothing
106108 }
107−
108−
109−--------------------------------------------------------------------------------
110−findDuplicate :: Ord a => [a] -> Maybe a
111−findDuplicate = go S.empty
112− where
113− go _ [] = Nothing
114− go s (x : xs)
115− | x `S.member` s = Just x
116− | otherwise = go (S.insert x s) xs
tests/Hakyll/Core/Rules/Tests.hs+7−9
@@ -10,7 +10,6 @@ import Data.IORef (IORef, newIORef, readIORef,
1010 writeIORef)
1111import qualified Data.Set as S
1212import Test.Framework (Test, testGroup)
13−import Test.Framework.Providers.HUnit (testCase)
1413import Test.HUnit (Assertion, assert, (@=?))
1514
1615
@@ -28,18 +27,17 @@ import TestSuite.Util
2827
2928--------------------------------------------------------------------------------
3029tests :: Test
31−tests = testGroup "Hakyll.Core.Rules.Tests"
32− [ testCase "runRules" rulesTest
33− ]
30+tests = testGroup "Hakyll.Core.Rules.Tests" $ fromAssertions "runRules"
31+ [case01]
3432
3533
3634--------------------------------------------------------------------------------
37−rulesTest :: Assertion
38−rulesTest = do
35+case01 :: Assertion
36+case01 = do
3937 ioref <- newIORef False
4038 store <- newTestStore
4139 provider <- newTestProvider store
42− ruleSet <- runRules (rules ioref) provider
40+ ruleSet <- runRules (rules01 ioref) provider
4341 let identifiers = S.fromList $ map fst $ rulesCompilers ruleSet
4442 routes = rulesRoutes ruleSet
4543
@@ -64,8 +62,8 @@ rulesTest = do
6462
6563
6664--------------------------------------------------------------------------------
67−rules :: IORef Bool -> Rules ()
68−rules ioref = do
65+rules01 :: IORef Bool -> Rules ()
66+rules01 ioref = do
6967 -- Compile some posts
7068 match "*.md" $ do
7169 route $ setExtension "html"
tests/Hakyll/Core/Runtime/Tests.hs+21−1
@@ -20,7 +20,8 @@ import TestSuite.Util
2020
2121--------------------------------------------------------------------------------
2222tests :: Test
23−tests = testGroup "Hakyll.Core.Runtime.Tests" $ fromAssertions "run" [case01]
23+tests = testGroup "Hakyll.Core.Runtime.Tests" $
24+ fromAssertions "run" [case01, case02]
2425
2526
2627--------------------------------------------------------------------------------
@@ -48,3 +49,22 @@ case01 = do
4849 head (lines bodies) @?= "This is an example."
4950
5051 cleanTestEnv
52+
53+
54+--------------------------------------------------------------------------------
55+case02 :: Assertion
56+case02 = do
57+ _ <- run testConfiguration Logger.Error $ do
58+ match "images/favicon.ico" $ do
59+ route $ gsubRoute "images/" (const "")
60+ compile $ makeItem ("Test" :: String)
61+
62+ match "images/**" $ do
63+ route idRoute
64+ compile copyFileCompiler
65+
66+ favicon <- readFile $
67+ destinationDirectory testConfiguration </> "favicon.ico"
68+ favicon @?= "Test"
69+
70+ cleanTestEnv
tests/data/images/favicon.iconew file

Binary file not shown.