From cfbacc7ffc95d23af16238434cd025f8110e9a7c Mon Sep 17 00:00:00 2001 From: maxwelbm Date: Wed, 16 Oct 2024 17:52:20 -0300 Subject: [PATCH] test: test unit table simple for command root, version :rotating_light: --- components/mdz/pkg/cmd/root/root_test.go | 50 +++++++++++++++++ .../mdz/pkg/cmd/version/version_test.go | 55 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 components/mdz/pkg/cmd/root/root_test.go create mode 100644 components/mdz/pkg/cmd/version/version_test.go diff --git a/components/mdz/pkg/cmd/root/root_test.go b/components/mdz/pkg/cmd/root/root_test.go new file mode 100644 index 00000000..176dc7dd --- /dev/null +++ b/components/mdz/pkg/cmd/root/root_test.go @@ -0,0 +1,50 @@ +package root + +import ( + "bytes" + "testing" + + "github.com/LerianStudio/midaz/components/mdz/pkg/factory" + "github.com/LerianStudio/midaz/components/mdz/pkg/iostreams" + "github.com/stretchr/testify/assert" +) + +func TestNewCmdRoot(t *testing.T) { + tests := []struct { + name string + version string + expectedOut string + expectedErr string + expectedHelp string + }{ + { + name: "Standard output", + version: "1.0.0", + expectedOut: "1.0.0", + }, + { + name: "Version empty", + version: "", + expectedOut: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + outBuf := new(bytes.Buffer) + errBuf := new(bytes.Buffer) + f := &factory.Factory{ + CLIVersion: tt.version, + IOStreams: &iostreams.IOStreams{ + Out: outBuf, + Err: errBuf, + }, + } + + cmd := NewCmdRoot(f) + cmd.SetArgs([]string{"--help"}) + err := cmd.Execute() + assert.NoError(t, err) + }) + } +} diff --git a/components/mdz/pkg/cmd/version/version_test.go b/components/mdz/pkg/cmd/version/version_test.go new file mode 100644 index 00000000..1e7a19b2 --- /dev/null +++ b/components/mdz/pkg/cmd/version/version_test.go @@ -0,0 +1,55 @@ +package version + +import ( + "bytes" + "testing" + + "github.com/LerianStudio/midaz/components/mdz/pkg/factory" + "github.com/LerianStudio/midaz/components/mdz/pkg/iostreams" + "github.com/fatih/color" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" +) + +func TestFactoryVersionRunE(t *testing.T) { + tests := []struct { + name string + version string + expected string + }{ + { + name: "Standard version output", + version: "1.0.0", + expected: color.New(color.Bold).Sprint("1.0.0") + "\n", + }, + { + name: "Empty version", + version: "", + expected: color.New(color.Bold).Sprint("") + "\n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := &factory.Factory{ + CLIVersion: tt.version, + IOStreams: &iostreams.IOStreams{ + Out: new(bytes.Buffer), + }, + } + + fVersion := &factoryVersion{ + factory: f, + } + cmd := &cobra.Command{ + RunE: fVersion.runE, + } + + err := cmd.Execute() + assert.NoError(t, err) + + output := f.IOStreams.Out.(*bytes.Buffer).String() + assert.Equal(t, tt.expected, output) + }) + } +}