diff options
-rw-r--r-- | src/Text/Hakyll/File.hs | 7 | ||||
-rw-r--r-- | tests/Tests.hs | 17 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/Text/Hakyll/File.hs b/src/Text/Hakyll/File.hs index 5f5e58b..c535b11 100644 --- a/src/Text/Hakyll/File.hs +++ b/src/Text/Hakyll/File.hs @@ -6,6 +6,7 @@ module Text.Hakyll.File , toURL , makeDirectories , getRecursiveContents + , havingExtension , isCacheValid , directory ) where @@ -47,6 +48,12 @@ getRecursiveContents topdir = do return (concat paths) where isProper = not . (== '.') . head +-- | A filter that takes all file names with a given extension. Prefix the +-- extension with a dot: +-- > havingExtension ".markdown" ["index.markdown", "style.css"] == ["index.markdown"] +havingExtension :: String -> [FilePath] -> [FilePath] +havingExtension extension = filter ((==) extension . takeExtension) + -- | Perform an IO action on every file in a given directory. directory :: (FilePath -> IO ()) -> FilePath -> IO () directory action dir = getRecursiveContents dir >>= mapM_ action diff --git a/tests/Tests.hs b/tests/Tests.hs index cedb717..f83399d 100644 --- a/tests/Tests.hs +++ b/tests/Tests.hs @@ -11,6 +11,7 @@ import qualified Data.ByteString.Lazy.Char8 as B import Text.Hakyll.CompressCSS import Text.Hakyll.Util import Text.Hakyll.Context +import Text.Hakyll.File main = defaultMain tests @@ -33,6 +34,11 @@ tests = [ testGroup "Util group" [ testProperty "trim length" prop_trim_length , testGroup "Context group" [ testCase "renderDate 1" test_render_date1 , testCase "renderDate 2" test_render_date1 ] + + , testGroup "File group" [ testProperty "havingExtension count" prop_having_extension_count + , testCase "havingExtension 1" test_having_extension1 + , testCase "havingExtension 2" test_having_extension2 + ] ] -- Test that a string always becomes shorter when trimmed. @@ -78,3 +84,14 @@ test_render_date2 = M.lookup (B.pack "date") rendered @?= Just (B.pack "Unknown "Unknown date" (M.singleton (B.pack "path") (B.pack "2009-badness-30-a-title.markdown")) + +-- Add an extension, and test that they have that extension +prop_having_extension_count names extension = + not (any ('.' `elem`) names || any (`elem` extension) "./\\") + ==> havingExtension fullExtension withExtensions == withExtensions + where fullExtension = '.' : extension + withExtensions = map (++ fullExtension) names + +-- Having extension test cases +test_having_extension1 = havingExtension ".foo" ["file.bar", "file.txt"] @?= [] +test_having_extension2 = havingExtension ".foo" ["file.foo", "file.txt"] @?= ["file.foo"] |