Skip to content

Commit

Permalink
feat: Include colors for hgold CLI output (#46)
Browse files Browse the repository at this point in the history
* feat: Include colors for terminal output

* fix: New hspec-golden version

* doc: Update CHANGELOG

* fix: Create wrapper for success, warning, and failure
  • Loading branch information
CristhianMotoche authored Jan 15, 2024
1 parent cdffafc commit 2c50dff
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog for hspec-golden
## 0.2.1.1
#### Add
* Add colors to hspec-golden CLI tool.

## 0.2.1.0
#### Add
* Instance for IO Golden type. Kudos to @dbalseiro
Expand Down
47 changes: 31 additions & 16 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module Main where

import System.Console.ANSI
import Control.Monad (forM_, when)
import Data.Version (showVersion)
import Paths_hspec_golden (version)
import Options.Applicative
import Data.Monoid ((<>))
import GHC.IO (catch)
import System.Directory (doesDirectoryExist, doesFileExist,
listDirectory, renameFile)
import qualified Test.Hspec.Golden as G
Expand All @@ -14,14 +16,10 @@ defaultDirGoldenTest = ".golden"

-- CLI Params

data Params =
Params
{ updateDir :: FilePath
-- , shouldUpdate :: Bool
} deriving Show

newtype Params = Params { updateDir :: FilePath } deriving Show

params :: Parser Params
params = Params
params = Params
<$> strOption
(long "update"
<> short 'u'
Expand All @@ -31,18 +29,28 @@ params = Params
<> help "The testing directory where you're dumping your results.")

versionOpt :: Parser (a->a)
versionOpt = infoOption (showVersion version)
versionOpt = infoOption (showVersion version)
(long "version"
<> short 'v'
<> short 'v'
<> help "Show version")


--Update Files
withColor :: Color -> IO () -> IO ()
withColor color action = do
setSGR [SetColor Foreground Dull color]
action
setSGR [Reset]

success, warning, failure :: IO () -> IO ()
success = withColor Green
warning = withColor Yellow
failure = withColor Red

-- Update golden files in the given directory
updateGolden :: FilePath -> IO ()
updateGolden dir = do
putStrLn "Replacing golden with actual..."
putStrLn "Replacing golden with actual:"
go dir
putStrLn "Finish..."
success $ putStrLn "Finished!"
where
go dir = do
entries <- listDirectory dir
Expand All @@ -59,9 +67,16 @@ mvActualToGolden testPath =
goldenFilePath = testPath ++ "/golden"
in do
actualFileExist <- doesFileExist actualFilePath
when actualFileExist (do
putStrLn $ " Replacing file: " ++ goldenFilePath ++ " with: " ++ actualFilePath
renameFile actualFilePath goldenFilePath)
when actualFileExist $ do
putStr " Replacing file: "
warning $ putStr goldenFilePath
putStr " with: "
success $ putStrLn actualFilePath
renameFile actualFilePath goldenFilePath `catch` handleErr
where
handleErr :: IOError -> IO ()
handleErr e =
failure $ putStr $ "Warning: Could not replace file due to error: " ++ show e


-- Main
Expand Down
7 changes: 4 additions & 3 deletions hspec-golden.cabal
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.2.
-- This file has been generated from package.yaml by hpack version 0.35.5.
--
-- see: https://github.com/sol/hpack

name: hspec-golden
version: 0.2.1.0
version: 0.2.1.1
synopsis: Golden tests for hspec
description: .
Golden tests store the expected output in a separated file. Each time a golden test
Expand Down Expand Up @@ -61,7 +61,8 @@ executable hgold
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.6 && <5
ansi-terminal >=1.0 && <2.0
, base >=4.6 && <5
, directory >=1.2.5.0
, hspec-golden
, optparse-applicative
Expand Down
3 changes: 2 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hspec-golden
version: 0.2.1.0
version: 0.2.1.1
github: "stackbuilders/hspec-golden"
license: MIT
author: "Stack Builders"
Expand Down Expand Up @@ -53,6 +53,7 @@ executables:
- hspec-golden
- directory >= 1.2.5.0
- optparse-applicative
- ansi-terminal >= 1.0 && < 2.0

tests:
hspec-golden-test:
Expand Down

0 comments on commit 2c50dff

Please sign in to comment.