-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
173 lines (163 loc) · 4.32 KB
/
main_test.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"flag"
"os"
"testing"
"github.com/drone/plugin/utils"
"github.com/stretchr/testify/assert"
)
// Save original command line arguments and flags
func saveOriginalFlags() []string {
oldArgs := make([]string, len(os.Args))
copy(oldArgs, os.Args)
return oldArgs
}
// Restore original command line arguments and flags
func restoreOriginalFlags(oldArgs []string) {
os.Args = make([]string, len(oldArgs))
copy(os.Args, oldArgs)
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}
func TestFlagParsing(t *testing.T) {
// Save original flags
oldArgs := saveOriginalFlags()
defer restoreOriginalFlags(oldArgs)
tests := []struct {
name string
args []string
expectedFlags struct {
name string
repo string
ref string
sha string
kind string
downloadOnly bool
disableClone bool
binarySources []string
}
}{
{
name: "all flags set",
args: []string{
"cmd",
"-name", "test-plugin",
"-repo", "github.com/test/repo",
"-ref", "main",
"-sha", "abc123",
"-kind", "harness",
"-download-only",
"-disable-clone",
"-sources", "source1;source2;source3",
},
expectedFlags: struct {
name string
repo string
ref string
sha string
kind string
downloadOnly bool
disableClone bool
binarySources []string
}{
name: "test-plugin",
repo: "github.com/test/repo",
ref: "main",
sha: "abc123",
kind: "harness",
downloadOnly: true,
disableClone: true,
binarySources: []string{"source1", "source2", "source3"},
},
},
{
name: "minimal flags",
args: []string{
"cmd",
"-name", "minimal-plugin",
"-repo", "github.com/test/minimal",
},
expectedFlags: struct {
name string
repo string
ref string
sha string
kind string
downloadOnly bool
disableClone bool
binarySources []string
}{
name: "minimal-plugin",
repo: "github.com/test/minimal",
ref: "",
sha: "",
kind: "",
downloadOnly: false,
disableClone: false,
binarySources: []string{},
},
},
{
name: "multiple sources",
args: []string{
"cmd",
"-name", "source-plugin",
"-sources", "source1;source2",
"-sources", "source3;source4",
},
expectedFlags: struct {
name string
repo string
ref string
sha string
kind string
downloadOnly bool
disableClone bool
binarySources []string
}{
name: "source-plugin",
repo: "",
ref: "",
sha: "",
kind: "",
downloadOnly: false,
disableClone: false,
binarySources: []string{"source1", "source2", "source3", "source4"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset flags for each test
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
os.Args = tt.args
// Reset global variables
name = ""
repo = ""
ref = ""
sha = ""
kind = ""
downloadOnly = false
disableClone = false
binarySources = utils.CustomStringSliceFlag{}
// Parse flags
flag.StringVar(&name, "name", "", "plugin name")
flag.StringVar(&repo, "repo", "", "plugin repository")
flag.StringVar(&ref, "ref", "", "plugin reference")
flag.StringVar(&sha, "sha", "", "plugin commit")
flag.StringVar(&kind, "kind", "", "plugin kind")
flag.BoolVar(&downloadOnly, "download-only", false, "plugin downloadOnly")
flag.BoolVar(&disableClone, "disable-clone", false, "disable clone functionality")
flag.Var(&binarySources, "sources", "source urls to download binaries")
flag.Parse()
// Assert flag values
assert.Equal(t, tt.expectedFlags.name, name)
assert.Equal(t, tt.expectedFlags.repo, repo)
assert.Equal(t, tt.expectedFlags.ref, ref)
assert.Equal(t, tt.expectedFlags.sha, sha)
assert.Equal(t, tt.expectedFlags.kind, kind)
assert.Equal(t, tt.expectedFlags.downloadOnly, downloadOnly)
assert.Equal(t, tt.expectedFlags.disableClone, disableClone)
assert.Equal(t, tt.expectedFlags.binarySources, binarySources.GetValue())
})
}
}