1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00
goreleaser/pipeline/snapcraft/snapcraft_test.go

206 lines
5.5 KiB
Go
Raw Normal View History

2017-07-27 00:30:48 +00:00
package snapcraft
import (
2017-08-17 18:41:08 -03:00
"fmt"
2017-07-27 00:30:48 +00:00
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
2017-12-17 21:22:36 -02:00
"github.com/goreleaser/goreleaser/internal/artifact"
2017-08-20 16:35:46 -03:00
"github.com/goreleaser/goreleaser/pipeline"
2017-07-27 00:30:48 +00:00
"github.com/stretchr/testify/assert"
2017-08-04 05:42:55 +00:00
yaml "gopkg.in/yaml.v2"
2017-07-27 00:30:48 +00:00
)
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.String())
2017-07-27 00:30:48 +00:00
}
2017-08-02 09:16:59 -03:00
func TestRunPipeMissingInfo(t *testing.T) {
2017-08-17 18:41:08 -03:00
for eerr, snap := range map[error]config.Snapcraft{
ErrNoSummary: {
2017-08-02 09:16:59 -03:00
Description: "dummy desc",
2017-07-27 00:30:48 +00:00
},
2017-08-17 18:41:08 -03:00
ErrNoDescription: {
2017-08-02 09:16:59 -03:00
Summary: "dummy summary",
2017-07-27 00:30:48 +00:00
},
2017-08-20 16:35:46 -03:00
pipeline.Skip("no summary nor description were provided"): {},
2017-08-02 09:16:59 -03:00
} {
2017-08-17 18:41:08 -03:00
t.Run(fmt.Sprintf("testing if %v happens", eerr), func(t *testing.T) {
2017-08-02 09:16:59 -03:00
var ctx = &context.Context{
Config: config.Project{
Snapcraft: snap,
},
}
assert.Equal(t, eerr, Pipe{}.Run(ctx))
2017-08-02 09:16:59 -03:00
})
2017-07-27 00:30:48 +00:00
}
}
func TestRunPipe(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(t, err)
2017-07-27 00:30:48 +00:00
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, err)
2017-07-27 00:30:48 +00:00
var ctx = &context.Context{
2017-12-17 21:22:36 -02:00
Version: "testversion",
Artifacts: artifact.New(),
2017-07-27 00:30:48 +00:00
Config: config.Project{
ProjectName: "mybin",
Dist: dist,
Snapcraft: config.Snapcraft{
2017-12-17 21:22:36 -02:00
NameTemplate: "foo_{{.Arch}}",
2017-12-29 17:07:06 -02:00
Summary: "test summary",
Description: "test description",
2017-12-17 21:22:36 -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)
var ctx = &context.Context{
Version: "testversion",
Artifacts: artifact.New(),
Config: config.Project{
ProjectName: "mybin",
Dist: dist,
2017-07-27 00:30:48 +00:00
Snapcraft: config.Snapcraft{
NameTemplate: "foo_{{.Arch}",
2017-12-29 17:07:06 -02:00
Summary: "test summary",
Description: "test description",
2017-07-27 00:30:48 +00:00
},
},
}
addBinaries(t, ctx, "mybin", dist)
assert.EqualError(t, Pipe{}.Run(ctx), `template: foo_{{.Arch}:1: unexpected "}" in operand`)
2017-07-27 00:30:48 +00:00
}
2017-08-07 16:28:04 +00:00
func TestRunPipeWithName(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(t, err)
2017-08-07 16:28:04 +00:00
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, err)
2017-08-07 16:28:04 +00:00
var ctx = &context.Context{
2017-12-17 21:22:36 -02:00
Version: "testversion",
Artifacts: artifact.New(),
2017-08-07 16:28:04 +00:00
Config: config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcraft: config.Snapcraft{
NameTemplate: "foo_{{.Arch}}",
2017-12-29 17:07:06 -02:00
Name: "testsnapname",
Summary: "test summary",
Description: "test description",
2017-08-07 16:28:04 +00:00
},
},
}
addBinaries(t, ctx, "testprojectname", dist)
assert.NoError(t, Pipe{}.Run(ctx))
2017-12-17 21:22:36 -02:00
yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
assert.NoError(t, err)
2017-09-14 21:19:56 -03:00
var metadata Metadata
err = yaml.Unmarshal(yamlFile, &metadata)
assert.NoError(t, err)
assert.Equal(t, metadata.Name, "testsnapname")
2017-08-07 16:28:04 +00:00
}
2017-08-04 05:42:55 +00:00
func TestRunPipeWithPlugsAndDaemon(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(t, err)
2017-08-04 05:42:55 +00:00
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, err)
2017-08-04 05:42:55 +00:00
var ctx = &context.Context{
2017-12-17 21:22:36 -02:00
Version: "testversion",
Artifacts: artifact.New(),
2017-08-04 05:42:55 +00:00
Config: config.Project{
ProjectName: "mybin",
Dist: dist,
Snapcraft: config.Snapcraft{
NameTemplate: "foo_{{.Arch}}",
2017-12-29 17:07:06 -02:00
Summary: "test summary",
Description: "test description",
2017-08-04 05:42:55 +00:00
Apps: map[string]config.SnapcraftAppMetadata{
2017-08-18 20:10:55 -03:00
"mybin": {
2017-08-04 05:42:55 +00:00
Plugs: []string{"home", "network"},
Daemon: "simple",
},
},
},
},
}
addBinaries(t, ctx, "mybin", dist)
assert.NoError(t, Pipe{}.Run(ctx))
2017-12-17 21:22:36 -02:00
yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
assert.NoError(t, err)
2017-09-14 21:19:56 -03:00
var metadata Metadata
err = yaml.Unmarshal(yamlFile, &metadata)
assert.NoError(t, err)
assert.Equal(t, metadata.Apps["mybin"].Plugs, []string{"home", "network"})
assert.Equal(t, metadata.Apps["mybin"].Daemon, "simple")
2017-08-04 05:42:55 +00:00
}
2017-07-27 00:30:48 +00:00
func TestNoSnapcraftInPath(t *testing.T) {
var path = os.Getenv("PATH")
defer func() {
assert.NoError(t, os.Setenv("PATH", path))
2017-07-27 00:30:48 +00:00
}()
assert.NoError(t, os.Setenv("PATH", ""))
2017-07-27 00:30:48 +00:00
var ctx = &context.Context{
Config: config.Project{
Snapcraft: config.Snapcraft{
Summary: "dummy",
Description: "dummy",
},
},
}
assert.EqualError(t, Pipe{}.Run(ctx), ErrNoSnapcraft.Error())
2017-07-27 00:30:48 +00:00
}
func TestDefault(t *testing.T) {
var ctx = context.New(config.Project{})
2017-12-29 17:07:06 -02:00
assert.NoError(t, Pipe{}.Default(ctx))
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 17:07:06 -02:00
assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(t, "foo", ctx.Config.Snapcraft.NameTemplate)
}
func addBinaries(t *testing.T, ctx *context.Context, name, dist string) {
2017-12-17 21:22:36 -02:00
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
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,
})
}
}
}