Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for version subcommand #26

Merged
merged 2 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions cmd/test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* -------------------------------------------------------------------------- *\
* Apache 2.0 License Copyright © 2022 The Aurae Authors *
* *
* +--------------------------------------------+ *
* | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | *
* | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | *
* | ███████║██║ ██║██████╔╝███████║█████╗ | *
* | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | *
* | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | *
* | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | *
* +--------------------------------------------+ *
* *
* Distributed Systems Runtime *
* *
* -------------------------------------------------------------------------- *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
\* -------------------------------------------------------------------------- */

package test

import (
"bytes"
"testing"

"github.com/spf13/cobra"
)

type Suite struct {
Title string
Args []string
ExpectedMessage string
IsErrorExpected bool
}

func ExecuteSuiteTest(t *testing.T, newCMD func() *cobra.Command, suites []Suite) {
for _, test := range suites {
t.Run(test.Title, func(t *testing.T) {
buffer := &bytes.Buffer{}
cmd := newCMD()
cmd.SetOut(buffer)
cmd.SetErr(buffer)
cmd.SetArgs(test.Args)

err := cmd.Execute()
if test.IsErrorExpected {
if !IsNil(err) {
if !IsEqualString(test.ExpectedMessage, err.Error()) {
t.Errorf("\nError message of command \"%s\" was incorrect, got: \n%s\nwant: \n%s.", test.Title, err.Error(), test.ExpectedMessage)
}
} else {
t.Errorf("\nError in command \"%s\" expected.", test.Title)
}
} else if IsNil(err) {
if !IsEqualString(test.ExpectedMessage, buffer.String()) {
t.Errorf("\nTest of command \"%s\" was incorrect, got: \n%swant: \n%s.", test.Title, buffer.String(), test.ExpectedMessage)
}
}
})
}
}

func IsEqualString(s1, s2 string) bool {
return s1 == s2
}

func IsNil(object interface{}) bool {
return object == nil
}
72 changes: 72 additions & 0 deletions cmd/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* -------------------------------------------------------------------------- *\
* Apache 2.0 License Copyright © 2022 The Aurae Authors *
* *
* +--------------------------------------------+ *
* | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | *
* | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | *
* | ███████║██║ ██║██████╔╝███████║█████╗ | *
* | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | *
* | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | *
* | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | *
* +--------------------------------------------+ *
* *
* Distributed Systems Runtime *
* *
* -------------------------------------------------------------------------- *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
\* -------------------------------------------------------------------------- */

package version

import (
"testing"

cmdTest "github.com/aurae-runtime/ae/cmd/test"
"github.com/prometheus/common/version"
)

func TestVersionCMD(t *testing.T) {
version.Version = "v0.1.0"
version.BuildDate = "2023-01-07"
version.Revision = "a7c46aa017bc447ece506629196bd0548cbbc469"
testSuite := []cmdTest.Suite{
{
Title: "empty args",
Args: []string{},
IsErrorExpected: false,
ExpectedMessage: `buildTime: "2023-01-07"
version: v0.1.0
commit: a7c46aa017bc447ece506629196bd0548cbbc469

`,
},
{
Title: "print version in json",
Args: []string{"--output", "json"},
IsErrorExpected: false,
ExpectedMessage: `{"buildTime":"2023-01-07","version":"v0.1.0","commit":"a7c46aa017bc447ece506629196bd0548cbbc469"}
`,
},
{
Title: "print short version",
Args: []string{"--short"},
IsErrorExpected: false,
ExpectedMessage: `version: v0.1.0

`,
},
}
cmdTest.ExecuteSuiteTest(t, NewCMD, testSuite)
}