hspec-golden 0.1.0.3 → 0.2.0.0
raw patch · 6 files changed
+38/−28 lines, 6 filesdep +filepath
Dependencies added: filepath
Files
- ChangeLog.md +5/−0
- README.md +2/−2
- app/Main.hs +1/−1
- hspec-golden.cabal +2/−1
- src/Test/Hspec/Golden.hs +19/−15
- test/Test/Hspec/GoldenSpec.hs +9/−9
ChangeLog.md view
@@ -1,4 +1,9 @@ # Changelog for hspec-golden+## 0.2.0.0+#### Add+* More flexible file locations+* Contributed by: @bitc and @christianheyn+ ## 0.1.0.2 #### Add * Lowerbound for `hspec-core` in `hspec-golden` library
README.md view
@@ -43,8 +43,8 @@ encodePretty = prettyText, writeToFile = T.writeFile, readFromFile = T.readFile,- testName = name,- directory = ".myGoldenTestDir",+ goldenFile = name,+ actualFile = Just (name </> "-actual"), failFirstTime = False }
app/Main.hs view
@@ -10,7 +10,7 @@ import qualified Test.Hspec.Golden as G defaultDirGoldenTest :: FilePath-defaultDirGoldenTest = G.directory (G.defaultGolden "" "")+defaultDirGoldenTest = ".golden" -- CLI Params
hspec-golden.cabal view
@@ -7,7 +7,7 @@ -- hash: 34c8c9ac8723c74e1b8dfdf2b6153ad8f1d4c677cbdf667841dc84bda2db22d4 name: hspec-golden-version: 0.1.0.3+version: 0.2.0.0 synopsis: Golden tests for hspec description: Please see the README on GitHub at <https://github.com/stackbuilders/hspec-golden#README> category: Testing@@ -37,6 +37,7 @@ build-depends: base >=4.6 && <5 , directory+ , filepath >=1.0 && <2.0 , hspec-core >=2.5 && <3.0 default-language: Haskell2010
src/Test/Hspec/Golden.hs view
@@ -26,6 +26,7 @@ import Data.IORef import System.Directory (createDirectoryIfMissing, doesFileExist)+import System.FilePath (takeDirectory, (</>)) import Test.Hspec.Core.Spec (Example (..), FailureReason (..), Result (..), ResultStatus (..)) @@ -43,8 +44,8 @@ -- encodePretty = prettyText, -- writeToFile = T.writeFile, -- readFromFile = T.readFile,--- testName = name,--- directory = ".specific-golden-dir",+-- goldenFile = ".specific-golden-dir" </> name </> "golden",+-- actualFile = Just (".specific-golden-dir" </> name </> "actual"), -- failFirstTime = False -- } --@@ -59,8 +60,8 @@ 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+ goldenFile :: FilePath, -- ^ Where to read/write the golden file for this test.+ actualFile :: Maybe FilePath, -- ^ Where to save the actual file for this test. If it is @Nothing@ then no file is written. failFirstTime :: Bool -- ^ Whether to record a failure the first time this test is run } @@ -103,10 +104,10 @@ Golden { output = output_, encodePretty = show,- testName = name, writeToFile = writeFile, readFromFile = readFile,- directory = ".golden",+ goldenFile = ".golden" </> name </> "golden",+ actualFile = Just (".golden" </> name </> "actual"), failFirstTime = False } @@ -122,25 +123,28 @@ runGolden :: Eq str => Golden str -> IO GoldenResult runGolden Golden{..} =- let goldenTestDir = directory ++ "/" ++ testName- goldenFilePath = goldenTestDir ++ "/" ++ "golden"- actualFilePath = goldenTestDir ++ "/" ++ "actual"+ let goldenTestDir = takeDirectory goldenFile in do createDirectoryIfMissing True goldenTestDir- goldenFileExist <- doesFileExist goldenFilePath+ goldenFileExist <- doesFileExist goldenFile - -- the actual file is always written, this way, hgold will always- -- upgrade based on the latest run- writeToFile actualFilePath output+ case actualFile of+ Nothing -> return ()+ Just actual -> do+ -- It is recommended to always write the actual file, this way,+ -- hgold will always upgrade based on the latest run+ let actualDir = takeDirectory actual+ createDirectoryIfMissing True actualDir+ writeToFile actual output if not goldenFileExist then do- writeToFile goldenFilePath output+ writeToFile goldenFile output return $ if failFirstTime then FirstExecutionFail else FirstExecutionSucceed else do- contentGolden <- readFromFile goldenFilePath+ contentGolden <- readFromFile goldenFile if contentGolden == output then return SameOutput
test/Test/Hspec/GoldenSpec.hs view
@@ -17,10 +17,10 @@ fixtureContent = "simple text" fixtureTestName = "id" -goldenTestDir, goldenFile, actualFile :: FilePath-goldenTestDir = directory (defaultGolden "" "") ++ "/" ++ "id"-goldenFile = goldenTestDir ++ "/" ++ "golden"-actualFile = goldenTestDir ++ "/" ++ "actual"+goldenTestDir, goldenFilePath, actualFilePath :: FilePath+goldenTestDir = ".golden" ++ "/" ++ "id"+goldenFilePath = goldenTestDir ++ "/" ++ "golden"+actualFilePath = goldenTestDir ++ "/" ++ "actual" fixtureTest :: String -> H.Spec fixtureTest content =@@ -43,12 +43,12 @@ context "when the test is executed for the first time" $ do it "should create a `golden` file" $ do void $ runSpec $ fixtureTest fixtureContent- goldenFileContent <- readFile goldenFile+ goldenFileContent <- readFile goldenFilePath goldenFileContent `shouldBe` fixtureContent it "should create a `actual` file" $ do void $ runSpec $ fixtureTest fixtureContent- actualFileContent <- readFile goldenFile+ actualFileContent <- readFile goldenFilePath actualFileContent `shouldBe` fixtureContent context "when the output is updated" $@@ -56,13 +56,13 @@ it "should create the `actual` output file" $ do void $ runSpec $ fixtureTest fixtureContent void $ runSpec $ fixtureTest fixtureUpdatedContent- actualFileContent <- readFile actualFile+ actualFileContent <- readFile actualFilePath actualFileContent `shouldBe` fixtureUpdatedContent it "shouldn't overide the `golden` file" $ do void $ runSpec $ fixtureTest fixtureContent void $ runSpec $ fixtureTest fixtureUpdatedContent- goldenFileContent <- readFile goldenFile+ goldenFileContent <- readFile goldenFilePath goldenFileContent `shouldBe` fixtureContent @@ -71,5 +71,5 @@ it "shouldn't change the `golden` file content" $ do void $ runSpec $ fixtureTest fixtureContent void $ runSpec $ fixtureTest fixtureContent- goldenFileContent <- readFile goldenFile+ goldenFileContent <- readFile goldenFilePath goldenFileContent `shouldBe` fixtureContent