github.1git.de/goreleaser/goreleaser@v0.92.0/internal/pipe/snapcraft/snapcraft_test.go (about) 1 package snapcraft 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/goreleaser/goreleaser/internal/artifact" 11 "github.com/goreleaser/goreleaser/internal/pipe" 12 "github.com/goreleaser/goreleaser/pkg/config" 13 "github.com/goreleaser/goreleaser/pkg/context" 14 "github.com/stretchr/testify/assert" 15 yaml "gopkg.in/yaml.v2" 16 ) 17 18 func TestDescription(t *testing.T) { 19 assert.NotEmpty(t, Pipe{}.String()) 20 } 21 22 func TestRunPipeMissingInfo(t *testing.T) { 23 for eerr, snap := range map[error]config.Snapcraft{ 24 ErrNoSummary: { 25 Description: "dummy desc", 26 }, 27 ErrNoDescription: { 28 Summary: "dummy summary", 29 }, 30 pipe.Skip("no summary nor description were provided"): {}, 31 } { 32 t.Run(fmt.Sprintf("testing if %v happens", eerr), func(t *testing.T) { 33 var ctx = &context.Context{ 34 Config: config.Project{ 35 Snapcraft: snap, 36 }, 37 } 38 assert.Equal(t, eerr, Pipe{}.Run(ctx)) 39 }) 40 } 41 } 42 43 func TestRunPipe(t *testing.T) { 44 folder, err := ioutil.TempDir("", "archivetest") 45 assert.NoError(t, err) 46 var dist = filepath.Join(folder, "dist") 47 assert.NoError(t, os.Mkdir(dist, 0755)) 48 assert.NoError(t, err) 49 var ctx = context.New(config.Project{ 50 ProjectName: "mybin", 51 Dist: dist, 52 Snapcraft: config.Snapcraft{ 53 NameTemplate: "foo_{{.Arch}}", 54 Summary: "test summary", 55 Description: "test description", 56 Publish: true, 57 }, 58 }) 59 ctx.Git.CurrentTag = "v1.2.3" 60 ctx.Version = "v1.2.3" 61 addBinaries(t, ctx, "mybin", dist) 62 assert.NoError(t, Pipe{}.Run(ctx)) 63 assert.Len(t, ctx.Artifacts.Filter(artifact.ByType(artifact.PublishableSnapcraft)).List(), 2) 64 } 65 66 func TestRunPipeInvalidNameTemplate(t *testing.T) { 67 folder, err := ioutil.TempDir("", "archivetest") 68 assert.NoError(t, err) 69 var dist = filepath.Join(folder, "dist") 70 assert.NoError(t, os.Mkdir(dist, 0755)) 71 assert.NoError(t, err) 72 var ctx = context.New(config.Project{ 73 ProjectName: "mybin", 74 Dist: dist, 75 Snapcraft: config.Snapcraft{ 76 NameTemplate: "foo_{{.Arch}", 77 Summary: "test summary", 78 Description: "test description", 79 }, 80 }) 81 ctx.Git.CurrentTag = "v1.2.3" 82 ctx.Version = "v1.2.3" 83 addBinaries(t, ctx, "mybin", dist) 84 assert.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1: unexpected "}" in operand`) 85 } 86 87 func TestRunPipeWithName(t *testing.T) { 88 folder, err := ioutil.TempDir("", "archivetest") 89 assert.NoError(t, err) 90 var dist = filepath.Join(folder, "dist") 91 assert.NoError(t, os.Mkdir(dist, 0755)) 92 assert.NoError(t, err) 93 var ctx = context.New(config.Project{ 94 ProjectName: "testprojectname", 95 Dist: dist, 96 Snapcraft: config.Snapcraft{ 97 NameTemplate: "foo_{{.Arch}}", 98 Name: "testsnapname", 99 Summary: "test summary", 100 Description: "test description", 101 }, 102 }) 103 ctx.Git.CurrentTag = "v1.2.3" 104 ctx.Version = "v1.2.3" 105 addBinaries(t, ctx, "testprojectname", dist) 106 assert.NoError(t, Pipe{}.Run(ctx)) 107 yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml")) 108 assert.NoError(t, err) 109 var metadata Metadata 110 err = yaml.Unmarshal(yamlFile, &metadata) 111 assert.NoError(t, err) 112 assert.Equal(t, metadata.Name, "testsnapname") 113 assert.Equal(t, metadata.Apps["mybin"].Command, "mybin") 114 assert.Equal(t, metadata.Apps["testsnapname"].Command, "mybin") 115 } 116 117 func TestRunPipeMetadata(t *testing.T) { 118 folder, err := ioutil.TempDir("", "archivetest") 119 assert.NoError(t, err) 120 var dist = filepath.Join(folder, "dist") 121 assert.NoError(t, os.Mkdir(dist, 0755)) 122 assert.NoError(t, err) 123 var ctx = context.New(config.Project{ 124 ProjectName: "testprojectname", 125 Dist: dist, 126 Snapcraft: config.Snapcraft{ 127 NameTemplate: "foo_{{.Arch}}", 128 Summary: "test summary", 129 Description: "test description", 130 Apps: map[string]config.SnapcraftAppMetadata{ 131 "mybin": { 132 Plugs: []string{"home", "network"}, 133 Daemon: "simple", 134 Args: "--foo --bar", 135 }, 136 }, 137 }, 138 }) 139 ctx.Git.CurrentTag = "v1.2.3" 140 ctx.Version = "v1.2.3" 141 addBinaries(t, ctx, "mybin", dist) 142 assert.NoError(t, Pipe{}.Run(ctx)) 143 yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml")) 144 assert.NoError(t, err) 145 var metadata Metadata 146 err = yaml.Unmarshal(yamlFile, &metadata) 147 assert.NoError(t, err) 148 assert.Equal(t, metadata.Apps["mybin"].Plugs, []string{"home", "network"}) 149 assert.Equal(t, metadata.Apps["mybin"].Daemon, "simple") 150 assert.Equal(t, metadata.Apps["mybin"].Command, "mybin --foo --bar") 151 assert.Equal(t, metadata.Apps["testprojectname"].Plugs, []string{"home", "network"}) 152 assert.Equal(t, metadata.Apps["testprojectname"].Daemon, "simple") 153 assert.Equal(t, metadata.Apps["testprojectname"].Command, "mybin --foo --bar") 154 } 155 156 func TestNoSnapcraftInPath(t *testing.T) { 157 var path = os.Getenv("PATH") 158 defer func() { 159 assert.NoError(t, os.Setenv("PATH", path)) 160 }() 161 assert.NoError(t, os.Setenv("PATH", "")) 162 var ctx = context.New(config.Project{ 163 Snapcraft: config.Snapcraft{ 164 Summary: "dummy", 165 Description: "dummy", 166 }, 167 }) 168 assert.EqualError(t, Pipe{}.Run(ctx), ErrNoSnapcraft.Error()) 169 } 170 171 func TestDefault(t *testing.T) { 172 var ctx = context.New(config.Project{}) 173 assert.NoError(t, Pipe{}.Default(ctx)) 174 assert.Equal(t, defaultNameTemplate, ctx.Config.Snapcraft.NameTemplate) 175 } 176 177 func TestPublish(t *testing.T) { 178 var ctx = context.New(config.Project{}) 179 ctx.Artifacts.Add(artifact.Artifact{ 180 Name: "mybin", 181 Path: "nope.snap", 182 Goarch: "amd64", 183 Goos: "linux", 184 Type: artifact.PublishableSnapcraft, 185 }) 186 err := Pipe{}.Publish(ctx) 187 assert.Contains(t, err.Error(), "failed to push nope.snap package") 188 } 189 190 func TestDefaultSet(t *testing.T) { 191 var ctx = context.New(config.Project{ 192 Snapcraft: config.Snapcraft{ 193 NameTemplate: "foo", 194 }, 195 }) 196 assert.NoError(t, Pipe{}.Default(ctx)) 197 assert.Equal(t, "foo", ctx.Config.Snapcraft.NameTemplate) 198 } 199 200 func addBinaries(t *testing.T, ctx *context.Context, name, dist string) { 201 for _, goos := range []string{"linux", "darwin"} { 202 for _, goarch := range []string{"amd64", "386", "arm6"} { 203 var folder = goos + goarch 204 assert.NoError(t, os.Mkdir(filepath.Join(dist, folder), 0755)) 205 var binPath = filepath.Join(dist, folder, name) 206 _, err := os.Create(binPath) 207 assert.NoError(t, err) 208 ctx.Artifacts.Add(artifact.Artifact{ 209 Name: "mybin", 210 Path: binPath, 211 Goarch: goarch, 212 Goos: goos, 213 Type: artifact.Binary, 214 }) 215 } 216 } 217 }