summaryrefslogtreecommitdiff
path: root/cmd/version_test.go
blob: 76c6532fc363d09b219e1320add1ac0e91d8dfbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/argoproj-labs/argocd-image-updater/pkg/version"
)

// Helper function to create the expected full version output
func fullVersionOutput() string {
	return version.Useragent() + "\n" +
		"  BuildDate: " + version.BuildDate() + "\n" +
		"  GitCommit: " + version.GitCommit() + "\n" +
		"  GoVersion: " + version.GoVersion() + "\n" +
		"  GoCompiler: " + version.GoCompiler() + "\n" +
		"  Platform: " + version.GoPlatform() + "\n"
}

func TestNewVersionCommand(t *testing.T) {
	tests := []struct {
		name     string
		args     []string
		expected string
	}{
		{
			name:     "default output",
			args:     []string{},
			expected: fullVersionOutput(),
		},
		{
			name:     "short flag output",
			args:     []string{"--short"},
			expected: version.Version() + "\n",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cmd := newVersionCommand()
			buf := new(bytes.Buffer)
			cmd.SetOut(buf)
			cmd.SetArgs(tt.args)
			err := cmd.Execute()
			assert.NoError(t, err)
			assert.Equal(t, tt.expected, buf.String())
		})
	}
}