|
| 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 | +} |
0 commit comments