From 8a1d397dba3d863752da8f223dc4a3fc0b9cc798 Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Thu, 20 Jul 2017 14:14:00 -0400 Subject: [PATCH] Emit mustache template warnings (#152) --- Criterion/Report.hs | 26 +++++++++++++++++++++++--- changelog.md | 6 ++++++ criterion.cabal | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Criterion/Report.hs b/Criterion/Report.hs index 5da95a04..9b24a4a2 100644 --- a/Criterion/Report.hs +++ b/Criterion/Report.hs @@ -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) @@ -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 @@ -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 diff --git a/changelog.md b/changelog.md index 0e313028..d648dd75 100644 --- a/changelog.md +++ b/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 diff --git a/criterion.cabal b/criterion.cabal index ba2578f6..1beb5cc3 100644 --- a/criterion.cabal +++ b/criterion.cabal @@ -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,