github.com/triarius/goreleaser@v1.12.5/internal/pipe/snapcraft/snapcraft_test.go (about)

     1  package snapcraft
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/triarius/goreleaser/internal/artifact"
    10  	"github.com/triarius/goreleaser/internal/gio"
    11  	"github.com/triarius/goreleaser/internal/pipe"
    12  	"github.com/triarius/goreleaser/internal/testlib"
    13  	"github.com/triarius/goreleaser/internal/yaml"
    14  	"github.com/triarius/goreleaser/pkg/config"
    15  	"github.com/triarius/goreleaser/pkg/context"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestDescription(t *testing.T) {
    20  	require.NotEmpty(t, Pipe{}.String())
    21  }
    22  
    23  func TestRunPipeMissingInfo(t *testing.T) {
    24  	for eerr, snap := range map[error]config.Snapcraft{
    25  		ErrNoSummary: {
    26  			Description: "dummy desc",
    27  		},
    28  		ErrNoDescription: {
    29  			Summary: "dummy summary",
    30  		},
    31  		pipe.Skip("no summary nor description were provided"): {},
    32  	} {
    33  		t.Run(fmt.Sprintf("testing if %v happens", eerr), func(t *testing.T) {
    34  			ctx := &context.Context{
    35  				Config: config.Project{
    36  					Snapcrafts: []config.Snapcraft{
    37  						snap,
    38  					},
    39  				},
    40  			}
    41  			require.Equal(t, eerr, Pipe{}.Run(ctx))
    42  		})
    43  	}
    44  }
    45  
    46  func TestRunPipe(t *testing.T) {
    47  	testlib.CheckPath(t, "snapcraft")
    48  	folder := t.TempDir()
    49  	dist := filepath.Join(folder, "dist")
    50  	require.NoError(t, os.Mkdir(dist, 0o755))
    51  	ctx := context.New(config.Project{
    52  		ProjectName: "mybin",
    53  		Dist:        dist,
    54  		Snapcrafts: []config.Snapcraft{
    55  			{
    56  				NameTemplate:     "foo_{{.Arch}}",
    57  				Summary:          "test summary {{.ProjectName}}",
    58  				Description:      "test description {{.ProjectName}}",
    59  				Publish:          true,
    60  				Builds:           []string{"foo"},
    61  				ChannelTemplates: []string{"stable"},
    62  			},
    63  			{
    64  				NameTemplate:     "foo_and_bar_{{.Arch}}",
    65  				Summary:          "test summary",
    66  				Description:      "test description",
    67  				Publish:          true,
    68  				Builds:           []string{"foo", "bar"},
    69  				ChannelTemplates: []string{"stable"},
    70  			},
    71  			{
    72  				NameTemplate:     "bar_{{.Arch}}",
    73  				Summary:          "test summary",
    74  				Description:      "test description",
    75  				Publish:          true,
    76  				Builds:           []string{"bar"},
    77  				ChannelTemplates: []string{"stable"},
    78  			},
    79  		},
    80  	})
    81  	ctx.Git.CurrentTag = "v1.2.3"
    82  	ctx.Version = "1.2.3"
    83  	addBinaries(t, ctx, "foo", filepath.Join(dist, "foo"))
    84  	addBinaries(t, ctx, "bar", filepath.Join(dist, "bar"))
    85  	require.NoError(t, Pipe{}.Run(ctx))
    86  	list := ctx.Artifacts.Filter(artifact.ByType(artifact.PublishableSnapcraft)).List()
    87  	require.Len(t, list, 9)
    88  }
    89  
    90  func TestBadTemolate(t *testing.T) {
    91  	testlib.CheckPath(t, "snapcraft")
    92  	folder := t.TempDir()
    93  	dist := filepath.Join(folder, "dist")
    94  	require.NoError(t, os.Mkdir(dist, 0o755))
    95  	ctx := context.New(config.Project{
    96  		ProjectName: "mybin",
    97  		Dist:        dist,
    98  		Snapcrafts: []config.Snapcraft{
    99  			{
   100  				NameTemplate:     "foo_{{.Arch}}",
   101  				Publish:          true,
   102  				Builds:           []string{"foo"},
   103  				ChannelTemplates: []string{"stable"},
   104  			},
   105  		},
   106  	})
   107  	ctx.Git.CurrentTag = "v1.2.3"
   108  	ctx.Version = "1.2.3"
   109  	addBinaries(t, ctx, "foo", filepath.Join(dist, "foo"))
   110  
   111  	t.Run("description", func(t *testing.T) {
   112  		ctx.Config.Snapcrafts[0].Description = "{{.Bad}}"
   113  		ctx.Config.Snapcrafts[0].Summary = "summary"
   114  		require.Error(t, Pipe{}.Run(ctx))
   115  	})
   116  
   117  	t.Run("summary", func(t *testing.T) {
   118  		ctx.Config.Snapcrafts[0].Description = "description"
   119  		ctx.Config.Snapcrafts[0].Summary = "{{.Bad}}"
   120  		require.Error(t, Pipe{}.Run(ctx))
   121  	})
   122  }
   123  
   124  func TestRunPipeInvalidNameTemplate(t *testing.T) {
   125  	testlib.CheckPath(t, "snapcraft")
   126  	folder := t.TempDir()
   127  	dist := filepath.Join(folder, "dist")
   128  	require.NoError(t, os.Mkdir(dist, 0o755))
   129  	ctx := context.New(config.Project{
   130  		ProjectName: "foo",
   131  		Dist:        dist,
   132  		Snapcrafts: []config.Snapcraft{
   133  			{
   134  				NameTemplate:     "foo_{{.Arch}",
   135  				Summary:          "test summary",
   136  				Description:      "test description",
   137  				Builds:           []string{"foo"},
   138  				ChannelTemplates: []string{"stable"},
   139  			},
   140  		},
   141  	})
   142  	ctx.Git.CurrentTag = "v1.2.3"
   143  	ctx.Version = "1.2.3"
   144  	addBinaries(t, ctx, "foo", dist)
   145  	testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   146  }
   147  
   148  func TestRunPipeWithName(t *testing.T) {
   149  	testlib.CheckPath(t, "snapcraft")
   150  	folder := t.TempDir()
   151  	dist := filepath.Join(folder, "dist")
   152  	require.NoError(t, os.Mkdir(dist, 0o755))
   153  	ctx := context.New(config.Project{
   154  		ProjectName: "testprojectname",
   155  		Dist:        dist,
   156  		Snapcrafts: []config.Snapcraft{
   157  			{
   158  				NameTemplate:     "foo_{{.Arch}}",
   159  				Name:             "testsnapname",
   160  				Base:             "core18",
   161  				License:          "MIT",
   162  				Summary:          "test summary",
   163  				Description:      "test description",
   164  				Builds:           []string{"foo"},
   165  				ChannelTemplates: []string{"stable"},
   166  			},
   167  		},
   168  	})
   169  	ctx.Git.CurrentTag = "v1.2.3"
   170  	ctx.Version = "1.2.3"
   171  	addBinaries(t, ctx, "foo", dist)
   172  	require.NoError(t, Pipe{}.Run(ctx))
   173  	yamlFile, err := os.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
   174  	require.NoError(t, err)
   175  	var metadata Metadata
   176  	err = yaml.Unmarshal(yamlFile, &metadata)
   177  	require.NoError(t, err)
   178  	require.Equal(t, "testsnapname", metadata.Name)
   179  	require.Equal(t, "core18", metadata.Base)
   180  	require.Equal(t, "MIT", metadata.License)
   181  	require.Equal(t, "foo", metadata.Apps["testsnapname"].Command)
   182  }
   183  
   184  func TestRunPipeMetadata(t *testing.T) {
   185  	testlib.CheckPath(t, "snapcraft")
   186  	folder := t.TempDir()
   187  	dist := filepath.Join(folder, "dist")
   188  	require.NoError(t, os.Mkdir(dist, 0o755))
   189  	ctx := context.New(config.Project{
   190  		ProjectName: "testprojectname",
   191  		Dist:        dist,
   192  		Snapcrafts: []config.Snapcraft{
   193  			{
   194  				Name:         "testprojectname",
   195  				NameTemplate: "foo_{{.Arch}}",
   196  				Summary:      "test summary",
   197  				Description:  "test description",
   198  				Layout: map[string]config.SnapcraftLayoutMetadata{
   199  					"/etc/testprojectname": {Bind: "$SNAP_DATA/etc"},
   200  				},
   201  				Apps: map[string]config.SnapcraftAppMetadata{
   202  					"before-foo": {
   203  						Before:  []string{"foo"},
   204  						Command: "foo",
   205  						Daemon:  "notify",
   206  					},
   207  					"after-foo": {
   208  						After:   []string{"foo"},
   209  						Command: "foo",
   210  						Daemon:  "notify",
   211  					},
   212  					"foo": {
   213  						Args:         "--foo --bar",
   214  						Adapter:      "foo_adapter",
   215  						Aliases:      []string{"dummy_alias"},
   216  						Autostart:    "foobar.desktop",
   217  						BusName:      "foo_busname",
   218  						CommandChain: []string{"foo_cmd_chain"},
   219  						CommonID:     "foo_common_id",
   220  						Completer:    "", // Separately tested in TestCompleter
   221  						Daemon:       "simple",
   222  						Desktop:      "foo_desktop",
   223  						Environment: map[string]interface{}{
   224  							"foo": "bar",
   225  						},
   226  						Extensions:  []string{"foo_extension"},
   227  						InstallMode: "disable",
   228  						Passthrough: map[string]interface{}{
   229  							"planet": "saturn",
   230  						},
   231  						Plugs:            []string{"home", "network", "network-bind", "personal-files"},
   232  						PostStopCommand:  "foo",
   233  						RefreshMode:      "endure",
   234  						ReloadCommand:    "foo",
   235  						RestartCondition: "always",
   236  						RestartDelay:     "42ms",
   237  						Slots:            []string{"foo_slot"},
   238  						Sockets: map[string]interface{}{
   239  							"sock": map[string]interface{}{
   240  								"listen-stream": "$SNAP_COMMON/socket",
   241  								"socket-group":  "socket-group",
   242  								"socket-mode":   0o640,
   243  							},
   244  						},
   245  						StartTimeout:    "43ms",
   246  						StopCommand:     "foo",
   247  						StopMode:        "sigterm",
   248  						StopTimeout:     "44ms",
   249  						Timer:           "00:00-24:00/24",
   250  						WatchdogTimeout: "45ms",
   251  					},
   252  				},
   253  				Plugs: map[string]interface{}{
   254  					"personal-files": map[string]interface{}{
   255  						"read": []string{"$HOME/test"},
   256  					},
   257  				},
   258  				Builds:           []string{"foo"},
   259  				ChannelTemplates: []string{"stable"},
   260  			},
   261  		},
   262  	})
   263  	ctx.Git.CurrentTag = "v1.2.3"
   264  	ctx.Version = "1.2.3"
   265  	addBinaries(t, ctx, "foo", dist)
   266  	require.NoError(t, Pipe{}.Run(ctx))
   267  	yamlFile, err := os.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
   268  	require.NoError(t, err)
   269  	var metadata Metadata
   270  	err = yaml.Unmarshal(yamlFile, &metadata)
   271  	require.NoError(t, err)
   272  	require.Equal(t, map[string]AppMetadata{
   273  		"before-foo": {
   274  			Before:  []string{"foo"},
   275  			Command: "foo",
   276  			Daemon:  "notify",
   277  		},
   278  		"after-foo": {
   279  			After:   []string{"foo"},
   280  			Command: "foo",
   281  			Daemon:  "notify",
   282  		},
   283  		"foo": {
   284  			Adapter:      "foo_adapter",
   285  			Aliases:      []string{"dummy_alias"},
   286  			Autostart:    "foobar.desktop",
   287  			BusName:      "foo_busname",
   288  			Command:      "foo --foo --bar",
   289  			CommandChain: []string{"foo_cmd_chain"},
   290  			CommonID:     "foo_common_id",
   291  			Completer:    "",
   292  			Daemon:       "simple",
   293  			Desktop:      "foo_desktop",
   294  			Environment: map[string]interface{}{
   295  				"foo": "bar",
   296  			},
   297  			Extensions:  []string{"foo_extension"},
   298  			InstallMode: "disable",
   299  			Passthrough: map[string]interface{}{
   300  				"planet": "saturn",
   301  			},
   302  			Plugs:            []string{"home", "network", "network-bind", "personal-files"},
   303  			PostStopCommand:  "foo",
   304  			RefreshMode:      "endure",
   305  			ReloadCommand:    "foo",
   306  			RestartCondition: "always",
   307  			RestartDelay:     "42ms",
   308  			Slots:            []string{"foo_slot"},
   309  			Sockets: map[string]interface{}{
   310  				"sock": map[string]interface{}{
   311  					"listen-stream": "$SNAP_COMMON/socket",
   312  					"socket-group":  "socket-group",
   313  					"socket-mode":   0o640,
   314  				},
   315  			},
   316  			StartTimeout:    "43ms",
   317  			StopCommand:     "foo",
   318  			StopMode:        "sigterm",
   319  			StopTimeout:     "44ms",
   320  			Timer:           "00:00-24:00/24",
   321  			WatchdogTimeout: "45ms",
   322  		},
   323  	}, metadata.Apps)
   324  	require.Equal(t, map[string]interface{}{"read": []interface{}{"$HOME/test"}}, metadata.Plugs["personal-files"])
   325  	require.Equal(t, "$SNAP_DATA/etc", metadata.Layout["/etc/testprojectname"].Bind)
   326  }
   327  
   328  func TestNoSnapcraftInPath(t *testing.T) {
   329  	path := os.Getenv("PATH")
   330  	defer func() {
   331  		require.NoError(t, os.Setenv("PATH", path))
   332  	}()
   333  	require.NoError(t, os.Setenv("PATH", ""))
   334  	ctx := context.New(config.Project{
   335  		Snapcrafts: []config.Snapcraft{
   336  			{
   337  				Summary:     "dummy",
   338  				Description: "dummy",
   339  			},
   340  		},
   341  	})
   342  	require.EqualError(t, Pipe{}.Run(ctx), ErrNoSnapcraft.Error())
   343  }
   344  
   345  func TestRunNoArguments(t *testing.T) {
   346  	testlib.CheckPath(t, "snapcraft")
   347  	folder := t.TempDir()
   348  	dist := filepath.Join(folder, "dist")
   349  	require.NoError(t, os.Mkdir(dist, 0o755))
   350  	ctx := context.New(config.Project{
   351  		ProjectName: "testprojectname",
   352  		Dist:        dist,
   353  		Snapcrafts: []config.Snapcraft{
   354  			{
   355  				NameTemplate: "foo_{{.Arch}}",
   356  				Summary:      "test summary",
   357  				Description:  "test description",
   358  				Apps: map[string]config.SnapcraftAppMetadata{
   359  					"foo": {
   360  						Daemon: "simple",
   361  						Args:   "",
   362  					},
   363  				},
   364  				Builds:           []string{"foo"},
   365  				ChannelTemplates: []string{"stable"},
   366  			},
   367  		},
   368  	})
   369  	ctx.Git.CurrentTag = "v1.2.3"
   370  	ctx.Version = "1.2.3"
   371  	addBinaries(t, ctx, "foo", dist)
   372  	require.NoError(t, Pipe{}.Run(ctx))
   373  	yamlFile, err := os.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
   374  	require.NoError(t, err)
   375  	var metadata Metadata
   376  	err = yaml.Unmarshal(yamlFile, &metadata)
   377  	require.NoError(t, err)
   378  	require.Equal(t, "foo", metadata.Apps["foo"].Command)
   379  }
   380  
   381  func TestCompleter(t *testing.T) {
   382  	testlib.CheckPath(t, "snapcraft")
   383  	folder := t.TempDir()
   384  	dist := filepath.Join(folder, "dist")
   385  	require.NoError(t, os.Mkdir(dist, 0o755))
   386  	ctx := context.New(config.Project{
   387  		ProjectName: "testprojectname",
   388  		Dist:        dist,
   389  		Snapcrafts: []config.Snapcraft{
   390  			{
   391  				NameTemplate: "foo_{{.Arch}}",
   392  				Summary:      "test summary",
   393  				Description:  "test description",
   394  				Apps: map[string]config.SnapcraftAppMetadata{
   395  					"foo": {
   396  						Daemon:    "simple",
   397  						Args:      "",
   398  						Completer: "testdata/foo-completer.bash",
   399  					},
   400  				},
   401  				Builds:           []string{"foo", "bar"},
   402  				ChannelTemplates: []string{"stable"},
   403  			},
   404  		},
   405  	})
   406  	ctx.Git.CurrentTag = "v1.2.3"
   407  	ctx.Version = "1.2.3"
   408  	addBinaries(t, ctx, "foo", dist)
   409  	addBinaries(t, ctx, "bar", dist)
   410  	require.NoError(t, Pipe{}.Run(ctx))
   411  	yamlFile, err := os.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
   412  	require.NoError(t, err)
   413  	var metadata Metadata
   414  	err = yaml.Unmarshal(yamlFile, &metadata)
   415  	require.NoError(t, err)
   416  	require.Equal(t, "foo", metadata.Apps["foo"].Command)
   417  	require.Equal(t, "testdata/foo-completer.bash", metadata.Apps["foo"].Completer)
   418  }
   419  
   420  func TestCommand(t *testing.T) {
   421  	testlib.CheckPath(t, "snapcraft")
   422  	folder := t.TempDir()
   423  	dist := filepath.Join(folder, "dist")
   424  	require.NoError(t, os.Mkdir(dist, 0o755))
   425  	ctx := context.New(config.Project{
   426  		ProjectName: "testprojectname",
   427  		Dist:        dist,
   428  		Snapcrafts: []config.Snapcraft{
   429  			{
   430  				NameTemplate: "foo_{{.Arch}}",
   431  				Summary:      "test summary",
   432  				Description:  "test description",
   433  				Apps: map[string]config.SnapcraftAppMetadata{
   434  					"foo": {
   435  						Daemon:  "simple",
   436  						Args:    "--bar custom command",
   437  						Command: "foo",
   438  					},
   439  				},
   440  				Builds:           []string{"foo"},
   441  				ChannelTemplates: []string{"stable"},
   442  			},
   443  		},
   444  	})
   445  	ctx.Git.CurrentTag = "v1.2.3"
   446  	ctx.Version = "1.2.3"
   447  	addBinaries(t, ctx, "foo", dist)
   448  	require.NoError(t, Pipe{}.Run(ctx))
   449  	yamlFile, err := os.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
   450  	require.NoError(t, err)
   451  	var metadata Metadata
   452  	err = yaml.Unmarshal(yamlFile, &metadata)
   453  	require.NoError(t, err)
   454  	require.Equal(t, "foo --bar custom command", metadata.Apps["foo"].Command)
   455  }
   456  
   457  func TestExtraFile(t *testing.T) {
   458  	testlib.CheckPath(t, "snapcraft")
   459  	folder := t.TempDir()
   460  	dist := filepath.Join(folder, "dist")
   461  	require.NoError(t, os.Mkdir(dist, 0o755))
   462  	ctx := context.New(config.Project{
   463  		ProjectName: "testprojectname",
   464  		Dist:        dist,
   465  		Snapcrafts: []config.Snapcraft{
   466  			{
   467  				NameTemplate: "foo_{{.Arch}}",
   468  				Summary:      "test summary",
   469  				Description:  "test description",
   470  				Files: []config.SnapcraftExtraFiles{
   471  					{
   472  						Source:      "testdata/extra-file.txt",
   473  						Destination: "a/b/c/extra-file.txt",
   474  						Mode:        0o755,
   475  					},
   476  					{
   477  						Source: "testdata/extra-file-2.txt",
   478  					},
   479  				},
   480  				Builds:           []string{"foo"},
   481  				ChannelTemplates: []string{"stable"},
   482  			},
   483  		},
   484  	})
   485  	ctx.Git.CurrentTag = "v1.2.3"
   486  	ctx.Version = "1.2.3"
   487  	addBinaries(t, ctx, "foo", dist)
   488  	require.NoError(t, Pipe{}.Run(ctx))
   489  
   490  	apath := filepath.Join(dist, "foo_amd64", "prime", "a", "b", "c", "extra-file.txt")
   491  	bpath := filepath.Join(dist, "foo_amd64", "prime", "testdata", "extra-file-2.txt")
   492  	requireEqualFileConents(t, "testdata/extra-file.txt", apath)
   493  	requireEqualFileConents(t, "testdata/extra-file-2.txt", bpath)
   494  }
   495  
   496  func TestDefault(t *testing.T) {
   497  	ctx := context.New(config.Project{
   498  		Builds: []config.Build{
   499  			{
   500  				ID: "foo",
   501  			},
   502  		},
   503  		Snapcrafts: []config.Snapcraft{
   504  			{},
   505  		},
   506  	})
   507  	require.NoError(t, Pipe{}.Default(ctx))
   508  	require.Equal(t, defaultNameTemplate, ctx.Config.Snapcrafts[0].NameTemplate)
   509  	require.Equal(t, []string{"foo"}, ctx.Config.Snapcrafts[0].Builds)
   510  }
   511  
   512  func TestPublish(t *testing.T) {
   513  	ctx := context.New(config.Project{})
   514  	ctx.Artifacts.Add(&artifact.Artifact{
   515  		Name:   "mybin",
   516  		Path:   "nope.snap",
   517  		Goarch: "amd64",
   518  		Goos:   "linux",
   519  		Type:   artifact.PublishableSnapcraft,
   520  		Extra: map[string]interface{}{
   521  			releasesExtra: []string{"stable", "candidate"},
   522  		},
   523  	})
   524  	err := Pipe{}.Publish(ctx)
   525  	require.Contains(t, err.Error(), "failed to push nope.snap package")
   526  }
   527  
   528  func TestPublishSkip(t *testing.T) {
   529  	ctx := context.New(config.Project{})
   530  	ctx.SkipPublish = true
   531  	ctx.Artifacts.Add(&artifact.Artifact{
   532  		Name:   "mybin",
   533  		Path:   "nope.snap",
   534  		Goarch: "amd64",
   535  		Goos:   "linux",
   536  		Type:   artifact.PublishableSnapcraft,
   537  		Extra: map[string]interface{}{
   538  			releasesExtra: []string{"stable"},
   539  		},
   540  	})
   541  	testlib.AssertSkipped(t, Pipe{}.Publish(ctx))
   542  }
   543  
   544  func TestDefaultSet(t *testing.T) {
   545  	ctx := context.New(config.Project{
   546  		Snapcrafts: []config.Snapcraft{
   547  			{
   548  				ID:           "devel",
   549  				NameTemplate: "foo",
   550  				Grade:        "devel",
   551  			},
   552  			{
   553  				ID:           "stable",
   554  				NameTemplate: "bar",
   555  				Grade:        "stable",
   556  			},
   557  		},
   558  	})
   559  	require.NoError(t, Pipe{}.Default(ctx))
   560  	require.Equal(t, "foo", ctx.Config.Snapcrafts[0].NameTemplate)
   561  	require.Equal(t, []string{"edge", "beta"}, ctx.Config.Snapcrafts[0].ChannelTemplates)
   562  	require.Equal(t, []string{"edge", "beta", "candidate", "stable"}, ctx.Config.Snapcrafts[1].ChannelTemplates)
   563  }
   564  
   565  func Test_processChannelsTemplates(t *testing.T) {
   566  	ctx := &context.Context{
   567  		Config: config.Project{
   568  			Builds: []config.Build{
   569  				{
   570  					ID: "default",
   571  				},
   572  			},
   573  			Snapcrafts: []config.Snapcraft{
   574  				{
   575  					Name: "mybin",
   576  					ChannelTemplates: []string{
   577  						"{{.Major}}.{{.Minor}}/stable",
   578  						"stable",
   579  					},
   580  				},
   581  			},
   582  		},
   583  	}
   584  
   585  	ctx.SkipPublish = true
   586  	ctx.Env = map[string]string{
   587  		"FOO": "123",
   588  	}
   589  	ctx.Version = "1.0.0"
   590  	ctx.Git = context.GitInfo{
   591  		CurrentTag: "v1.0.0",
   592  		Commit:     "a1b2c3d4",
   593  	}
   594  	ctx.Semver = context.Semver{
   595  		Major: 1,
   596  		Minor: 0,
   597  		Patch: 0,
   598  	}
   599  
   600  	require.NoError(t, Pipe{}.Default(ctx))
   601  
   602  	snap := ctx.Config.Snapcrafts[0]
   603  	require.Equal(t, "mybin", snap.Name)
   604  
   605  	channels, err := processChannelsTemplates(ctx, snap)
   606  	require.NoError(t, err)
   607  	require.Equal(t, []string{
   608  		"1.0/stable",
   609  		"stable",
   610  	}, channels)
   611  }
   612  
   613  func addBinaries(t *testing.T, ctx *context.Context, name, dist string) {
   614  	t.Helper()
   615  	for _, goos := range []string{"linux", "darwin"} {
   616  		for _, goarch := range []string{"amd64", "386", "arm"} {
   617  			binPath := filepath.Join(dist, name)
   618  			require.NoError(t, os.MkdirAll(filepath.Dir(binPath), 0o755))
   619  			f, err := os.Create(binPath)
   620  			require.NoError(t, err)
   621  			require.NoError(t, f.Close())
   622  			switch goarch {
   623  			case "arm":
   624  				ctx.Artifacts.Add(&artifact.Artifact{
   625  					Name:   "subdir/" + name,
   626  					Path:   binPath,
   627  					Goarch: goarch,
   628  					Goos:   goos,
   629  					Goarm:  "6",
   630  					Type:   artifact.Binary,
   631  					Extra: map[string]interface{}{
   632  						artifact.ExtraID: name,
   633  					},
   634  				})
   635  
   636  			case "amd64":
   637  				ctx.Artifacts.Add(&artifact.Artifact{
   638  					Name:    "subdir/" + name,
   639  					Path:    binPath,
   640  					Goarch:  goarch,
   641  					Goos:    goos,
   642  					Goamd64: "v1",
   643  					Type:    artifact.Binary,
   644  					Extra: map[string]interface{}{
   645  						artifact.ExtraID: name,
   646  					},
   647  				})
   648  			default:
   649  				ctx.Artifacts.Add(&artifact.Artifact{
   650  					Name:   "subdir/" + name,
   651  					Path:   binPath,
   652  					Goarch: goarch,
   653  					Goos:   goos,
   654  					Type:   artifact.Binary,
   655  					Extra: map[string]interface{}{
   656  						artifact.ExtraID: name,
   657  					},
   658  				})
   659  			}
   660  		}
   661  	}
   662  }
   663  
   664  func TestSeveralSnapssWithTheSameID(t *testing.T) {
   665  	ctx := &context.Context{
   666  		Config: config.Project{
   667  			Snapcrafts: []config.Snapcraft{
   668  				{
   669  					ID: "a",
   670  				},
   671  				{
   672  					ID: "a",
   673  				},
   674  			},
   675  		},
   676  	}
   677  	require.EqualError(t, Pipe{}.Default(ctx), "found 2 snapcrafts with the ID 'a', please fix your config")
   678  }
   679  
   680  func Test_isValidArch(t *testing.T) {
   681  	tests := []struct {
   682  		arch string
   683  		want bool
   684  	}{
   685  		{"s390x", true},
   686  		{"ppc64el", true},
   687  		{"arm64", true},
   688  		{"armhf", true},
   689  		{"i386", true},
   690  		{"mips", false},
   691  		{"armel", false},
   692  	}
   693  	for _, tt := range tests {
   694  		t.Run(tt.arch, func(t *testing.T) {
   695  			require.Equal(t, tt.want, isValidArch(tt.arch))
   696  		})
   697  	}
   698  }
   699  
   700  func TestSkip(t *testing.T) {
   701  	t.Run("skip", func(t *testing.T) {
   702  		require.True(t, Pipe{}.Skip(context.New(config.Project{})))
   703  	})
   704  
   705  	t.Run("dont skip", func(t *testing.T) {
   706  		ctx := context.New(config.Project{
   707  			Snapcrafts: []config.Snapcraft{
   708  				{},
   709  			},
   710  		})
   711  		require.False(t, Pipe{}.Skip(ctx))
   712  	})
   713  }
   714  
   715  func requireEqualFileConents(tb testing.TB, a, b string) {
   716  	tb.Helper()
   717  	eq, err := gio.EqualFileContents(a, b)
   718  	require.NoError(tb, err)
   719  	require.True(tb, eq, "%s != %s", a, b)
   720  }