2020-04-28 02:42:44 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-05-15 15:13:43 +02:00
|
|
|
"strings"
|
2020-04-28 02:42:44 +02:00
|
|
|
"testing"
|
|
|
|
|
2023-04-30 19:26:17 +02:00
|
|
|
goversion "github.com/caarlos0/go-version"
|
2024-05-15 15:13:43 +02:00
|
|
|
"github.com/spf13/cobra"
|
2020-04-28 02:42:44 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-04-30 19:26:17 +02:00
|
|
|
var testversion = goversion.Info{
|
|
|
|
GitVersion: "1.2.3",
|
|
|
|
}
|
|
|
|
|
2020-04-28 02:42:44 +02:00
|
|
|
func TestRootCmd(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
mem := &exitMemento{}
|
2023-04-30 19:26:17 +02:00
|
|
|
Execute(testversion, mem.Exit, []string{"-h"})
|
2020-04-28 02:42:44 +02:00
|
|
|
require.Equal(t, 0, mem.code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootCmdHelp(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
mem := &exitMemento{}
|
2023-04-30 19:26:17 +02:00
|
|
|
cmd := newRootCmd(testversion, mem.Exit).cmd
|
2020-04-28 02:42:44 +02:00
|
|
|
cmd.SetArgs([]string{"-h"})
|
|
|
|
require.NoError(t, cmd.Execute())
|
|
|
|
require.Equal(t, 0, mem.code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootCmdVersion(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
2021-04-25 19:20:49 +02:00
|
|
|
mem := &exitMemento{}
|
2023-04-30 19:26:17 +02:00
|
|
|
cmd := newRootCmd(testversion, mem.Exit).cmd
|
2020-04-28 02:42:44 +02:00
|
|
|
cmd.SetOut(&b)
|
|
|
|
cmd.SetArgs([]string{"-v"})
|
|
|
|
require.NoError(t, cmd.Execute())
|
2023-04-30 19:26:17 +02:00
|
|
|
require.Contains(t, b.String(), "1.2.3")
|
2020-04-28 02:42:44 +02:00
|
|
|
require.Equal(t, 0, mem.code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootCmdExitCode(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
mem := &exitMemento{}
|
2023-04-30 19:26:17 +02:00
|
|
|
cmd := newRootCmd(testversion, mem.Exit)
|
2021-04-25 19:20:49 +02:00
|
|
|
args := []string{"check", "--deprecated", "-f", "testdata/good.yml"}
|
2020-04-28 02:42:44 +02:00
|
|
|
cmd.Execute(args)
|
|
|
|
require.Equal(t, 2, mem.code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootRelease(t *testing.T) {
|
2020-12-12 18:27:35 +02:00
|
|
|
setup(t)
|
2021-04-25 19:20:49 +02:00
|
|
|
mem := &exitMemento{}
|
2023-04-30 19:26:17 +02:00
|
|
|
cmd := newRootCmd(testversion, mem.Exit)
|
2020-04-28 02:42:44 +02:00
|
|
|
cmd.Execute([]string{})
|
|
|
|
require.Equal(t, 1, mem.code)
|
|
|
|
}
|
|
|
|
|
2024-06-04 03:12:04 +02:00
|
|
|
func TestRootReleaseVerbose(t *testing.T) {
|
2020-12-12 18:27:35 +02:00
|
|
|
setup(t)
|
2021-04-25 19:20:49 +02:00
|
|
|
mem := &exitMemento{}
|
2023-04-30 19:26:17 +02:00
|
|
|
cmd := newRootCmd(testversion, mem.Exit)
|
2024-06-04 03:12:04 +02:00
|
|
|
cmd.Execute([]string{"r", "--verbose"})
|
2020-04-28 02:42:44 +02:00
|
|
|
require.Equal(t, 1, mem.code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldPrependRelease(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
result := func(args []string) bool {
|
2023-04-30 19:26:17 +02:00
|
|
|
return shouldPrependRelease(newRootCmd(testversion, func(_ int) {}).cmd, args)
|
2020-04-28 02:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no args", func(t *testing.T) {
|
|
|
|
require.True(t, result([]string{}))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("release args", func(t *testing.T) {
|
2024-05-25 20:04:08 +02:00
|
|
|
require.True(t, result([]string{"--skip=validate"}))
|
2020-04-28 02:42:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("several release args", func(t *testing.T) {
|
2024-05-25 20:04:08 +02:00
|
|
|
require.True(t, result([]string{"--skip=validate", "--snapshot"}))
|
2020-04-28 02:42:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
for _, s := range []string{"--help", "-h", "-v", "--version"} {
|
|
|
|
t.Run(s, func(t *testing.T) {
|
|
|
|
require.False(t, result([]string{s}))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("check", func(t *testing.T) {
|
|
|
|
require.False(t, result([]string{"check", "-f", "testdata/good.yml"}))
|
|
|
|
})
|
2020-09-08 20:31:04 +02:00
|
|
|
|
|
|
|
t.Run("help", func(t *testing.T) {
|
|
|
|
require.False(t, result([]string{"help"}))
|
|
|
|
})
|
2020-10-16 13:01:36 +02:00
|
|
|
|
|
|
|
t.Run("__complete", func(t *testing.T) {
|
2020-11-27 14:33:19 +02:00
|
|
|
require.False(t, result([]string{"__complete"}))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("__completeNoDesc", func(t *testing.T) {
|
|
|
|
require.False(t, result([]string{"__completeNoDesc"}))
|
2020-10-16 13:01:36 +02:00
|
|
|
})
|
2020-04-28 02:42:44 +02:00
|
|
|
}
|
2024-05-15 15:13:43 +02:00
|
|
|
|
|
|
|
func TestShouldDisableLogs(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{nil, false},
|
|
|
|
{[]string{"release"}, false},
|
|
|
|
{[]string{"release", "--clean"}, false},
|
|
|
|
{[]string{"help"}, true},
|
|
|
|
{[]string{"completion"}, true},
|
|
|
|
{[]string{"man"}, true},
|
|
|
|
{[]string{"jsonschema"}, true},
|
|
|
|
{[]string{"docs"}, true},
|
|
|
|
{[]string{cobra.ShellCompRequestCmd}, true},
|
|
|
|
{[]string{cobra.ShellCompNoDescRequestCmd}, true},
|
|
|
|
}
|
|
|
|
for _, tC := range testCases {
|
|
|
|
t.Run(strings.Join(tC.args, " "), func(t *testing.T) {
|
|
|
|
require.Equal(t, tC.expect, shouldDisableLogs(tC.args))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|