Skip to content

Commit

Permalink
Fixups for CLI goldens on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed Aug 6, 2021
1 parent 8502c1f commit 73ecf8b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
39 changes: 39 additions & 0 deletions lib/cli/test/data/Cardano/CLISpec/key --help.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Usage: key COMMAND

About public/private keys

Available options:
-h,--help Show this help text

Available commands:
from-recovery-phrase Convert a recovery phrase to an
extended private key
child Derive child keys from a parent
public/private key
public Get the public counterpart of a
private key
inspect Show information about a key
hash Get the hash of a public key

Example:
$ unit.exe recovery-phrase generate --size 15 \
| unit.exe key from-recovery-phrase Shelley > root.prv

$ cat root.prv \
| unit.exe key child 1852H/1815H/0H \
| tee acct.prv \
| unit.exe key public --with-chain-code > acct.pub

$ unit.exe key inspect <<< $(cat acct.prv)
{
"key_type": "private",
"chain_code": "67bef6f80df02c7452e20e76ffb4bb57cae8aac2adf042b21a6b19e4f7b1f511",
"extended_key": "90ead3efad7aacac242705ede323665387f49ed847bed025eb333708ccf6aa54403482a867daeb18f38c57d6cddd7e6fd6aed4a3209f7425a3d1c5d9987a9c5f"
}

$ unit.exe key inspect <<< $(cat acct.pub)
{
"key_type": "public",
"chain_code": "67bef6f80df02c7452e20e76ffb4bb57cae8aac2adf042b21a6b19e4f7b1f511",
"extended_key": "d306350ee88f51fb710252e27f0c40006c58e994761b383e02d400e2be59b3cc"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Usage: key from-recovery-phrase STYLE

Convert a recovery phrase to an extended private key

Available options:
-h,--help Show this help text
STYLE Byron | Icarus | Shelley | Shared

The recovery phrase is read from stdin.

Example:
$ unit.exe recovery-phrase generate \
| unit.exe key from-recovery-phrase Icarus
3 changes: 2 additions & 1 deletion lib/cli/test/unit/Cardano/CLISpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ spec = do

-- IDEA: We could traverse the optparse-applicative structure
-- to get this list automatically.
mapM_ (usageGolden ($(getTestData) </> "Cardano" </> "CLISpec"))
let goldenDir = $(getTestData) </> "Cardano" </> "CLISpec"
mapM_ (usageGolden goldenDir)
[ ["--help"]
, ["recovery-phrase", "--help"]
, ["recovery-phrase", "generate", "--help"]
Expand Down
21 changes: 20 additions & 1 deletion lib/test-utils/src/Test/Hspec/Goldens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
--
-- In contrast to `hspec-golden-aeson` this module works with manually provided
-- values instead of QuickCheck, and with text instead of JSON.
--
-- If the tests are run with the OVERWRITE_GOLDENS environment variable set,
-- they will all fail, and write the current values to disk.
--
-- If some goldens are os-dependent (like some optparse-applicative help text
-- using getProgName), you can create a windows specific variant by appending
-- ".win" to the golden file name.
--
-- This seems to work for now with just two windows specific goldens, but if
-- there are more, it might be more conventient if windows goldens always are
-- separate, as OVERWRITE_GOLDENS will never currently write the .win version.
module Test.Hspec.Goldens
( Settings (..)
, textGolden
Expand All @@ -14,6 +25,8 @@ module Test.Hspec.Goldens

import Prelude

import Control.Applicative
( (<|>) )
import Control.Exception
( try )
import Data.Maybe
Expand All @@ -24,6 +37,8 @@ import System.Environment
( lookupEnv )
import System.FilePath
( (</>) )
import System.Info
( os )
import Test.Hspec

import qualified Data.Text.IO as TIO
Expand All @@ -47,8 +62,12 @@ textGolden settings title value = do
writeGoldenAndFail f value "No existing golden file found"

where
-- If running on windows, will try to read the windows-specific golden
-- first.
readGolden :: FilePath -> IO (Either IOError Text)
readGolden f = try (TIO.readFile f)
readGolden f
| os == "mingw32" = try $ TIO.readFile (f <> ".win") <|> TIO.readFile f
| otherwise = try $ TIO.readFile f

-- NOTE: Not the most elegant error handling, but using e.g. ExceptT seemed
-- to get unnecessarily complicated and not interplay with the IO `shouldBe`
Expand Down

0 comments on commit 73ecf8b

Please sign in to comment.