2014-03-30 17:12:33 +00:00
|
|
|
module Property.File where
|
|
|
|
|
2014-03-30 19:31:57 +00:00
|
|
|
import Common
|
2014-03-30 19:14:36 +00:00
|
|
|
|
2014-03-30 19:31:57 +00:00
|
|
|
type Line = String
|
2014-03-30 17:12:33 +00:00
|
|
|
|
|
|
|
{- Replaces all the content of a file. -}
|
|
|
|
hasContent :: FilePath -> [Line] -> Property
|
2014-03-30 19:31:57 +00:00
|
|
|
f `hasContent` newcontent = fileProperty ("replace " ++ f)
|
|
|
|
(\_oldcontent -> newcontent) f
|
2014-03-30 17:12:33 +00:00
|
|
|
|
|
|
|
{- Ensures that a line is present in a file, adding it to the end if not. -}
|
|
|
|
containsLine :: FilePath -> Line -> Property
|
2014-03-30 19:31:57 +00:00
|
|
|
f `containsLine` l = fileProperty (f ++ " contains:" ++ l) go f
|
2014-03-30 17:12:33 +00:00
|
|
|
where
|
|
|
|
go ls
|
|
|
|
| l `elem` ls = ls
|
|
|
|
| otherwise = ls++[l]
|
|
|
|
|
|
|
|
{- Ensures that a line is not present in a file.
|
|
|
|
- Note that the file is ensured to exist, so if it doesn't, an empty
|
|
|
|
- file will be written. -}
|
|
|
|
lacksLine :: FilePath -> Line -> Property
|
2014-03-30 19:31:57 +00:00
|
|
|
f `lacksLine` l = fileProperty (f ++ " remove: " ++ l) (filter (/= l)) f
|
2014-03-30 19:14:36 +00:00
|
|
|
|
|
|
|
{- Note: Does not remove symlinks or non-plain-files. -}
|
|
|
|
notPresent :: FilePath -> Property
|
2014-03-30 19:31:57 +00:00
|
|
|
notPresent f = check (doesFileExist f) $ Property (f ++ " not present") $
|
2014-03-30 19:14:36 +00:00
|
|
|
makeChange $ nukeFile f
|
2014-03-30 19:31:57 +00:00
|
|
|
|
|
|
|
fileProperty :: Desc -> ([Line] -> [Line]) -> FilePath -> Property
|
|
|
|
fileProperty desc a f = Property desc $ go =<< doesFileExist f
|
|
|
|
where
|
|
|
|
go True = do
|
|
|
|
ls <- lines <$> catchDefaultIO [] (readFile f)
|
|
|
|
let ls' = a ls
|
|
|
|
if ls' == ls
|
|
|
|
then noChange
|
|
|
|
else makeChange $ viaTmp writeFile f (unlines ls')
|
|
|
|
go False = makeChange $ writeFile f (unlines $ a [])
|