Skip to content

Commit b4e626c

Browse files
authored
chore: add unit tests for printKeyTable (header, rows, uppercase subtype) (argoproj#24274) (argoproj#24706)
Signed-off-by: Yoonji Heo <myeunee@gmail.com>
1 parent d59276a commit b4e626c

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

cmd/argocd/commands/gpg_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package commands
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
11+
appsv1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
12+
)
13+
14+
// splitColumns splits a line produced by tabwriter using runs of 2 or more spaces
15+
// as delimiters to obtain logical columns regardless of alignment padding.
16+
func splitColumns(line string) []string {
17+
re := regexp.MustCompile(`\s{2,}`)
18+
return re.Split(strings.TrimSpace(line), -1)
19+
}
20+
21+
func Test_printKeyTable_Empty(t *testing.T) {
22+
out, err := captureOutput(func() error {
23+
printKeyTable([]appsv1.GnuPGPublicKey{})
24+
return nil
25+
})
26+
require.NoError(t, err)
27+
28+
lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
29+
require.Len(t, lines, 1)
30+
31+
headerCols := splitColumns(lines[0])
32+
assert.Equal(t, []string{"KEYID", "TYPE", "IDENTITY"}, headerCols)
33+
}
34+
35+
func Test_printKeyTable_Single(t *testing.T) {
36+
keys := []appsv1.GnuPGPublicKey{
37+
{
38+
KeyID: "ABCDEF1234567890",
39+
SubType: "rsa4096",
40+
Owner: "Alice <alice@example.com>",
41+
},
42+
}
43+
44+
out, err := captureOutput(func() error {
45+
printKeyTable(keys)
46+
return nil
47+
})
48+
require.NoError(t, err)
49+
50+
lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
51+
require.Len(t, lines, 2)
52+
53+
// Header
54+
assert.Equal(t, []string{"KEYID", "TYPE", "IDENTITY"}, splitColumns(lines[0]))
55+
56+
// Row
57+
row := splitColumns(lines[1])
58+
require.Len(t, row, 3)
59+
assert.Equal(t, "ABCDEF1234567890", row[0])
60+
assert.Equal(t, "RSA4096", row[1]) // subtype upper-cased
61+
assert.Equal(t, "Alice <alice@example.com>", row[2])
62+
}
63+
64+
func Test_printKeyTable_Multiple(t *testing.T) {
65+
keys := []appsv1.GnuPGPublicKey{
66+
{
67+
KeyID: "ABCD",
68+
SubType: "ed25519",
69+
Owner: "User One <one@example.com>",
70+
},
71+
{
72+
KeyID: "0123456789ABCDEF",
73+
SubType: "rsa2048",
74+
Owner: "Second User <second@example.com>",
75+
},
76+
}
77+
78+
out, err := captureOutput(func() error {
79+
printKeyTable(keys)
80+
return nil
81+
})
82+
require.NoError(t, err)
83+
84+
lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
85+
require.Len(t, lines, 3)
86+
87+
// Header
88+
assert.Equal(t, []string{"KEYID", "TYPE", "IDENTITY"}, splitColumns(lines[0]))
89+
90+
// First row
91+
row1 := splitColumns(lines[1])
92+
require.Len(t, row1, 3)
93+
assert.Equal(t, "ABCD", row1[0])
94+
assert.Equal(t, "ED25519", row1[1])
95+
assert.Equal(t, "User One <one@example.com>", row1[2])
96+
97+
// Second row
98+
row2 := splitColumns(lines[2])
99+
require.Len(t, row2, 3)
100+
assert.Equal(t, "0123456789ABCDEF", row2[0])
101+
assert.Equal(t, "RSA2048", row2[1])
102+
assert.Equal(t, "Second User <second@example.com>", row2[2])
103+
}

0 commit comments

Comments
 (0)