github.com/sudo-bmitch/version-bump@v0.0.0-20240503123857-70b0e3f646dd/internal/source/source_test.go (about) 1 package source 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 "github.com/sudo-bmitch/version-bump/internal/config" 9 ) 10 11 func TestSource(t *testing.T) { 12 tests := []struct { 13 name string 14 confSrc config.Source 15 data config.SourceTmplData 16 errGetSource error 17 errGet error 18 errKey error 19 expectGet string 20 expectKey string 21 }{ 22 { 23 name: "unknown type", 24 confSrc: config.Source{ 25 Type: "unknown", 26 }, 27 errGetSource: fmt.Errorf("source type not known: unknown"), 28 }, 29 { 30 name: "custom", 31 confSrc: config.Source{ 32 Name: "custom", 33 Type: "custom", 34 Key: "custom-test", 35 Args: map[string]string{ 36 "cmd": "echo 1.2.3.4", 37 }, 38 }, 39 data: config.SourceTmplData{ 40 Filename: "/dev/null", 41 ScanArgs: map[string]string{}, 42 ScanMatch: map[string]string{}, 43 SourceArgs: map[string]string{ 44 "cmd": "echo 1.2.3.4", 45 }, 46 }, 47 expectGet: "1.2.3.4", 48 expectKey: "custom-test", 49 }, 50 { 51 name: "manual", 52 confSrc: config.Source{ 53 Name: "manual", 54 Type: "manual", 55 Key: "{{ .ScanArgs.Key }}", 56 Args: map[string]string{}, 57 }, 58 data: config.SourceTmplData{ 59 Filename: "/dev/null", 60 ScanArgs: map[string]string{ 61 "Key": "manual-test", 62 }, 63 ScanMatch: map[string]string{ 64 "Version": "4.3.2.1", 65 }, 66 SourceArgs: map[string]string{}, 67 }, 68 expectGet: "4.3.2.1", 69 expectKey: "manual-test", 70 }, 71 { 72 name: "git tags", 73 confSrc: config.Source{ 74 Name: "git tags", 75 Type: "git", 76 Key: "git tag", 77 Args: map[string]string{ 78 // TODO: switch to version-bump repo after it has enough tags 79 "url": "https://github.com/regclient/regclient.git", 80 "type": "tag", 81 }, 82 Filter: config.SourceFilter{ 83 Expr: `^v0.4.[1-5]$`, 84 }, 85 Sort: config.SourceSort{ 86 Method: "semver", 87 }, 88 Template: `["{{ index .VerMap ( index .VerList 1 ) }}", "{{ index .VerMap ( index .VerList 0 ) }}"]`, 89 }, 90 data: config.SourceTmplData{ 91 Filename: "/dev/null", 92 ScanArgs: map[string]string{}, 93 ScanMatch: map[string]string{}, 94 SourceArgs: map[string]string{}, 95 }, 96 expectGet: `["v0.4.4", "v0.4.5"]`, 97 expectKey: "git tag", 98 }, 99 { 100 name: "git ref", 101 confSrc: config.Source{ 102 Name: "git ref", 103 Type: "git", 104 Key: "git ref", 105 Args: map[string]string{ 106 // TODO: switch to version-bump repo after it has enough tags 107 "url": "https://github.com/regclient/regclient.git", 108 "type": "ref", 109 }, 110 Filter: config.SourceFilter{ 111 Expr: `^v0.4.3$`, 112 }, 113 }, 114 data: config.SourceTmplData{ 115 Filename: "/dev/null", 116 ScanArgs: map[string]string{}, 117 ScanMatch: map[string]string{}, 118 SourceArgs: map[string]string{}, 119 }, 120 expectGet: "b0ac3e9413b1079c8b14df5c201a2a2129d9d9e1", 121 expectKey: "git ref", 122 }, 123 { 124 name: "registry tags", 125 confSrc: config.Source{ 126 Name: "registry tags", 127 Type: "registry", 128 Key: "registry tag", 129 Args: map[string]string{ 130 // TODO: switch to version-bump repo after it has enough tags 131 "repo": "ghcr.io/regclient/regctl", 132 "type": "tag", 133 }, 134 Filter: config.SourceFilter{ 135 Expr: `^v0.4.[1-5]$`, 136 }, 137 Sort: config.SourceSort{ 138 Method: "semver", 139 Asc: true, // reverse the sort 140 }, 141 Template: `["{{ index .VerMap ( index .VerList 1 ) }}", "{{ index .VerMap ( index .VerList 0 ) }}"]`, 142 }, 143 data: config.SourceTmplData{ 144 Filename: "/dev/null", 145 ScanArgs: map[string]string{}, 146 ScanMatch: map[string]string{}, 147 SourceArgs: map[string]string{}, 148 }, 149 expectGet: `["v0.4.2", "v0.4.1"]`, 150 expectKey: "registry tag", 151 }, 152 { 153 name: "registry digest", 154 confSrc: config.Source{ 155 Name: "registry digest", 156 Type: "registry", 157 Key: "registry digest", 158 Args: map[string]string{ 159 // TODO: switch to version-bump repo after it has enough tags 160 "image": "ghcr.io/regclient/regctl:v0.4.3", 161 "type": "digest", 162 }, 163 }, 164 data: config.SourceTmplData{ 165 Filename: "/dev/null", 166 ScanArgs: map[string]string{}, 167 ScanMatch: map[string]string{}, 168 SourceArgs: map[string]string{}, 169 }, 170 expectGet: "sha256:b76626b3eb7e2380183b29f550bea56dea67685907d4ec61b56ff770ae2d7138", 171 expectKey: "registry digest", 172 }, 173 { 174 name: "github release version", 175 confSrc: config.Source{ 176 Name: "github release", 177 Type: "gh-release", 178 Key: "github release", 179 Args: map[string]string{ 180 // TODO: switch to version-bump repo after it has enough tags 181 "repo": "regclient/regclient", 182 }, 183 Filter: config.SourceFilter{ 184 Expr: `^v0.4.[1-5]$`, 185 Template: `{{ .Meta.TagName }}`, 186 }, 187 Sort: config.SourceSort{ 188 Method: "semver", 189 }, 190 Template: `["{{ index .VerMap ( index .VerList 1 ) }}", "{{ index .VerMap ( index .VerList 0 ) }}"]`, 191 }, 192 data: config.SourceTmplData{ 193 Filename: "/dev/null", 194 ScanArgs: map[string]string{}, 195 ScanMatch: map[string]string{}, 196 SourceArgs: map[string]string{}, 197 }, 198 expectGet: `["v0.4.4", "v0.4.5"]`, 199 expectKey: "github release", 200 }, 201 { 202 name: "github release artifact", 203 confSrc: config.Source{ 204 Name: "github artifact", 205 Type: "gh-release", 206 Key: "github artifact", 207 Args: map[string]string{ 208 // TODO: switch to version-bump repo after it has enough tags 209 "repo": "regclient/regclient", 210 "type": "artifact", 211 "artifact": "regctl-linux-amd64", 212 }, 213 Filter: config.SourceFilter{ 214 Expr: `^v0.4.[1-5]$`, 215 }, 216 Sort: config.SourceSort{ 217 Method: "semver", 218 }, 219 // Template: `["{{ index .VerMap ( index .VerList 1 ) }}", "{{ index .VerMap ( index .VerList 0 ) }}"]`, 220 }, 221 data: config.SourceTmplData{ 222 Filename: "/dev/null", 223 ScanArgs: map[string]string{}, 224 ScanMatch: map[string]string{}, 225 SourceArgs: map[string]string{}, 226 }, 227 expectGet: `https://github.com/regclient/regclient/releases/download/v0.4.5/regctl-linux-amd64`, 228 expectKey: "github artifact", 229 }, 230 // TODO: numeric sort 231 } 232 for _, tt := range tests { 233 t.Run(tt.name, func(t *testing.T) { 234 src, err := Get(tt.confSrc) 235 if tt.errGetSource != nil { 236 if err == nil { 237 t.Errorf("get source did not fail") 238 } else if !errors.Is(err, tt.errGetSource) && err.Error() != tt.errGetSource.Error() { 239 t.Errorf("unexpected error, expected %v, received %v", tt.errGetSource, err) 240 } 241 return 242 } else if err != nil { 243 t.Errorf("get source failed: %v", err) 244 return 245 } 246 getStr, err := src.Get(tt.data) 247 if tt.errGet != nil { 248 if err == nil { 249 t.Errorf("get did not fail") 250 } else if !errors.Is(err, tt.errGet) && err.Error() != tt.errGet.Error() { 251 t.Errorf("unexpected error, expected %v, received %v", tt.errGet, err) 252 } 253 return 254 } else if err != nil { 255 t.Errorf("get failed: %v", err) 256 return 257 } else if tt.expectGet != getStr { 258 t.Errorf("get unexpected response, expected %s, received %s", tt.expectGet, getStr) 259 } 260 keyStr, err := src.Key(tt.data) 261 if tt.errKey != nil { 262 if err == nil { 263 t.Errorf("key did not fail") 264 } else if !errors.Is(err, tt.errKey) && err.Error() != tt.errKey.Error() { 265 t.Errorf("unexpected error, expected %v, received %v", tt.errKey, err) 266 } 267 return 268 } else if err != nil { 269 t.Errorf("key failed: %v", err) 270 return 271 } else if tt.expectKey != keyStr { 272 t.Errorf("key unexpected response, expected %s, received %s", tt.expectKey, keyStr) 273 } 274 }) 275 } 276 277 }