summaryrefslogtreecommitdiff
path: root/tests/TestSuite/Util.hs
blob: 6c07c50ca8e4c6c75bb528260151356e9c37296d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
--------------------------------------------------------------------------------
-- | Test utilities
module TestSuite.Util
    ( fromAssertions
    , newTestStore
    , cleanTestStore
    , withTestStore
    , newTestProvider
    , testCompiler
    , testCompilerDone
    , withTestConfiguration
    ) where


--------------------------------------------------------------------------------
import           Data.Monoid                    (mempty)
import           System.Directory               (removeDirectoryRecursive)
import           Test.Framework
import           Test.Framework.Providers.HUnit
import           Test.HUnit                     hiding (Test)
import           Text.Printf                    (printf)


--------------------------------------------------------------------------------
import           Hakyll.Core.Compiler.Internal
import           Hakyll.Core.Configuration
import           Hakyll.Core.Identifier
import qualified Hakyll.Core.Logger             as Logger
import           Hakyll.Core.Provider
import           Hakyll.Core.Store              (Store)
import qualified Hakyll.Core.Store              as Store


--------------------------------------------------------------------------------
fromAssertions :: String       -- ^ Name
               -> [Assertion]  -- ^ Cases
               -> [Test]       -- ^ Result tests
fromAssertions name =
    zipWith testCase [printf "%s [%3d]" name n | n <- [1 :: Int ..]]


--------------------------------------------------------------------------------
newTestStore :: IO Store
newTestStore = Store.new True "_teststore"


--------------------------------------------------------------------------------
cleanTestStore :: IO ()
cleanTestStore = removeDirectoryRecursive "_teststore"


--------------------------------------------------------------------------------
withTestStore :: (Store -> IO a) -> IO a
withTestStore f = do
    store  <- newTestStore
    result <- f store
    cleanTestStore
    return result


--------------------------------------------------------------------------------
newTestProvider :: Store -> IO Provider
newTestProvider store = newProvider store (const False) "tests/data"


--------------------------------------------------------------------------------
testCompiler :: Store -> Provider -> Identifier -> Compiler a
             -> IO (CompilerResult a)
testCompiler store provider underlying compiler = do
    logger <- Logger.new Logger.Debug (\_ -> return ())
    let read' = CompilerRead
            { compilerUnderlying = underlying
            , compilerProvider   = provider
            , compilerUniverse   = []
            , compilerRoutes     = mempty
            , compilerStore      = store
            , compilerLogger     = logger
            }

    result <- runCompiler compiler read'
    Logger.flush logger
    return result


--------------------------------------------------------------------------------
testCompilerDone :: Store -> Provider -> Identifier -> Compiler a -> IO a
testCompilerDone store provider underlying compiler = do
    result <- testCompiler store provider underlying compiler
    case result of
        CompilerDone x _    -> return x
        CompilerError e     -> error $
            "TestSuite.Util.testCompilerDone: compiler " ++ show underlying ++
            " threw: " ++ e
        CompilerRequire i _ -> error $
            "TestSuite.Util.testCompilerDone: compiler " ++ show underlying ++
            " requires: " ++ show i



--------------------------------------------------------------------------------
withTestConfiguration :: (Configuration -> IO a) -> IO a
withTestConfiguration f = do
    x <- f config
    removeDirectoryRecursive $ destinationDirectory config
    removeDirectoryRecursive $ storeDirectory config
    return x
  where
    config = defaultConfiguration
        { destinationDirectory = "_testsite"
        , storeDirectory       = "_teststore"
        , providerDirectory    = "tests/data"
        , verbosity            = Logger.Error
        }