Skip to content

Commit 412a159

Browse files
committed
[oss] TestRunner: nicer failure diagnostics
Before: ``` FAIL Exception: HUnitFailure (Just (SrcLoc {srcLocPackage = "glean-0.1.0.0-inplace-regression-test-lib", srcLocModule = "Glean.Regression.Snapshot.Result", srcLocFile = "glean/test/regression/Glean/Regression/Snapshot/Result.hs", srcLocStartLine = 34, srcLocStartCol = 25, srcLocEndLine = 34, srcLocEndCol = 44})) (Reason "filerange.out: unexpected result\n19,29d18\n< \"columnBegin\": 1,\n< \"lineEnd\": 53,\n< \"columnEnd\": 1\n< }\n< }\n< },\n< {\n< \"key\": {\n< \"file\": { \"key\": \"example.ts\" },\n< \"range\": {\n< \"lineBegin\": 9,\n74,84d62\n< \"columnBegin\": 1,\n< \"lineEnd\": 42,\n< \"columnEnd\": 3\n< }\n< }\n< },\n< {\n< \"key\": {\n< \"file\": { \"key\": \"example.ts\" },\n< \"range\": {\n< \"lineBegin\": 14,\n107,117d84\n< \"columnBegin\": 3,\n< \"lineEnd\": 33,\n< \"columnEnd\": 3\n< }\n< }\n< },\n< {\n< \"key\": {\n< \"file\": { \"key\": \"example.ts\" },\n< \"range\": {\n< \"lineBegin\": 15,\n382,392d348\n< \"columnBegin\": 3,\n< \"lineEnd\": 41,\n< \"columnEnd\": 5\n< }\n< }\n< },\n< {\n< \"key\": {\n< \"file\": { \"key\": \"example.ts\" },\n< \"range\": {\n< \"lineBegin\": 34,\n528,538d483\n< }\n< }\n< },\n< {\n< \"key\": {\n< \"file\": { \"key\": \"example.ts\" },\n< \"range\": {\n< \"lineBegin\": 46,\n< \"columnBegin\": 1,\n< \"lineEnd\": 52,\n< \"columnEnd\": 1\n\nfilerange.perf: unexpected result\n3c3\n< \"facts_searched\": { \"scip.FileRange.1\": 62 },\n---\n> \"facts_searched\": { \"scip.FileRange.1\": 57 },\n\n") ``` After: ``` FAIL (4.24s) glean/test/regression/Glean/Regression/Snapshot/Result.hs:34: filerange.out: unexpected result 19,29d18 < "columnBegin": 1, < "lineEnd": 53, < "columnEnd": 1 < } < } < }, < { < "key": { < "file": { "key": "example.ts" }, < "range": { < "lineBegin": 9, 74,84d62 < "columnBegin": 1, < "lineEnd": 42, < "columnEnd": 3 < } < } < }, < { < "key": { < "file": { "key": "example.ts" }, < "range": { < "lineBegin": 14, 107,117d84 < "columnBegin": 3, < "lineEnd": 33, < "columnEnd": 3 < } < } < }, < { < "key": { < "file": { "key": "example.ts" }, < "range": { < "lineBegin": 15, 382,392d348 < "columnBegin": 3, < "lineEnd": 41, < "columnEnd": 5 < } < } < }, < { < "key": { < "file": { "key": "example.ts" }, < "range": { < "lineBegin": 34, 528,538d483 < } < } < }, < { < "key": { < "file": { "key": "example.ts" }, < "range": { < "lineBegin": 46, < "columnBegin": 1, < "lineEnd": 52, < "columnEnd": 1 filerange.perf: unexpected result 3c3 < "facts_searched": { "scip.FileRange.1": 62 }, --- > "facts_searched": { "scip.FileRange.1": 57 }, ```
1 parent d44726a commit 412a159

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

glean.cabal.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ library stubs
228228
mangle,
229229
template-haskell,
230230
tasty,
231-
tasty-hunit-adapter
231+
tasty-hunit
232232

233233
library logger
234234
import: fb-haskell, fb-cpp, deps

glean/github/TestRunner.hs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88

99
module TestRunner (module TestRunner) where
1010

11+
import Control.Exception
1112
import System.Environment (withArgs)
1213
import Test.HUnit
1314
import qualified Test.Tasty as Tasty
14-
import qualified Test.Tasty.HUnit.Adapter as Tasty
15+
import qualified Test.HUnit.Base as HUB
16+
import qualified Test.Tasty.HUnit as TFH
17+
import Test.HUnit.Lang as HUnitInternals (HUnitFailure(..), formatFailureReason)
1518

1619
-- | Wraps the HUnit test using tasty, so we get parallel test runs,
1720
-- test listing, and test selection.
1821
testRunner :: Test -> IO ()
1922
testRunner t =
2023
Tasty.defaultMain $
2124
Tasty.testGroup "test" $
22-
Tasty.hUnitTestToTestTree t
25+
hUnitTestToTestTree t
2326

2427
data TestAction = TestAction
2528

@@ -40,4 +43,28 @@ testRunnerAction _ t =
4043
withArgs [] $
4144
Tasty.defaultMain $
4245
Tasty.testGroup "test" $
43-
Tasty.hUnitTestToTestTree t
46+
hUnitTestToTestTree t
47+
48+
-- | Convert existing HUnit test to a TestTree list that can be used
49+
-- with tasty. This is modified from the tasty-hunit-adapter package,
50+
-- adding conversion of the original HUnitFailure exception into
51+
-- Tasty's version, so that we get better diagnostics.
52+
hUnitTestToTestTree :: HUB.Test -> [Tasty.TestTree]
53+
hUnitTestToTestTree = go ""
54+
where
55+
go desc (HUB.TestCase a) = [TFH.testCase desc (rethrow a)]
56+
go desc (HUB.TestLabel s t)
57+
| null desc = go s t
58+
| otherwise = go (desc ++ ":" ++ s) t
59+
go desc (HUB.TestList ts)
60+
-- If the list occurs at the top level (with no description above it),
61+
-- just return that list straightforwardly
62+
| null desc = concatMap (go "") ts
63+
-- If the list occurs with a description, turn that into a honest-to-god
64+
-- test group. This is heuristic, but likely to give good results
65+
| otherwise = [Tasty.testGroup desc (concatMap (go "") ts)]
66+
67+
rethrow t = --recomp
68+
t `catch` \(HUnitInternals.HUnitFailure loc reason) ->
69+
throwIO $
70+
TFH.HUnitFailure loc (HUnitInternals.formatFailureReason reason)

0 commit comments

Comments
 (0)