Skip to content

Commit 688b0d5

Browse files
committed
Add flaky test infrastructure to hls-test-utils
1 parent 7bc494a commit 688b0d5

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

hls-test-utils/src/Test/Hls/Util.hs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module Test.Hls.Util
2121
, knownBrokenForGhcVersions
2222
, knownBrokenInEnv
2323
, onlyWorkForGhcVersions
24+
, knownFlakyInEnv
25+
, knownFlaky
2426
-- * Extract code actions
2527
, fromAction
2628
, fromCommand
@@ -71,9 +73,11 @@ import System.IO.Temp
7173
import System.Time.Extra (Seconds, sleep)
7274
import Test.Tasty (TestTree)
7375
import Test.Tasty.ExpectedFailure (expectFailBecause,
74-
ignoreTestBecause)
76+
ignoreTestBecause, wrapTest)
7577
import Test.Tasty.HUnit (Assertion, assertFailure,
7678
(@?=))
79+
import Test.Tasty.Runners (Result(..), resultSuccessful)
80+
import qualified Test.Tasty.Runners as Runners
7781

7882
noLiteralCaps :: C.ClientCapabilities
7983
noLiteralCaps = def & textDocument ?~ textDocumentCaps
@@ -143,6 +147,57 @@ onlyRunForGhcVersions vers =
143147
then const id
144148
else ignoreTestBecause
145149

150+
-- | Mark test as flaky for a specific target.
151+
-- This runs the test, but its result does not matter for the test suite.
152+
--
153+
-- We define a test as flaky if it is fundamentally correct, but
154+
-- fails occasionally due to lsp-test shenanigans. In particular, when the
155+
-- test times out *sometimes*, but not always.
156+
--
157+
-- A flaky test is a bug, which we are only not fixing right away
158+
-- because it might be difficult to fix with lsp-test.
159+
-- If your test isn't using lsp-test, then using this function
160+
-- is not permitted.
161+
knownFlakyInEnv :: [EnvSpec] -> String -> TestTree -> TestTree
162+
knownFlakyInEnv envSpecs reason
163+
| any matchesCurrentEnv envSpecs = knownFlakyBecause reason
164+
| otherwise = id
165+
166+
-- | Mark test as flaky for all supported GHC versions.
167+
-- This runs the test, but its result does not matter for the test suite.
168+
--
169+
-- We define a test as flaky if it is fundamentally correct, but
170+
-- fails occasionally due to lsp-test shenanigans. In particular, when the
171+
-- test times out *sometimes*, but not always.
172+
--
173+
-- A flaky test is a bug, which we are only not fixing right away
174+
-- because it might be difficult to fix with lsp-test.
175+
-- If your test isn't using lsp-test, then using this function
176+
-- is not permitted.
177+
knownFlaky :: String -> TestTree -> TestTree
178+
knownFlaky = knownFlakyInEnv (map GhcVer [minBound .. maxBound])
179+
180+
-- | The test can no longer fail and is marked as flaky.
181+
-- Flaky tests are tests that sometimes fail but sometimes also succeed.
182+
-- Ideally, such test cases should not exist at all, but at least in this codebase,
183+
-- the reality is, that we do have a number of flaky test cases.
184+
--
185+
-- The code is basically copy-pasted from 'tasty-expected-failure' and if this
186+
-- function proves useful in practice, we will upstream it.
187+
knownFlakyBecause :: String -> TestTree -> TestTree
188+
knownFlakyBecause info = wrapTest (fmap change)
189+
where
190+
change r
191+
| resultSuccessful r
192+
= r { resultDescription = resultDescription r <> " (flaky testcase: " <> info <> ")"
193+
, resultShortDescription = resultShortDescription r <> " (flaky testcase)"
194+
}
195+
| otherwise
196+
= r { resultOutcome = Runners.Success
197+
, resultDescription = resultDescription r <> " (failed flaky testcase failed: " <> info <> ")"
198+
, resultShortDescription = resultShortDescription r <> " (failed flaky testcase)"
199+
}
200+
146201
-- ---------------------------------------------------------------------
147202

148203
-- | Like 'withCurrentDirectory', but will copy the directory over to the system

0 commit comments

Comments
 (0)