summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstackbuilders <>2020-06-29 19:16:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2020-06-29 19:16:00 (GMT)
commita039ff41cf1ae9e76ec230ed4843d3fd852ea2b5 (patch)
treef43d22a21e101fd1c2940b2cd84e7322ce7cd3db
parent063fb8a33ca1ed5a03f9fa42b1cc6c631f535cf5 (diff)
version 0.1.0.2HEAD0.1.0.2master
-rw-r--r--ChangeLog.md5
-rw-r--r--LICENSE2
-rw-r--r--README.md3
-rw-r--r--app/Main.hs26
-rw-r--r--hspec-golden.cabal8
-rw-r--r--src/Test/Hspec/Golden.hs38
6 files changed, 53 insertions, 29 deletions
diff --git a/ChangeLog.md b/ChangeLog.md
index 197b86b..9823717 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -1,5 +1,10 @@
# Changelog for hspec-golden
+## 0.1.0.2
+#### Add
+* CLI updates tests recursively #19
+* Allow first execution to fail #16
+
## 0.1.0.1
#### Add
* CLI changed to work with optparse-applicative.
diff --git a/LICENSE b/LICENSE
index a9e5639..d238d12 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright Stack Builders (c) 2019
+Copyright Stack Builders (c) 2019-2020
All rights reserved.
diff --git a/README.md b/README.md
index 509fe23..31b9e3b 100644
--- a/README.md
+++ b/README.md
@@ -44,7 +44,8 @@ myGoldenTest name actualOutput =
writeToFile = T.writeFile,
readFromFile = T.readFile,
testName = name,
- directory = ".myGoldenTestDir"
+ directory = ".myGoldenTestDir",
+ failFirstTime = False
}
describe "myTextFunc" $
diff --git a/app/Main.hs b/app/Main.hs
index 253f4a5..126715f 100644
--- a/app/Main.hs
+++ b/app/Main.hs
@@ -5,8 +5,8 @@ import Data.Version (showVersion)
import Paths_hspec_golden (version)
import Options.Applicative
import Data.Monoid ((<>))
-import System.Directory (doesFileExist, listDirectory,
- renameFile)
+import System.Directory (doesDirectoryExist, doesFileExist,
+ listDirectory, renameFile)
import qualified Test.Hspec.Golden as G
defaultDirGoldenTest :: FilePath
@@ -41,14 +41,22 @@ versionOpt = infoOption (showVersion version)
updateGolden :: FilePath -> IO ()
updateGolden dir = do
putStrLn "Replacing golden with actual..."
- goldenTests <- listDirectory dir
- forM_ goldenTests (mvActualToGolden dir)
+ go dir
putStrLn "Finish..."
+ where
+ go dir = do
+ entries <- listDirectory dir
+ forM_ entries $ \entry -> do
+ let entryInDir = dir ++ "/" ++ entry
+ isDir <- doesDirectoryExist entryInDir
+ when isDir $ do
+ mvActualToGolden entryInDir
+ go entryInDir
-mvActualToGolden :: FilePath -> FilePath -> IO ()
-mvActualToGolden goldenDir testName =
- let actualFilePath = goldenDir ++ "/" ++ testName ++ "/" ++ "actual"
- goldenFilePath = goldenDir ++ "/" ++ testName ++ "/" ++ "golden"
+mvActualToGolden :: FilePath -> IO ()
+mvActualToGolden testPath =
+ let actualFilePath = testPath ++ "/actual"
+ goldenFilePath = testPath ++ "/golden"
in do
actualFileExist <- doesFileExist actualFilePath
when actualFileExist (do
@@ -63,4 +71,4 @@ main = updateGolden =<< execParser opts
where
opts = info ((updateDir <$> params) <**> versionOpt <**> helper)
- ( fullDesc <> header "Update your golden files" ) \ No newline at end of file
+ ( fullDesc <> header "Update your golden files" )
diff --git a/hspec-golden.cabal b/hspec-golden.cabal
index 029b6d6..6f954cf 100644
--- a/hspec-golden.cabal
+++ b/hspec-golden.cabal
@@ -1,13 +1,13 @@
cabal-version: 1.12
--- This file has been generated from package.yaml by hpack version 0.31.2.
+-- This file has been generated from package.yaml by hpack version 0.33.1.
--
-- see: https://github.com/sol/hpack
--
--- hash: 47827519449632063e1d3d47aaedfb8a42618dfbcf2406caefab51c360611a22
+-- hash: a35e55cff149f915e3e5a385717c03730159ae8ac6ccee1967031d55df1f9f99
name: hspec-golden
-version: 0.1.0.1
+version: 0.1.0.2
synopsis: Golden tests for hspec
description: Please see the README on GitHub at <https://github.com/stackbuilders/hspec-golden#README>
category: Testing
@@ -15,7 +15,7 @@ homepage: https://github.com/stackbuilders/hspec-golden#readme
bug-reports: https://github.com/stackbuilders/hspec-golden/issues
author: Stack Builders
maintainer: cmotoche@stackbuilders.com
-copyright: 2019 Stack Builders Inc
+copyright: 2019-2020 Stack Builders Inc
license: MIT
license-file: LICENSE
build-type: Simple
diff --git a/src/Test/Hspec/Golden.hs b/src/Test/Hspec/Golden.hs
index e33eb5c..ac9c6f5 100644
--- a/src/Test/Hspec/Golden.hs
+++ b/src/Test/Hspec/Golden.hs
@@ -1,7 +1,7 @@
{-|
Module : Test.Hspec.Golden
Description : Golden tests for Hspec
-Copyright : Stack Builders (c), 2019
+Copyright : Stack Builders (c), 2019-2020
License : MIT
Maintainer : cmotoche@stackbuilders.com
Stability : experimental
@@ -44,7 +44,8 @@ import Test.Hspec.Core.Spec (Example (..), FailureReason (..),
-- writeToFile = T.writeFile,
-- readFromFile = T.readFile,
-- testName = name,
--- directory = ".specific-golden-dir"
+-- directory = ".specific-golden-dir",
+-- failFirstTime = False
-- }
--
-- describe "myTextFunc" $
@@ -54,12 +55,13 @@ import Test.Hspec.Core.Spec (Example (..), FailureReason (..),
data Golden str =
Golden {
- output :: str, -- ^ Output
- encodePretty :: str -> String, -- ^ Makes the comparison pretty when the test fails
- writeToFile :: FilePath -> str -> IO (), -- ^ How to write into the golden file the file
- readFromFile :: FilePath -> IO str, -- ^ How to read the file,
- testName :: String, -- ^ Test name (make sure it's unique otherwise it could be override)
- directory :: FilePath -- ^ Directory where you write your tests
+ output :: str, -- ^ Output
+ encodePretty :: str -> String, -- ^ Makes the comparison pretty when the test fails
+ writeToFile :: FilePath -> str -> IO (), -- ^ How to write into the golden file the file
+ readFromFile :: FilePath -> IO str, -- ^ How to read the file,
+ testName :: String, -- ^ Test name (make sure it's unique otherwise it could be override)
+ directory :: FilePath, -- ^ Directory where you write your tests
+ failFirstTime :: Bool -- ^ Whether to record a failure the first time this test is run
}
instance Eq str => Example (Golden str) where
@@ -78,8 +80,11 @@ instance Eq str => Example (arg -> Golden str) where
-- | Transform a GoldenResult into a Result from Hspec
fromGoldenResult :: GoldenResult -> Result
-fromGoldenResult FirstExecution = Result "First time execution. Golden file created." Success
-fromGoldenResult SameOutput = Result "Golden and Actual output hasn't changed" Success
+fromGoldenResult SameOutput = Result "Golden and Actual output hasn't changed" Success
+fromGoldenResult FirstExecutionSucceed = Result "First time execution. Golden file created." Success
+fromGoldenResult FirstExecutionFail =
+ Result "First time execution. Golden file created."
+ (Failure Nothing (Reason "failFirstTime is set to True"))
fromGoldenResult (MissmatchOutput expected actual) =
Result "Files golden and actual not match"
(Failure Nothing (ExpectedButGot Nothing expected actual))
@@ -101,7 +106,8 @@ defaultGolden name output_ =
testName = name,
writeToFile = writeFile,
readFromFile = readFile,
- directory = ".golden"
+ directory = ".golden",
+ failFirstTime = False
}
-- | Possible results from a golden test execution
@@ -109,7 +115,8 @@ defaultGolden name output_ =
data GoldenResult =
MissmatchOutput String String
| SameOutput
- | FirstExecution
+ | FirstExecutionSucceed
+ | FirstExecutionFail
-- | Runs a Golden test.
@@ -127,8 +134,11 @@ runGolden Golden{..} =
writeToFile actualFilePath output
if not goldenFileExist
- then writeToFile goldenFilePath output
- >> return FirstExecution
+ then do
+ writeToFile goldenFilePath output
+ return $ if failFirstTime
+ then FirstExecutionFail
+ else FirstExecutionSucceed
else do
contentGolden <- readFromFile goldenFilePath