github.com/amane3/goreleaser@v0.182.0/internal/pipe/nfpm/nfpm_test.go (about)

     1  package nfpm
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"testing"
     9  
    10  	"github.com/amane3/goreleaser/internal/artifact"
    11  	"github.com/amane3/goreleaser/internal/testlib"
    12  	"github.com/amane3/goreleaser/pkg/config"
    13  	"github.com/amane3/goreleaser/pkg/context"
    14  	"github.com/goreleaser/nfpm/v2/files"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestDescription(t *testing.T) {
    19  	require.NotEmpty(t, Pipe{}.String())
    20  }
    21  
    22  func TestRunPipeNoFormats(t *testing.T) {
    23  	var ctx = &context.Context{
    24  		Version: "1.0.0",
    25  		Git: context.GitInfo{
    26  			CurrentTag: "v1.0.0",
    27  		},
    28  		Config: config.Project{
    29  			NFPMs: []config.NFPM{
    30  				{},
    31  			},
    32  		},
    33  		Parallelism: runtime.NumCPU(),
    34  	}
    35  	require.NoError(t, Pipe{}.Default(ctx))
    36  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    37  }
    38  
    39  func TestRunPipeInvalidFormat(t *testing.T) {
    40  	var ctx = context.New(config.Project{
    41  		ProjectName: "nope",
    42  		NFPMs: []config.NFPM{
    43  			{
    44  				Bindir:  "/usr/bin",
    45  				Formats: []string{"nope"},
    46  				Builds:  []string{"foo"},
    47  				NFPMOverridables: config.NFPMOverridables{
    48  					PackageName:      "foo",
    49  					FileNameTemplate: defaultNameTemplate,
    50  				},
    51  			},
    52  		},
    53  	})
    54  	ctx.Version = "1.2.3"
    55  	ctx.Git = context.GitInfo{
    56  		CurrentTag: "v1.2.3",
    57  	}
    58  	for _, goos := range []string{"linux", "darwin"} {
    59  		for _, goarch := range []string{"amd64", "386"} {
    60  			ctx.Artifacts.Add(&artifact.Artifact{
    61  				Name:   "mybin",
    62  				Path:   "testdata/testfile.txt",
    63  				Goarch: goarch,
    64  				Goos:   goos,
    65  				Type:   artifact.Binary,
    66  				Extra: map[string]interface{}{
    67  					"ID": "foo",
    68  				},
    69  			})
    70  		}
    71  	}
    72  	require.Contains(t, Pipe{}.Run(ctx).Error(), `no packager registered for the format nope`)
    73  }
    74  
    75  func TestRunPipe(t *testing.T) {
    76  	var folder = t.TempDir()
    77  	var dist = filepath.Join(folder, "dist")
    78  	require.NoError(t, os.Mkdir(dist, 0755))
    79  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
    80  	var binPath = filepath.Join(dist, "mybin", "mybin")
    81  	_, err := os.Create(binPath)
    82  	require.NoError(t, err)
    83  	var ctx = context.New(config.Project{
    84  		ProjectName: "mybin",
    85  		Dist:        dist,
    86  		NFPMs: []config.NFPM{
    87  			{
    88  				ID:          "someid",
    89  				Bindir:      "/usr/bin",
    90  				Builds:      []string{"default"},
    91  				Formats:     []string{"deb", "rpm", "apk"},
    92  				Description: "Some description",
    93  				License:     "MIT",
    94  				Maintainer:  "me@me",
    95  				Vendor:      "asdf",
    96  				Homepage:    "https://goreleaser.github.io",
    97  				NFPMOverridables: config.NFPMOverridables{
    98  					FileNameTemplate: defaultNameTemplate + "-{{ .Release }}-{{ .Epoch }}",
    99  					PackageName:      "foo",
   100  					Dependencies:     []string{"make"},
   101  					Recommends:       []string{"svn"},
   102  					Suggests:         []string{"bzr"},
   103  					Replaces:         []string{"fish"},
   104  					Conflicts:        []string{"git"},
   105  					EmptyFolders:     []string{"/var/log/foobar"},
   106  					Release:          "10",
   107  					Epoch:            "20",
   108  					Contents: []*files.Content{
   109  						{
   110  							Source:      "./testdata/testfile.txt",
   111  							Destination: "/usr/share/testfile.txt",
   112  						},
   113  						{
   114  							Source:      "./testdata/testfile.txt",
   115  							Destination: "/etc/nope.conf",
   116  							Type:        "config",
   117  						},
   118  						{
   119  							Source:      "./testdata/testfile.txt",
   120  							Destination: "/etc/nope-rpm.conf",
   121  							Type:        "config",
   122  							Packager:    "rpm",
   123  						},
   124  						{
   125  							Source:      "/etc/nope.conf",
   126  							Destination: "/etc/nope2.conf",
   127  							Type:        "symlink",
   128  						},
   129  					},
   130  					Replacements: map[string]string{
   131  						"linux": "Tux",
   132  					},
   133  				},
   134  			},
   135  		},
   136  	})
   137  	ctx.Version = "1.0.0"
   138  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   139  	for _, goos := range []string{"linux", "darwin"} {
   140  		for _, goarch := range []string{"amd64", "386"} {
   141  			ctx.Artifacts.Add(&artifact.Artifact{
   142  				Name:   "mybin",
   143  				Path:   binPath,
   144  				Goarch: goarch,
   145  				Goos:   goos,
   146  				Type:   artifact.Binary,
   147  				Extra: map[string]interface{}{
   148  					"ID": "default",
   149  				},
   150  			})
   151  		}
   152  	}
   153  	require.NoError(t, Pipe{}.Run(ctx))
   154  	var packages = ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
   155  	require.Len(t, packages, 6)
   156  	for _, pkg := range packages {
   157  		var format = pkg.ExtraOr("Format", "").(string)
   158  		require.NotEmpty(t, format)
   159  		require.Equal(t, pkg.Name, "mybin_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format)
   160  		require.Equal(t, pkg.ExtraOr("ID", ""), "someid")
   161  	}
   162  	require.Len(t, ctx.Config.NFPMs[0].Contents, 4, "should not modify the config file list")
   163  
   164  }
   165  
   166  func TestInvalidNameTemplate(t *testing.T) {
   167  	var ctx = &context.Context{
   168  		Parallelism: runtime.NumCPU(),
   169  		Artifacts:   artifact.New(),
   170  		Config: config.Project{
   171  			NFPMs: []config.NFPM{
   172  				{
   173  					NFPMOverridables: config.NFPMOverridables{
   174  						PackageName:      "foo",
   175  						FileNameTemplate: "{{.Foo}",
   176  					},
   177  					Formats: []string{"deb"},
   178  					Builds:  []string{"default"},
   179  				},
   180  			},
   181  		},
   182  	}
   183  	ctx.Artifacts.Add(&artifact.Artifact{
   184  		Name:   "mybin",
   185  		Goos:   "linux",
   186  		Goarch: "amd64",
   187  		Type:   artifact.Binary,
   188  		Extra: map[string]interface{}{
   189  			"ID": "default",
   190  		},
   191  	})
   192  	require.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1: unexpected "}" in operand`)
   193  }
   194  
   195  func TestNoBuildsFound(t *testing.T) {
   196  	var ctx = &context.Context{
   197  		Parallelism: runtime.NumCPU(),
   198  		Artifacts:   artifact.New(),
   199  		Config: config.Project{
   200  			NFPMs: []config.NFPM{
   201  				{
   202  					Formats: []string{"deb"},
   203  					Builds:  []string{"nope"},
   204  				},
   205  			},
   206  		},
   207  	}
   208  	ctx.Artifacts.Add(&artifact.Artifact{
   209  		Name:   "mybin",
   210  		Goos:   "linux",
   211  		Goarch: "amd64",
   212  		Type:   artifact.Binary,
   213  		Extra: map[string]interface{}{
   214  			"ID": "default",
   215  		},
   216  	})
   217  	require.EqualError(t, Pipe{}.Run(ctx), `no linux binaries found for builds [nope]`)
   218  }
   219  
   220  func TestCreateFileDoesntExist(t *testing.T) {
   221  	var folder = t.TempDir()
   222  	var dist = filepath.Join(folder, "dist")
   223  	require.NoError(t, os.Mkdir(dist, 0755))
   224  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
   225  	var ctx = context.New(config.Project{
   226  		Dist:        dist,
   227  		ProjectName: "asd",
   228  		NFPMs: []config.NFPM{
   229  			{
   230  				Formats: []string{"deb", "rpm"},
   231  				Builds:  []string{"default"},
   232  				NFPMOverridables: config.NFPMOverridables{
   233  					PackageName: "foo",
   234  					Contents: []*files.Content{
   235  						{
   236  							Source:      "testdata/testfile.txt",
   237  							Destination: "/var/lib/test/testfile.txt",
   238  						},
   239  					},
   240  				},
   241  			},
   242  		},
   243  	})
   244  	ctx.Version = "1.2.3"
   245  	ctx.Git = context.GitInfo{
   246  		CurrentTag: "v1.2.3",
   247  	}
   248  	ctx.Artifacts.Add(&artifact.Artifact{
   249  		Name:   "mybin",
   250  		Path:   filepath.Join(dist, "mybin", "mybin"),
   251  		Goos:   "linux",
   252  		Goarch: "amd64",
   253  		Type:   artifact.Binary,
   254  		Extra: map[string]interface{}{
   255  			"ID": "default",
   256  		},
   257  	})
   258  	require.Contains(t, Pipe{}.Run(ctx).Error(), `dist/mybin/mybin": file does not exist`)
   259  }
   260  
   261  func TestInvalidConfig(t *testing.T) {
   262  	var folder = t.TempDir()
   263  	var dist = filepath.Join(folder, "dist")
   264  	require.NoError(t, os.Mkdir(dist, 0755))
   265  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
   266  	var ctx = context.New(config.Project{
   267  		Dist: dist,
   268  		NFPMs: []config.NFPM{
   269  			{
   270  				Formats: []string{"deb"},
   271  				Builds:  []string{"default"},
   272  			},
   273  		},
   274  	})
   275  	ctx.Git.CurrentTag = "v1.2.3"
   276  	ctx.Version = "v1.2.3"
   277  	ctx.Artifacts.Add(&artifact.Artifact{
   278  		Name:   "mybin",
   279  		Path:   filepath.Join(dist, "mybin", "mybin"),
   280  		Goos:   "linux",
   281  		Goarch: "amd64",
   282  		Type:   artifact.Binary,
   283  		Extra: map[string]interface{}{
   284  			"ID": "default",
   285  		},
   286  	})
   287  	require.Contains(t, Pipe{}.Run(ctx).Error(), `invalid nfpm config: package name must be provided`)
   288  }
   289  
   290  func TestDefault(t *testing.T) {
   291  	var ctx = &context.Context{
   292  		Config: config.Project{
   293  			ProjectName: "foobar",
   294  			NFPMs: []config.NFPM{
   295  				{},
   296  			},
   297  			Builds: []config.Build{
   298  				{ID: "foo"},
   299  				{ID: "bar"},
   300  			},
   301  		},
   302  	}
   303  	require.NoError(t, Pipe{}.Default(ctx))
   304  	require.Equal(t, "/usr/local/bin", ctx.Config.NFPMs[0].Bindir)
   305  	require.Equal(t, []string{"foo", "bar"}, ctx.Config.NFPMs[0].Builds)
   306  	require.Equal(t, defaultNameTemplate, ctx.Config.NFPMs[0].FileNameTemplate)
   307  	require.Equal(t, ctx.Config.ProjectName, ctx.Config.NFPMs[0].PackageName)
   308  }
   309  
   310  func TestDefaultDeprecatedOptions(t *testing.T) {
   311  	var ctx = &context.Context{
   312  		Config: config.Project{
   313  			ProjectName: "foobar",
   314  			NFPMs: []config.NFPM{
   315  				{
   316  					NFPMOverridables: config.NFPMOverridables{
   317  						Files: map[string]string{
   318  							"testdata/testfile.txt": "/bin/foo",
   319  						},
   320  						ConfigFiles: map[string]string{
   321  							"testdata/testfile.txt": "/etc/foo.conf",
   322  						},
   323  						Symlinks: map[string]string{
   324  							"/etc/foo.conf": "/etc/foov2.conf",
   325  						},
   326  						RPM: config.NFPMRPM{
   327  							GhostFiles: []string{"/etc/ghost.conf"},
   328  							ConfigNoReplaceFiles: map[string]string{
   329  								"testdata/testfile.txt": "/etc/foo_keep.conf",
   330  							},
   331  						},
   332  					},
   333  				},
   334  			},
   335  			Builds: []config.Build{
   336  				{ID: "foo"},
   337  				{ID: "bar"},
   338  			},
   339  		},
   340  	}
   341  	require.NoError(t, Pipe{}.Default(ctx))
   342  	require.Equal(t, "/usr/local/bin", ctx.Config.NFPMs[0].Bindir)
   343  	require.Equal(t, []string{"foo", "bar"}, ctx.Config.NFPMs[0].Builds)
   344  	require.ElementsMatch(t, []*files.Content{
   345  		{Source: "testdata/testfile.txt", Destination: "/bin/foo"},
   346  		{Source: "testdata/testfile.txt", Destination: "/etc/foo.conf", Type: "config"},
   347  		{Source: "/etc/foo.conf", Destination: "/etc/foov2.conf", Type: "symlink"},
   348  		{Destination: "/etc/ghost.conf", Type: "ghost", Packager: "rpm"},
   349  		{Source: "testdata/testfile.txt", Destination: "/etc/foo_keep.conf", Type: "config|noreplace", Packager: "rpm"},
   350  	}, ctx.Config.NFPMs[0].Contents)
   351  	require.Equal(t, defaultNameTemplate, ctx.Config.NFPMs[0].FileNameTemplate)
   352  	require.Equal(t, ctx.Config.ProjectName, ctx.Config.NFPMs[0].PackageName)
   353  }
   354  
   355  func TestDefaultSet(t *testing.T) {
   356  	var ctx = &context.Context{
   357  		Config: config.Project{
   358  			Builds: []config.Build{
   359  				{ID: "foo"},
   360  				{ID: "bar"},
   361  			},
   362  			NFPMs: []config.NFPM{
   363  				{
   364  					Builds: []string{"foo"},
   365  					Bindir: "/bin",
   366  					NFPMOverridables: config.NFPMOverridables{
   367  						FileNameTemplate: "foo",
   368  					},
   369  				},
   370  			},
   371  		},
   372  	}
   373  	require.NoError(t, Pipe{}.Default(ctx))
   374  	require.Equal(t, "/bin", ctx.Config.NFPMs[0].Bindir)
   375  	require.Equal(t, "foo", ctx.Config.NFPMs[0].FileNameTemplate)
   376  	require.Equal(t, []string{"foo"}, ctx.Config.NFPMs[0].Builds)
   377  }
   378  
   379  func TestOverrides(t *testing.T) {
   380  	var ctx = &context.Context{
   381  		Config: config.Project{
   382  			NFPMs: []config.NFPM{
   383  				{
   384  					Bindir: "/bin",
   385  					NFPMOverridables: config.NFPMOverridables{
   386  						FileNameTemplate: "foo",
   387  					},
   388  					Overrides: map[string]config.NFPMOverridables{
   389  						"deb": {
   390  							FileNameTemplate: "bar",
   391  						},
   392  					},
   393  				},
   394  			},
   395  		},
   396  	}
   397  	require.NoError(t, Pipe{}.Default(ctx))
   398  	merged, err := mergeOverrides(ctx.Config.NFPMs[0], "deb")
   399  	require.NoError(t, err)
   400  	require.Equal(t, "/bin", ctx.Config.NFPMs[0].Bindir)
   401  	require.Equal(t, "foo", ctx.Config.NFPMs[0].FileNameTemplate)
   402  	require.Equal(t, "bar", ctx.Config.NFPMs[0].Overrides["deb"].FileNameTemplate)
   403  	require.Equal(t, "bar", merged.FileNameTemplate)
   404  }
   405  
   406  func TestDebSpecificConfig(t *testing.T) {
   407  	var folder = t.TempDir()
   408  	var dist = filepath.Join(folder, "dist")
   409  	require.NoError(t, os.Mkdir(dist, 0755))
   410  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
   411  	var binPath = filepath.Join(dist, "mybin", "mybin")
   412  	_, err := os.Create(binPath)
   413  	require.NoError(t, err)
   414  	var ctx = context.New(config.Project{
   415  		ProjectName: "mybin",
   416  		Dist:        dist,
   417  		NFPMs: []config.NFPM{
   418  			{
   419  				ID:      "someid",
   420  				Builds:  []string{"default"},
   421  				Formats: []string{"deb"},
   422  				NFPMOverridables: config.NFPMOverridables{
   423  					PackageName: "foo",
   424  					Contents: []*files.Content{
   425  						{
   426  							Source:      "testdata/testfile.txt",
   427  							Destination: "/usr/share/testfile.txt",
   428  						},
   429  					},
   430  					Deb: config.NFPMDeb{
   431  						Signature: config.NFPMDebSignature{
   432  							KeyFile: "./testdata/privkey.gpg",
   433  						},
   434  					},
   435  				},
   436  			},
   437  		},
   438  	})
   439  	ctx.Version = "1.0.0"
   440  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   441  	for _, goos := range []string{"linux", "darwin"} {
   442  		for _, goarch := range []string{"amd64", "386"} {
   443  			ctx.Artifacts.Add(&artifact.Artifact{
   444  				Name:   "mybin",
   445  				Path:   binPath,
   446  				Goarch: goarch,
   447  				Goos:   goos,
   448  				Type:   artifact.Binary,
   449  				Extra: map[string]interface{}{
   450  					"ID": "default",
   451  				},
   452  			})
   453  		}
   454  	}
   455  
   456  	t.Run("no passphrase set", func(t *testing.T) {
   457  		require.Contains(
   458  			t,
   459  			Pipe{}.Run(ctx).Error(),
   460  			`key is encrypted but no passphrase was provided`,
   461  		)
   462  	})
   463  
   464  	t.Run("general passphrase set", func(t *testing.T) {
   465  		ctx.Env = map[string]string{
   466  			"NFPM_SOMEID_PASSPHRASE": "hunter2",
   467  		}
   468  		require.NoError(t, Pipe{}.Run(ctx))
   469  	})
   470  
   471  	t.Run("packager specific passphrase set", func(t *testing.T) {
   472  		ctx.Env = map[string]string{
   473  			"NFPM_SOMEID_DEB_PASSPHRASE": "hunter2",
   474  		}
   475  		require.NoError(t, Pipe{}.Run(ctx))
   476  	})
   477  }
   478  
   479  func TestRPMSpecificConfig(t *testing.T) {
   480  	var folder = t.TempDir()
   481  	var dist = filepath.Join(folder, "dist")
   482  	require.NoError(t, os.Mkdir(dist, 0755))
   483  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
   484  	var binPath = filepath.Join(dist, "mybin", "mybin")
   485  	_, err := os.Create(binPath)
   486  	require.NoError(t, err)
   487  	var ctx = context.New(config.Project{
   488  		ProjectName: "mybin",
   489  		Dist:        dist,
   490  		NFPMs: []config.NFPM{
   491  			{
   492  				ID:      "someid",
   493  				Builds:  []string{"default"},
   494  				Formats: []string{"rpm"},
   495  				NFPMOverridables: config.NFPMOverridables{
   496  					PackageName: "foo",
   497  					Contents: []*files.Content{
   498  						{
   499  							Source:      "testdata/testfile.txt",
   500  							Destination: "/usr/share/testfile.txt",
   501  						},
   502  					},
   503  					RPM: config.NFPMRPM{
   504  						Signature: config.NFPMRPMSignature{
   505  							KeyFile: "./testdata/privkey.gpg",
   506  						},
   507  					},
   508  				},
   509  			},
   510  		},
   511  	})
   512  	ctx.Version = "1.0.0"
   513  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   514  	for _, goos := range []string{"linux", "darwin"} {
   515  		for _, goarch := range []string{"amd64", "386"} {
   516  			ctx.Artifacts.Add(&artifact.Artifact{
   517  				Name:   "mybin",
   518  				Path:   binPath,
   519  				Goarch: goarch,
   520  				Goos:   goos,
   521  				Type:   artifact.Binary,
   522  				Extra: map[string]interface{}{
   523  					"ID": "default",
   524  				},
   525  			})
   526  		}
   527  	}
   528  
   529  	t.Run("no passphrase set", func(t *testing.T) {
   530  		require.Contains(
   531  			t,
   532  			Pipe{}.Run(ctx).Error(),
   533  			`key is encrypted but no passphrase was provided`,
   534  		)
   535  	})
   536  
   537  	t.Run("general passphrase set", func(t *testing.T) {
   538  		ctx.Env = map[string]string{
   539  			"NFPM_SOMEID_PASSPHRASE": "hunter2",
   540  		}
   541  		require.NoError(t, Pipe{}.Run(ctx))
   542  	})
   543  
   544  	t.Run("packager specific passphrase set", func(t *testing.T) {
   545  		ctx.Env = map[string]string{
   546  			"NFPM_SOMEID_RPM_PASSPHRASE": "hunter2",
   547  		}
   548  		require.NoError(t, Pipe{}.Run(ctx))
   549  	})
   550  }
   551  
   552  func TestAPKSpecificConfig(t *testing.T) {
   553  	var folder = t.TempDir()
   554  	var dist = filepath.Join(folder, "dist")
   555  	require.NoError(t, os.Mkdir(dist, 0755))
   556  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
   557  	var binPath = filepath.Join(dist, "mybin", "mybin")
   558  	_, err := os.Create(binPath)
   559  	require.NoError(t, err)
   560  	var ctx = context.New(config.Project{
   561  		ProjectName: "mybin",
   562  		Dist:        dist,
   563  		NFPMs: []config.NFPM{
   564  			{
   565  				ID:         "someid",
   566  				Maintainer: "me@me",
   567  				Builds:     []string{"default"},
   568  				Formats:    []string{"apk"},
   569  				NFPMOverridables: config.NFPMOverridables{
   570  					PackageName: "foo",
   571  					Contents: []*files.Content{
   572  						{
   573  							Source:      "testdata/testfile.txt",
   574  							Destination: "/usr/share/testfile.txt",
   575  						},
   576  					},
   577  					APK: config.NFPMAPK{
   578  						Signature: config.NFPMAPKSignature{
   579  							KeyFile: "./testdata/rsa.priv",
   580  						},
   581  					},
   582  				},
   583  			},
   584  		},
   585  	})
   586  	ctx.Version = "1.0.0"
   587  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   588  	for _, goos := range []string{"linux", "darwin"} {
   589  		for _, goarch := range []string{"amd64", "386"} {
   590  			ctx.Artifacts.Add(&artifact.Artifact{
   591  				Name:   "mybin",
   592  				Path:   binPath,
   593  				Goarch: goarch,
   594  				Goos:   goos,
   595  				Type:   artifact.Binary,
   596  				Extra: map[string]interface{}{
   597  					"ID": "default",
   598  				},
   599  			})
   600  		}
   601  	}
   602  
   603  	t.Run("no passphrase set", func(t *testing.T) {
   604  		require.Contains(
   605  			t,
   606  			Pipe{}.Run(ctx).Error(),
   607  			`key is encrypted but no passphrase was provided`,
   608  		)
   609  	})
   610  
   611  	t.Run("general passphrase set", func(t *testing.T) {
   612  		ctx.Env = map[string]string{
   613  			"NFPM_SOMEID_PASSPHRASE": "hunter2",
   614  		}
   615  		require.NoError(t, Pipe{}.Run(ctx))
   616  	})
   617  
   618  	t.Run("packager specific passphrase set", func(t *testing.T) {
   619  		ctx.Env = map[string]string{
   620  			"NFPM_SOMEID_APK_PASSPHRASE": "hunter2",
   621  		}
   622  		require.NoError(t, Pipe{}.Run(ctx))
   623  	})
   624  }
   625  
   626  func TestSeveralNFPMsWithTheSameID(t *testing.T) {
   627  	var ctx = &context.Context{
   628  		Config: config.Project{
   629  			NFPMs: []config.NFPM{
   630  				{
   631  					ID: "a",
   632  				},
   633  				{
   634  					ID: "a",
   635  				},
   636  			},
   637  		},
   638  	}
   639  	require.EqualError(t, Pipe{}.Default(ctx), "found 2 nfpms with the ID 'a', please fix your config")
   640  }
   641  
   642  func TestMeta(t *testing.T) {
   643  	var folder = t.TempDir()
   644  	var dist = filepath.Join(folder, "dist")
   645  	require.NoError(t, os.Mkdir(dist, 0755))
   646  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
   647  	var binPath = filepath.Join(dist, "mybin", "mybin")
   648  	_, err := os.Create(binPath)
   649  	require.NoError(t, err)
   650  	var ctx = context.New(config.Project{
   651  		ProjectName: "mybin",
   652  		Dist:        dist,
   653  		NFPMs: []config.NFPM{
   654  			{
   655  				ID:          "someid",
   656  				Bindir:      "/usr/bin",
   657  				Builds:      []string{"default"},
   658  				Formats:     []string{"deb", "rpm"},
   659  				Description: "Some description",
   660  				License:     "MIT",
   661  				Maintainer:  "me@me",
   662  				Vendor:      "asdf",
   663  				Homepage:    "https://goreleaser.github.io",
   664  				Meta:        true,
   665  				NFPMOverridables: config.NFPMOverridables{
   666  					FileNameTemplate: defaultNameTemplate + "-{{ .Release }}-{{ .Epoch }}",
   667  					PackageName:      "foo",
   668  					Dependencies:     []string{"make"},
   669  					Recommends:       []string{"svn"},
   670  					Suggests:         []string{"bzr"},
   671  					Replaces:         []string{"fish"},
   672  					Conflicts:        []string{"git"},
   673  					EmptyFolders:     []string{"/var/log/foobar"},
   674  					Release:          "10",
   675  					Epoch:            "20",
   676  					Contents: []*files.Content{
   677  						{
   678  							Source:      "testdata/testfile.txt",
   679  							Destination: "/usr/share/testfile.txt",
   680  						},
   681  						{
   682  							Source:      "./testdata/testfile.txt",
   683  							Destination: "/etc/nope.conf",
   684  							Type:        "config",
   685  						},
   686  						{
   687  							Source:      "./testdata/testfile.txt",
   688  							Destination: "/etc/nope-rpm.conf",
   689  							Type:        "config",
   690  							Packager:    "rpm",
   691  						},
   692  					},
   693  					Replacements: map[string]string{
   694  						"linux": "Tux",
   695  					},
   696  				},
   697  			},
   698  		},
   699  	})
   700  	ctx.Version = "1.0.0"
   701  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   702  	for _, goos := range []string{"linux", "darwin"} {
   703  		for _, goarch := range []string{"amd64", "386"} {
   704  			ctx.Artifacts.Add(&artifact.Artifact{
   705  				Name:   "mybin",
   706  				Path:   binPath,
   707  				Goarch: goarch,
   708  				Goos:   goos,
   709  				Type:   artifact.Binary,
   710  				Extra: map[string]interface{}{
   711  					"ID": "default",
   712  				},
   713  			})
   714  		}
   715  	}
   716  	require.NoError(t, Pipe{}.Run(ctx))
   717  	var packages = ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
   718  	require.Len(t, packages, 4)
   719  	for _, pkg := range packages {
   720  		var format = pkg.ExtraOr("Format", "").(string)
   721  		require.NotEmpty(t, format)
   722  		require.Equal(t, pkg.Name, "mybin_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format)
   723  		require.Equal(t, pkg.ExtraOr("ID", ""), "someid")
   724  	}
   725  
   726  	require.Len(t, ctx.Config.NFPMs[0].Contents, 3, "should not modify the config file list")
   727  
   728  	// ensure that no binaries added
   729  	for _, pkg := range packages {
   730  		contents := pkg.ExtraOr("Files", files.Contents{}).(files.Contents)
   731  		for _, f := range contents {
   732  			require.NotEqual(t, "/usr/bin/mybin", f.Destination, "binary file should not be added")
   733  		}
   734  	}
   735  }
   736  
   737  func TestSkipSign(t *testing.T) {
   738  	folder, err := ioutil.TempDir("", "archivetest")
   739  	require.NoError(t, err)
   740  	var dist = filepath.Join(folder, "dist")
   741  	require.NoError(t, os.Mkdir(dist, 0755))
   742  	require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
   743  	var binPath = filepath.Join(dist, "mybin", "mybin")
   744  	_, err = os.Create(binPath)
   745  	require.NoError(t, err)
   746  	var ctx = context.New(config.Project{
   747  		ProjectName: "mybin",
   748  		Dist:        dist,
   749  		NFPMs: []config.NFPM{
   750  			{
   751  				ID:      "someid",
   752  				Builds:  []string{"default"},
   753  				Formats: []string{"deb", "rpm", "apk"},
   754  				NFPMOverridables: config.NFPMOverridables{
   755  					PackageName:      "foo",
   756  					FileNameTemplate: defaultNameTemplate,
   757  					Contents: []*files.Content{
   758  						{
   759  							Source:      "testdata/testfile.txt",
   760  							Destination: "/usr/share/testfile.txt",
   761  						},
   762  					},
   763  					Deb: config.NFPMDeb{
   764  						Signature: config.NFPMDebSignature{
   765  							KeyFile: "/does/not/exist.gpg",
   766  						},
   767  					},
   768  					RPM: config.NFPMRPM{
   769  						Signature: config.NFPMRPMSignature{
   770  							KeyFile: "/does/not/exist.gpg",
   771  						},
   772  					},
   773  					APK: config.NFPMAPK{
   774  						Signature: config.NFPMAPKSignature{
   775  							KeyFile: "/does/not/exist.gpg",
   776  						},
   777  					},
   778  				},
   779  			},
   780  		},
   781  	})
   782  	ctx.Version = "1.0.0"
   783  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   784  	for _, goos := range []string{"linux", "darwin"} {
   785  		for _, goarch := range []string{"amd64", "386"} {
   786  			ctx.Artifacts.Add(&artifact.Artifact{
   787  				Name:   "mybin",
   788  				Path:   binPath,
   789  				Goarch: goarch,
   790  				Goos:   goos,
   791  				Type:   artifact.Binary,
   792  				Extra: map[string]interface{}{
   793  					"ID": "default",
   794  				},
   795  			})
   796  		}
   797  	}
   798  
   799  	t.Run("skip sign not set", func(t *testing.T) {
   800  		require.Contains(
   801  			t,
   802  			Pipe{}.Run(ctx).Error(),
   803  			`nfpm failed: failed to create signatures: call to signer failed: signing error: reading PGP key file: open /does/not/exist.gpg: no such file or directory`,
   804  		)
   805  	})
   806  
   807  	t.Run("skip sign set", func(t *testing.T) {
   808  		ctx.SkipSign = true
   809  		require.NoError(t, Pipe{}.Run(ctx))
   810  	})
   811  }