diff --git a/completion.go b/completion.go index d5aded3..0b89234 100644 --- a/completion.go +++ b/completion.go @@ -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", @@ -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()) } }, } diff --git a/completion_test.go b/completion_test.go new file mode 100644 index 0000000..32d2eb8 --- /dev/null +++ b/completion_test.go @@ -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()) +} diff --git a/main.go b/main.go index e5a8843..6fa589c 100644 --- a/main.go +++ b/main.go @@ -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), @@ -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 } } @@ -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() {