github.com/triarius/goreleaser@v1.12.5/internal/pipe/nfpm/nfpm_test.go (about)

     1  package nfpm
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/triarius/goreleaser/internal/artifact"
    10  	"github.com/triarius/goreleaser/internal/testlib"
    11  	"github.com/triarius/goreleaser/pkg/config"
    12  	"github.com/triarius/goreleaser/pkg/context"
    13  	"github.com/goreleaser/nfpm/v2/files"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestDescription(t *testing.T) {
    18  	require.NotEmpty(t, Pipe{}.String())
    19  }
    20  
    21  func TestRunPipeNoFormats(t *testing.T) {
    22  	ctx := &context.Context{
    23  		Version: "1.0.0",
    24  		Git: context.GitInfo{
    25  			CurrentTag: "v1.0.0",
    26  		},
    27  		Config: config.Project{
    28  			NFPMs: []config.NFPM{
    29  				{},
    30  			},
    31  		},
    32  		Parallelism: runtime.NumCPU(),
    33  	}
    34  	require.NoError(t, Pipe{}.Default(ctx))
    35  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    36  }
    37  
    38  func TestRunPipeInvalidFormat(t *testing.T) {
    39  	ctx := context.New(config.Project{
    40  		ProjectName: "nope",
    41  		NFPMs: []config.NFPM{
    42  			{
    43  				Bindir:  "/usr/bin",
    44  				Formats: []string{"nope"},
    45  				Builds:  []string{"foo"},
    46  				NFPMOverridables: config.NFPMOverridables{
    47  					PackageName:      "foo",
    48  					FileNameTemplate: defaultNameTemplate,
    49  				},
    50  			},
    51  		},
    52  	})
    53  	ctx.Version = "1.2.3"
    54  	ctx.Git = context.GitInfo{
    55  		CurrentTag: "v1.2.3",
    56  	}
    57  	for _, goos := range []string{"linux", "darwin"} {
    58  		for _, goarch := range []string{"amd64", "386"} {
    59  			ctx.Artifacts.Add(&artifact.Artifact{
    60  				Name:   "mybin",
    61  				Path:   "testdata/testfile.txt",
    62  				Goarch: goarch,
    63  				Goos:   goos,
    64  				Type:   artifact.Binary,
    65  				Extra: map[string]interface{}{
    66  					artifact.ExtraID: "foo",
    67  				},
    68  			})
    69  		}
    70  	}
    71  	require.Contains(t, Pipe{}.Run(ctx).Error(), `no packager registered for the format nope`)
    72  }
    73  
    74  func TestRunPipe(t *testing.T) {
    75  	folder := t.TempDir()
    76  	dist := filepath.Join(folder, "dist")
    77  	require.NoError(t, os.Mkdir(dist, 0o755))
    78  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
    79  	binPath := filepath.Join(dist, "mybin", "mybin")
    80  	f, err := os.Create(binPath)
    81  	require.NoError(t, err)
    82  	require.NoError(t, f.Close())
    83  	ctx := context.New(config.Project{
    84  		ProjectName: "mybin",
    85  		Dist:        dist,
    86  		Env: []string{
    87  			"PRO=pro",
    88  			"DESC=templates",
    89  		},
    90  		NFPMs: []config.NFPM{
    91  			{
    92  				ID:          "someid",
    93  				Bindir:      "/usr/bin",
    94  				Builds:      []string{"default"},
    95  				Formats:     []string{"deb", "rpm", "apk", "termux.deb"},
    96  				Section:     "somesection",
    97  				Priority:    "standard",
    98  				Description: "Some description with {{ .Env.DESC }}",
    99  				License:     "MIT",
   100  				Maintainer:  "me@me",
   101  				Vendor:      "asdf",
   102  				Homepage:    "https://goreleaser.com/{{ .Env.PRO }}",
   103  				Changelog:   "./testdata/changelog.yaml",
   104  				NFPMOverridables: config.NFPMOverridables{
   105  					FileNameTemplate: defaultNameTemplate + "-{{ .Release }}-{{ .Epoch }}",
   106  					PackageName:      "foo",
   107  					Dependencies:     []string{"make"},
   108  					Recommends:       []string{"svn"},
   109  					Suggests:         []string{"bzr"},
   110  					Replaces:         []string{"fish"},
   111  					Conflicts:        []string{"git"},
   112  					Provides:         []string{"ash"},
   113  					Release:          "10",
   114  					Epoch:            "20",
   115  					Contents: []*files.Content{
   116  						{
   117  							Destination: "/var/log/foobar",
   118  							Type:        "dir",
   119  						},
   120  						{
   121  							Source:      "./testdata/testfile.txt",
   122  							Destination: "/usr/share/testfile.txt",
   123  						},
   124  						{
   125  							Source:      "./testdata/testfile.txt",
   126  							Destination: "/etc/nope.conf",
   127  							Type:        "config",
   128  						},
   129  						{
   130  							Destination: "/etc/mydir",
   131  							Type:        "dir",
   132  						},
   133  						{
   134  							Source:      "./testdata/testfile.txt",
   135  							Destination: "/etc/nope-rpm.conf",
   136  							Type:        "config",
   137  							Packager:    "rpm",
   138  						},
   139  						{
   140  							Source:      "/etc/nope.conf",
   141  							Destination: "/etc/nope2.conf",
   142  							Type:        "symlink",
   143  						},
   144  						{
   145  							Source:      "./testdata/testfile-{{ .Arch }}{{.Amd64}}{{.Arm}}{{.Mips}}.txt",
   146  							Destination: "/etc/nope3_{{ .ProjectName }}.conf",
   147  						},
   148  						{
   149  							Source:      "./testdata/folder",
   150  							Destination: "/etc/folder",
   151  						},
   152  					},
   153  					Replacements: map[string]string{
   154  						"linux": "Tux",
   155  					},
   156  				},
   157  			},
   158  		},
   159  	})
   160  	ctx.Version = "1.0.0"
   161  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   162  	for _, goos := range []string{"linux", "darwin", "ios"} {
   163  		for _, goarch := range []string{"amd64", "386", "arm64", "arm", "mips"} {
   164  			if goos == "ios" && goarch != "arm64" {
   165  				continue
   166  			}
   167  			switch goarch {
   168  			case "arm":
   169  				for _, goarm := range []string{"6", "7"} {
   170  					ctx.Artifacts.Add(&artifact.Artifact{
   171  						Name:   "subdir/mybin",
   172  						Path:   binPath,
   173  						Goarch: goarch,
   174  						Goos:   goos,
   175  						Goarm:  goarm,
   176  						Type:   artifact.Binary,
   177  						Extra: map[string]interface{}{
   178  							artifact.ExtraID: "default",
   179  						},
   180  					})
   181  				}
   182  			case "amd64":
   183  				for _, goamd64 := range []string{"v1", "v2", "v3", "v4"} {
   184  					ctx.Artifacts.Add(&artifact.Artifact{
   185  						Name:    "subdir/mybin",
   186  						Path:    binPath,
   187  						Goarch:  goarch,
   188  						Goos:    goos,
   189  						Goamd64: goamd64,
   190  						Type:    artifact.Binary,
   191  						Extra: map[string]interface{}{
   192  							artifact.ExtraID: "default",
   193  						},
   194  					})
   195  				}
   196  			case "mips":
   197  				for _, gomips := range []string{"softfloat", "hardfloat"} {
   198  					ctx.Artifacts.Add(&artifact.Artifact{
   199  						Name:   "subdir/mybin",
   200  						Path:   binPath,
   201  						Goarch: goarch,
   202  						Goos:   goos,
   203  						Gomips: gomips,
   204  						Type:   artifact.Binary,
   205  						Extra: map[string]interface{}{
   206  							artifact.ExtraID: "default",
   207  						},
   208  					})
   209  				}
   210  			default:
   211  				ctx.Artifacts.Add(&artifact.Artifact{
   212  					Name:   "subdir/mybin",
   213  					Path:   binPath,
   214  					Goarch: goarch,
   215  					Goos:   goos,
   216  					Type:   artifact.Binary,
   217  					Extra: map[string]interface{}{
   218  						artifact.ExtraID: "default",
   219  					},
   220  				})
   221  			}
   222  		}
   223  	}
   224  	require.NoError(t, Pipe{}.Run(ctx))
   225  	packages := ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
   226  	require.Len(t, packages, 40)
   227  	for _, pkg := range packages {
   228  		format := pkg.Format()
   229  		require.NotEmpty(t, format)
   230  		arch := pkg.Goarch
   231  		if pkg.Goarm != "" {
   232  			arch += "v" + pkg.Goarm
   233  		}
   234  		if pkg.Goamd64 != "v1" {
   235  			arch += pkg.Goamd64
   236  		}
   237  		if pkg.Gomips != "" {
   238  			arch += "_" + pkg.Gomips
   239  		}
   240  		if pkg.Goos == "linux" {
   241  			require.Equal(t, "foo_1.0.0_Tux_"+arch+"-10-20."+format, pkg.Name)
   242  		} else {
   243  			require.Equal(t, "foo_1.0.0_ios_arm64-10-20."+format, pkg.Name)
   244  		}
   245  		require.Equal(t, "someid", pkg.ID())
   246  		require.ElementsMatch(t, []string{
   247  			"./testdata/testfile.txt",
   248  			"./testdata/testfile.txt",
   249  			"./testdata/testfile.txt",
   250  			"/etc/nope.conf",
   251  			"./testdata/folder",
   252  			"./testdata/testfile-" + pkg.Goarch + pkg.Goamd64 + pkg.Goarm + pkg.Gomips + ".txt",
   253  			binPath,
   254  		}, sources(artifact.ExtraOr(*pkg, extraFiles, files.Contents{})))
   255  
   256  		bin := "/usr/bin/subdir/"
   257  		if format == termuxFormat {
   258  			bin = filepath.Join("/data/data/com.termux/files", bin)
   259  		}
   260  		bin = filepath.Join(bin, "mybin")
   261  		require.ElementsMatch(t, []string{
   262  			"/var/log/foobar",
   263  			"/usr/share/testfile.txt",
   264  			"/etc/mydir",
   265  			"/etc/nope.conf",
   266  			"/etc/nope-rpm.conf",
   267  			"/etc/nope2.conf",
   268  			"/etc/nope3_mybin.conf",
   269  			"/etc/folder",
   270  			bin,
   271  		}, destinations(artifact.ExtraOr(*pkg, extraFiles, files.Contents{})))
   272  	}
   273  	require.Len(t, ctx.Config.NFPMs[0].Contents, 8, "should not modify the config file list")
   274  }
   275  
   276  func TestRunPipeConventionalNameTemplate(t *testing.T) {
   277  	folder := t.TempDir()
   278  	dist := filepath.Join(folder, "dist")
   279  	require.NoError(t, os.Mkdir(dist, 0o755))
   280  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
   281  	binPath := filepath.Join(dist, "mybin", "mybin")
   282  	f, err := os.Create(binPath)
   283  	require.NoError(t, err)
   284  	require.NoError(t, f.Close())
   285  	ctx := context.New(config.Project{
   286  		ProjectName: "mybin",
   287  		Dist:        dist,
   288  		NFPMs: []config.NFPM{
   289  			{
   290  				ID:          "someid",
   291  				Builds:      []string{"default"},
   292  				Formats:     []string{"deb", "rpm", "apk"},
   293  				Section:     "somesection",
   294  				Priority:    "standard",
   295  				Description: "Some description ",
   296  				License:     "MIT",
   297  				Maintainer:  "me@me",
   298  				Vendor:      "asdf",
   299  				Homepage:    "https://goreleaser.com/",
   300  				Bindir:      "/usr/bin",
   301  				NFPMOverridables: config.NFPMOverridables{
   302  					FileNameTemplate: `{{ trimsuffix (trimsuffix (trimsuffix .ConventionalFileName ".deb") ".rpm") ".apk" }}{{ if not (eq .Amd64 "v1")}}{{ .Amd64 }}{{ end }}`,
   303  					PackageName:      "foo",
   304  				},
   305  			},
   306  		},
   307  	})
   308  	ctx.Version = "1.0.0"
   309  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   310  	for _, goos := range []string{"linux", "darwin"} {
   311  		for _, goarch := range []string{"amd64", "386", "arm64", "arm", "mips"} {
   312  			switch goarch {
   313  			case "arm":
   314  				for _, goarm := range []string{"6", "7"} {
   315  					ctx.Artifacts.Add(&artifact.Artifact{
   316  						Name:   "subdir/mybin",
   317  						Path:   binPath,
   318  						Goarch: goarch,
   319  						Goos:   goos,
   320  						Goarm:  goarm,
   321  						Type:   artifact.Binary,
   322  						Extra: map[string]interface{}{
   323  							artifact.ExtraID: "default",
   324  						},
   325  					})
   326  				}
   327  			case "amd64":
   328  				for _, goamd64 := range []string{"v1", "v2", "v3", "v4"} {
   329  					ctx.Artifacts.Add(&artifact.Artifact{
   330  						Name:    "subdir/mybin",
   331  						Path:    binPath,
   332  						Goarch:  goarch,
   333  						Goos:    goos,
   334  						Goamd64: goamd64,
   335  						Type:    artifact.Binary,
   336  						Extra: map[string]interface{}{
   337  							artifact.ExtraID: "default",
   338  						},
   339  					})
   340  				}
   341  			case "mips":
   342  				for _, gomips := range []string{"softfloat", "hardfloat"} {
   343  					ctx.Artifacts.Add(&artifact.Artifact{
   344  						Name:   "subdir/mybin",
   345  						Path:   binPath,
   346  						Goarch: goarch,
   347  						Goos:   goos,
   348  						Gomips: gomips,
   349  						Type:   artifact.Binary,
   350  						Extra: map[string]interface{}{
   351  							artifact.ExtraID: "default",
   352  						},
   353  					})
   354  				}
   355  			default:
   356  				ctx.Artifacts.Add(&artifact.Artifact{
   357  					Name:   "subdir/mybin",
   358  					Path:   binPath,
   359  					Goarch: goarch,
   360  					Goos:   goos,
   361  					Type:   artifact.Binary,
   362  					Extra: map[string]interface{}{
   363  						artifact.ExtraID: "default",
   364  					},
   365  				})
   366  			}
   367  		}
   368  	}
   369  	require.NoError(t, Pipe{}.Run(ctx))
   370  	packages := ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
   371  	require.Len(t, packages, 30)
   372  	for _, pkg := range packages {
   373  		format := pkg.Format()
   374  		require.NotEmpty(t, format)
   375  		require.Contains(t, []string{
   376  			"foo-1.0.0.aarch64.rpm",
   377  			"foo-1.0.0.armv6hl.rpm",
   378  			"foo-1.0.0.armv7hl.rpm",
   379  			"foo-1.0.0.i386.rpm",
   380  			"foo-1.0.0.mipshardfloat.rpm",
   381  			"foo-1.0.0.mipssoftfloat.rpm",
   382  			"foo-1.0.0.x86_64.rpm",
   383  			"foo-1.0.0.x86_64v2.rpm",
   384  			"foo-1.0.0.x86_64v3.rpm",
   385  			"foo-1.0.0.x86_64v4.rpm",
   386  			"foo_1.0.0_aarch64.apk",
   387  			"foo_1.0.0_amd64.deb",
   388  			"foo_1.0.0_amd64v2.deb",
   389  			"foo_1.0.0_amd64v3.deb",
   390  			"foo_1.0.0_amd64v4.deb",
   391  			"foo_1.0.0_arm64.deb",
   392  			"foo_1.0.0_armhf.apk",
   393  			"foo_1.0.0_armhf.deb",
   394  			"foo_1.0.0_armv7.apk",
   395  			"foo_1.0.0_i386.deb",
   396  			"foo_1.0.0_mipshardfloat.apk",
   397  			"foo_1.0.0_mipshardfloat.deb",
   398  			"foo_1.0.0_mipssoftfloat.apk",
   399  			"foo_1.0.0_mipssoftfloat.deb",
   400  			"foo_1.0.0_x86.apk",
   401  			"foo_1.0.0_x86_64.apk",
   402  			"foo_1.0.0_x86_64v2.apk",
   403  			"foo_1.0.0_x86_64v3.apk",
   404  			"foo_1.0.0_x86_64v4.apk",
   405  		}, pkg.Name, "package name is not expected")
   406  		require.Equal(t, "someid", pkg.ID())
   407  		require.ElementsMatch(t, []string{binPath}, sources(artifact.ExtraOr(*pkg, extraFiles, files.Contents{})))
   408  		require.ElementsMatch(t, []string{"/usr/bin/subdir/mybin"}, destinations(artifact.ExtraOr(*pkg, extraFiles, files.Contents{})))
   409  	}
   410  }
   411  
   412  func TestInvalidTemplate(t *testing.T) {
   413  	makeCtx := func() *context.Context {
   414  		ctx := &context.Context{
   415  			Version:     "1.2.3",
   416  			Parallelism: runtime.NumCPU(),
   417  			Artifacts:   artifact.New(),
   418  			Config: config.Project{
   419  				ProjectName: "test",
   420  				NFPMs: []config.NFPM{
   421  					{
   422  						Formats: []string{"deb"},
   423  						Builds:  []string{"default"},
   424  					},
   425  				},
   426  			},
   427  		}
   428  		ctx.Artifacts.Add(&artifact.Artifact{
   429  			Name:   "mybin",
   430  			Goos:   "linux",
   431  			Goarch: "amd64",
   432  			Type:   artifact.Binary,
   433  			Extra: map[string]interface{}{
   434  				artifact.ExtraID: "default",
   435  			},
   436  		})
   437  		return ctx
   438  	}
   439  
   440  	t.Run("filename_template", func(t *testing.T) {
   441  		ctx := makeCtx()
   442  		ctx.Config.NFPMs[0].Meta = true
   443  		ctx.Config.NFPMs[0].NFPMOverridables = config.NFPMOverridables{
   444  			FileNameTemplate: "{{.Foo}",
   445  		}
   446  		require.NoError(t, Pipe{}.Default(ctx))
   447  		testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   448  	})
   449  
   450  	t.Run("source", func(t *testing.T) {
   451  		ctx := makeCtx()
   452  		ctx.Config.NFPMs[0].NFPMOverridables = config.NFPMOverridables{
   453  			Contents: files.Contents{
   454  				{
   455  					Source:      "{{ .NOPE_SOURCE }}",
   456  					Destination: "/foo",
   457  				},
   458  			},
   459  		}
   460  		testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   461  	})
   462  
   463  	t.Run("target", func(t *testing.T) {
   464  		ctx := makeCtx()
   465  		ctx.Config.NFPMs[0].NFPMOverridables = config.NFPMOverridables{
   466  			Contents: files.Contents{
   467  				{
   468  					Source:      "./testdata/testfile.txt",
   469  					Destination: "{{ .NOPE_TARGET }}",
   470  				},
   471  			},
   472  		}
   473  		testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   474  	})
   475  
   476  	t.Run("description", func(t *testing.T) {
   477  		ctx := makeCtx()
   478  		ctx.Config.NFPMs[0].Description = "{{ .NOPE_DESC }}"
   479  		testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   480  	})
   481  
   482  	t.Run("maintainer", func(t *testing.T) {
   483  		ctx := makeCtx()
   484  		ctx.Config.NFPMs[0].Maintainer = "{{ .NOPE_DESC }}"
   485  		testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   486  	})
   487  
   488  	t.Run("homepage", func(t *testing.T) {
   489  		ctx := makeCtx()
   490  		ctx.Config.NFPMs[0].Homepage = "{{ .NOPE_HOMEPAGE }}"
   491  		testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   492  	})
   493  
   494  	t.Run("deb key file", func(t *testing.T) {
   495  		ctx := makeCtx()
   496  		ctx.Config.NFPMs[0].Deb.Signature.KeyFile = "{{ .NOPE_KEY_FILE }}"
   497  		testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   498  	})
   499  
   500  	t.Run("rpm key file", func(t *testing.T) {
   501  		ctx := makeCtx()
   502  		ctx.Config.NFPMs[0].RPM.Signature.KeyFile = "{{ .NOPE_KEY_FILE }}"
   503  		testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   504  	})
   505  
   506  	t.Run("apk key file", func(t *testing.T) {
   507  		ctx := makeCtx()
   508  		ctx.Config.NFPMs[0].APK.Signature.KeyFile = "{{ .NOPE_KEY_FILE }}"
   509  		testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   510  	})
   511  
   512  	t.Run("bindir", func(t *testing.T) {
   513  		ctx := makeCtx()
   514  		ctx.Config.NFPMs[0].Bindir = "/usr/{{ .NOPE }}"
   515  		testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   516  	})
   517  }
   518  
   519  func TestRunPipeInvalidContentsSourceTemplate(t *testing.T) {
   520  	ctx := &context.Context{
   521  		Parallelism: runtime.NumCPU(),
   522  		Artifacts:   artifact.New(),
   523  		Config: config.Project{
   524  			NFPMs: []config.NFPM{
   525  				{
   526  					NFPMOverridables: config.NFPMOverridables{
   527  						PackageName: "foo",
   528  						Contents: []*files.Content{
   529  							{
   530  								Source:      "{{.asdsd}",
   531  								Destination: "testfile",
   532  							},
   533  						},
   534  					},
   535  					Formats: []string{"deb"},
   536  					Builds:  []string{"default"},
   537  				},
   538  			},
   539  		},
   540  	}
   541  	ctx.Artifacts.Add(&artifact.Artifact{
   542  		Name:   "mybin",
   543  		Goos:   "linux",
   544  		Goarch: "amd64",
   545  		Type:   artifact.Binary,
   546  		Extra: map[string]interface{}{
   547  			artifact.ExtraID: "default",
   548  		},
   549  	})
   550  	testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   551  }
   552  
   553  func TestNoBuildsFound(t *testing.T) {
   554  	ctx := &context.Context{
   555  		Parallelism: runtime.NumCPU(),
   556  		Artifacts:   artifact.New(),
   557  		Config: config.Project{
   558  			NFPMs: []config.NFPM{
   559  				{
   560  					Formats: []string{"deb"},
   561  					Builds:  []string{"nope"},
   562  				},
   563  			},
   564  		},
   565  	}
   566  	ctx.Artifacts.Add(&artifact.Artifact{
   567  		Name:   "mybin",
   568  		Goos:   "linux",
   569  		Goarch: "amd64",
   570  		Type:   artifact.Binary,
   571  		Extra: map[string]interface{}{
   572  			artifact.ExtraID: "default",
   573  		},
   574  	})
   575  	require.EqualError(t, Pipe{}.Run(ctx), `no linux binaries found for builds [nope]`)
   576  }
   577  
   578  func TestCreateFileDoesntExist(t *testing.T) {
   579  	folder := t.TempDir()
   580  	dist := filepath.Join(folder, "dist")
   581  	require.NoError(t, os.Mkdir(dist, 0o755))
   582  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
   583  	ctx := context.New(config.Project{
   584  		Dist:        dist,
   585  		ProjectName: "asd",
   586  		NFPMs: []config.NFPM{
   587  			{
   588  				Formats: []string{"deb", "rpm"},
   589  				Builds:  []string{"default"},
   590  				NFPMOverridables: config.NFPMOverridables{
   591  					PackageName: "foo",
   592  					Contents: []*files.Content{
   593  						{
   594  							Source:      "testdata/testfile.txt",
   595  							Destination: "/var/lib/test/testfile.txt",
   596  						},
   597  					},
   598  				},
   599  			},
   600  		},
   601  	})
   602  	ctx.Version = "1.2.3"
   603  	ctx.Git = context.GitInfo{
   604  		CurrentTag: "v1.2.3",
   605  	}
   606  	ctx.Artifacts.Add(&artifact.Artifact{
   607  		Name:   "mybin",
   608  		Path:   filepath.Join(dist, "mybin", "mybin"),
   609  		Goos:   "linux",
   610  		Goarch: "amd64",
   611  		Type:   artifact.Binary,
   612  		Extra: map[string]interface{}{
   613  			artifact.ExtraID: "default",
   614  		},
   615  	})
   616  	require.Contains(t, Pipe{}.Run(ctx).Error(), `dist/mybin/mybin": file does not exist`)
   617  }
   618  
   619  func TestInvalidConfig(t *testing.T) {
   620  	folder := t.TempDir()
   621  	dist := filepath.Join(folder, "dist")
   622  	require.NoError(t, os.Mkdir(dist, 0o755))
   623  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
   624  	ctx := context.New(config.Project{
   625  		Dist: dist,
   626  		NFPMs: []config.NFPM{
   627  			{
   628  				Formats: []string{"deb"},
   629  				Builds:  []string{"default"},
   630  			},
   631  		},
   632  	})
   633  	ctx.Git.CurrentTag = "v1.2.3"
   634  	ctx.Version = "1.2.3"
   635  	ctx.Artifacts.Add(&artifact.Artifact{
   636  		Name:   "mybin",
   637  		Path:   filepath.Join(dist, "mybin", "mybin"),
   638  		Goos:   "linux",
   639  		Goarch: "amd64",
   640  		Type:   artifact.Binary,
   641  		Extra: map[string]interface{}{
   642  			artifact.ExtraID: "default",
   643  		},
   644  	})
   645  	require.Contains(t, Pipe{}.Run(ctx).Error(), `nfpm failed: package name must be provided`)
   646  }
   647  
   648  func TestDefault(t *testing.T) {
   649  	ctx := &context.Context{
   650  		Config: config.Project{
   651  			ProjectName: "foobar",
   652  			NFPMs: []config.NFPM{
   653  				{},
   654  			},
   655  		},
   656  	}
   657  	require.NoError(t, Pipe{}.Default(ctx))
   658  	require.Equal(t, "/usr/bin", ctx.Config.NFPMs[0].Bindir)
   659  	require.Empty(t, ctx.Config.NFPMs[0].Builds)
   660  	require.Equal(t, defaultNameTemplate, ctx.Config.NFPMs[0].FileNameTemplate)
   661  	require.Equal(t, ctx.Config.ProjectName, ctx.Config.NFPMs[0].PackageName)
   662  }
   663  
   664  func TestDefaultSet(t *testing.T) {
   665  	ctx := &context.Context{
   666  		Config: config.Project{
   667  			NFPMs: []config.NFPM{
   668  				{
   669  					Builds: []string{"foo"},
   670  					Bindir: "/bin",
   671  					NFPMOverridables: config.NFPMOverridables{
   672  						FileNameTemplate: "foo",
   673  					},
   674  				},
   675  			},
   676  		},
   677  	}
   678  	require.NoError(t, Pipe{}.Default(ctx))
   679  	require.Equal(t, "/bin", ctx.Config.NFPMs[0].Bindir)
   680  	require.Equal(t, "foo", ctx.Config.NFPMs[0].FileNameTemplate)
   681  	require.Equal(t, []string{"foo"}, ctx.Config.NFPMs[0].Builds)
   682  	require.Equal(t, config.NFPMRPMScripts{}, ctx.Config.NFPMs[0].RPM.Scripts)
   683  }
   684  
   685  func TestOverrides(t *testing.T) {
   686  	ctx := &context.Context{
   687  		Config: config.Project{
   688  			NFPMs: []config.NFPM{
   689  				{
   690  					Bindir: "/bin",
   691  					NFPMOverridables: config.NFPMOverridables{
   692  						FileNameTemplate: "foo",
   693  					},
   694  					Overrides: map[string]config.NFPMOverridables{
   695  						"deb": {
   696  							FileNameTemplate: "bar",
   697  						},
   698  					},
   699  				},
   700  			},
   701  		},
   702  	}
   703  	require.NoError(t, Pipe{}.Default(ctx))
   704  	merged, err := mergeOverrides(ctx.Config.NFPMs[0], "deb")
   705  	require.NoError(t, err)
   706  	require.Equal(t, "/bin", ctx.Config.NFPMs[0].Bindir)
   707  	require.Equal(t, "foo", ctx.Config.NFPMs[0].FileNameTemplate)
   708  	require.Equal(t, "bar", ctx.Config.NFPMs[0].Overrides["deb"].FileNameTemplate)
   709  	require.Equal(t, "bar", merged.FileNameTemplate)
   710  }
   711  
   712  func TestDebSpecificConfig(t *testing.T) {
   713  	setupContext := func(tb testing.TB) *context.Context {
   714  		tb.Helper()
   715  		folder := t.TempDir()
   716  		dist := filepath.Join(folder, "dist")
   717  		require.NoError(t, os.Mkdir(dist, 0o755))
   718  		require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
   719  		binPath := filepath.Join(dist, "mybin", "mybin")
   720  		f, err := os.Create(binPath)
   721  		require.NoError(t, err)
   722  		require.NoError(t, f.Close())
   723  		ctx := context.New(config.Project{
   724  			ProjectName: "mybin",
   725  			Dist:        dist,
   726  			NFPMs: []config.NFPM{
   727  				{
   728  					ID:         "someid",
   729  					Builds:     []string{"default"},
   730  					Formats:    []string{"deb"},
   731  					Maintainer: "foo",
   732  					NFPMOverridables: config.NFPMOverridables{
   733  						PackageName: "foo",
   734  						Contents: []*files.Content{
   735  							{
   736  								Source:      "testdata/testfile.txt",
   737  								Destination: "/usr/share/testfile.txt",
   738  							},
   739  						},
   740  						Deb: config.NFPMDeb{
   741  							Signature: config.NFPMDebSignature{
   742  								KeyFile: "./testdata/privkey.gpg",
   743  							},
   744  						},
   745  					},
   746  				},
   747  			},
   748  		})
   749  		ctx.Version = "1.0.0"
   750  		ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   751  		for _, goos := range []string{"linux", "darwin"} {
   752  			for _, goarch := range []string{"amd64", "386"} {
   753  				ctx.Artifacts.Add(&artifact.Artifact{
   754  					Name:   "mybin",
   755  					Path:   binPath,
   756  					Goarch: goarch,
   757  					Goos:   goos,
   758  					Type:   artifact.Binary,
   759  					Extra: map[string]interface{}{
   760  						artifact.ExtraID: "default",
   761  					},
   762  				})
   763  			}
   764  		}
   765  		return ctx
   766  	}
   767  
   768  	t.Run("no passphrase set", func(t *testing.T) {
   769  		require.Contains(
   770  			t,
   771  			Pipe{}.Run(setupContext(t)).Error(),
   772  			`key is encrypted but no passphrase was provided`,
   773  		)
   774  	})
   775  
   776  	t.Run("general passphrase set", func(t *testing.T) {
   777  		ctx := setupContext(t)
   778  		ctx.Env = map[string]string{
   779  			"NFPM_SOMEID_PASSPHRASE": "hunter2",
   780  		}
   781  		require.NoError(t, Pipe{}.Run(ctx))
   782  	})
   783  
   784  	t.Run("packager specific passphrase set", func(t *testing.T) {
   785  		ctx := setupContext(t)
   786  		ctx.Env = map[string]string{
   787  			"NFPM_SOMEID_DEB_PASSPHRASE": "hunter2",
   788  		}
   789  		require.NoError(t, Pipe{}.Run(ctx))
   790  	})
   791  
   792  	t.Run("lintian", func(t *testing.T) {
   793  		ctx := setupContext(t)
   794  		ctx.Env = map[string]string{
   795  			"NFPM_SOMEID_DEB_PASSPHRASE": "hunter2",
   796  		}
   797  		ctx.Config.NFPMs[0].NFPMOverridables.Deb.Lintian = []string{
   798  			"statically-linked-binary",
   799  			"changelog-file-missing-in-native-package",
   800  		}
   801  		require.NoError(t, Pipe{}.Run(ctx))
   802  
   803  		for _, goarch := range []string{"amd64", "386"} {
   804  			bts, err := os.ReadFile(filepath.Join(ctx.Config.Dist, "deb/foo_"+goarch+"/.lintian"))
   805  			require.NoError(t, err)
   806  			require.Equal(t, "foo: statically-linked-binary\nfoo: changelog-file-missing-in-native-package", string(bts))
   807  		}
   808  	})
   809  }
   810  
   811  func TestRPMSpecificConfig(t *testing.T) {
   812  	folder := t.TempDir()
   813  	dist := filepath.Join(folder, "dist")
   814  	require.NoError(t, os.Mkdir(dist, 0o755))
   815  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
   816  	binPath := filepath.Join(dist, "mybin", "mybin")
   817  	f, err := os.Create(binPath)
   818  	require.NoError(t, err)
   819  	require.NoError(t, f.Close())
   820  	ctx := context.New(config.Project{
   821  		ProjectName: "mybin",
   822  		Dist:        dist,
   823  		NFPMs: []config.NFPM{
   824  			{
   825  				ID:      "someid",
   826  				Builds:  []string{"default"},
   827  				Formats: []string{"rpm"},
   828  				NFPMOverridables: config.NFPMOverridables{
   829  					PackageName: "foo",
   830  					Contents: []*files.Content{
   831  						{
   832  							Source:      "testdata/testfile.txt",
   833  							Destination: "/usr/share/testfile.txt",
   834  						},
   835  					},
   836  					RPM: config.NFPMRPM{
   837  						Signature: config.NFPMRPMSignature{
   838  							KeyFile: "./testdata/privkey.gpg",
   839  						},
   840  					},
   841  				},
   842  			},
   843  		},
   844  	})
   845  	ctx.Version = "1.0.0"
   846  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   847  	for _, goos := range []string{"linux", "darwin"} {
   848  		for _, goarch := range []string{"amd64", "386"} {
   849  			ctx.Artifacts.Add(&artifact.Artifact{
   850  				Name:   "mybin",
   851  				Path:   binPath,
   852  				Goarch: goarch,
   853  				Goos:   goos,
   854  				Type:   artifact.Binary,
   855  				Extra: map[string]interface{}{
   856  					artifact.ExtraID: "default",
   857  				},
   858  			})
   859  		}
   860  	}
   861  
   862  	t.Run("no passphrase set", func(t *testing.T) {
   863  		require.Contains(
   864  			t,
   865  			Pipe{}.Run(ctx).Error(),
   866  			`key is encrypted but no passphrase was provided`,
   867  		)
   868  	})
   869  
   870  	t.Run("general passphrase set", func(t *testing.T) {
   871  		ctx.Env = map[string]string{
   872  			"NFPM_SOMEID_PASSPHRASE": "hunter2",
   873  		}
   874  		require.NoError(t, Pipe{}.Run(ctx))
   875  	})
   876  
   877  	t.Run("packager specific passphrase set", func(t *testing.T) {
   878  		ctx.Env = map[string]string{
   879  			"NFPM_SOMEID_RPM_PASSPHRASE": "hunter2",
   880  		}
   881  		require.NoError(t, Pipe{}.Run(ctx))
   882  	})
   883  }
   884  
   885  func TestRPMSpecificScriptsConfig(t *testing.T) {
   886  	folder := t.TempDir()
   887  	dist := filepath.Join(folder, "dist")
   888  	require.NoError(t, os.Mkdir(dist, 0o755))
   889  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
   890  	binPath := filepath.Join(dist, "mybin", "mybin")
   891  	f, err := os.Create(binPath)
   892  	require.NoError(t, err)
   893  	require.NoError(t, f.Close())
   894  	ctx := context.New(config.Project{
   895  		ProjectName: "mybin",
   896  		Dist:        dist,
   897  		NFPMs: []config.NFPM{
   898  			{
   899  				ID:      "someid",
   900  				Builds:  []string{"default"},
   901  				Formats: []string{"rpm"},
   902  				NFPMOverridables: config.NFPMOverridables{
   903  					PackageName: "foo",
   904  					RPM: config.NFPMRPM{
   905  						Scripts: config.NFPMRPMScripts{
   906  							PreTrans:  "/does/not/exist_pretrans.sh",
   907  							PostTrans: "/does/not/exist_posttrans.sh",
   908  						},
   909  					},
   910  				},
   911  			},
   912  		},
   913  	})
   914  	ctx.Version = "1.0.0"
   915  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   916  	for _, goos := range []string{"linux", "darwin"} {
   917  		for _, goarch := range []string{"amd64", "386"} {
   918  			ctx.Artifacts.Add(&artifact.Artifact{
   919  				Name:   "mybin",
   920  				Path:   binPath,
   921  				Goarch: goarch,
   922  				Goos:   goos,
   923  				Type:   artifact.Binary,
   924  				Extra: map[string]interface{}{
   925  					artifact.ExtraID: "default",
   926  				},
   927  			})
   928  		}
   929  	}
   930  
   931  	t.Run("PreTrans script file does not exist", func(t *testing.T) {
   932  		require.Contains(
   933  			t,
   934  			Pipe{}.Run(ctx).Error(),
   935  			`nfpm failed: open /does/not/exist_pretrans.sh: no such file or directory`,
   936  		)
   937  	})
   938  
   939  	t.Run("PostTrans script file does not exist", func(t *testing.T) {
   940  		ctx.Config.NFPMs[0].RPM.Scripts.PreTrans = "testdata/testfile.txt"
   941  
   942  		require.Contains(
   943  			t,
   944  			Pipe{}.Run(ctx).Error(),
   945  			`nfpm failed: open /does/not/exist_posttrans.sh: no such file or directory`,
   946  		)
   947  	})
   948  
   949  	t.Run("pretrans and posttrans scriptlets set", func(t *testing.T) {
   950  		ctx.Config.NFPMs[0].RPM.Scripts.PreTrans = "testdata/testfile.txt"
   951  		ctx.Config.NFPMs[0].RPM.Scripts.PostTrans = "testdata/testfile.txt"
   952  
   953  		require.NoError(t, Pipe{}.Run(ctx))
   954  	})
   955  }
   956  
   957  func TestAPKSpecificConfig(t *testing.T) {
   958  	folder := t.TempDir()
   959  	dist := filepath.Join(folder, "dist")
   960  	require.NoError(t, os.Mkdir(dist, 0o755))
   961  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
   962  	binPath := filepath.Join(dist, "mybin", "mybin")
   963  	f, err := os.Create(binPath)
   964  	require.NoError(t, err)
   965  	require.NoError(t, f.Close())
   966  	ctx := context.New(config.Project{
   967  		ProjectName: "mybin",
   968  		Dist:        dist,
   969  		NFPMs: []config.NFPM{
   970  			{
   971  				ID:         "someid",
   972  				Maintainer: "me@me",
   973  				Builds:     []string{"default"},
   974  				Formats:    []string{"apk"},
   975  				NFPMOverridables: config.NFPMOverridables{
   976  					PackageName: "foo",
   977  					Contents: []*files.Content{
   978  						{
   979  							Source:      "testdata/testfile.txt",
   980  							Destination: "/usr/share/testfile.txt",
   981  						},
   982  					},
   983  					APK: config.NFPMAPK{
   984  						Signature: config.NFPMAPKSignature{
   985  							KeyFile: "./testdata/rsa.priv",
   986  						},
   987  					},
   988  				},
   989  			},
   990  		},
   991  	})
   992  	ctx.Version = "1.0.0"
   993  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   994  	for _, goos := range []string{"linux", "darwin"} {
   995  		for _, goarch := range []string{"amd64", "386"} {
   996  			ctx.Artifacts.Add(&artifact.Artifact{
   997  				Name:   "mybin",
   998  				Path:   binPath,
   999  				Goarch: goarch,
  1000  				Goos:   goos,
  1001  				Type:   artifact.Binary,
  1002  				Extra: map[string]interface{}{
  1003  					artifact.ExtraID: "default",
  1004  				},
  1005  			})
  1006  		}
  1007  	}
  1008  
  1009  	t.Run("no passphrase set", func(t *testing.T) {
  1010  		require.Contains(
  1011  			t,
  1012  			Pipe{}.Run(ctx).Error(),
  1013  			`key is encrypted but no passphrase was provided`,
  1014  		)
  1015  	})
  1016  
  1017  	t.Run("general passphrase set", func(t *testing.T) {
  1018  		ctx.Env = map[string]string{
  1019  			"NFPM_SOMEID_PASSPHRASE": "hunter2",
  1020  		}
  1021  		require.NoError(t, Pipe{}.Run(ctx))
  1022  	})
  1023  
  1024  	t.Run("packager specific passphrase set", func(t *testing.T) {
  1025  		ctx.Env = map[string]string{
  1026  			"NFPM_SOMEID_APK_PASSPHRASE": "hunter2",
  1027  		}
  1028  		require.NoError(t, Pipe{}.Run(ctx))
  1029  	})
  1030  }
  1031  
  1032  func TestAPKSpecificScriptsConfig(t *testing.T) {
  1033  	folder := t.TempDir()
  1034  	dist := filepath.Join(folder, "dist")
  1035  	require.NoError(t, os.Mkdir(dist, 0o755))
  1036  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
  1037  	binPath := filepath.Join(dist, "mybin", "mybin")
  1038  	f, err := os.Create(binPath)
  1039  	require.NoError(t, err)
  1040  	require.NoError(t, f.Close())
  1041  	scripts := config.NFPMAPKScripts{
  1042  		PreUpgrade:  "/does/not/exist_preupgrade.sh",
  1043  		PostUpgrade: "/does/not/exist_postupgrade.sh",
  1044  	}
  1045  	ctx := context.New(config.Project{
  1046  		ProjectName: "mybin",
  1047  		Dist:        dist,
  1048  		NFPMs: []config.NFPM{
  1049  			{
  1050  				ID:         "someid",
  1051  				Maintainer: "me@me",
  1052  				Builds:     []string{"default"},
  1053  				Formats:    []string{"apk"},
  1054  				NFPMOverridables: config.NFPMOverridables{
  1055  					PackageName: "foo",
  1056  					Contents: []*files.Content{
  1057  						{
  1058  							Source:      "testdata/testfile.txt",
  1059  							Destination: "/usr/share/testfile.txt",
  1060  						},
  1061  					},
  1062  					APK: config.NFPMAPK{
  1063  						Scripts: scripts,
  1064  					},
  1065  				},
  1066  			},
  1067  		},
  1068  	})
  1069  	ctx.Version = "1.0.0"
  1070  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
  1071  	for _, goos := range []string{"linux", "darwin"} {
  1072  		for _, goarch := range []string{"amd64", "386"} {
  1073  			ctx.Artifacts.Add(&artifact.Artifact{
  1074  				Name:   "mybin",
  1075  				Path:   binPath,
  1076  				Goarch: goarch,
  1077  				Goos:   goos,
  1078  				Type:   artifact.Binary,
  1079  				Extra: map[string]interface{}{
  1080  					artifact.ExtraID: "default",
  1081  				},
  1082  			})
  1083  		}
  1084  	}
  1085  
  1086  	t.Run("PreUpgrade script file does not exist", func(t *testing.T) {
  1087  		ctx.Config.NFPMs[0].APK.Scripts = scripts
  1088  		ctx.Config.NFPMs[0].APK.Scripts.PostUpgrade = "testdata/testfile.txt"
  1089  
  1090  		require.Contains(
  1091  			t,
  1092  			Pipe{}.Run(ctx).Error(),
  1093  			`nfpm failed: stat /does/not/exist_preupgrade.sh: no such file or directory`,
  1094  		)
  1095  	})
  1096  
  1097  	t.Run("PostUpgrade script file does not exist", func(t *testing.T) {
  1098  		ctx.Config.NFPMs[0].APK.Scripts = scripts
  1099  		ctx.Config.NFPMs[0].APK.Scripts.PreUpgrade = "testdata/testfile.txt"
  1100  
  1101  		require.Contains(
  1102  			t,
  1103  			Pipe{}.Run(ctx).Error(),
  1104  			`nfpm failed: stat /does/not/exist_postupgrade.sh: no such file or directory`,
  1105  		)
  1106  	})
  1107  
  1108  	t.Run("preupgrade and postupgrade scriptlets set", func(t *testing.T) {
  1109  		ctx.Config.NFPMs[0].APK.Scripts.PreUpgrade = "testdata/testfile.txt"
  1110  		ctx.Config.NFPMs[0].APK.Scripts.PostUpgrade = "testdata/testfile.txt"
  1111  
  1112  		require.NoError(t, Pipe{}.Run(ctx))
  1113  	})
  1114  }
  1115  
  1116  func TestSeveralNFPMsWithTheSameID(t *testing.T) {
  1117  	ctx := &context.Context{
  1118  		Config: config.Project{
  1119  			NFPMs: []config.NFPM{
  1120  				{
  1121  					ID: "a",
  1122  				},
  1123  				{
  1124  					ID: "a",
  1125  				},
  1126  			},
  1127  		},
  1128  	}
  1129  	require.EqualError(t, Pipe{}.Default(ctx), "found 2 nfpms with the ID 'a', please fix your config")
  1130  }
  1131  
  1132  func TestMeta(t *testing.T) {
  1133  	folder := t.TempDir()
  1134  	dist := filepath.Join(folder, "dist")
  1135  	require.NoError(t, os.Mkdir(dist, 0o755))
  1136  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
  1137  	binPath := filepath.Join(dist, "mybin", "mybin")
  1138  	f, err := os.Create(binPath)
  1139  	require.NoError(t, err)
  1140  	require.NoError(t, f.Close())
  1141  	ctx := context.New(config.Project{
  1142  		ProjectName: "mybin",
  1143  		Dist:        dist,
  1144  		NFPMs: []config.NFPM{
  1145  			{
  1146  				ID:          "someid",
  1147  				Bindir:      "/usr/bin",
  1148  				Builds:      []string{"default"},
  1149  				Formats:     []string{"deb", "rpm"},
  1150  				Section:     "somesection",
  1151  				Priority:    "standard",
  1152  				Description: "Some description",
  1153  				License:     "MIT",
  1154  				Maintainer:  "me@me",
  1155  				Vendor:      "asdf",
  1156  				Homepage:    "https://goreleaser.github.io",
  1157  				Meta:        true,
  1158  				NFPMOverridables: config.NFPMOverridables{
  1159  					FileNameTemplate: defaultNameTemplate + "-{{ .Release }}-{{ .Epoch }}",
  1160  					PackageName:      "foo",
  1161  					Dependencies:     []string{"make"},
  1162  					Recommends:       []string{"svn"},
  1163  					Suggests:         []string{"bzr"},
  1164  					Replaces:         []string{"fish"},
  1165  					Conflicts:        []string{"git"},
  1166  					Release:          "10",
  1167  					Epoch:            "20",
  1168  					Contents: []*files.Content{
  1169  						{
  1170  							Source:      "testdata/testfile.txt",
  1171  							Destination: "/usr/share/testfile.txt",
  1172  						},
  1173  						{
  1174  							Source:      "./testdata/testfile.txt",
  1175  							Destination: "/etc/nope.conf",
  1176  							Type:        "config",
  1177  						},
  1178  						{
  1179  							Source:      "./testdata/testfile.txt",
  1180  							Destination: "/etc/nope-rpm.conf",
  1181  							Type:        "config",
  1182  							Packager:    "rpm",
  1183  						},
  1184  						{
  1185  							Destination: "/var/log/foobar",
  1186  							Type:        "dir",
  1187  						},
  1188  					},
  1189  					Replacements: map[string]string{
  1190  						"linux": "Tux",
  1191  					},
  1192  				},
  1193  			},
  1194  		},
  1195  	})
  1196  	ctx.Version = "1.0.0"
  1197  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
  1198  	for _, goos := range []string{"linux", "darwin"} {
  1199  		for _, goarch := range []string{"amd64", "386"} {
  1200  			ctx.Artifacts.Add(&artifact.Artifact{
  1201  				Name:   "mybin",
  1202  				Path:   binPath,
  1203  				Goarch: goarch,
  1204  				Goos:   goos,
  1205  				Type:   artifact.Binary,
  1206  				Extra: map[string]interface{}{
  1207  					artifact.ExtraID: "default",
  1208  				},
  1209  			})
  1210  		}
  1211  	}
  1212  	require.NoError(t, Pipe{}.Run(ctx))
  1213  	packages := ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
  1214  	require.Len(t, packages, 4)
  1215  	for _, pkg := range packages {
  1216  		format := pkg.Format()
  1217  		require.NotEmpty(t, format)
  1218  		require.Equal(t, pkg.Name, "foo_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format)
  1219  		require.Equal(t, pkg.ID(), "someid")
  1220  		require.ElementsMatch(t, []string{
  1221  			"/var/log/foobar",
  1222  			"/usr/share/testfile.txt",
  1223  			"/etc/nope.conf",
  1224  			"/etc/nope-rpm.conf",
  1225  		}, destinations(artifact.ExtraOr(*pkg, extraFiles, files.Contents{})))
  1226  	}
  1227  
  1228  	require.Len(t, ctx.Config.NFPMs[0].Contents, 4, "should not modify the config file list")
  1229  }
  1230  
  1231  func TestSkipSign(t *testing.T) {
  1232  	folder, err := os.MkdirTemp("", "archivetest")
  1233  	require.NoError(t, err)
  1234  	dist := filepath.Join(folder, "dist")
  1235  	require.NoError(t, os.Mkdir(dist, 0o755))
  1236  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
  1237  	binPath := filepath.Join(dist, "mybin", "mybin")
  1238  	_, err = os.Create(binPath)
  1239  	require.NoError(t, err)
  1240  	ctx := context.New(config.Project{
  1241  		ProjectName: "mybin",
  1242  		Dist:        dist,
  1243  		NFPMs: []config.NFPM{
  1244  			{
  1245  				ID:      "someid",
  1246  				Builds:  []string{"default"},
  1247  				Formats: []string{"deb", "rpm", "apk"},
  1248  				NFPMOverridables: config.NFPMOverridables{
  1249  					PackageName:      "foo",
  1250  					FileNameTemplate: defaultNameTemplate,
  1251  					Contents: []*files.Content{
  1252  						{
  1253  							Source:      "testdata/testfile.txt",
  1254  							Destination: "/usr/share/testfile.txt",
  1255  						},
  1256  					},
  1257  					Deb: config.NFPMDeb{
  1258  						Signature: config.NFPMDebSignature{
  1259  							KeyFile: "/does/not/exist.gpg",
  1260  						},
  1261  					},
  1262  					RPM: config.NFPMRPM{
  1263  						Signature: config.NFPMRPMSignature{
  1264  							KeyFile: "/does/not/exist.gpg",
  1265  						},
  1266  					},
  1267  					APK: config.NFPMAPK{
  1268  						Signature: config.NFPMAPKSignature{
  1269  							KeyFile: "/does/not/exist.gpg",
  1270  						},
  1271  					},
  1272  				},
  1273  			},
  1274  		},
  1275  	})
  1276  	ctx.Version = "1.0.0"
  1277  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
  1278  	for _, goos := range []string{"linux", "darwin"} {
  1279  		for _, goarch := range []string{"amd64", "386"} {
  1280  			ctx.Artifacts.Add(&artifact.Artifact{
  1281  				Name:   "mybin",
  1282  				Path:   binPath,
  1283  				Goarch: goarch,
  1284  				Goos:   goos,
  1285  				Type:   artifact.Binary,
  1286  				Extra: map[string]interface{}{
  1287  					artifact.ExtraID: "default",
  1288  				},
  1289  			})
  1290  		}
  1291  	}
  1292  
  1293  	t.Run("skip sign not set", func(t *testing.T) {
  1294  		contains := "open /does/not/exist.gpg: no such file or directory"
  1295  		if runtime.GOOS == "windows" {
  1296  			contains = "open /does/not/exist.gpg: The system cannot find the path specified."
  1297  		}
  1298  		require.Contains(
  1299  			t,
  1300  			Pipe{}.Run(ctx).Error(),
  1301  			contains,
  1302  		)
  1303  	})
  1304  
  1305  	t.Run("skip sign set", func(t *testing.T) {
  1306  		ctx.SkipSign = true
  1307  		require.NoError(t, Pipe{}.Run(ctx))
  1308  	})
  1309  }
  1310  
  1311  func TestBinDirTemplating(t *testing.T) {
  1312  	folder := t.TempDir()
  1313  	dist := filepath.Join(folder, "dist")
  1314  	require.NoError(t, os.Mkdir(dist, 0o755))
  1315  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
  1316  	binPath := filepath.Join(dist, "mybin", "mybin")
  1317  	f, err := os.Create(binPath)
  1318  	require.NoError(t, err)
  1319  	require.NoError(t, f.Close())
  1320  	ctx := context.New(config.Project{
  1321  		ProjectName: "mybin",
  1322  		Dist:        dist,
  1323  		Env: []string{
  1324  			"PRO=pro",
  1325  			"DESC=templates",
  1326  			"MAINTAINER=me@me",
  1327  		},
  1328  		NFPMs: []config.NFPM{
  1329  			{
  1330  				ID: "someid",
  1331  				// Bindir should pass through the template engine
  1332  				Bindir:      "/usr/lib/{{ .Env.PRO }}/nagios/plugins",
  1333  				Builds:      []string{"default"},
  1334  				Formats:     []string{"rpm"},
  1335  				Section:     "somesection",
  1336  				Priority:    "standard",
  1337  				Description: "Some description with {{ .Env.DESC }}",
  1338  				License:     "MIT",
  1339  				Maintainer:  "{{ .Env.MAINTAINER }}",
  1340  				Vendor:      "asdf",
  1341  				Homepage:    "https://goreleaser.com/{{ .Env.PRO }}",
  1342  				NFPMOverridables: config.NFPMOverridables{
  1343  					PackageName: "foo",
  1344  				},
  1345  			},
  1346  		},
  1347  	})
  1348  	ctx.Version = "1.0.0"
  1349  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
  1350  	for _, goos := range []string{"linux"} {
  1351  		for _, goarch := range []string{"amd64", "386"} {
  1352  			ctx.Artifacts.Add(&artifact.Artifact{
  1353  				Name:   "subdir/mybin",
  1354  				Path:   binPath,
  1355  				Goarch: goarch,
  1356  				Goos:   goos,
  1357  				Type:   artifact.Binary,
  1358  				Extra: map[string]interface{}{
  1359  					artifact.ExtraID: "default",
  1360  				},
  1361  			})
  1362  		}
  1363  	}
  1364  	require.NoError(t, Pipe{}.Run(ctx))
  1365  	packages := ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
  1366  
  1367  	for _, pkg := range packages {
  1368  		format := pkg.Format()
  1369  		require.NotEmpty(t, format)
  1370  		// the final binary should contain the evaluated bindir (after template eval)
  1371  		require.ElementsMatch(t, []string{
  1372  			"/usr/lib/pro/nagios/plugins/subdir/mybin",
  1373  		}, destinations(artifact.ExtraOr(*pkg, extraFiles, files.Contents{})))
  1374  	}
  1375  }
  1376  
  1377  func TestSkip(t *testing.T) {
  1378  	t.Run("skip", func(t *testing.T) {
  1379  		require.True(t, Pipe{}.Skip(context.New(config.Project{})))
  1380  	})
  1381  
  1382  	t.Run("dont skip", func(t *testing.T) {
  1383  		ctx := context.New(config.Project{
  1384  			NFPMs: []config.NFPM{
  1385  				{},
  1386  			},
  1387  		})
  1388  		require.False(t, Pipe{}.Skip(ctx))
  1389  	})
  1390  }
  1391  
  1392  func sources(contents files.Contents) []string {
  1393  	result := make([]string, 0, len(contents))
  1394  	for _, f := range contents {
  1395  		if f.Source == "" {
  1396  			continue
  1397  		}
  1398  		result = append(result, f.Source)
  1399  	}
  1400  	return result
  1401  }