Skip to content

Commit efbc434

Browse files
committed
[feat]: [CI-17150]: uts
1 parent dc5df99 commit efbc434

2 files changed

Lines changed: 264 additions & 0 deletions

File tree

main_test.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"os"
6+
"testing"
7+
8+
"github.com/drone/plugin/utils"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
// Save original command line arguments and flags
13+
func saveOriginalFlags() []string {
14+
oldArgs := make([]string, len(os.Args))
15+
copy(oldArgs, os.Args)
16+
return oldArgs
17+
}
18+
19+
// Restore original command line arguments and flags
20+
func restoreOriginalFlags(oldArgs []string) {
21+
os.Args = make([]string, len(oldArgs))
22+
copy(os.Args, oldArgs)
23+
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
24+
}
25+
26+
func TestFlagParsing(t *testing.T) {
27+
// Save original flags
28+
oldArgs := saveOriginalFlags()
29+
defer restoreOriginalFlags(oldArgs)
30+
31+
tests := []struct {
32+
name string
33+
args []string
34+
expectedFlags struct {
35+
name string
36+
repo string
37+
ref string
38+
sha string
39+
kind string
40+
downloadOnly bool
41+
disableClone bool
42+
binarySources []string
43+
}
44+
}{
45+
{
46+
name: "all flags set",
47+
args: []string{
48+
"cmd",
49+
"-name", "test-plugin",
50+
"-repo", "github.com/test/repo",
51+
"-ref", "main",
52+
"-sha", "abc123",
53+
"-kind", "harness",
54+
"-download-only",
55+
"-disable-clone",
56+
"-sources", "source1;source2;source3",
57+
},
58+
expectedFlags: struct {
59+
name string
60+
repo string
61+
ref string
62+
sha string
63+
kind string
64+
downloadOnly bool
65+
disableClone bool
66+
binarySources []string
67+
}{
68+
name: "test-plugin",
69+
repo: "github.com/test/repo",
70+
ref: "main",
71+
sha: "abc123",
72+
kind: "harness",
73+
downloadOnly: true,
74+
disableClone: true,
75+
binarySources: []string{"source1", "source2", "source3"},
76+
},
77+
},
78+
{
79+
name: "minimal flags",
80+
args: []string{
81+
"cmd",
82+
"-name", "minimal-plugin",
83+
"-repo", "github.com/test/minimal",
84+
},
85+
expectedFlags: struct {
86+
name string
87+
repo string
88+
ref string
89+
sha string
90+
kind string
91+
downloadOnly bool
92+
disableClone bool
93+
binarySources []string
94+
}{
95+
name: "minimal-plugin",
96+
repo: "github.com/test/minimal",
97+
ref: "",
98+
sha: "",
99+
kind: "",
100+
downloadOnly: false,
101+
disableClone: false,
102+
binarySources: []string{},
103+
},
104+
},
105+
{
106+
name: "multiple sources",
107+
args: []string{
108+
"cmd",
109+
"-name", "source-plugin",
110+
"-sources", "source1;source2",
111+
"-sources", "source3;source4",
112+
},
113+
expectedFlags: struct {
114+
name string
115+
repo string
116+
ref string
117+
sha string
118+
kind string
119+
downloadOnly bool
120+
disableClone bool
121+
binarySources []string
122+
}{
123+
name: "source-plugin",
124+
repo: "",
125+
ref: "",
126+
sha: "",
127+
kind: "",
128+
downloadOnly: false,
129+
disableClone: false,
130+
binarySources: []string{"source1", "source2", "source3", "source4"},
131+
},
132+
},
133+
}
134+
135+
for _, tt := range tests {
136+
t.Run(tt.name, func(t *testing.T) {
137+
// Reset flags for each test
138+
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
139+
os.Args = tt.args
140+
141+
// Reset global variables
142+
name = ""
143+
repo = ""
144+
ref = ""
145+
sha = ""
146+
kind = ""
147+
downloadOnly = false
148+
disableClone = false
149+
binarySources = utils.CustomStringSliceFlag{}
150+
151+
// Parse flags
152+
flag.StringVar(&name, "name", "", "plugin name")
153+
flag.StringVar(&repo, "repo", "", "plugin repository")
154+
flag.StringVar(&ref, "ref", "", "plugin reference")
155+
flag.StringVar(&sha, "sha", "", "plugin commit")
156+
flag.StringVar(&kind, "kind", "", "plugin kind")
157+
flag.BoolVar(&downloadOnly, "download-only", false, "plugin downloadOnly")
158+
flag.BoolVar(&disableClone, "disable-clone", false, "disable clone functionality")
159+
flag.Var(&binarySources, "sources", "source urls to download binaries")
160+
flag.Parse()
161+
162+
// Assert flag values
163+
assert.Equal(t, tt.expectedFlags.name, name)
164+
assert.Equal(t, tt.expectedFlags.repo, repo)
165+
assert.Equal(t, tt.expectedFlags.ref, ref)
166+
assert.Equal(t, tt.expectedFlags.sha, sha)
167+
assert.Equal(t, tt.expectedFlags.kind, kind)
168+
assert.Equal(t, tt.expectedFlags.downloadOnly, downloadOnly)
169+
assert.Equal(t, tt.expectedFlags.disableClone, disableClone)
170+
assert.Equal(t, tt.expectedFlags.binarySources, binarySources.GetValue())
171+
})
172+
}
173+
}

utils/custom_string_slice_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCustomStringSliceFlag_Set(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
input string
13+
expected []string
14+
}{
15+
{
16+
name: "empty string",
17+
input: "",
18+
expected: []string{""},
19+
},
20+
{
21+
name: "single value",
22+
input: "test",
23+
expected: []string{"test"},
24+
},
25+
{
26+
name: "multiple values",
27+
input: "test1;test2;test3",
28+
expected: []string{"test1", "test2", "test3"},
29+
},
30+
{
31+
name: "values with spaces",
32+
input: "test 1; test 2 ; test 3",
33+
expected: []string{"test 1", "test 2", "test 3"},
34+
},
35+
{
36+
name: "empty values between delimiters",
37+
input: "test1;;test2",
38+
expected: []string{"test1", "", "test2"},
39+
},
40+
}
41+
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
flag := &CustomStringSliceFlag{}
45+
err := flag.Set(tt.input)
46+
assert.NoError(t, err)
47+
assert.Equal(t, tt.expected, flag.Value)
48+
})
49+
}
50+
}
51+
52+
func TestCustomStringSliceFlag_MultipleSet(t *testing.T) {
53+
flag := &CustomStringSliceFlag{}
54+
55+
// Test multiple Set calls
56+
inputs := []string{
57+
"test1;test2",
58+
"test3",
59+
"test4;test5",
60+
}
61+
62+
expected := []string{
63+
"test1", "test2", "test3", "test4", "test5",
64+
}
65+
66+
for _, input := range inputs {
67+
err := flag.Set(input)
68+
assert.NoError(t, err)
69+
}
70+
71+
assert.Equal(t, expected, flag.Value)
72+
}
73+
74+
func TestCustomStringSliceFlag_Integration(t *testing.T) {
75+
flag := &CustomStringSliceFlag{}
76+
77+
// Test the complete workflow
78+
err := flag.Set("value1;value2")
79+
assert.NoError(t, err)
80+
81+
err = flag.Set("value3")
82+
assert.NoError(t, err)
83+
84+
// Test GetValue
85+
values := flag.GetValue()
86+
assert.Equal(t, []string{"value1", "value2", "value3"}, values)
87+
88+
// Test String
89+
str := flag.String()
90+
assert.Equal(t, "value1;value2;value3", str)
91+
}

0 commit comments

Comments
 (0)