Skip to content
Permalink
Browse files

Move CLI usage comment as golden tests specification in the CLI unit …

…tests
  • Loading branch information...
KtorZ committed Jul 12, 2019
1 parent e7b8b7d commit 1140e9790d7576eac3293f6c607921c50c103867
Showing with 183 additions and 36 deletions.
  1. +0 −18 lib/cli/src/Cardano/CLI.hs
  2. +183 −18 lib/cli/test/unit/Cardano/CLISpec.hs
@@ -262,8 +262,6 @@ runCli = join . customExecParser preferences

{-------------------------------------------------------------------------------
Commands - 'mnemonic'
cardano-wallet mnemonic generate [--size=INT]
-------------------------------------------------------------------------------}

-- | cardano-wallet mnemonic
@@ -296,13 +294,6 @@ cmdMnemonicGenerate = command "generate" $ info (helper <*> cmd) $ mempty

{-------------------------------------------------------------------------------
Commands - 'wallet'
cardano-wallet wallet list [--port=INT]
cardano-wallet wallet create [--port=INT] <name> [--address-pool-gap=INT]
cardano-wallet wallet get [--port=INT] <wallet-id>
cardano-wallet wallet update name [--port=INT] <wallet-id> STRING
cardano-wallet wallet update passphrase [--port=INT] <wallet-id>
cardano-wallet wallet delete [--port=INT] <wallet-id>
-------------------------------------------------------------------------------}

-- | cardano-wallet wallet
@@ -483,9 +474,6 @@ cmdWalletDelete = command "delete" $ info (helper <*> cmd) $ mempty

{-------------------------------------------------------------------------------
Commands - 'transaction'
cardano-wallet transaction create [--port=INT] <wallet-id> --payment=PAYMENT...
cardano-wallet transaction fees [--port=INT] <wallet-id> --payment=PAYMENT...
-------------------------------------------------------------------------------}

-- | cardano-wallet transaction
@@ -551,8 +539,6 @@ cmdTransactionFees = command "fees" $ info (helper <*> cmd) $ mempty

{-------------------------------------------------------------------------------
Commands - 'address'
cardano-wallet address list [--port=INT] [--state=STRING] <wallet-id>
-------------------------------------------------------------------------------}

-- | cardano-wallet address
@@ -589,8 +575,6 @@ cmdAddressList = command "list" $ info (helper <*> cmd) $ mempty

{-------------------------------------------------------------------------------
Commands - 'version'
cardano-wallet version
-------------------------------------------------------------------------------}

-- | cardano-wallet version
@@ -605,8 +589,6 @@ cmdVersion = command "version" $ info cmd $ mempty

{-------------------------------------------------------------------------------
Commands - 'launch'
cardano-wallet launch ...
-------------------------------------------------------------------------------}

-- | Execute 'launch' commands. This differs from the 'serve' command as it
@@ -17,7 +17,6 @@ import Cardano.CLI
, cmdAddress
, cmdMnemonic
, cmdTransaction
, cmdVersion
, cmdWallet
, hGetLine
, hGetSensitiveLine
@@ -45,25 +44,20 @@ import Data.Text
import Data.Text.Class
( FromText (..), TextDecodingError (..), toText )
import Options.Applicative
( ParserFailure (..)
, ParserHelp (..)
, ParserResult (..)
, execParserPure
, prefs
)
import Options.Applicative.Help.Chunk
( unChunk )
( ParserResult (..), columns, execParserPure, prefs, renderFailure )
import System.IO
( Handle, IOMode (..), hClose, openFile )
import Test.Hspec
( Spec, describe, expectationFailure, it, shouldBe )
( Spec, describe, it, shouldBe )
import Test.QuickCheck
( Arbitrary (..)
, Large (..)
, arbitraryBoundedEnum
, checkCoverage
, counterexample
, cover
, genericShrink
, property
, (===)
)
import Test.Text.Roundtrip
@@ -81,22 +75,193 @@ spec = do
<> cmdWallet @DummyTarget
<> cmdTransaction @DummyTarget
<> cmdAddress @DummyTarget
<> cmdVersion

let defaultPrefs = prefs mempty
let defaultPrefs = prefs (mempty <> columns 65)

let expectationFailure = flip counterexample False

let shouldShowUsage args expected =
let shouldShowUsage args expected = it (unwords args) $
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
Failure failure -> property $
let (usage, _) = renderFailure failure mempty
in counterexample usage $ expected === lines usage

["--help"] `shouldShowUsage`
[ "The CLI is a proxy to the wallet server, which is required for"
, "most commands. Commands are turned into corresponding API calls,"
, "and submitted to an up-and-running server. Some commands do not"
, "require an active server and can be run offline (e.g. 'mnemonic"
, "generate')."
, ""
, "Usage: COMMAND"
, " Cardano Wallet Command-Line Interface (CLI)"
, ""
, "Available options:"
, " -h,--help Show this help text"
, ""
, "Available commands:"
, " mnemonic "
, " wallet "
, " transaction "
, " address "
]

["mnemonic", "--help"] `shouldShowUsage`
[ "Usage: mnemonic COMMAND"
, ""
, "Available options:"
, " -h,--help Show this help text"
, ""
, "Available commands:"
, " generate Generate English BIP-0039 compatible"
, " mnemonic words."
]

["mnemonic", "generate", "--help"] `shouldShowUsage`
[ "Usage: mnemonic generate [--size INT]"
, " Generate English BIP-0039 compatible mnemonic words."
, ""
, "Available options:"
, " -h,--help Show this help text"
, " --size INT number of mnemonic words to"
, " generate. (default: 15)"
]

["wallet", "--help"] `shouldShowUsage`
[ "Usage: wallet COMMAND"
, ""
, "Available options:"
, " -h,--help Show this help text"
, ""
, "Available commands:"
, " list List all known wallets."
, " create Create a new wallet using a sequential"
, " address scheme."
, " get Fetch the wallet with specified id."
, " update Update a wallet."
, " delete Deletes wallet with specified wallet"
, " id."
]

["wallet", "list", "--help"] `shouldShowUsage`
[ "Usage: wallet list [--port INT]"
, " List all known wallets."
, ""
, "Available options:"
, " -h,--help Show this help text"
, " --port INT port used for serving the wallet"
, " API. (default: 8090)"
]

["wallet", "create", "--help"] `shouldShowUsage`
[ "Usage: wallet create [--port INT] STRING"
, " [--address-pool-gap INT]"
, " Create a new wallet using a sequential address scheme."
, ""
, "Available options:"
, " -h,--help Show this help text"
, " --port INT port used for serving the wallet"
, " API. (default: 8090)"
, " --address-pool-gap INT number of unused consecutive addresses"
, " to keep track of. (default: 20)"
]

["wallet", "get", "--help"] `shouldShowUsage`
[ "Usage: wallet get [--port INT] WALLET_ID"
, " Fetch the wallet with specified id."
, ""
, "Available options:"
, " -h,--help Show this help text"
, " --port INT port used for serving the wallet"
, " API. (default: 8090)"
]

["wallet", "update", "--help"] `shouldShowUsage`
[ "Usage: wallet update COMMAND"
, " Update a wallet."
, ""
, "Available options:"
, " -h,--help Show this help text"
, ""
, "Available commands:"
, " name Update a wallet's name."
, " passphrase Update a wallet's passphrase."
]

["wallet", "delete", "--help"] `shouldShowUsage`
[ "Usage: wallet delete [--port INT] WALLET_ID"
, " Deletes wallet with specified wallet id."
, ""
, "Available options:"
, " -h,--help Show this help text"
, " --port INT port used for serving the wallet"
, " API. (default: 8090)"
]

["transaction", "--help"] `shouldShowUsage`
[ "Usage: transaction COMMAND"
, ""
, "Available options:"
, " -h,--help Show this help text"
, ""
, "Available commands:"
, " create Create and submit a new transaction."
, " fees Estimate fees for a transaction."
]

it "--help" $ ["--help"] `shouldShowUsage` mconcat
[]
["transaction", "create", "--help"] `shouldShowUsage`
[ "Usage: transaction create [--port INT] WALLET_ID"
, " --payment PAYMENT"
, " Create and submit a new transaction."
, ""
, "Available options:"
, " -h,--help Show this help text"
, " --port INT port used for serving the wallet"
, " API. (default: 8090)"
, " --payment PAYMENT address to send to and amount to send"
, " separated by @, e.g."
, " '<amount>@<address>'"
]

["transaction", "fees", "--help"] `shouldShowUsage`
[ "Usage: transaction fees [--port INT] WALLET_ID --payment PAYMENT"
, " Estimate fees for a transaction."
, ""
, "Available options:"
, " -h,--help Show this help text"
, " --port INT port used for serving the wallet"
, " API. (default: 8090)"
, " --payment PAYMENT address to send to and amount to send"
, " separated by @, e.g."
, " '<amount>@<address>'"
]

["address", "--help"] `shouldShowUsage`
[ "Usage: address COMMAND"
, ""
, "Available options:"
, " -h,--help Show this help text"
, ""
, "Available commands:"
, " list List all known addresses of a given"
, " wallet."
]

["address", "list", "--help"] `shouldShowUsage`
[ "Usage: address list [--port INT] [--state STRING] WALLET_ID"
, " List all known addresses of a given wallet."
, ""
, "Available options:"
, " -h,--help Show this help text"
, " --port INT port used for serving the wallet"
, " API. (default: 8090)"
, " --state STRING only addresses with the given state:"
, " either 'used' or 'unused'."
]

describe "Can perform roundtrip textual encoding & decoding" $ do
textRoundtrip $ Proxy @(Port "test")

0 comments on commit 1140e97

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