Debian Codegraph @

Test/Tasty/Runners/AntXML.hs

raw
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE DeriveGeneric #-}
4 {-# LANGUAGE FlexibleContexts #-}
5 {-# LANGUAGE FlexibleInstances #-}
6 {-# LANGUAGE NoMonomorphismRestriction #-}
7 -- | Run a 'Tasty.TestTree' and produce an XML file summarising the test results
8 -- in the same schema that would be produced by Apache Ant's JUnit test runner.
9 -- This schema can be intepreted by the Jenkins continuous integration server,
10 -- amongst other tools.
11 module Test.Tasty.Runners.AntXML (antXMLRunner, AntXMLPath(..) ) where
12
13 import Numeric (showFFloat)
14 import Control.Applicative
15 import Control.Arrow (first)
16 import Control.Monad.IO.Class (liftIO)
17 import Data.Foldable (fold)
18 import Data.List (intercalate)
19 import Data.Maybe (fromMaybe)
20 import Data.Monoid (Monoid(..), Endo(..), Sum(..))
21 import Data.Proxy (Proxy(..))
22 import Data.Tagged (Tagged(..))
23 import Data.Typeable (Typeable)
24 import GHC.Generics (Generic)
25 import Generics.Deriving.Monoid (memptydefault, mappenddefault)
26 import System.Directory (createDirectoryIfMissing, canonicalizePath)
27 import System.FilePath (takeDirectory)
28
29 import qualified Control.Concurrent.STM as STM
30 import qualified Control.Monad.State as State
31 import qualified Control.Monad.Reader as Reader
32 import qualified Data.Functor.Compose as Functor
33 import qualified Data.IntMap as IntMap
34 import qualified Test.Tasty as Tasty
35 import qualified Test.Tasty.Providers as Tasty
36 import qualified Test.Tasty.Options as Tasty
37 import qualified Test.Tasty.Runners as Tasty
38 import qualified Text.XML.Light as XML
39
40 --------------------------------------------------------------------------------
41 newtype AntXMLPath = AntXMLPath FilePath
42 deriving (Typeable)
43
44 instance Tasty.IsOption (Maybe AntXMLPath) where
45 defaultValue = Nothing
46 parseValue = Just . Just . AntXMLPath
47 optionName = Tagged "xml"
48 optionHelp = Tagged "A file path to store the test results in Ant-compatible XML"
49
50
51 --------------------------------------------------------------------------------
52 data Summary = Summary { summaryFailures :: Sum Int
53 , summaryErrors :: Sum Int
54 , summarySuccesses :: Sum Int
55 , xmlRenderer :: Endo [XML.Element]
56 } deriving (Generic)
57
58 instance Monoid Summary where
59 mempty = memptydefault
60 #if !MIN_VERSION_base(4,11,0)
61 mappend = mappenddefault
62 #else
63 instance Semigroup Summary where
64 (<>) = mappenddefault
65 #endif
66
67
68 --------------------------------------------------------------------------------
69 {-|
70
71 To run tests using this ingredient, use 'Tasty.defaultMainWithIngredients',
72 passing 'antXMLRunner' as one possible ingredient. This ingredient will run
73 tests if you pass the @--xml@ command line option. For example,
74 @--xml=junit.xml@ will run all the tests and generate @junit.xml@ as output.
75
76 -}
77 antXMLRunner :: Tasty.Ingredient
78 antXMLRunner = Tasty.TestReporter optionDescription runner
79 where
80 optionDescription = [ Tasty.Option (Proxy :: Proxy (Maybe AntXMLPath)) ]
81 runner options testTree = do
82 AntXMLPath path <- Tasty.lookupOption options
83
84 return $ \statusMap ->
85 let
86 timeDigits = 3
87 showTime time = showFFloat (Just timeDigits) time ""
88
89 runTest :: (Tasty.IsTest t)
90 => Tasty.OptionSet
91 -> Tasty.TestName
92 -> t
93 -> Tasty.Traversal (Functor.Compose (Reader.ReaderT [String] (State.StateT IntMap.Key IO)) (Const Summary))
94 runTest _ testName _ = Tasty.Traversal $ Functor.Compose $ do
95 i <- State.get
96 groupNames <- Reader.ask
97
98 summary <- liftIO $ STM.atomically $ do
99 status <- STM.readTVar $
100 fromMaybe (error "Attempted to lookup test by index outside bounds") $
101 IntMap.lookup i statusMap
102
103 let testCaseAttributes time = map (uncurry XML.Attr . first XML.unqual)
104 [ ("name", testName)
105 , ("time", showTime time)
106 , ("classname", intercalate "." (reverse groupNames))
107 ]
108
109 mkSummary contents =
110 mempty { xmlRenderer = Endo
111 (XML.node (XML.unqual "testcase") contents :)
112 }
113
114 mkSuccess time = (mkSummary (testCaseAttributes time)) { summarySuccesses = Sum 1 }
115
116 mkFailure time reason =
117 mkSummary ( testCaseAttributes time
118 , XML.node (XML.unqual "failure") reason
119 )
120
121 case status of
122 -- If the test is done, generate XML for it
123 Tasty.Done result
124 | Tasty.resultSuccessful result -> pure (mkSuccess (Tasty.resultTime result))
125 | otherwise ->
126 case resultException result of
127 Just e -> pure $ (mkFailure (Tasty.resultTime result) (show e)) { summaryErrors = Sum 1 }
128 Nothing -> pure $
129 if resultTimedOut result
130 then (mkFailure (Tasty.resultTime result) "TimeOut") { summaryErrors = Sum 1 }
131 else (mkFailure (Tasty.resultTime result) (Tasty.resultDescription result))
132 { summaryFailures = Sum 1 }
133
134 -- Otherwise the test has either not been started or is currently
135 -- executing
136 _ -> STM.retry
137
138 Const summary <$ State.modify (+ 1)
139
140 runGroup
141 :: Reader.MonadReader [String] f
142 => String
143 -> Tasty.Traversal (Functor.Compose f (Const Summary))
144 -> Tasty.Traversal (Functor.Compose f (Const Summary))
145 runGroup groupName children = Tasty.Traversal $ Functor.Compose $ do
146 Const soFar <- Reader.local (groupName :) $ Functor.getCompose $ Tasty.getTraversal children
147
148 let grouped =
149 XML.node (XML.unqual "testsuite")
150 ([ XML.Attr (XML.unqual "name") groupName
151 , XML.Attr (XML.unqual "tests")
152 (show . getSum . (summaryFailures `mappend` summaryErrors `mappend` summarySuccesses) $ soFar)
153 ]
154 , appEndo (xmlRenderer soFar) []
155 )
156
157 pure $ Const
158 soFar { xmlRenderer = Endo (grouped :)
159 }
160
161 runGroup' _options groupName =
162 #if MIN_VERSION_tasty(1, 5, 0)
163 runGroup groupName . fold
164 #else
165 runGroup groupName
166 #endif
167
168 in do
169 (Const summary, tests) <-
170 flip State.runStateT 0 $ flip Reader.runReaderT [] $ Functor.getCompose $ Tasty.getTraversal $
171 Tasty.foldTestTree
172 Tasty.trivialFold { Tasty.foldSingle = runTest, Tasty.foldGroup = runGroup' }
173 options
174 testTree
175
176 return $ \elapsedTime -> do
177 createPathDirIfMissing path
178 writeFile path $
179 XML.showTopElement $
180 XML.node
181 (XML.unqual "testsuites")
182 ([ XML.Attr (XML.unqual "errors")
183 (show . getSum . summaryErrors $ summary)
184 , XML.Attr (XML.unqual "failures")
185 (show . getSum . summaryFailures $ summary)
186 , XML.Attr (XML.unqual "tests") (show tests)
187 , XML.Attr (XML.unqual "time") (showTime elapsedTime)
188 ]
189 , appEndo (xmlRenderer summary) [])
190
191 return (getSum ((summaryFailures `mappend` summaryErrors) summary) == 0)
192
193 resultException r =
194 case Tasty.resultOutcome r of
195 Tasty.Failure (Tasty.TestThrewException e) -> Just e
196 _ -> Nothing
197
198 resultTimedOut r =
199 case Tasty.resultOutcome r of
200 Tasty.Failure (Tasty.TestTimedOut _) -> True
201 _ -> False
202
203 createPathDirIfMissing path = fmap takeDirectory (canonicalizePath path)
204 >>= createDirectoryIfMissing True