Be able to include timestamps in print formatter and failure report formatter

This commit is contained in:
Tom McLaughlin 2023-11-17 20:51:04 -08:00
parent 81d6330490
commit 341f0aa1fd
4 changed files with 43 additions and 8 deletions

View File

@ -46,6 +46,7 @@ data FailureReportFormatter = FailureReportFormatter {
, failureReportIncludeCallStacks :: Bool , failureReportIncludeCallStacks :: Bool
, failureReportIndentSize :: Int , failureReportIndentSize :: Int
, failureReportVisibilityThreshold :: Int , failureReportVisibilityThreshold :: Int
, failureReportIncludeTimestamps :: IncludeTimestamps
} deriving (Show) } deriving (Show)
defaultFailureReportFormatter :: FailureReportFormatter defaultFailureReportFormatter :: FailureReportFormatter
@ -55,6 +56,7 @@ defaultFailureReportFormatter = FailureReportFormatter {
, failureReportIncludeCallStacks = True , failureReportIncludeCallStacks = True
, failureReportIndentSize = 4 , failureReportIndentSize = 4
, failureReportVisibilityThreshold = 50 , failureReportVisibilityThreshold = 50
, failureReportIncludeTimestamps = IncludeTimestampsNever
} }
instance Formatter FailureReportFormatter where instance Formatter FailureReportFormatter where
@ -72,6 +74,7 @@ printFailureReport frf@(FailureReportFormatter {..}) rts _bc = do
, printFormatterVisibilityThreshold = maxBound , printFormatterVisibilityThreshold = maxBound
, printFormatterIncludeCallStacks = failureReportIncludeCallStacks , printFormatterIncludeCallStacks = failureReportIncludeCallStacks
, printFormatterIndentSize = failureReportIndentSize , printFormatterIndentSize = failureReportIndentSize
, printFormatterIncludeTimestamps = failureReportIncludeTimestamps
} }
let extractFromNode node = let RunNodeCommonWithStatus {..} = runNodeCommon node in (runTreeId, (T.pack runTreeLabel, runTreeVisibilityLevel)) let extractFromNode node = let RunNodeCommonWithStatus {..} = runNodeCommon node in (runTreeId, (T.pack runTreeLabel, runTreeVisibilityLevel))

View File

@ -27,7 +27,7 @@ import Test.Sandwich.Formatters.Common.Count
import Test.Sandwich.Formatters.Common.Util import Test.Sandwich.Formatters.Common.Util
import Test.Sandwich.Formatters.Print.Common import Test.Sandwich.Formatters.Print.Common
import Test.Sandwich.Formatters.Print.FailureReason import Test.Sandwich.Formatters.Print.FailureReason
import Test.Sandwich.Formatters.Print.Printing import Test.Sandwich.Formatters.Print.Printing as Printing
import Test.Sandwich.Formatters.Print.Types import Test.Sandwich.Formatters.Print.Types
import Test.Sandwich.Formatters.Print.Util import Test.Sandwich.Formatters.Print.Util
import Test.Sandwich.Interpreters.RunTree.Util import Test.Sandwich.Interpreters.RunTree.Util
@ -76,16 +76,34 @@ runWithIndentation :: RunNode context -> ReaderT (PrintFormatter, Int, Handle) I
runWithIndentation node@(RunNodeIt {..}) = do runWithIndentation node@(RunNodeIt {..}) = do
let common@(RunNodeCommonWithStatus {..}) = runNodeCommon let common@(RunNodeCommonWithStatus {..}) = runNodeCommon
(PrintFormatter {..}, _, _) <- ask
result <- liftIO $ waitForTree node result <- liftIO $ waitForTree node
let printTiming = liftIO (readTVarIO runTreeStatus) >>= \case
Done {..} -> p [i| (#{diffUTCTime statusEndTime statusStartTime})|]
_ -> return () -- Shouldn't happen
-- Print the main header -- Print the main header
case result of case result of
Success -> pGreenLn runTreeLabel Success -> do
DryRun -> pin runTreeLabel pGreen runTreeLabel
Cancelled -> pin runTreeLabel when (printFormatterIncludeTimestamps == IncludeTimestampsAlways) printTiming
p "\n"
DryRun -> do
Printing.pi runTreeLabel
when (printFormatterIncludeTimestamps == IncludeTimestampsAlways) printTiming
p "\n"
Cancelled -> do
Printing.pi runTreeLabel
when (printFormatterIncludeTimestamps == IncludeTimestampsAlways) printTiming
p "\n"
(Failure (Pending _ _)) -> pYellowLn runTreeLabel (Failure (Pending _ _)) -> pYellowLn runTreeLabel
(Failure reason) -> do (Failure reason) -> do
pRedLn runTreeLabel pRed runTreeLabel
when (printFormatterIncludeTimestamps /= IncludeTimestampsNever) printTiming
p "\n"
withBumpIndent $ printFailureReason reason withBumpIndent $ printFailureReason reason
finishPrinting common result finishPrinting common result

View File

@ -25,9 +25,14 @@ pc color msg = printWithColor (Just (SetRGBColor Foreground color)) msg
pn msg = printWithColor Nothing (msg <> "\n") pn msg = printWithColor Nothing (msg <> "\n")
pcn color msg = printWithColor (Just (SetRGBColor Foreground color)) (msg <> "\n") pcn color msg = printWithColor (Just (SetRGBColor Foreground color)) (msg <> "\n")
pGreenLn msg = printIndentedWithColor (Just (SetColor Foreground Dull Green)) (msg <> "\n") pGreen msg = printIndentedWithColor (Just (SetColor Foreground Dull Green)) msg
pYellowLn msg = printIndentedWithColor (Just (SetColor Foreground Dull Yellow)) (msg <> "\n") pGreenLn msg = pGreen (msg <> "\n")
pRedLn msg = printIndentedWithColor (Just (SetColor Foreground Dull Red)) (msg <> "\n") -- Tried solarizedRed here but it was too orange
pYellow msg = printIndentedWithColor (Just (SetColor Foreground Dull Yellow)) msg
pYellowLn msg = pYellow (msg <> "\n")
pRed msg = printIndentedWithColor (Just (SetColor Foreground Dull Red)) msg -- Tried solarizedRed here but it was too orange
pRedLn msg = pRed (msg <> "\n")
printIndentedWithColor maybeColor msg = do printIndentedWithColor maybeColor msg = do
(PrintFormatter {}, indent, h) <- ask (PrintFormatter {}, indent, h) <- ask

View File

@ -14,8 +14,16 @@ data PrintFormatter = PrintFormatter {
-- ^ Whether to include callstacks with failures. -- ^ Whether to include callstacks with failures.
, printFormatterIndentSize :: Int , printFormatterIndentSize :: Int
-- ^ The indentation unit in spaces. Defaults to 4. -- ^ The indentation unit in spaces. Defaults to 4.
, printFormatterIncludeTimestamps :: IncludeTimestamps
-- ^ Whether to include timestamps in output. Defaults to 'IncludeTimestampsNever'.
} deriving (Show) } deriving (Show)
data IncludeTimestamps =
IncludeTimestampsAlways
| IncludeTimestampsNever
| IncludeTimestampsFailuresOnly
deriving (Show, Eq)
defaultPrintFormatter :: PrintFormatter defaultPrintFormatter :: PrintFormatter
defaultPrintFormatter = PrintFormatter { defaultPrintFormatter = PrintFormatter {
printFormatterUseColor = True printFormatterUseColor = True
@ -23,4 +31,5 @@ defaultPrintFormatter = PrintFormatter {
, printFormatterVisibilityThreshold = 50 , printFormatterVisibilityThreshold = 50
, printFormatterIncludeCallStacks = True , printFormatterIncludeCallStacks = True
, printFormatterIndentSize = 4 , printFormatterIndentSize = 4
, printFormatterIncludeTimestamps = IncludeTimestampsNever
} }