github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/internal/artifact/artifact_test.go (about) 1 package artifact 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 "golang.org/x/sync/errgroup" 12 ) 13 14 // ensure Type implements the stringer interface... 15 var _ fmt.Stringer = Type(0) 16 17 func TestAdd(t *testing.T) { 18 var g errgroup.Group 19 artifacts := New() 20 for _, a := range []*Artifact{ 21 { 22 Name: "foo", 23 Type: UploadableArchive, 24 }, 25 { 26 Name: "bar", 27 Type: Binary, 28 }, 29 { 30 Name: "foobar", 31 Type: DockerImage, 32 }, 33 { 34 Name: "check", 35 Type: Checksum, 36 }, 37 } { 38 a := a 39 g.Go(func() error { 40 artifacts.Add(a) 41 return nil 42 }) 43 } 44 require.NoError(t, g.Wait()) 45 require.Len(t, artifacts.List(), 4) 46 } 47 48 func TestFilter(t *testing.T) { 49 data := []*Artifact{ 50 { 51 Name: "foo", 52 Goos: "linux", 53 Goarch: "arm", 54 }, 55 { 56 Name: "bar", 57 Goarch: "amd64", 58 }, 59 { 60 Name: "foobar", 61 Goarm: "6", 62 }, 63 { 64 Name: "check", 65 Type: Checksum, 66 }, 67 { 68 Name: "checkzumm", 69 Type: Checksum, 70 }, 71 } 72 artifacts := New() 73 for _, a := range data { 74 artifacts.Add(a) 75 } 76 77 require.Len(t, artifacts.Filter(ByGoos("linux")).items, 1) 78 require.Len(t, artifacts.Filter(ByGoos("darwin")).items, 0) 79 80 require.Len(t, artifacts.Filter(ByGoarch("amd64")).items, 1) 81 require.Len(t, artifacts.Filter(ByGoarch("386")).items, 0) 82 83 require.Len(t, artifacts.Filter(ByGoarm("6")).items, 1) 84 require.Len(t, artifacts.Filter(ByGoarm("7")).items, 0) 85 86 require.Len(t, artifacts.Filter(ByType(Checksum)).items, 2) 87 require.Len(t, artifacts.Filter(ByType(Binary)).items, 0) 88 89 require.Len(t, artifacts.Filter(nil).items, 5) 90 91 require.Len(t, artifacts.Filter( 92 And( 93 ByType(Checksum), 94 func(a *Artifact) bool { 95 return a.Name == "checkzumm" 96 }, 97 ), 98 ).List(), 1) 99 100 require.Len(t, artifacts.Filter( 101 Or( 102 ByType(Checksum), 103 And( 104 ByGoos("linux"), 105 ByGoarm("arm"), 106 ), 107 ), 108 ).List(), 2) 109 } 110 111 func TestGroupByPlatform(t *testing.T) { 112 data := []*Artifact{ 113 { 114 Name: "foo", 115 Goos: "linux", 116 Goarch: "amd64", 117 }, 118 { 119 Name: "bar", 120 Goos: "linux", 121 Goarch: "amd64", 122 }, 123 { 124 Name: "foobar", 125 Goos: "linux", 126 Goarch: "arm", 127 Goarm: "6", 128 }, 129 { 130 Name: "foobar", 131 Goos: "linux", 132 Goarch: "mips", 133 Goarm: "softfloat", 134 }, 135 { 136 Name: "foobar", 137 Goos: "linux", 138 Goarch: "mips", 139 Goarm: "hardfloat", 140 }, 141 { 142 Name: "check", 143 Type: Checksum, 144 }, 145 } 146 artifacts := New() 147 for _, a := range data { 148 artifacts.Add(a) 149 } 150 151 groups := artifacts.GroupByPlatform() 152 require.Len(t, groups["linuxamd64"], 2) 153 require.Len(t, groups["linuxarm6"], 1) 154 require.Len(t, groups["linuxmipssoftfloat"], 1) 155 require.Len(t, groups["linuxmipshardfloat"], 1) 156 } 157 158 func TestChecksum(t *testing.T) { 159 folder := t.TempDir() 160 file := filepath.Join(folder, "subject") 161 require.NoError(t, os.WriteFile(file, []byte("lorem ipsum"), 0o644)) 162 163 artifact := Artifact{ 164 Path: file, 165 } 166 167 for algo, result := range map[string]string{ 168 "sha256": "5e2bf57d3f40c4b6df69daf1936cb766f832374b4fc0259a7cbff06e2f70f269", 169 "sha512": "f80eebd9aabb1a15fb869ed568d858a5c0dca3d5da07a410e1bd988763918d973e344814625f7c844695b2de36ffd27af290d0e34362c51dee5947d58d40527a", 170 "sha1": "bfb7759a67daeb65410490b4d98bb9da7d1ea2ce", 171 "crc32": "72d7748e", 172 "md5": "80a751fde577028640c419000e33eba6", 173 "sha224": "e191edf06005712583518ced92cc2ac2fac8d6e4623b021a50736a91", 174 "sha384": "597493a6cf1289757524e54dfd6f68b332c7214a716a3358911ef5c09907adc8a654a18c1d721e183b0025f996f6e246", 175 } { 176 t.Run(algo, func(t *testing.T) { 177 sum, err := artifact.Checksum(algo) 178 require.NoError(t, err) 179 require.Equal(t, result, sum) 180 }) 181 } 182 } 183 184 func TestChecksumFileDoesntExist(t *testing.T) { 185 file := filepath.Join(t.TempDir(), "nope") 186 artifact := Artifact{ 187 Path: file, 188 } 189 sum, err := artifact.Checksum("sha1") 190 require.EqualError(t, err, fmt.Sprintf(`failed to checksum: open %s: no such file or directory`, file)) 191 require.Empty(t, sum) 192 } 193 194 func TestInvalidAlgorithm(t *testing.T) { 195 f, err := ioutil.TempFile(t.TempDir(), "") 196 require.NoError(t, err) 197 require.NoError(t, f.Close()) 198 artifact := Artifact{ 199 Path: f.Name(), 200 } 201 sum, err := artifact.Checksum("sha1ssss") 202 require.EqualError(t, err, `invalid algorithm: sha1ssss`) 203 require.Empty(t, sum) 204 } 205 206 func TestExtraOr(t *testing.T) { 207 a := &Artifact{ 208 Extra: map[string]interface{}{ 209 "Foo": "foo", 210 }, 211 } 212 require.Equal(t, "foo", a.ExtraOr("Foo", "bar")) 213 require.Equal(t, "bar", a.ExtraOr("Foobar", "bar")) 214 } 215 216 func TestByIDs(t *testing.T) { 217 data := []*Artifact{ 218 { 219 Name: "foo", 220 Extra: map[string]interface{}{ 221 "ID": "foo", 222 }, 223 }, 224 { 225 Name: "bar", 226 Extra: map[string]interface{}{ 227 "ID": "bar", 228 }, 229 }, 230 { 231 Name: "foobar", 232 Extra: map[string]interface{}{ 233 "ID": "foo", 234 }, 235 }, 236 { 237 Name: "check", 238 Extra: map[string]interface{}{ 239 "ID": "check", 240 }, 241 }, 242 { 243 Name: "checksum", 244 Type: Checksum, 245 }, 246 } 247 artifacts := New() 248 for _, a := range data { 249 artifacts.Add(a) 250 } 251 252 require.Len(t, artifacts.Filter(ByIDs("check")).items, 2) 253 require.Len(t, artifacts.Filter(ByIDs("foo")).items, 3) 254 require.Len(t, artifacts.Filter(ByIDs("foo", "bar")).items, 4) 255 } 256 257 func TestByFormats(t *testing.T) { 258 data := []*Artifact{ 259 { 260 Name: "foo", 261 Extra: map[string]interface{}{ 262 "Format": "zip", 263 }, 264 }, 265 { 266 Name: "bar", 267 Extra: map[string]interface{}{ 268 "Format": "tar.gz", 269 }, 270 }, 271 { 272 Name: "foobar", 273 Extra: map[string]interface{}{ 274 "Format": "zip", 275 }, 276 }, 277 { 278 Name: "bin", 279 Extra: map[string]interface{}{ 280 "Format": "binary", 281 }, 282 }, 283 } 284 artifacts := New() 285 for _, a := range data { 286 artifacts.Add(a) 287 } 288 289 require.Len(t, artifacts.Filter(ByFormats("binary")).items, 1) 290 require.Len(t, artifacts.Filter(ByFormats("zip")).items, 2) 291 require.Len(t, artifacts.Filter(ByFormats("zip", "tar.gz")).items, 3) 292 } 293 294 func TestTypeToString(t *testing.T) { 295 for _, a := range []Type{ 296 UploadableArchive, 297 UploadableBinary, 298 UploadableFile, 299 Binary, 300 LinuxPackage, 301 PublishableSnapcraft, 302 Snapcraft, 303 PublishableDockerImage, 304 DockerImage, 305 DockerManifest, 306 Checksum, 307 Signature, 308 UploadableSourceArchive, 309 BrewTap, 310 ScoopManifest, 311 } { 312 t.Run(a.String(), func(t *testing.T) { 313 require.NotEqual(t, "unknown", a.String()) 314 }) 315 } 316 t.Run("unknown", func(t *testing.T) { 317 require.Equal(t, "unknown", Type(9999).String()) 318 }) 319 } 320 321 func TestPaths(t *testing.T) { 322 paths := []string{"a/b", "b/c", "d/e", "f/g"} 323 artifacts := New() 324 for _, a := range paths { 325 artifacts.Add(&Artifact{ 326 Path: a, 327 }) 328 } 329 require.ElementsMatch(t, paths, artifacts.Paths()) 330 }