From 375940841f792c64d174c460d0b360eb35a51785 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 17 Dec 2017 15:18:46 -0200 Subject: [PATCH] refactor: remove old code from context pkg --- context/context.go | 69 ++--------------------------------- context/context_test.go | 80 +++-------------------------------------- 2 files changed, 7 insertions(+), 142 deletions(-) diff --git a/context/context.go b/context/context.go index 594def0e4..b92619ecf 100644 --- a/context/context.go +++ b/context/context.go @@ -9,12 +9,10 @@ package context import ( ctx "context" "os" - "path/filepath" "strings" - "sync" - "github.com/apex/log" "github.com/goreleaser/goreleaser/config" + "github.com/goreleaser/goreleaser/internal/artifact" ) // GitInfo includes tags and diffs used in some point @@ -23,11 +21,6 @@ type GitInfo struct { Commit string } -// Binary with pretty name and path -type Binary struct { - Name, Path string -} - // Context carries along some data through the pipes type Context struct { ctx.Context @@ -35,8 +28,7 @@ type Context struct { Env map[string]string Token string Git GitInfo - Binaries map[string]map[string][]Binary - Artifacts []string + Artifacts artifact.Artifacts Checksums []string Dockers []string ReleaseNotes string @@ -49,63 +41,6 @@ type Context struct { Parallelism int } -var ( - artifactsLock sync.Mutex - checksumsLock sync.Mutex - dockersLock sync.Mutex - binariesLock sync.Mutex -) - -// AddArtifact adds a file to upload list -func (ctx *Context) AddArtifact(file string) { - artifactsLock.Lock() - defer artifactsLock.Unlock() - file = strings.TrimPrefix(file, ctx.Config.Dist+string(filepath.Separator)) - ctx.Artifacts = append(ctx.Artifacts, file) - log.WithField("artifact", file).Info("new release artifact") -} - -// AddChecksum adds a checksum file. -func (ctx *Context) AddChecksum(file string) { - checksumsLock.Lock() - defer checksumsLock.Unlock() - file = strings.TrimPrefix(file, ctx.Config.Dist+string(filepath.Separator)) - ctx.Checksums = append(ctx.Checksums, file) - log.WithField("checksum", file).Info("new checksum file") -} - -// AddDocker adds a docker image to the docker images list -func (ctx *Context) AddDocker(image string) { - dockersLock.Lock() - defer dockersLock.Unlock() - ctx.Dockers = append(ctx.Dockers, image) - log.WithField("image", image).Info("new docker image") -} - -// AddBinary adds a built binary to the current context -func (ctx *Context) AddBinary(platform, folder, name, path string) { - binariesLock.Lock() - defer binariesLock.Unlock() - if ctx.Binaries == nil { - ctx.Binaries = map[string]map[string][]Binary{} - } - if ctx.Binaries[platform] == nil { - ctx.Binaries[platform] = map[string][]Binary{} - } - ctx.Binaries[platform][folder] = append( - ctx.Binaries[platform][folder], - Binary{ - Name: name, - Path: path, - }, - ) - log.WithField("platform", platform). - WithField("folder", folder). - WithField("name", name). - WithField("path", path). - Debug("new binary") -} - // New context func New(config config.Project) *Context { return &Context{ diff --git a/context/context_test.go b/context/context_test.go index 40e167c12..faab26e05 100644 --- a/context/context_test.go +++ b/context/context_test.go @@ -4,81 +4,11 @@ import ( "testing" "github.com/goreleaser/goreleaser/config" - "github.com/stretchr/testify/assert" - "golang.org/x/sync/errgroup" + "github.com/tj/assert" ) -func TestMultipleAdds(t *testing.T) { - var artifacts = []string{ - "dist/a", - "dist/b", - "dist/c", - "dist/d", - } - var checksums = []string{ - "dist/a.sha256", - } - var dockerfiles = []string{ - "a/b:1.0.0", - "c/d:2.0.0", - "e/f:3.0.0", - } - var ctx = New(config.Project{ - Dist: "dist", - }) - var g errgroup.Group - for _, f := range artifacts { - f := f - g.Go(func() error { - ctx.AddArtifact(f) - return nil - }) - } - assert.NoError(t, g.Wait()) - for _, c := range checksums { - c := c - g.Go(func() error { - ctx.AddChecksum(c) - return nil - }) - } - assert.NoError(t, g.Wait()) - for _, d := range dockerfiles { - d := d - g.Go(func() error { - ctx.AddDocker(d) - return nil - }) - } - assert.NoError(t, g.Wait()) - assert.Len(t, ctx.Artifacts, len(artifacts)) - assert.Contains(t, ctx.Artifacts, "a", "b", "c", "d") - assert.Len(t, ctx.Checksums, len(checksums)) - assert.Contains(t, ctx.Checksums, "a.sha256") - assert.Len(t, ctx.Dockers, len(dockerfiles)) - assert.Contains(t, ctx.Dockers, "a/b:1.0.0", "c/d:2.0.0", "e/f:3.0.0") -} - -func TestMultipleBinaryAdds(t *testing.T) { - var list = map[string]string{ - "a": "folder/a", - "b": "folder/b", - "c": "folder/c", - "d": "folder/d", - } - var ctx = New(config.Project{ - Dist: "dist", - }) - var g errgroup.Group - for k, f := range list { - f := f - k := k - g.Go(func() error { - ctx.AddBinary("linuxamd64", k, k, f) - return nil - }) - } - assert.NoError(t, g.Wait()) - assert.Len(t, ctx.Binaries["linuxamd64"], len(list)) - assert.Len(t, ctx.Binaries, 1) +func TestNew(t *testing.T) { + var ctx = New(config.Project{}) + assert.NotEmpty(t, ctx.Env) + assert.Equal(t, 4, ctx.Parallelism) }