1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/pipe/snapcraft/snapcraft_test.go

370 lines
10 KiB
Go
Raw Normal View History

2017-07-27 02:30:48 +02:00
package snapcraft
import (
2017-08-17 23:41:08 +02:00
"fmt"
2017-07-27 02:30:48 +02:00
"io/ioutil"
"os"
"path/filepath"
"testing"
2017-12-18 01:22:36 +02:00
"github.com/goreleaser/goreleaser/internal/artifact"
2018-09-12 19:18:01 +02:00
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
2017-07-27 02:30:48 +02:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2017-08-04 07:42:55 +02:00
yaml "gopkg.in/yaml.v2"
2017-07-27 02:30:48 +02:00
)
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.String())
2017-07-27 02:30:48 +02:00
}
2017-08-02 14:16:59 +02:00
func TestRunPipeMissingInfo(t *testing.T) {
2017-08-17 23:41:08 +02:00
for eerr, snap := range map[error]config.Snapcraft{
ErrNoSummary: {
2017-08-02 14:16:59 +02:00
Description: "dummy desc",
2017-07-27 02:30:48 +02:00
},
2017-08-17 23:41:08 +02:00
ErrNoDescription: {
2017-08-02 14:16:59 +02:00
Summary: "dummy summary",
2017-07-27 02:30:48 +02:00
},
2018-09-12 19:18:01 +02:00
pipe.Skip("no summary nor description were provided"): {},
2017-08-02 14:16:59 +02:00
} {
2017-08-17 23:41:08 +02:00
t.Run(fmt.Sprintf("testing if %v happens", eerr), func(t *testing.T) {
2017-08-02 14:16:59 +02:00
var ctx = &context.Context{
Config: config.Project{
Snapcrafts: []config.Snapcraft{
snap,
},
2017-08-02 14:16:59 +02:00
},
}
assert.Equal(t, eerr, Pipe{}.Run(ctx))
2017-08-02 14:16:59 +02:00
})
2017-07-27 02:30:48 +02:00
}
}
func TestRunPipe(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(t, err)
2017-07-27 02:30:48 +02:00
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, err)
2017-12-30 00:19:55 +02:00
var ctx = context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
Snapcrafts: []config.Snapcraft{
{
NameTemplate: "foo_{{.Arch}}",
Summary: "test summary",
Description: "test description",
Publish: true,
Builds: []string{"foo"},
},
{
NameTemplate: "foo_and_bar_{{.Arch}}",
Summary: "test summary",
Description: "test description",
Publish: true,
Builds: []string{"foo", "bar"},
},
{
NameTemplate: "bar_{{.Arch}}",
Summary: "test summary",
Description: "test description",
Publish: true,
Builds: []string{"bar"},
},
},
2017-12-30 00:19:55 +02:00
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "v1.2.3"
addBinaries(t, ctx, "foo", filepath.Join(dist, "foo"), "foo")
addBinaries(t, ctx, "bar", filepath.Join(dist, "bar"), "bar")
assert.NoError(t, Pipe{}.Run(ctx))
assert.Len(t, ctx.Artifacts.Filter(artifact.ByType(artifact.PublishableSnapcraft)).List(), 9)
}
func TestRunPipeInvalidNameTemplate(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(t, err)
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, err)
2017-12-30 00:19:55 +02:00
var ctx = context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
Snapcrafts: []config.Snapcraft{
{
NameTemplate: "foo_{{.Arch}",
Summary: "test summary",
Description: "test description",
Builds: []string{"foo"},
},
2017-07-27 02:30:48 +02:00
},
2017-12-30 00:19:55 +02:00
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "v1.2.3"
addBinaries(t, ctx, "foo", dist, "mybin")
assert.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1: unexpected "}" in operand`)
2017-07-27 02:30:48 +02:00
}
2017-08-07 18:28:04 +02:00
func TestRunPipeWithName(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(t, err)
2017-08-07 18:28:04 +02:00
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, err)
2017-12-30 00:19:55 +02:00
var ctx = context.New(config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcrafts: []config.Snapcraft{
{
NameTemplate: "foo_{{.Arch}}",
Name: "testsnapname",
Base: "core18",
License: "MIT",
Summary: "test summary",
Description: "test description",
Builds: []string{"foo"},
},
2017-08-07 18:28:04 +02:00
},
2017-12-30 00:19:55 +02:00
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "v1.2.3"
addBinaries(t, ctx, "foo", dist, "mybin")
assert.NoError(t, Pipe{}.Run(ctx))
yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
assert.NoError(t, err)
var metadata Metadata
err = yaml.Unmarshal(yamlFile, &metadata)
assert.NoError(t, err)
assert.Equal(t, "testsnapname", metadata.Name)
assert.Equal(t, "core18", metadata.Base)
2019-04-17 22:26:30 +02:00
assert.Equal(t, "MIT", metadata.License)
assert.Equal(t, "mybin", metadata.Apps["mybin"].Command)
assert.Equal(t, "mybin", metadata.Apps["testsnapname"].Command)
}
func TestRunPipeWithBinaryInDir(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(t, err)
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, err)
var ctx = context.New(config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcrafts: []config.Snapcraft{
{
NameTemplate: "foo_{{.Arch}}",
Name: "testsnapname",
Summary: "test summary",
Description: "test description",
Builds: []string{"foo"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "v1.2.3"
addBinaries(t, ctx, "foo", dist, "bin/mybin")
assert.NoError(t, Pipe{}.Run(ctx))
2017-12-18 01:22:36 +02:00
yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
assert.NoError(t, err)
2017-09-15 02:19:56 +02:00
var metadata Metadata
err = yaml.Unmarshal(yamlFile, &metadata)
assert.NoError(t, err)
assert.Equal(t, "testsnapname", metadata.Name)
assert.Equal(t, "", metadata.Base)
2019-04-17 22:26:30 +02:00
assert.Equal(t, "", metadata.License)
assert.Equal(t, "mybin", metadata.Apps["mybin"].Command)
assert.Equal(t, "mybin", metadata.Apps["testsnapname"].Command)
2017-08-07 18:28:04 +02:00
}
2018-06-25 02:12:53 +02:00
func TestRunPipeMetadata(t *testing.T) {
2017-08-04 07:42:55 +02:00
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(t, err)
2017-08-04 07:42:55 +02:00
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, err)
2017-12-30 00:19:55 +02:00
var ctx = context.New(config.Project{
ProjectName: "testprojectname",
2017-12-30 00:19:55 +02:00
Dist: dist,
Snapcrafts: []config.Snapcraft{
{
NameTemplate: "foo_{{.Arch}}",
Summary: "test summary",
Description: "test description",
Apps: map[string]config.SnapcraftAppMetadata{
"mybin": {
Plugs: []string{"home", "network", "personal-files"},
Daemon: "simple",
Args: "--foo --bar",
},
2017-08-04 07:42:55 +02:00
},
Plugs: map[string]interface{}{
"personal-files": map[string]interface{}{
"read": []string{"$HOME/test"},
},
},
Builds: []string{"foo"},
},
2017-08-04 07:42:55 +02:00
},
2017-12-30 00:19:55 +02:00
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "v1.2.3"
addBinaries(t, ctx, "foo", dist, "mybin")
assert.NoError(t, Pipe{}.Run(ctx))
2017-12-18 01:22:36 +02:00
yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
assert.NoError(t, err)
2017-09-15 02:19:56 +02:00
var metadata Metadata
err = yaml.Unmarshal(yamlFile, &metadata)
assert.NoError(t, err)
assert.Equal(t, []string{"home", "network", "personal-files"}, metadata.Apps["mybin"].Plugs)
assert.Equal(t, "simple", metadata.Apps["mybin"].Daemon)
assert.Equal(t, "mybin --foo --bar", metadata.Apps["mybin"].Command)
assert.Equal(t, []string{"home", "network", "personal-files"}, metadata.Apps["testprojectname"].Plugs)
assert.Equal(t, "simple", metadata.Apps["testprojectname"].Daemon)
assert.Equal(t, "mybin --foo --bar", metadata.Apps["testprojectname"].Command)
assert.Equal(t, map[interface{}]interface{}(map[interface{}]interface{}{"read": []interface{}{"$HOME/test"}}), metadata.Plugs["personal-files"])
2017-08-04 07:42:55 +02:00
}
2017-07-27 02:30:48 +02:00
func TestNoSnapcraftInPath(t *testing.T) {
var path = os.Getenv("PATH")
defer func() {
assert.NoError(t, os.Setenv("PATH", path))
2017-07-27 02:30:48 +02:00
}()
assert.NoError(t, os.Setenv("PATH", ""))
2017-12-30 00:19:55 +02:00
var ctx = context.New(config.Project{
Snapcrafts: []config.Snapcraft{
{
Summary: "dummy",
Description: "dummy",
},
2017-07-27 02:30:48 +02:00
},
2017-12-30 00:19:55 +02:00
})
assert.EqualError(t, Pipe{}.Run(ctx), ErrNoSnapcraft.Error())
2017-07-27 02:30:48 +02:00
}
func TestRunNoArguments(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(t, err)
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, err)
var ctx = context.New(config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcrafts: []config.Snapcraft{
{
NameTemplate: "foo_{{.Arch}}",
Summary: "test summary",
Description: "test description",
Apps: map[string]config.SnapcraftAppMetadata{
"mybin": {
Daemon: "simple",
Args: "",
},
},
Builds: []string{"foo"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "v1.2.3"
addBinaries(t, ctx, "foo", dist, "mybin")
assert.NoError(t, Pipe{}.Run(ctx))
yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
assert.NoError(t, err)
var metadata Metadata
err = yaml.Unmarshal(yamlFile, &metadata)
assert.NoError(t, err)
assert.Equal(t, "mybin", metadata.Apps["mybin"].Command)
}
func TestDefault(t *testing.T) {
var ctx = context.New(config.Project{
Builds: []config.Build{
{
ID: "foo",
},
},
})
2017-12-29 21:07:06 +02:00
assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(t, defaultNameTemplate, ctx.Config.Snapcrafts[0].NameTemplate)
assert.Equal(t, []string{"foo"}, ctx.Config.Snapcrafts[0].Builds)
}
2018-10-20 22:02:38 +02:00
func TestPublish(t *testing.T) {
var ctx = context.New(config.Project{})
ctx.Artifacts.Add(artifact.Artifact{
Name: "mybin",
Path: "nope.snap",
Goarch: "amd64",
Goos: "linux",
Type: artifact.PublishableSnapcraft,
})
err := Pipe{}.Publish(ctx)
assert.Contains(t, err.Error(), "failed to push nope.snap package")
}
func TestDefaultSet(t *testing.T) {
var ctx = context.New(config.Project{
Snapcrafts: []config.Snapcraft{
{
NameTemplate: "foo",
},
},
})
assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(t, "foo", ctx.Config.Snapcrafts[0].NameTemplate)
}
func TestDefaultDeprecate(t *testing.T) {
var ctx = context.New(config.Project{
Snapcraft: config.Snapcraft{
NameTemplate: "foo",
},
})
2017-12-29 21:07:06 +02:00
assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(t, "foo", ctx.Config.Snapcrafts[0].NameTemplate)
}
func addBinaries(t *testing.T, ctx *context.Context, name, dist, dest string) {
2017-12-18 01:22:36 +02:00
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386", "arm6"} {
2017-12-18 01:22:36 +02:00
var folder = goos + goarch
assert.NoError(t, os.MkdirAll(filepath.Join(dist, folder), 0755))
2017-12-18 01:22:36 +02:00
var binPath = filepath.Join(dist, folder, name)
_, err := os.Create(binPath)
assert.NoError(t, err)
ctx.Artifacts.Add(artifact.Artifact{
Name: dest,
2017-12-18 01:22:36 +02:00
Path: binPath,
Goarch: goarch,
Goos: goos,
Type: artifact.Binary,
Extra: map[string]interface{}{
"ID": name,
},
2017-12-18 01:22:36 +02:00
})
}
}
}
func TestSeveralSnapssWithTheSameID(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Snapcrafts: []config.Snapcraft{
{
ID: "a",
},
{
ID: "a",
},
},
},
}
require.EqualError(t, Pipe{}.Default(ctx), "found 2 items with the ID 'a', please fix your config")
}