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

164 lines
4.6 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"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"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) {
assert.NotEmpty(t, Pipe{}.Description())
}
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-17 23:41:08 +02:00
nil: {}, // should skip instead of error
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 assert = assert.New(t)
var ctx = &context.Context{
Config: config.Project{
Snapcraft: snap,
},
}
2017-08-17 23:41:08 +02:00
assert.Equal(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) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(err)
var dist = filepath.Join(folder, "dist")
assert.NoError(os.Mkdir(dist, 0755))
assert.NoError(err)
var ctx = &context.Context{
Version: "testversion",
Config: config.Project{
ProjectName: "mybin",
Dist: dist,
Snapcraft: config.Snapcraft{
Summary: "test summary",
Description: "test description",
},
},
}
2017-08-04 03:29:02 +02:00
for _, plat := range []string{"linuxamd64", "linux386", "darwinamd64", "linuxarm64", "linuxarmhf"} {
2017-08-02 14:22:51 +02:00
var folder = "mybin_" + plat
assert.NoError(os.Mkdir(filepath.Join(dist, folder), 0755))
var binPath = filepath.Join(dist, folder, "mybin")
_, err = os.Create(binPath)
2017-08-19 00:14:50 +02:00
assert.NoError(err)
2017-08-02 14:22:51 +02:00
ctx.AddBinary(plat, folder, "mybin", binPath)
2017-07-27 02:30:48 +02:00
}
assert.NoError(Pipe{}.Run(ctx))
}
2017-08-07 18:28:04 +02:00
func TestRunPipeWithName(t *testing.T) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(err)
var dist = filepath.Join(folder, "dist")
assert.NoError(os.Mkdir(dist, 0755))
assert.NoError(err)
var ctx = &context.Context{
Version: "testversion",
Config: config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcraft: config.Snapcraft{
Name: "testsnapname",
Summary: "test summary",
Description: "test description",
},
},
}
for _, plat := range []string{"linuxamd64", "linux386", "darwinamd64", "linuxarm64", "linuxarmhf"} {
var folder = "testprojectname_" + plat
assert.NoError(os.Mkdir(filepath.Join(dist, folder), 0755))
var binPath = filepath.Join(dist, folder, "testprojectname")
_, err = os.Create(binPath)
ctx.AddBinary(plat, folder, "testprojectname", binPath)
}
assert.NoError(Pipe{}.Run(ctx))
yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "testprojectname_linuxamd64", "prime", "meta", "snap.yaml"))
assert.NoError(err)
var snapcraftMetadata SnapcraftMetadata
err = yaml.Unmarshal(yamlFile, &snapcraftMetadata)
assert.NoError(err)
assert.Equal(snapcraftMetadata.Name, "testsnapname")
}
2017-08-04 07:42:55 +02:00
func TestRunPipeWithPlugsAndDaemon(t *testing.T) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(err)
var dist = filepath.Join(folder, "dist")
assert.NoError(os.Mkdir(dist, 0755))
assert.NoError(err)
var ctx = &context.Context{
Version: "testversion",
Config: config.Project{
ProjectName: "mybin",
Dist: dist,
Snapcraft: config.Snapcraft{
Summary: "test summary",
Description: "test description",
Apps: map[string]config.SnapcraftAppMetadata{
"mybin": config.SnapcraftAppMetadata{
Plugs: []string{"home", "network"},
Daemon: "simple",
},
},
},
},
}
for _, plat := range []string{"linuxamd64", "linux386", "darwinamd64", "linuxarm64", "linuxarmhf"} {
var folder = "mybin_" + plat
assert.NoError(os.Mkdir(filepath.Join(dist, folder), 0755))
var binPath = filepath.Join(dist, folder, "mybin")
_, err = os.Create(binPath)
ctx.AddBinary(plat, folder, "mybin", binPath)
}
assert.NoError(Pipe{}.Run(ctx))
yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "mybin_linuxamd64", "prime", "meta", "snap.yaml"))
assert.NoError(err)
var snapcraftMetadata SnapcraftMetadata
err = yaml.Unmarshal(yamlFile, &snapcraftMetadata)
assert.NoError(err)
assert.Equal(snapcraftMetadata.Apps["mybin"].Plugs, []string{"home", "network"})
assert.Equal(snapcraftMetadata.Apps["mybin"].Daemon, "simple")
}
2017-07-27 02:30:48 +02:00
func TestNoSnapcraftInPath(t *testing.T) {
var assert = assert.New(t)
var path = os.Getenv("PATH")
defer func() {
assert.NoError(os.Setenv("PATH", path))
}()
assert.NoError(os.Setenv("PATH", ""))
var ctx = &context.Context{
Config: config.Project{
Snapcraft: config.Snapcraft{
Summary: "dummy",
Description: "dummy",
},
},
}
assert.EqualError(Pipe{}.Run(ctx), ErrNoSnapcraft.Error())
}