aboutsummaryrefslogtreecommitdiff
path: root/tests/Helper.hs
blob: fe2aadc8dbebce1dc9ff94d4b783596310e6493c (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
module Helper
  ( forceGetConfig
  , getConfig
  , eitherToMaybe
  ) where


import System.IO (hClose)
import System.IO.Temp (withSystemTempFile)
import System.Directory (removeFile)

import Data.Text (Text)
import qualified Data.Text.IO as T
import Data.Maybe (fromJust)

import Data.ConfigManager
import Data.ConfigManager.Types (Config)

forceGetConfig :: Text -> IO Config
forceGetConfig = (fmap fromJust) . getConfig

getConfig :: Text -> IO (Maybe Config)
getConfig input =
  withSystemTempFile "config-manager-test" (\filePath handle -> do
    hClose handle
    T.writeFile filePath input
    config <- readConfig filePath
    removeFile filePath
    return $ eitherToMaybe config
  )

eitherToMaybe :: Either a b -> Maybe b
eitherToMaybe (Left _) = Nothing
eitherToMaybe (Right x) = Just x