Skip to content

Commit 4a46d16

Browse files
committed
Use assert{Bool,Failure} from tasty-hunit
Following the discussion at UnkindPartition/tasty#327 (comment) @andreasabel wrote: There is a `Show` instance for `HUnitFailure` which gives the _Haskell_ representation of this exception. So far so good. I am looking for a function that _pretty-prints_ the exception, for presentation to the user. It should look nicer than what `Show` gives me: ``` Exception: HUnitFailure (Just (SrcLoc {srcLocPackage = "main", srcLocModule = "Main", srcLocFile = "tests/PackageTestMain.hs", srcLocStartLine = 127, srcLocStartCol = 3, srcLocEndLine = 127, srcLocEndCol = 17})) (Reason "Expected success but got: \"correct-package-0.1.0.0/correct-package.cabal:11:34: \\nunexpected Unknown cabal spec version specified: 1.10123456\\nexpecting \\\".\\\", \\\"-\\\", white space, \\\"&&\\\" or \\\"|| \\\"\\n\"") ``` @VictorCMiraldo wrote: I started working on this today but I couldn't reproduce the problem. Eventually I discovered that @andreasabel and I did something wrong. What we did is some variation on: ```haskell import qualified Test.HUnit as Raw import qualified Test.Tasty.HUnit as Tasty t1 = Tasty.testCase "Some Test" $ Raw.assertFailure "Omg" ``` The issue comes from the fact that `Raw.HUnitAssertion` is a different type altogether from [`Tasty.HUnitAssertion`](https://github.com/UnkindPartition/tasty/blob/16289a77495eb8279c5e544886ef52503becd148/hunit/Test/Tasty/HUnit/Orig.hs#L136). Because `Tasty.Assertion == Raw.Assertion == IO ()`, and failures are communicated through exceptions, we're bypassing the `try` on [`Tasty.HUnit.run`](https://github.com/UnkindPartition/tasty/blob/master/hunit/Test/Tasty/HUnit.hs#L102) and flagging the test as failed through a random exception, since that method is `try`ing on `Tasty.HUnitAssertion`. The solution is to *not* depend on `HUnit`, depend exclusively on `tasty-hunit` and use the functions from there. Your test failures will be printed properly then.
1 parent b4ea1c7 commit 4a46d16

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

tests/PackageTestMain.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ import Data.List (isInfixOf)
1010

1111
import qualified Codec.Archive.Tar as Tar
1212
import qualified Codec.Compression.GZip as GZip
13-
import qualified Test.HUnit as HUnit
1413

1514
import Distribution.Server.Packages.Unpack
1615
import Distribution.Server.Packages.UnpackTest
1716

1817
import Test.Tasty (defaultMain, TestTree, testGroup)
19-
import Test.Tasty.HUnit (testCase, Assertion, HasCallStack)
18+
import Test.Tasty.HUnit (assertBool, assertFailure, testCase, Assertion, HasCallStack)
2019

2120
main :: IO ()
2221
main = defaultMain allTests
@@ -81,9 +80,9 @@ missingConfigureScriptTest =
8180
do tar <- tarGzFile "missing-configure-0.1.0.0"
8281
now <- getCurrentTime
8382
case unpackPackage now "missing-configure-0.1.0.0.tar.gz" tar of
84-
Right _ -> HUnit.assertFailure "error: unexpected success"
83+
Right _ -> assertFailure "error: unexpected success"
8584
Left err ->
86-
HUnit.assertBool
85+
assertBool
8786
("Error found, but not about missing ./configure: " ++ err)
8887
("The 'build-type' is 'Configure'" `isInfixOf` err)
8988

@@ -94,9 +93,9 @@ badSpecVer =
9493
do tar <- tarGzFile "bad-specver-package-0"
9594
now <- getCurrentTime
9695
case unpackPackage now "bad-specver-package-0.tar.gz" tar of
97-
Right _ -> HUnit.assertFailure "error: unexpected success"
96+
Right _ -> assertFailure "error: unexpected success"
9897
Left err ->
99-
HUnit.assertBool
98+
assertBool
10099
("Error found, but not about invalid spec version: " ++ err)
101100
("cabal spec version" `isInfixOf` err)
102101

@@ -146,7 +145,7 @@ successTestTGZ pkg tar = do
146145
case unpackPackage now (pkg ++ ".tar.gz") tar of
147146
Right _ -> return ()
148147
Left err ->
149-
HUnit.assertFailure $ "Expected success, but got: " ++ show err
148+
assertFailure $ "Expected success, but got: " ++ show err
150149

151150
---------------------------------------------------------------------------
152151
-- * Tar utilities

0 commit comments

Comments
 (0)