FileGeneratorTests.hs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. {-# LANGUAGE TemplateHaskell, QuasiQuotes, OverloadedStrings #-}
  2. module FileGeneratorTests (fileGenSpecs) where
  3. import Control.Exception
  4. import Control.Monad (forM_)
  5. import GeneratorTestUtil
  6. import Test.Hspec
  7. import Test.HUnit (assertFailure, assertEqual)
  8. import Yesod.EmbeddedStatic.Generators
  9. import qualified Data.ByteString.Lazy as BL
  10. -- | Embeds the LICENSE file
  11. license :: GenTestResult
  12. license = $(embedFile "LICENSE" >>=
  13. testOneEntry (Just "_LICENSE") "LICENSE" (BL.readFile "LICENSE")
  14. )
  15. licenseAt :: GenTestResult
  16. licenseAt = $(embedFileAt "abc.txt" "LICENSE" >>=
  17. testOneEntry (Just "abc_txt") "abc.txt" (BL.readFile "LICENSE")
  18. )
  19. embDir :: [GenTestResult]
  20. embDir = $(embedDir "test/embed-dir" >>=
  21. testEntries
  22. [ (Just "abc_def_txt", "abc/def.txt", BL.readFile "test/embed-dir/abc/def.txt")
  23. , (Just "lorem_txt", "lorem.txt", BL.readFile "test/embed-dir/lorem.txt")
  24. , (Just "foo", "foo", BL.readFile "test/embed-dir/foo")
  25. ]
  26. )
  27. embDirAt :: [GenTestResult]
  28. embDirAt = $(embedDirAt "xxx" "test/embed-dir" >>=
  29. testEntries
  30. [ (Just "xxx_abc_def_txt", "xxx/abc/def.txt", BL.readFile "test/embed-dir/abc/def.txt")
  31. , (Just "xxx_lorem_txt", "xxx/lorem.txt", BL.readFile "test/embed-dir/lorem.txt")
  32. , (Just "xxx_foo", "xxx/foo", BL.readFile "test/embed-dir/foo")
  33. ]
  34. )
  35. concatR :: GenTestResult
  36. concatR = $(concatFiles "out.txt" [ "test/embed-dir/abc/def.txt", "test/embed-dir/foo"] >>=
  37. testOneEntry (Just "out_txt") "out.txt" (return "Yesod Rocks\nBar\n")
  38. )
  39. -- The transform function should only run at compile for the production content
  40. concatWithR :: GenTestResult
  41. concatWithR = $(concatFilesWith "out2.txt"
  42. (\x -> return $ x `BL.append` "Extra")
  43. [ "test/embed-dir/abc/def.txt", "test/embed-dir/foo"] >>=
  44. testOneEntry (Just "out2_txt") "out2.txt" (return "Yesod Rocks\nBar\nExtra")
  45. )
  46. fileGenSpecs :: Spec
  47. fileGenSpecs = do
  48. describe "Embed File" $ do
  49. it "embeds a single file" $
  50. assertGenResult (BL.readFile "LICENSE") license
  51. it "embeds a single file at a location" $
  52. assertGenResult (BL.readFile "LICENSE") licenseAt
  53. describe "Embed Directory" $ do
  54. it "embeds a directory" $
  55. forM_ [embDir, embDirAt] $ \d -> case d of
  56. [GenError e] -> assertFailure e
  57. [def, foo, lorem] -> do
  58. assertGenResult (BL.readFile "test/embed-dir/abc/def.txt") def
  59. assertGenResult (BL.readFile "test/embed-dir/foo") foo
  60. assertGenResult (BL.readFile "test/embed-dir/lorem.txt") lorem
  61. _ -> assertFailure "Bad directory list"
  62. describe "Concat Files" $ do
  63. it "simple concat" $
  64. assertGenResult (return "Yesod Rocks\nBar\n") concatR
  65. it "concat with processing function" $
  66. assertGenResult (return "Yesod Rocks\nBar\n") concatWithR -- no Extra since this is development
  67. describe "Compress" $ do
  68. it "compress tool function" $ do
  69. out <- compressTool "runhaskell" [] "main = putStrLn \"Hello World\""
  70. assertEqual "" "Hello World\n" out
  71. it "tryCompressTools" $ do
  72. out <- flip tryCompressTools "abcdef"
  73. [ const $ throwIO $ ErrorCall "An expected error"
  74. , const $ return "foo"
  75. , const $ return "bar"
  76. ]
  77. assertEqual "" "foo" out
  78. out2 <- flip tryCompressTools "abcdef"
  79. [ const $ throwIO $ ErrorCall "An expected error"]
  80. assertEqual "" "abcdef" out2