github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/artifact/artifact_test.go (about) 1 package artifact 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 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 var 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 assert.NoError(t, g.Wait()) 45 assert.Len(t, artifacts.List(), 4) 46 } 47 48 func TestFilter(t *testing.T) { 49 var 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 var artifacts = New() 73 for _, a := range data { 74 artifacts.Add(a) 75 } 76 77 assert.Len(t, artifacts.Filter(ByGoos("linux")).items, 1) 78 assert.Len(t, artifacts.Filter(ByGoos("darwin")).items, 0) 79 80 assert.Len(t, artifacts.Filter(ByGoarch("amd64")).items, 1) 81 assert.Len(t, artifacts.Filter(ByGoarch("386")).items, 0) 82 83 assert.Len(t, artifacts.Filter(ByGoarm("6")).items, 1) 84 assert.Len(t, artifacts.Filter(ByGoarm("7")).items, 0) 85 86 assert.Len(t, artifacts.Filter(ByType(Checksum)).items, 2) 87 assert.Len(t, artifacts.Filter(ByType(Binary)).items, 0) 88 89 assert.Len(t, artifacts.Filter( 90 And( 91 ByType(Checksum), 92 func(a Artifact) bool { 93 return a.Name == "checkzumm" 94 }, 95 ), 96 ).List(), 1) 97 98 assert.Len(t, artifacts.Filter( 99 Or( 100 ByType(Checksum), 101 And( 102 ByGoos("linux"), 103 ByGoarm("arm"), 104 ), 105 ), 106 ).List(), 2) 107 } 108 109 func TestGroupByPlatform(t *testing.T) { 110 var data = []Artifact{ 111 { 112 Name: "foo", 113 Goos: "linux", 114 Goarch: "amd64", 115 }, 116 { 117 Name: "bar", 118 Goos: "linux", 119 Goarch: "amd64", 120 }, 121 { 122 Name: "foobar", 123 Goos: "linux", 124 Goarch: "arm", 125 Goarm: "6", 126 }, 127 { 128 Name: "check", 129 Type: Checksum, 130 }, 131 } 132 var artifacts = New() 133 for _, a := range data { 134 artifacts.Add(a) 135 } 136 137 var groups = artifacts.GroupByPlatform() 138 assert.Len(t, groups["linuxamd64"], 2) 139 assert.Len(t, groups["linuxarm6"], 1) 140 } 141 142 func TestChecksum(t *testing.T) { 143 folder, err := ioutil.TempDir("", "goreleasertest") 144 require.NoError(t, err) 145 var file = filepath.Join(folder, "subject") 146 require.NoError(t, ioutil.WriteFile(file, []byte("lorem ipsum"), 0644)) 147 148 var artifact = Artifact{ 149 Path: file, 150 } 151 152 sum, err := artifact.Checksum() 153 require.NoError(t, err) 154 require.Equal(t, "5e2bf57d3f40c4b6df69daf1936cb766f832374b4fc0259a7cbff06e2f70f269", sum) 155 } 156 157 func TestChecksumFileDoesntExist(t *testing.T) { 158 var artifact = Artifact{ 159 Path: "/tmp/adasdasdas/asdasd/asdas", 160 } 161 sum, err := artifact.Checksum() 162 require.EqualError(t, err, `failed to checksum: open /tmp/adasdasdas/asdasd/asdas: no such file or directory`) 163 require.Empty(t, sum) 164 }