Skip to content

Commit

Permalink
Emit mustache template warnings (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanGlScott committed Jul 20, 2017
1 parent 746e75c commit 8a1d397
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
26 changes: 23 additions & 3 deletions Criterion/Report.hs
Expand Up @@ -28,7 +28,7 @@ module Criterion.Report
) where

import Control.Exception (Exception, IOException, throwIO)
import Control.Monad (mplus)
import Control.Monad (mplus, unless)
import Control.Monad.IO.Class (MonadIO(liftIO))
import Control.Monad.Reader (ask)
import Criterion.Monad (Criterion)
Expand All @@ -42,7 +42,9 @@ import Paths_criterion (getDataFileName)
import Statistics.Function (minMax)
import System.Directory (doesFileExist)
import System.FilePath ((</>), (<.>), isPathSeparator)
import Text.Microstache (Key (..), Template (..), Node (..), compileMustacheText, renderMustache)
import System.IO (hPutStrLn, stderr)
import Text.Microstache (Key (..), MustacheWarning (..), Node (..), Template (..),
compileMustacheText, displayMustacheWarning, renderMustacheW)
import Prelude ()
import Prelude.Compat
import qualified Control.Exception as E
Expand Down Expand Up @@ -106,7 +108,25 @@ formatReport reports templateName = do
, "js-flot" .= flot
]

return (renderMustache template context)
let (warnings, formatted) = renderMustacheW template context
-- If there were any issues during mustache template rendering, make sure
-- to inform the user. See #127.
forM_ warnings $ \warning -> do
-- The one thing we choose not to warn about is substituting in the `json`
-- key. The reason is that `json` is used in:
--
-- var reports = {{{json}}};
--
-- So `json` represents a raw JavaScript array. This is a bit skeevy by
-- mustache conventions, but redesigning the template to avoid this
-- warning would be more work than just substituting the array directly.
unless (warning == MustacheDirectlyRenderedValue (Key ["json"])) $
mapM_ (hPutStrLn stderr)
[ "criterion: warning:"
, " " ++ displayMustacheWarning warning
, ""
]
return formatted
where
includeTemplate :: (FilePath -> IO T.Text) -> Template -> IO Template
includeTemplate f Template {..} = fmap
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
@@ -1,3 +1,9 @@
next

* Have `criterion` emit warnings if suspicious things happen during mustache
template substitution when creating HTML reports. This can be useful when
using custom templates with the `--template` flag.

1.2.1.0

* Add `GCStatistics`, `getGCStatistics`, and `applyGCStatistics` to
Expand Down
2 changes: 1 addition & 1 deletion criterion.cabal
Expand Up @@ -94,7 +94,7 @@ library
exceptions >= 0.8.2 && < 0.9,
filepath,
Glob >= 0.7.2,
microstache >= 1 && < 1.1,
microstache >= 1.0.1 && < 1.1,
js-flot,
js-jquery,
mtl >= 2,
Expand Down

0 comments on commit 8a1d397

Please sign in to comment.