From 6a6aa61b54966544987063df9337dc9d2ff058a5 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 31 Jan 2017 19:58:49 -0700 Subject: document File.containsLines --- src/Propellor/Property/File.hs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/Propellor/Property/File.hs') diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs index 95fc6f81..b6f1a8d6 100644 --- a/src/Propellor/Property/File.hs +++ b/src/Propellor/Property/File.hs @@ -21,6 +21,12 @@ f `hasContent` newcontent = fileProperty containsLine :: FilePath -> Line -> Property UnixLike f `containsLine` l = f `containsLines` [l] +-- | Ensures that a list of lines are present in a file, adding any that are not +-- to the end of the file. +-- +-- Note that this property does not guarantee that the lines will appear +-- consecutively, nor in the order specified. If you need either of these, use +-- 'File.containsBlock'. containsLines :: FilePath -> [Line] -> Property UnixLike f `containsLines` ls = fileProperty (f ++ " contains:" ++ show ls) go f where -- cgit v1.3-2-g0d8e From 4e08fe0a738617160c43c268c99441168aa7bd9b Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 31 Jan 2017 19:59:00 -0700 Subject: add File.containsBlock --- src/Propellor/Property/File.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/Propellor/Property/File.hs') diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs index b6f1a8d6..f57480a3 100644 --- a/src/Propellor/Property/File.hs +++ b/src/Propellor/Property/File.hs @@ -6,6 +6,7 @@ import Propellor.Base import Utility.FileMode import qualified Data.ByteString.Lazy as L +import Data.List (isInfixOf, isPrefixOf) import System.Posix.Files import System.Exit @@ -32,6 +33,22 @@ f `containsLines` ls = fileProperty (f ++ " contains:" ++ show ls) go f where go content = content ++ filter (`notElem` content) ls +-- | Ensures that a block of consecutive lines is present in a file, adding it +-- to the end if not. Revert to ensure that the block is not present (though +-- the lines it contains could be present, non-consecutively). +containsBlock :: FilePath -> [Line] -> RevertableProperty UnixLike UnixLike +f `containsBlock` ls = + fileProperty (f ++ " contains block:" ++ show ls) add f + fileProperty (f ++ " lacks block:" ++ show ls) remove f + where + add content + | ls `isInfixOf` content = content + | otherwise = content ++ ls + remove [] = [] + remove content@(x:xs) + | ls `isPrefixOf` content = x : remove (drop (length ls) xs) + | otherwise = x : remove xs + -- | 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. -- cgit v1.3-2-g0d8e From 7ba44cc7fb3b0897fa396a5b66068a2db7961afb Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 31 Jan 2017 21:29:33 -0700 Subject: fix removal of blocks --- src/Propellor/Property/File.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Propellor/Property/File.hs') diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs index f57480a3..9f73b14a 100644 --- a/src/Propellor/Property/File.hs +++ b/src/Propellor/Property/File.hs @@ -46,7 +46,7 @@ f `containsBlock` ls = | otherwise = content ++ ls remove [] = [] remove content@(x:xs) - | ls `isPrefixOf` content = x : remove (drop (length ls) xs) + | ls `isPrefixOf` content = remove (drop (length ls) content) | otherwise = x : remove xs -- | Ensures that a line is not present in a file. -- cgit v1.3-2-g0d8e