Skip to content

Commit

Permalink
Add tests to test the cli
Browse files Browse the repository at this point in the history
Test the version, completion and help

Signed-off-by: Jo Vandeginste <Jo.Vandeginste@kuleuven.be>
  • Loading branch information
jovandeginste committed Aug 9, 2024
1 parent d1e69e8 commit fcd03c5
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 14 deletions.
15 changes: 7 additions & 8 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package main

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

func completionCmd(rootCmd *cobra.Command) *cobra.Command {
//nolint:errcheck
func completionCmd(cmd *cobra.Command) *cobra.Command {
return &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Expand Down Expand Up @@ -49,21 +49,20 @@ PowerShell:
# To load completions for every new session, run:
PS> %[1]s completion powershell > %[1]s.ps1
# and source this file from your PowerShell profile.
`, rootCmd.Root().Name()),
`, cmd.Root().Name()),
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
//nolint:errcheck
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
cmd.Root().GenBashCompletion(cmd.OutOrStdout())
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
cmd.Root().GenZshCompletion(cmd.OutOrStdout())
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
cmd.Root().GenFishCompletion(cmd.OutOrStdout(), true)
case "powershell":
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
cmd.Root().GenPowerShellCompletionWithDesc(cmd.OutOrStdout())
}
},
}
Expand Down
99 changes: 99 additions & 0 deletions completion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"bytes"
"testing"

"github.com/jovandeginste/payme/payment"
"github.com/stretchr/testify/assert"
)

func TestHelpCommand(t *testing.T) {
q := qrParams{
Payment: payment.New(),
}

cmdRoot, err := newCommand(&q)
assert.NoError(t, err)

actualOut := new(bytes.Buffer)
actualErr := new(bytes.Buffer)

cmdRoot.SetOut(actualOut)
cmdRoot.SetErr(actualErr)
cmdRoot.SetArgs([]string{"help"})

_, err = cmdRoot.ExecuteC()

assert.NoError(t, err)
assert.Contains(t, actualOut.String(), "Generate SEPA payment QR code")
assert.Empty(t, actualErr.String())
}

func TestCompletionHelp(t *testing.T) {
q := qrParams{
Payment: payment.New(),
}

cmdRoot, err := newCommand(&q)
assert.NoError(t, err)

actualOut := new(bytes.Buffer)
actualErr := new(bytes.Buffer)

cmdRoot.SetOut(actualOut)
cmdRoot.SetErr(actualErr)
cmdRoot.SetArgs([]string{"completion", "--help"})

_, err = cmdRoot.ExecuteC()

assert.NoError(t, err)
assert.Contains(t, actualOut.String(), "payme completion [bash|zsh|fish|powershell]")
assert.Empty(t, actualErr.String())
}

func TestCompletionShells(t *testing.T) {
q := qrParams{
Payment: payment.New(),
}

cmdRoot, err := newCommand(&q)
assert.NoError(t, err)

for _, shell := range []string{"bash", "zsh", "fish", "powershell"} {
actualOut := new(bytes.Buffer)
actualErr := new(bytes.Buffer)

cmdRoot.SetOut(actualOut)
cmdRoot.SetErr(actualErr)
cmdRoot.SetArgs([]string{"completion", shell})

_, err = cmdRoot.ExecuteC()

assert.NoError(t, err)
assert.Contains(t, actualOut.String(), shell+" completion for payme")
assert.Empty(t, actualErr.String())
}
}

func TestVersionCommand(t *testing.T) {
q := qrParams{
Payment: payment.New(),
}

cmdRoot, err := newCommand(&q)
assert.NoError(t, err)

actualOut := new(bytes.Buffer)
actualErr := new(bytes.Buffer)

cmdRoot.SetOut(actualOut)
cmdRoot.SetErr(actualErr)
cmdRoot.SetArgs([]string{"--version"})

_, err = cmdRoot.ExecuteC()

assert.NoError(t, err)
assert.Contains(t, actualOut.String(), "payme version local (local), built manually")
assert.Empty(t, actualErr.String())
}
25 changes: 19 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ func main() {
Payment: payment.New(),
}

cmdRoot, err := newCommand(&q)
if err != nil {
log.Fatal(err)
}

if err := cmdRoot.Execute(); err != nil {
log.Fatal(err)
}
}

func newCommand(q *qrParams) (*cobra.Command, error) {
cmdRoot := &cobra.Command{
Use: "payme",
Version: fmt.Sprintf("%s (%s), built %s\n", gitRefName, gitCommit, buildTime),
Expand All @@ -46,19 +57,19 @@ func main() {

cmdRoot.AddCommand(completionCmd(cmdRoot))

q.init(cmdRoot)

if err := cmdRoot.Execute(); err != nil {
log.Fatal(err)
if err := q.init(cmdRoot); err != nil {
return nil, err
}

return cmdRoot, nil
}

func (q *qrParams) init(cmdRoot *cobra.Command) {
func (q *qrParams) init(cmdRoot *cobra.Command) error {
viper.SetEnvPrefix("PAYME")

for _, e := range []string{"name", "bic", "iban"} {
if err := viper.BindEnv(e); err != nil {
log.Fatal(err)
return err
}
}

Expand All @@ -75,6 +86,8 @@ func (q *qrParams) init(cmdRoot *cobra.Command) {
cmdRoot.Flags().StringVar(&q.Payment.Remittance, "remittance", "", "Remittance (message)")
cmdRoot.Flags().StringVar(&q.Payment.Purpose, "purpose", "", "Purpose of the transaction")
cmdRoot.Flags().BoolVar(&q.Payment.RemittanceIsStructured, "structured", false, "Make the remittance (message) structured")

return nil
}

func (q *qrParams) generate() {
Expand Down

0 comments on commit fcd03c5

Please sign in to comment.