packages feed

hspec-slow 0.1.0.0 → 0.2.0.1

raw patch · 4 files changed

+141/−12 lines, 4 filesdep +hspec-coredep ~basenew-uploaderPVP ok

version bump matches the API change (PVP)

Dependencies added: hspec-core

Dependency ranges changed: base

API changes (from Hackage documentation)

+ Test.Hspec.Slow: timeThese :: SlowConfiguration -> SpecWith a -> SpecWith a

Files

+ Changelog.md view
@@ -0,0 +1,14 @@+# Change log for hspec-slow project++## Version 0.2.0.1 - 2020.11.26+Riskbook takes over maintainership++++## Version 0.2.0.0 - 2020.11.04++Add alternative api that doesn't require using custom it.++## Version 0.1.0.0 - 2016.04.24++Initial release
hspec-slow.cabal view
@@ -1,27 +1,33 @@+cabal-version:       >=1.10 name:                hspec-slow-version:             0.1.0.0+version:             0.2.0.1 synopsis:            Find slow test cases description:         Records and prints out slow Hspec tests-homepage:            https://github.com/bobjflong/hspec-slow#readme license:             BSD3 license-file:        LICENSE-author:              Bob Long-maintainer:          robertjflong@gmail.com+author:              Bob Long, riskbook <support@riskbook.com>+maintainer:          riskbook <support@riskbook.com> copyright:           2016 Bob Long category:            Testing build-type:          Simple--- extra-source-files:-cabal-version:       >=1.10+Homepage:            https://github.com/riskbook/hspec-slow+Bug-reports:         https://github.com/riskbook/hspec-slow/issues+Tested-with:         GHC==8.8.3+extra-source-files:+    readme.md+    LICENSE+    Changelog.md  library   hs-source-dirs:      src   exposed-modules:     Test.Hspec.Slow-  build-depends:       base >= 4.7 && < 5+  build-depends:       base >= 4.7 && < 4.15                        , stm                        , time                        , transformers                        , mtl                        , hspec+                       , hspec-core >= 2.0.0 && < 3.0   default-language:    Haskell2010  test-suite hspec-slow-test@@ -38,4 +44,4 @@  source-repository head   type:     git-  location: https://github.com/bobjflong/hspec-slow+  location: https://github.com/riskbook/hspec-slow
+ readme.md view
@@ -0,0 +1,85 @@+## hspec-slow++Track and display slow specs in Hspec runs.++## Example spec++```haskell+main :: IO ()+main = do+  conf <- configure 1 -- Track specs that take longer than 1s+  timedHspec conf $ \it ->+    describe "Main" $ do+      it "should foo" $ do+        threadDelay 3000000+        1 `shouldBe` 1+      it "should bar" $ do+        threadDelay 1000+        1 `shouldBe` 1+      it "should baz" $ do+        threadDelay 4000000+        1 `shouldBe` 1+```++Checkout [hspec slow everything](#hspec-slow-everything) to run this on your entire test suite.++## Example Output++```+Main+  should foo+  should bar+  should baz+Slow examples:+3.002925s: should foo+4.006186s: should baz++Finished in 7.0141 seconds+3 examples, 0 failures+```++## Parallel specs++Parallel specs are supported. They are run similarly to above:++```haskell+timedHspecParallel conf $ \it -> do+  -- ...+```++Output:++```+Main+  should foo+  should bar+  should baz+Slow examples:+3.005728s: should foo+4.00143s: should baz++Finished in 4.0024 seconds+3 examples, 0 failures+```++## hspec slow everything++`Main.hs`:+```+module Main where++import qualified Spec+import Test.Hspec.Slow+import Test.Hspec(hspec)+import ClassyPrelude++main :: IO ()+main = do+  config <- configure 2+  hspec $ timeThese config Spec.spec+```+`Spec.hs`:+```+{-# OPTIONS_GHC -fno-warn-implicit-prelude #-}+{-# OPTIONS_GHC -F -pgmF hspec-discover -optF --module-name=Spec #-}+```
src/Test/Hspec/Slow.hs view
@@ -4,7 +4,8 @@ module Test.Hspec.Slow (     configure,     timedHspec,-    timedHspecParallel+    timedHspecParallel,+    timeThese   ) where  import           Control.Concurrent.STM.TVar@@ -13,6 +14,8 @@ import           Control.Monad.STM import           Data.Time.Clock import           Test.Hspec+import           Data.Maybe+import           Test.Hspec.Core.Spec  type SlowResults = [(String, NominalDiffTime)] type SlowResultTracker = TVar SlowResults@@ -51,12 +54,33 @@  slowReport :: (MonadIO m) => SlowConfiguration -> m () slowReport s = do-  slows <- liftIO $ readTVarIO (tracker s)-  liftIO $ putStrLn "Slow examples:"-  liftIO $ mapM_ (\(t, v) -> putStrLn $ show v ++ ": " ++ t) slows+  liftIO $ do+    slows <- readTVarIO (tracker s)+    putStrLn "Slow examples:"+    mapM_ (\(t, v) -> putStrLn $ show v ++ ": " ++ t) slows  timedHspec :: SlowConfiguration -> (Timer -> SpecWith ()) -> IO () timedHspec t x = hspec $ (afterAll_ . slowReport) t $ x (timed t)  timedHspecParallel :: SlowConfiguration -> (Timer -> SpecWith ()) -> IO () timedHspecParallel t x = hspec $ (afterAll_ . slowReport) t $ parallel $ x (timed t)++-- | times all tests without having to use a custom `it` function+timeThese :: SlowConfiguration -> SpecWith a -> SpecWith a+timeThese config = (afterAll_ (slowReport config)) . mapSpecItem_ (modifyAroundAction $ adhocMeasure config)++adhocMeasure :: SlowConfiguration -> Item a -> (a -> IO ()) -> a -> IO ()+adhocMeasure config item theTestF a = runReaderT (trackedAction (makeDescription item) $ theTestF a) config++makeDescription :: Item a -> String+makeDescription item = defaultDescription (itemLocation item)  <> "\n\t" <>+  itemRequirement item++defaultDescription :: Maybe Location -> String+defaultDescription stack = fromMaybe ("source location not found: " <> show stack)  $ do+  Location locationFile locationLine locationColumn <- stack+  pure $ (locationFile ++ "[" ++ show locationLine ++ ":" ++ show locationColumn ++ "]")++modifyAroundAction :: (Item a -> ActionWith a -> ActionWith b) -> Item a -> Item b+modifyAroundAction action item@Item{itemExample = e} =+  item{ itemExample = \params aroundAction -> e params (aroundAction . action item) }