| 1 |
-------------------------------------------------------------------------------- |
| 2 |
{-# LANGUAGE OverloadedStrings #-} |
| 3 |
module Hakyll.Core.Runtime.Tests |
| 4 |
( tests |
| 5 |
) where |
| 6 |
|
| 7 |
|
| 8 |
-------------------------------------------------------------------------------- |
| 9 |
import Control.Monad (void) |
| 10 |
import qualified Data.ByteString as B |
| 11 |
import Data.List (isInfixOf) |
| 12 |
import System.Exit (ExitCode (..)) |
| 13 |
import System.FilePath ((</>)) |
| 14 |
import Test.Tasty (TestTree, testGroup) |
| 15 |
import Test.Tasty.HUnit (Assertion, assertBool, (@?=)) |
| 16 |
|
| 17 |
|
| 18 |
-------------------------------------------------------------------------------- |
| 19 |
import Hakyll |
| 20 |
import qualified Hakyll.Core.Logger as Logger |
| 21 |
import Hakyll.Core.Runtime |
| 22 |
import TestSuite.Util |
| 23 |
|
| 24 |
|
| 25 |
-------------------------------------------------------------------------------- |
| 26 |
tests :: TestTree |
| 27 |
tests = testGroup "Hakyll.Core.Runtime.Tests" $ fromAssertions "run" |
| 28 |
[ case01 |
| 29 |
, case02 |
| 30 |
, case03 |
| 31 |
, case04 |
| 32 |
, case05 |
| 33 |
, case06 |
| 34 |
, issue1000 |
| 35 |
, issue1014 |
| 36 |
] |
| 37 |
|
| 38 |
|
| 39 |
-------------------------------------------------------------------------------- |
| 40 |
case01 :: Assertion |
| 41 |
case01 = do |
| 42 |
logger <- Logger.new Logger.Error |
| 43 |
_ <- run RunModeNormal testConfiguration logger $ do |
| 44 |
match "images/*" $ do |
| 45 |
route idRoute |
| 46 |
compile copyFileCompiler |
| 47 |
|
| 48 |
match "*.md" $ do |
| 49 |
route $ setExtension "html" |
| 50 |
compile $ do |
| 51 |
getResourceBody |
| 52 |
>>= saveSnapshot "raw" |
| 53 |
>>= renderParagraphs |
| 54 |
|
| 55 |
match (fromList ["partial.html", "partial-helper.html"]) $ |
| 56 |
compile templateCompiler |
| 57 |
create ["partial.html.out"] $ do |
| 58 |
route idRoute |
| 59 |
compile $ do |
| 60 |
example <- loadSnapshotBody "example.md" "raw" |
| 61 |
makeItem example |
| 62 |
>>= loadAndApplyTemplate "partial.html" defaultContext |
| 63 |
|
| 64 |
create ["bodies.txt"] $ do |
| 65 |
route idRoute |
| 66 |
compile $ do |
| 67 |
items <- loadAllSnapshots "*.md" "raw" |
| 68 |
makeItem $ concat $ map itemBody (items :: [Item String]) |
| 69 |
|
| 70 |
favicon <- B.readFile $ |
| 71 |
providerDirectory testConfiguration </> "images/favicon.ico" |
| 72 |
favicon' <- B.readFile $ |
| 73 |
destinationDirectory testConfiguration </> "images/favicon.ico" |
| 74 |
favicon @?= favicon' |
| 75 |
|
| 76 |
example <- readFile $ |
| 77 |
destinationDirectory testConfiguration </> "example.html" |
| 78 |
lines example @?= ["<p>This is an example.</p>"] |
| 79 |
|
| 80 |
bodies <- readFile $ destinationDirectory testConfiguration </> "bodies.txt" |
| 81 |
head' (lines bodies) @?= "This is an example." |
| 82 |
|
| 83 |
partial <- readFile $ providerDirectory testConfiguration </> "partial.html.out" |
| 84 |
partial' <- readFile $ destinationDirectory testConfiguration </> "partial.html.out" |
| 85 |
partial @?= partial' |
| 86 |
|
| 87 |
cleanTestEnv |
| 88 |
|
| 89 |
where |
| 90 |
head' (x:_) = x |
| 91 |
head' [] = error "Hakyll.Core.Runtime.Tests.case01: impossible" |
| 92 |
|
| 93 |
|
| 94 |
-------------------------------------------------------------------------------- |
| 95 |
case02 :: Assertion |
| 96 |
case02 = do |
| 97 |
logger <- Logger.new Logger.Error |
| 98 |
_ <- run RunModeNormal testConfiguration logger $ do |
| 99 |
match "images/favicon.ico" $ do |
| 100 |
route $ gsubRoute "images/" (const "") |
| 101 |
compile $ makeItem ("Test" :: String) |
| 102 |
|
| 103 |
match "images/**" $ do |
| 104 |
route idRoute |
| 105 |
compile copyFileCompiler |
| 106 |
|
| 107 |
favicon <- readFile $ |
| 108 |
destinationDirectory testConfiguration </> "favicon.ico" |
| 109 |
favicon @?= "Test" |
| 110 |
|
| 111 |
cleanTestEnv |
| 112 |
|
| 113 |
|
| 114 |
-------------------------------------------------------------------------------- |
| 115 |
-- Test that dependency cycles are correctly identified |
| 116 |
case03 :: Assertion |
| 117 |
case03 = do |
| 118 |
(logger, inMemLog) <- Logger.newInMem |
| 119 |
(ec, _) <- run RunModeNormal testConfiguration logger $ do |
| 120 |
|
| 121 |
create ["partial.html.out1"] $ do |
| 122 |
route idRoute |
| 123 |
compile $ do |
| 124 |
example <- loadBody "partial.html.out2" |
| 125 |
makeItem example |
| 126 |
>>= loadAndApplyTemplate "partial.html" defaultContext |
| 127 |
|
| 128 |
create ["partial.html.out2"] $ do |
| 129 |
route idRoute |
| 130 |
compile $ do |
| 131 |
example <- loadBody "partial.html.out1" |
| 132 |
makeItem example |
| 133 |
>>= loadAndApplyTemplate "partial.html" defaultContext |
| 134 |
|
| 135 |
ec @?= ExitFailure 1 |
| 136 |
msgs <- inMemLog |
| 137 |
length |
| 138 |
[ msg |
| 139 |
| (Logger.Error, msg) <- msgs, "Dependency cycles:" `isInfixOf` msg |
| 140 |
] @?= 1 |
| 141 |
|
| 142 |
cleanTestEnv |
| 143 |
|
| 144 |
|
| 145 |
-------------------------------------------------------------------------------- |
| 146 |
-- Test that dependency cycles are correctly identified when snapshots |
| 147 |
-- are also involved. See issue #878. |
| 148 |
case04 :: Assertion |
| 149 |
case04 = do |
| 150 |
(logger, inMemLog) <- Logger.newInMem |
| 151 |
(ec, _) <- run RunModeNormal testConfiguration logger $ do |
| 152 |
|
| 153 |
create ["partial.html.out1"] $ do |
| 154 |
route idRoute |
| 155 |
compile $ do |
| 156 |
example <- loadSnapshotBody "partial.html.out2" "raw" |
| 157 |
makeItem example |
| 158 |
>>= loadAndApplyTemplate "partial.html" defaultContext |
| 159 |
|
| 160 |
create ["partial.html.out2"] $ do |
| 161 |
route idRoute |
| 162 |
compile $ do |
| 163 |
example <- loadSnapshotBody "partial.html.out1" "raw" |
| 164 |
makeItem example |
| 165 |
>>= loadAndApplyTemplate "partial.html" defaultContext |
| 166 |
|
| 167 |
ec @?= ExitFailure 1 |
| 168 |
msgs <- inMemLog |
| 169 |
length |
| 170 |
[ msg |
| 171 |
| (Logger.Error, msg) <- msgs, "Dependency cycles:" `isInfixOf` msg |
| 172 |
] @?= 1 |
| 173 |
|
| 174 |
cleanTestEnv |
| 175 |
|
| 176 |
|
| 177 |
-------------------------------------------------------------------------------- |
| 178 |
-- Test that dependency cycles are correctly identified in the presence of |
| 179 |
-- snapshots. See issue #878. |
| 180 |
case05 :: Assertion |
| 181 |
case05 = do |
| 182 |
logger <- Logger.new Logger.Error |
| 183 |
(ec, _) <- run RunModeNormal testConfiguration logger $ do |
| 184 |
|
| 185 |
match "posts/*" $ do |
| 186 |
route $ setExtension "html" |
| 187 |
compile $ do |
| 188 |
let applyDefaultTemplate item = do |
| 189 |
footer <- loadBody "footer.html" |
| 190 |
let postCtx' = |
| 191 |
constField "footer" footer `mappend` |
| 192 |
defaultContext |
| 193 |
loadAndApplyTemplate "template-empty.html" postCtx' item |
| 194 |
|
| 195 |
pandocCompiler |
| 196 |
>>= saveSnapshot "content" |
| 197 |
>>= loadAndApplyTemplate "template-empty.html" defaultContext |
| 198 |
>>= applyDefaultTemplate |
| 199 |
>>= relativizeUrls |
| 200 |
|
| 201 |
create ["footer.html"] $ |
| 202 |
compile $ do |
| 203 |
posts <- fmap (take 5) . recentFirst =<< loadAllSnapshots "posts/*" "content" |
| 204 |
let footerCtx = |
| 205 |
listField "posts" defaultContext (return posts) `mappend` |
| 206 |
defaultContext |
| 207 |
|
| 208 |
makeItem "" |
| 209 |
>>= loadAndApplyTemplate "template-empty.html" footerCtx |
| 210 |
|
| 211 |
create ["template-empty.html"] $ compile templateCompiler |
| 212 |
|
| 213 |
ec @?= ExitSuccess |
| 214 |
|
| 215 |
cleanTestEnv |
| 216 |
|
| 217 |
|
| 218 |
-------------------------------------------------------------------------------- |
| 219 |
-- Test that dependency cycles are correctly identified in the presence of |
| 220 |
-- snapshots. The test case below was presented as an example which invalidated |
| 221 |
-- a previous approach to dependency cycle checking. |
| 222 |
-- See https://github.com/jaspervdj/hakyll/pull/880#discussion_r708650172 |
| 223 |
case06 :: Assertion |
| 224 |
case06 = do |
| 225 |
logger <- Logger.new Logger.Error |
| 226 |
(ec, _) <- run RunModeNormal testConfiguration logger $ do |
| 227 |
|
| 228 |
create ["one.html"] $ do |
| 229 |
route idRoute |
| 230 |
compile $ do |
| 231 |
void $ makeItem ("one-one" :: String) >>= saveSnapshot "one" |
| 232 |
_ <- loadSnapshotBody "two.html" "two" :: Compiler String |
| 233 |
void $ makeItem ("one-three" :: String) >>= saveSnapshot "three" |
| 234 |
makeItem ("one-two" :: String) >>= saveSnapshot "two" |
| 235 |
|
| 236 |
create ["two.html"] $ do |
| 237 |
route idRoute |
| 238 |
compile $ do |
| 239 |
_ <- loadSnapshotBody "one.html" "one" :: Compiler String |
| 240 |
void $ makeItem ("two-two" :: String) >>= saveSnapshot "two" |
| 241 |
text <- loadSnapshotBody "one.html" "two" |
| 242 |
makeItem (text :: String) |
| 243 |
|
| 244 |
ec @?= ExitSuccess |
| 245 |
|
| 246 |
cleanTestEnv |
| 247 |
|
| 248 |
|
| 249 |
-------------------------------------------------------------------------------- |
| 250 |
issue1000 :: Assertion |
| 251 |
issue1000 = do |
| 252 |
(logger, inMemLog) <- Logger.newInMem |
| 253 |
(ec, _) <- run RunModeNormal testConfiguration logger $ do |
| 254 |
match "*.md" $ do |
| 255 |
route $ setExtension "html" |
| 256 |
compile getResourceBody |
| 257 |
match "*.md" $ version "nav" $ do |
| 258 |
route $ setExtension "html" |
| 259 |
compile $ getResourceBody >>= traverse (pure . reverse) |
| 260 |
|
| 261 |
ec @?= ExitFailure 1 |
| 262 |
msgs <- inMemLog |
| 263 |
assertBool "missing 'multiple writes' errors" $ not $ null $ |
| 264 |
[ msg |
| 265 |
| (Logger.Error, msg) <- msgs, "multiple writes" `isInfixOf` msg |
| 266 |
] |
| 267 |
|
| 268 |
cleanTestEnv |
| 269 |
|
| 270 |
|
| 271 |
-------------------------------------------------------------------------------- |
| 272 |
issue1014 :: Assertion |
| 273 |
issue1014 = do |
| 274 |
(logger, inMemLog) <- Logger.newInMem |
| 275 |
(ec, _) <- run RunModeNormal testConfiguration logger $ do |
| 276 |
match "*.md" $ do |
| 277 |
route $ setExtension "html" |
| 278 |
-- This compiler will succeed due to laziness, but writing the |
| 279 |
-- result will throw an exception. |
| 280 |
compile $ makeItem ("hello" ++ error "lazyworld") |
| 281 |
|
| 282 |
ec @?= ExitFailure 1 |
| 283 |
msgs <- inMemLog |
| 284 |
assertBool "missing 'lazyworld' error" $ not $ null $ |
| 285 |
[ msg |
| 286 |
| (Logger.Error, msg) <- msgs, "lazyworld" `isInfixOf` msg |
| 287 |
] |
| 288 |
assertBool "unwanted 'Success' message" $ not $ |
| 289 |
(Logger.Message, "Success") `elem` msgs |
| 290 |
cleanTestEnv |