github.com/amane3/goreleaser@v0.182.0/internal/pipe/sourcearchive/source_test.go (about) 1 package sourcearchive 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/amane3/goreleaser/internal/artifact" 10 "github.com/amane3/goreleaser/internal/testlib" 11 "github.com/amane3/goreleaser/pkg/config" 12 "github.com/amane3/goreleaser/pkg/context" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestArchive(t *testing.T) { 17 for _, format := range []string{"tar.gz", "tar", "zip"} { 18 t.Run(format, func(t *testing.T) { 19 var tmp = testlib.Mktmp(t) 20 require.NoError(t, os.Mkdir("dist", 0744)) 21 22 testlib.GitInit(t) 23 require.NoError(t, ioutil.WriteFile("code.txt", []byte("not really code"), 0655)) 24 require.NoError(t, ioutil.WriteFile("README.md", []byte("# my dope fake project"), 0655)) 25 testlib.GitAdd(t) 26 testlib.GitCommit(t, "feat: first") 27 28 var ctx = context.New(config.Project{ 29 ProjectName: "foo", 30 Dist: "dist", 31 Source: config.Source{ 32 Format: format, 33 Enabled: true, 34 }, 35 }) 36 ctx.Git.FullCommit = "HEAD" 37 ctx.Version = "1.0.0" 38 39 require.NoError(t, Pipe{}.Default(ctx)) 40 require.NoError(t, Pipe{}.Run(ctx)) 41 42 var artifacts = ctx.Artifacts.List() 43 require.Len(t, artifacts, 1) 44 require.Equal(t, artifact.Artifact{ 45 Type: artifact.UploadableSourceArchive, 46 Name: "foo-1.0.0." + format, 47 Path: "dist/foo-1.0.0." + format, 48 Extra: map[string]interface{}{ 49 "Format": format, 50 }, 51 }, *artifacts[0]) 52 stat, err := os.Stat(filepath.Join(tmp, "dist", "foo-1.0.0."+format)) 53 require.NoError(t, err) 54 require.Greater(t, stat.Size(), int64(100)) 55 }) 56 } 57 } 58 59 func TestDefault(t *testing.T) { 60 var ctx = context.New(config.Project{}) 61 require.NoError(t, Pipe{}.Default(ctx)) 62 require.Equal(t, config.Source{ 63 NameTemplate: "{{ .ProjectName }}-{{ .Version }}", 64 Format: "tar.gz", 65 }, ctx.Config.Source) 66 } 67 68 func TestInvalidNameTemplate(t *testing.T) { 69 var ctx = context.New(config.Project{ 70 Source: config.Source{ 71 Enabled: true, 72 NameTemplate: "{{ .foo }-asdda", 73 }, 74 }) 75 require.EqualError(t, Pipe{}.Run(ctx), "template: tmpl:1: unexpected \"}\" in operand") 76 } 77 78 func TestDisabled(t *testing.T) { 79 testlib.AssertSkipped(t, Pipe{}.Run(context.New(config.Project{}))) 80 } 81 82 func TestString(t *testing.T) { 83 require.NotEmpty(t, Pipe{}.String()) 84 }