hakyll-images 0.3.1 → 0.4.0
raw patch · 5 files changed
+103/−9 lines, 5 filesdep +binarydep +directorydep +filepathdep ~HUnit-approxdep ~tastydep ~tasty-hunit
Dependencies added: binary, directory, filepath
Dependency ranges changed: HUnit-approx, tasty, tasty-hunit
Files
- hakyll-images.cabal +7/−2
- library/Hakyll/Images/Common.hs +25/−4
- package.yaml +5/−1
- tests/Hakyll/Images/Common/Tests.hs +62/−0
- tests/TestSuite.hs +4/−2
hakyll-images.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: ae9f128028bfb7b003b7940adbbacf13e15a99ee309055e70cf616edbd4fb9b2+-- hash: 937597bab3f482f7bbc2688e6a13a148528dc5fbd319eceea97fd955b04b54fa name: hakyll-images-version: 0.3.1+version: 0.4.0 synopsis: Hakyll utilities to work with images description: hakyll-images is an add-on to the hakyll package. It adds utilities to work with images, including JPEG compression. category: Web@@ -46,6 +46,7 @@ JuicyPixels >=3 , JuicyPixels-extra >0.3 , base >=4.8 && <5+ , binary >=0.5 && <0.10 , bytestring >=0.9 && <0.11 , hakyll >4 default-language: Haskell2010@@ -54,6 +55,7 @@ type: exitcode-stdio-1.0 main-is: TestSuite.hs other-modules:+ Hakyll.Images.Common.Tests Hakyll.Images.CompressJpg.Tests Hakyll.Images.Resize.Tests Hakyll.Images@@ -70,7 +72,10 @@ , JuicyPixels >=3 , JuicyPixels-extra >0.3 , base >=4.8 && <5+ , binary >=0.5 && <0.10 , bytestring >=0.9 && <0.11+ , directory >=1.0+ , filepath >=1.0 && <1.5 , hakyll >4 , hakyll-images , tasty >=0.11 && <1.2
library/Hakyll/Images/Common.hs view
@@ -1,4 +1,7 @@- +{-# LANGUAGE TypeSynonymInstances #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE DeriveGeneric #-} {-| Module : Hakyll.Images.Common Description : Types and utilities for Hakyll.Images @@ -23,11 +26,15 @@ import Codec.Picture.Types (DynamicImage) import Codec.Picture.Saving +import Data.Binary (Binary(..)) import Data.ByteString.Lazy (toStrict) import Data.ByteString (ByteString) +import Data.Typeable (Typeable) +import GHC.Generics (Generic) import Hakyll.Core.Compiler (Compiler, getResourceLBS, getUnderlyingExtension) import Hakyll.Core.Item (Item(..)) +import Hakyll.Core.Writable (Writable(..)) -- Supported (i.e. encodable) image formats data ImageFormat @@ -35,15 +42,29 @@ | Png | Bitmap | Tiff - deriving (Eq) + deriving (Eq, Generic) --- Polymorphic type only to get an instance of functor -data Image_ a = Image ImageFormat a +instance Binary ImageFormat +-- Polymorphic type only to get an instance of functor. +-- Do not use this type. +data Image_ a = Image ImageFormat a + deriving (Typeable) + instance Functor Image_ where fmap f (Image fmt a) = Image fmt (f a) type Image = Image_ ByteString + +-- When writing to disk, we ignore the image format. +-- Trusting users to route correctly. +instance Writable Image where + -- Write the bytestring content + write fp item = write fp (image <$> item) + +instance Binary Image where + put (Image fmt content) = put fmt >> put content + get = Image <$> get <*> get -- | Extract format from an image format :: Image_ a -> ImageFormat
package.yaml view
@@ -1,5 +1,5 @@ name: hakyll-images -version: '0.3.1' +version: '0.4.0' github: "LaurentRDC/hakyll-images" license: BSD3 author: "Laurent P. René de Cotret" @@ -26,6 +26,7 @@ library: dependencies: - base >= 4.8 && < 5 + - binary >= 0.5 && < 0.10 - bytestring >= 0.9 && < 0.11 - hakyll > 4 - JuicyPixels >= 3 @@ -47,8 +48,11 @@ - tasty >= 0.11 && < 1.2 - tasty-hunit >= 0.9 && < 0.11 - HUnit-approx >= 1.0.0.0 + - filepath >= 1.0 && < 1.5 + - directory >= 1.0 # Base hakyll-images dependencies - base >= 4.8 && < 5 + - binary >= 0.5 && < 0.10 - bytestring >= 0.9 && < 0.11 - hakyll > 4 - JuicyPixels >= 3
+ tests/Hakyll/Images/Common/Tests.hs view
@@ -0,0 +1,62 @@+{-# LANGUAGE OverloadedStrings #-} +-------------------------------------------------------------------------------- +module Hakyll.Images.Common.Tests + ( tests + ) where + + +-------------------------------------------------------------------------------- +import Test.Tasty (TestTree, testGroup) +import Test.Tasty.HUnit (Assertion, testCase, assertBool) + + +-------------------------------------------------------------------------------- +import Hakyll +import qualified Hakyll.Core.Logger as L +import Hakyll.Core.Runtime +import Hakyll.Images + +import System.Directory (doesFileExist) +import System.FilePath ((</>)) +import Text.Printf (printf) + +fromAssertions :: String -- ^ Name + -> [Assertion] -- ^ Cases + -> [TestTree] -- ^ Result tests +fromAssertions name = + zipWith testCase [printf "[%2d] %s" n name | n <- [1 :: Int ..]] + +testConfiguration :: Configuration +testConfiguration = defaultConfiguration + { destinationDirectory = "_testsite" + , storeDirectory = "_teststore" + , tmpDirectory = "_testtmp" + , providerDirectory = "tests/data" + } + +cleanTestEnv :: IO () +cleanTestEnv = do + removeDirectory $ destinationDirectory testConfiguration + removeDirectory $ storeDirectory testConfiguration + removeDirectory $ tmpDirectory testConfiguration + +case1 :: Assertion +case1 = do + logger <- L.new L.Error + _ <- run testConfiguration logger $ do + match "*.jpg" $ do + route idRoute + compile $ loadImage + >>= compressJpgCompiler 50 + + _ <- assertBool "Image was not written" <$> + (doesFileExist $ destinationDirectory testConfiguration </> "piccolo.jpg") + + cleanTestEnv + + +-------------------------------------------------------------------------------- +tests :: TestTree +tests = testGroup "Hakyll.Images.Common.Tests" $ concat + [ fromAssertions "run" [ case1 ] + ]
tests/TestSuite.hs view
@@ -9,14 +9,16 @@ -------------------------------------------------------------------------------- -import qualified Hakyll.Images.CompressJpg.Tests +import qualified Hakyll.Images.Common.Tests +import qualified Hakyll.Images.CompressJpg.Tests import qualified Hakyll.Images.Resize.Tests -------------------------------------------------------------------------------- main :: IO () main = defaultMain $ testGroup "Hakyll" - [ Hakyll.Images.CompressJpg.Tests.tests + [ Hakyll.Images.Common.Tests.tests + , Hakyll.Images.CompressJpg.Tests.tests , Hakyll.Images.Resize.Tests.tests ]