Skip to content

Commit

Permalink
fix: prevent having whitespaces in artifact names (#4515)
Browse files Browse the repository at this point in the history
refs #4513

this does not prevent the `dist` filepath to have spaces in it, although
that's likely less of an issue, but it will remove the spaces from
artifact's names.

Ideally, we could add a `tmpl.ApplyTrim` (or similar) that applies and
trim spaces, and use it everywhere it makes sense (which is likely a lot
of places).

Doing it on regular `Apply` might break things like release
footers/headers, which usually rely on empty lines (although maybe its
easier to treat those cases differently then).

Anyway, still thinking about it. Opinions are welcome :)

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Jan 7, 2024
1 parent 6e0fc79 commit e7f4b10
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
15 changes: 15 additions & 0 deletions internal/artifact/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func shouldRelPath(a *Artifact) bool {
func (artifacts *Artifacts) Add(a *Artifact) {
artifacts.lock.Lock()
defer artifacts.lock.Unlock()
a.Name = cleanName(*a)
if shouldRelPath(a) {
rel, err := relPath(a)
if rel != "" && err == nil {
Expand Down Expand Up @@ -575,3 +576,17 @@ func (artifacts *Artifacts) Visit(fn VisitFn) error {
}
return nil
}

func cleanName(a Artifact) string {
name := a.Name
ext := filepath.Ext(name)
result := strings.TrimSpace(strings.TrimSuffix(name, ext)) + ext
if name != result {
log.WithField("name", a.Name).
WithField("new name", result).
WithField("type", a.Type).
WithField("path", a.Path).
Warn("removed trailing whitespaces from artifact name")
}
return result
}
16 changes: 14 additions & 2 deletions internal/artifact/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ func TestAdd(t *testing.T) {
wd, _ := os.Getwd()
for _, a := range []*Artifact{
{
Name: "foo",
Name: " whitespaces .zip",
Type: UploadableArchive,
Path: filepath.Join(wd, "/foo/bar.tgz"),
},
{
Name: "bar",
Type: Binary,
},
{
Name: " whitespaces ",
Type: UploadableBinary,
},
{
Name: "foobar",
Type: DockerImage,
Expand All @@ -47,9 +51,13 @@ func TestAdd(t *testing.T) {
})
}
require.NoError(t, g.Wait())
require.Len(t, artifacts.List(), 4)
require.Len(t, artifacts.List(), 5)
archives := artifacts.Filter(ByType(UploadableArchive)).List()
require.Len(t, archives, 1)
require.Equal(t, "whitespaces.zip", archives[0].Name)
binaries := artifacts.Filter(ByType(UploadableBinary)).List()
require.Len(t, binaries, 1)
require.Equal(t, "whitespaces", binaries[0].Name)
}

func TestFilter(t *testing.T) {
Expand Down Expand Up @@ -933,4 +941,8 @@ func TestArtifactTypeStringer(t *testing.T) {
require.NotEqual(t, "unknown", Type(i).String())
})
}

t.Run("unknown", func(t *testing.T) {
require.Equal(t, "unknown", Type(99999).String())
})
}
46 changes: 46 additions & 0 deletions internal/pipe/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,52 @@ func TestRunPipeInvalidGlob(t *testing.T) {
require.EqualError(t, Pipe{}.Run(ctx), `failed to find files to archive: globbing failed for pattern [x-]: compile glob pattern: unexpected end of input`)
}

func TestRunPipeNameTemplateWithSpace(t *testing.T) {
folder := testlib.Mktmp(t)
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0o755))
f, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := testctx.NewWithCfg(
config.Project{
Dist: dist,
Archives: []config.Archive{
{
Builds: []string{"default"},
NameTemplate: " foo_{{.Os}}_{{.Arch}} ",
Format: "zip",
},
{
Builds: []string{"default"},
NameTemplate: " foo_{{.Os}}_{{.Arch}} ",
Format: "binary",
},
},
},
testctx.WithCurrentTag("v0.0.1"),
)
ctx.Artifacts.Add(&artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
Name: "mybin",
Path: filepath.Join("dist", "darwinamd64", "mybin"),
Type: artifact.Binary,
Extra: map[string]interface{}{
artifact.ExtraBinary: "mybin",
artifact.ExtraID: "default",
},
})
require.NoError(t, Pipe{}.Run(ctx))
list := ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableBinary)).List()
require.Len(t, list, 1)
require.Equal(t, "foo_darwin_amd64", list[0].Name)
list = ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableArchive)).List()
require.Len(t, list, 1)
require.Equal(t, "foo_darwin_amd64.zip", list[0].Name)
}

func TestRunPipeInvalidNameTemplate(t *testing.T) {
folder := testlib.Mktmp(t)
dist := filepath.Join(folder, "dist")
Expand Down

0 comments on commit e7f4b10

Please sign in to comment.