propellor/Property.hs

129 lines
3.6 KiB
Haskell
Raw Normal View History

module Property where
import System.Directory
import Control.Monad
import System.Console.ANSI
import System.Exit
2014-03-30 05:24:57 +00:00
import System.IO
2014-03-30 05:13:53 +00:00
import Utility.Monad
import Utility.Exception
2014-03-30 19:31:57 +00:00
data Property = Property
{ propertyDesc :: Desc
-- must be idempotent; may run repeatedly
, propertySatisfy :: IO Result
}
type Desc = String
data Result = NoChange | MadeChange | FailedChange
deriving (Show, Eq)
combineResult :: Result -> Result -> Result
combineResult FailedChange _ = FailedChange
combineResult _ FailedChange = FailedChange
combineResult MadeChange _ = MadeChange
combineResult _ MadeChange = MadeChange
combineResult NoChange NoChange = NoChange
2014-03-30 19:31:57 +00:00
makeChange :: IO () -> IO Result
makeChange a = a >> return MadeChange
noChange :: IO Result
noChange = return NoChange
2014-03-30 06:26:23 +00:00
{- Combines a list of properties, resulting in a single property
- that when run will run each property in the list in turn,
- and print out the description of each as it's run. Does not stop
- on failure; does propigate overall success/failure.
-}
propertyList :: Desc -> [Property] -> Property
2014-03-30 19:31:57 +00:00
propertyList desc ps = Property desc $ ensureProperties' ps
2014-03-30 06:26:23 +00:00
{- Combines a list of properties, resulting in one property that
- ensures each in turn, stopping on failure. -}
combineProperties :: Desc -> [Property] -> Property
2014-03-30 19:31:57 +00:00
combineProperties desc ps = Property desc $ go ps NoChange
where
go [] rs = return rs
go (l:ls) rs = do
r <- ensureProperty l
case r of
FailedChange -> return FailedChange
_ -> go ls (combineResult r rs)
2014-03-30 19:31:57 +00:00
{- Makes a perhaps non-idempotent Property be idempotent by using a flag
- file to indicate whether it has run before.
- Use with caution. -}
flagFile :: Property -> FilePath -> Property
flagFile property flagfile = Property (propertyDesc property) $
go =<< doesFileExist flagfile
where
2014-03-30 19:31:57 +00:00
go True = return NoChange
go False = do
r <- ensureProperty property
when (r == MadeChange) $
writeFile flagfile ""
return r
{- Whenever a change has to be made for a Property, causes a hook
- Property to also be run, but not otherwise. -}
onChange :: Property -> Property -> Property
property `onChange` hook = Property (propertyDesc property) $ do
r <- ensureProperty property
case r of
MadeChange -> do
r' <- ensureProperty hook
return $ combineResult r r'
_ -> return r
{- Indicates that the first property can only be satisfied once
- the second is. -}
requires :: Property -> Property -> Property
x `requires` y = combineProperties (propertyDesc x) [y, x]
2014-03-30 19:53:35 +00:00
describe :: Property -> Desc -> Property
describe p d = p { propertyDesc = d }
2014-03-30 19:31:57 +00:00
{- Makes a Property only be performed when a test succeeds. -}
check :: IO Bool -> Property -> Property
check c property = Property (propertyDesc property) $ ifM c
( ensureProperty property
, return NoChange
)
ensureProperty :: Property -> IO Result
ensureProperty = catchDefaultIO FailedChange . propertySatisfy
2014-03-30 04:08:02 +00:00
ensureProperties :: [Property] -> IO ()
ensureProperties ps = do
2014-03-30 06:57:02 +00:00
r <- ensureProperties' [propertyList "overall" ps]
case r of
FailedChange -> exitWith (ExitFailure 1)
_ -> exitWith ExitSuccess
2014-03-30 06:26:23 +00:00
ensureProperties' :: [Property] -> IO Result
ensureProperties' ps = ensure ps NoChange
where
ensure [] rs = return rs
ensure (l:ls) rs = do
2014-03-30 05:24:57 +00:00
putStr $ propertyDesc l ++ "... "
hFlush stdout
r <- ensureProperty l
2014-03-30 05:24:57 +00:00
clearFromCursorToLineBeginning
setCursorColumn 0
2014-03-30 04:28:56 +00:00
putStr $ propertyDesc l ++ "... "
case r of
FailedChange -> do
setSGR [SetColor Foreground Vivid Red]
putStrLn "failed"
NoChange -> do
setSGR [SetColor Foreground Dull Green]
2014-03-30 05:21:21 +00:00
putStrLn "unchanged"
MadeChange -> do
setSGR [SetColor Foreground Vivid Green]
2014-03-30 05:24:57 +00:00
putStrLn "done"
setSGR []
ensure ls (combineResult r rs)