Skip to content
Permalink
Browse files

bootstrap command-line tests to verify CLI usage

  • Loading branch information...
KtorZ committed Jul 12, 2019
1 parent 3687284 commit e7b8b7db353a59abc4e9596ecb9f86c752ec1a45
Showing with 79 additions and 2 deletions.
  1. +4 −0 lib/cli/cardano-wallet-cli.cabal
  2. +75 −2 lib/cli/test/unit/Cardano/CLISpec.hs
@@ -67,8 +67,12 @@ test-suite unit
-Werror
build-depends:
base
, cardano-crypto
, cardano-wallet-cli
, cardano-wallet-core
, hspec
, memory
, optparse-applicative
, QuickCheck
, text
, text-class
@@ -11,23 +11,52 @@ module Cardano.CLISpec
import Prelude

import Cardano.CLI
( MnemonicSize (..), Port (..), hGetLine, hGetSensitiveLine )
( MnemonicSize (..)
, Port (..)
, cli
, cmdAddress
, cmdMnemonic
, cmdTransaction
, cmdVersion
, cmdWallet
, hGetLine
, hGetSensitiveLine
)
import Cardano.Crypto.Wallet
( unXPub )
import Cardano.Wallet.Primitive.AddressDerivation
( KeyToAddress (..), getKey )
import Cardano.Wallet.Primitive.Types
( Address (..), DecodeAddress (..), EncodeAddress (..) )
import Control.Concurrent
( forkFinally )
import Control.Concurrent.MVar
( newEmptyMVar, putMVar, takeMVar )
import Control.Monad
( mapM_ )
import Data.Bifunctor
( bimap )
import Data.ByteArray.Encoding
( Base (Base16), convertFromBase, convertToBase )
import Data.Proxy
( Proxy (..) )
import Data.Text
( Text )
import Data.Text.Class
( FromText (..), TextDecodingError (..), toText )
import Options.Applicative
( ParserFailure (..)
, ParserHelp (..)
, ParserResult (..)
, execParserPure
, prefs
)
import Options.Applicative.Help.Chunk
( unChunk )
import System.IO
( Handle, IOMode (..), hClose, openFile )
import Test.Hspec
( Spec, describe, it, shouldBe )
( Spec, describe, expectationFailure, it, shouldBe )
import Test.QuickCheck
( Arbitrary (..)
, Large (..)
@@ -41,10 +70,34 @@ import Test.Text.Roundtrip
( textRoundtrip )

import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as TIO

spec :: Spec
spec = do
describe "Specification / Usage Overview" $ do
let parser = cli $ mempty
<> cmdMnemonic
<> cmdWallet @DummyTarget
<> cmdTransaction @DummyTarget
<> cmdAddress @DummyTarget
<> cmdVersion

let defaultPrefs = prefs mempty

let shouldShowUsage args expected =
case execParserPure defaultPrefs parser args of
Success _ -> expectationFailure
"expected parser to show usage but it has succeeded"
CompletionInvoked _ -> expectationFailure
"expected parser to show usage but it offered completion"
Failure (ParserFailure help) -> do
let (ParserHelp _ _ _ usage _ _, _, _) = help mempty
maybe mempty show (unChunk usage) `shouldBe` expected

it "--help" $ ["--help"] `shouldShowUsage` mconcat
[]

describe "Can perform roundtrip textual encoding & decoding" $ do
textRoundtrip $ Proxy @(Port "test")
textRoundtrip $ Proxy @MnemonicSize
@@ -186,3 +239,23 @@ instance Arbitrary (Port "test") where
shrink p
| p == minBound = []
| otherwise = [pred p]

{-------------------------------------------------------------------------------
Dummy Target
-------------------------------------------------------------------------------}

data DummyTarget

instance KeyToAddress DummyTarget where
keyToAddress = Address . unXPub . getKey

instance EncodeAddress DummyTarget where
encodeAddress _ = T.decodeUtf8 . convertToBase Base16 . unAddress

instance DecodeAddress DummyTarget where
decodeAddress _ = bimap decodingError Address
. convertFromBase Base16
. T.encodeUtf8
where
decodingError _ = TextDecodingError
"Unable to decode Address: expected Base16 encoding"

0 comments on commit e7b8b7d

Please sign in to comment.
You can’t perform that action at this time.