mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-04-15 11:56:56 +02:00
fix: prevent having whitespaces in artifact names (#4515)
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>
This commit is contained in:
parent
6e0fc795ee
commit
e7f4b10fc6
@ -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 {
|
||||
@ -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
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ 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"),
|
||||
},
|
||||
@ -31,6 +31,10 @@ func TestAdd(t *testing.T) {
|
||||
Name: "bar",
|
||||
Type: Binary,
|
||||
},
|
||||
{
|
||||
Name: " whitespaces ",
|
||||
Type: UploadableBinary,
|
||||
},
|
||||
{
|
||||
Name: "foobar",
|
||||
Type: DockerImage,
|
||||
@ -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) {
|
||||
@ -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())
|
||||
})
|
||||
}
|
||||
|
@ -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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user