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-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipeline"
|
|
|
|
"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"
|
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) {
|
2017-12-02 23:53:19 +02:00
|
|
|
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
|
|
|
},
|
2017-08-20 21:35:46 +02:00
|
|
|
pipeline.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{
|
|
|
|
Snapcraft: snap,
|
|
|
|
},
|
|
|
|
}
|
2017-09-27 00:24:49 +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")
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
2017-07-27 02:30:48 +02:00
|
|
|
var dist = filepath.Join(folder, "dist")
|
2017-09-27 00:24:49 +02:00
|
|
|
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,
|
|
|
|
Snapcraft: config.Snapcraft{
|
|
|
|
NameTemplate: "foo_{{.Arch}}",
|
|
|
|
Summary: "test summary",
|
|
|
|
Description: "test description",
|
2017-12-27 01:44:11 +02:00
|
|
|
},
|
2017-12-30 00:19:55 +02:00
|
|
|
})
|
2018-07-09 05:47:30 +02:00
|
|
|
ctx.Git.CurrentTag = "v1.2.3"
|
|
|
|
ctx.Version = "v1.2.3"
|
2017-12-27 01:44:11 +02:00
|
|
|
addBinaries(t, ctx, "mybin", dist)
|
|
|
|
assert.NoError(t, Pipe{}.Run(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Snapcraft: config.Snapcraft{
|
|
|
|
NameTemplate: "foo_{{.Arch}",
|
|
|
|
Summary: "test summary",
|
|
|
|
Description: "test description",
|
2017-07-27 02:30:48 +02:00
|
|
|
},
|
2017-12-30 00:19:55 +02:00
|
|
|
})
|
2018-07-09 05:47:30 +02:00
|
|
|
ctx.Git.CurrentTag = "v1.2.3"
|
|
|
|
ctx.Version = "v1.2.3"
|
2017-08-20 01:20:59 +02:00
|
|
|
addBinaries(t, ctx, "mybin", dist)
|
2018-07-09 05:47:30 +02:00
|
|
|
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")
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
2017-08-07 18:28:04 +02:00
|
|
|
var dist = filepath.Join(folder, "dist")
|
2017-09-27 00:24:49 +02:00
|
|
|
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,
|
|
|
|
Snapcraft: config.Snapcraft{
|
|
|
|
NameTemplate: "foo_{{.Arch}}",
|
|
|
|
Name: "testsnapname",
|
|
|
|
Summary: "test summary",
|
|
|
|
Description: "test description",
|
2017-08-07 18:28:04 +02:00
|
|
|
},
|
2017-12-30 00:19:55 +02:00
|
|
|
})
|
2018-07-09 05:47:30 +02:00
|
|
|
ctx.Git.CurrentTag = "v1.2.3"
|
|
|
|
ctx.Version = "v1.2.3"
|
2017-08-20 01:20:59 +02:00
|
|
|
addBinaries(t, ctx, "testprojectname", dist)
|
2017-09-27 00:24:49 +02:00
|
|
|
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"))
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
2017-09-15 02:19:56 +02:00
|
|
|
var metadata Metadata
|
|
|
|
err = yaml.Unmarshal(yamlFile, &metadata)
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, metadata.Name, "testsnapname")
|
2018-08-15 14:38:42 +02:00
|
|
|
assert.Equal(t, metadata.Apps["mybin"].Command, "mybin")
|
|
|
|
assert.Equal(t, metadata.Apps["testsnapname"].Command, "mybin")
|
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")
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
2017-08-04 07:42:55 +02:00
|
|
|
var dist = filepath.Join(folder, "dist")
|
2017-09-27 00:24:49 +02:00
|
|
|
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,
|
|
|
|
Snapcraft: config.Snapcraft{
|
|
|
|
NameTemplate: "foo_{{.Arch}}",
|
|
|
|
Summary: "test summary",
|
|
|
|
Description: "test description",
|
|
|
|
Apps: map[string]config.SnapcraftAppMetadata{
|
|
|
|
"mybin": {
|
|
|
|
Plugs: []string{"home", "network"},
|
|
|
|
Daemon: "simple",
|
2018-06-25 02:12:53 +02:00
|
|
|
Args: "--foo --bar",
|
2017-08-04 07:42:55 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-12-30 00:19:55 +02:00
|
|
|
})
|
2018-07-09 05:47:30 +02:00
|
|
|
ctx.Git.CurrentTag = "v1.2.3"
|
|
|
|
ctx.Version = "v1.2.3"
|
2017-08-20 01:20:59 +02:00
|
|
|
addBinaries(t, ctx, "mybin", dist)
|
2017-09-27 00:24:49 +02:00
|
|
|
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"))
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
2017-09-15 02:19:56 +02:00
|
|
|
var metadata Metadata
|
|
|
|
err = yaml.Unmarshal(yamlFile, &metadata)
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, metadata.Apps["mybin"].Plugs, []string{"home", "network"})
|
|
|
|
assert.Equal(t, metadata.Apps["mybin"].Daemon, "simple")
|
2018-06-25 02:12:53 +02:00
|
|
|
assert.Equal(t, metadata.Apps["mybin"].Command, "mybin --foo --bar")
|
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() {
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, os.Setenv("PATH", path))
|
2017-07-27 02:30:48 +02:00
|
|
|
}()
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, os.Setenv("PATH", ""))
|
2017-12-30 00:19:55 +02:00
|
|
|
var ctx = context.New(config.Project{
|
|
|
|
Snapcraft: config.Snapcraft{
|
|
|
|
Summary: "dummy",
|
|
|
|
Description: "dummy",
|
2017-07-27 02:30:48 +02:00
|
|
|
},
|
2017-12-30 00:19:55 +02:00
|
|
|
})
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), ErrNoSnapcraft.Error())
|
2017-07-27 02:30:48 +02:00
|
|
|
}
|
2017-08-20 01:20:59 +02:00
|
|
|
|
2017-12-27 01:44:11 +02:00
|
|
|
func TestDefault(t *testing.T) {
|
|
|
|
var ctx = context.New(config.Project{})
|
2017-12-29 21:07:06 +02:00
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
2017-12-27 01:44:11 +02:00
|
|
|
assert.Equal(t, defaultNameTemplate, ctx.Config.Snapcraft.NameTemplate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultSet(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))
|
2017-12-27 01:44:11 +02:00
|
|
|
assert.Equal(t, "foo", ctx.Config.Snapcraft.NameTemplate)
|
|
|
|
}
|
|
|
|
|
2017-08-20 01:20:59 +02:00
|
|
|
func addBinaries(t *testing.T, ctx *context.Context, name, dist string) {
|
2017-12-18 01:22:36 +02:00
|
|
|
for _, goos := range []string{"linux", "darwin"} {
|
2018-06-20 14:39:10 +02:00
|
|
|
for _, goarch := range []string{"amd64", "386", "arm6"} {
|
2017-12-18 01:22:36 +02:00
|
|
|
var folder = goos + goarch
|
|
|
|
assert.NoError(t, os.Mkdir(filepath.Join(dist, folder), 0755))
|
|
|
|
var binPath = filepath.Join(dist, folder, name)
|
|
|
|
_, err := os.Create(binPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ctx.Artifacts.Add(artifact.Artifact{
|
|
|
|
Name: "mybin",
|
|
|
|
Path: binPath,
|
|
|
|
Goarch: goarch,
|
|
|
|
Goos: goos,
|
|
|
|
Type: artifact.Binary,
|
|
|
|
})
|
|
|
|
}
|
2017-08-20 01:20:59 +02:00
|
|
|
}
|
|
|
|
}
|