2018-02-17 16:16:06 +02:00
package nfpm
import (
2020-12-14 13:51:14 +02:00
"io/ioutil"
2018-02-17 16:16:06 +02:00
"os"
"path/filepath"
"runtime"
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testlib"
2018-08-15 04:50:20 +02:00
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
2020-12-24 04:07:48 +02:00
"github.com/goreleaser/nfpm/v2/files"
2019-05-07 11:59:53 +02:00
"github.com/stretchr/testify/require"
2018-02-17 16:16:06 +02:00
)
func TestDescription ( t * testing . T ) {
2019-05-07 11:59:53 +02:00
require . NotEmpty ( t , Pipe { } . String ( ) )
2018-02-17 16:16:06 +02:00
}
func TestRunPipeNoFormats ( t * testing . T ) {
var ctx = & context . Context {
2019-10-05 19:58:46 +02:00
Version : "1.0.0" ,
2018-02-24 22:59:08 +02:00
Git : context . GitInfo {
CurrentTag : "v1.0.0" ,
} ,
2019-12-27 19:51:40 +02:00
Config : config . Project {
NFPMs : [ ] config . NFPM {
{ } ,
} ,
} ,
2018-02-17 16:16:06 +02:00
Parallelism : runtime . NumCPU ( ) ,
}
2019-05-07 11:59:53 +02:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2018-02-17 16:16:06 +02:00
testlib . AssertSkipped ( t , Pipe { } . Run ( ctx ) )
}
func TestRunPipeInvalidFormat ( t * testing . T ) {
var ctx = context . New ( config . Project {
2018-04-06 01:41:12 +02:00
ProjectName : "nope" ,
2019-05-07 11:59:53 +02:00
NFPMs : [ ] config . NFPM {
{
Bindir : "/usr/bin" ,
Formats : [ ] string { "nope" } ,
Builds : [ ] string { "foo" } ,
NFPMOverridables : config . NFPMOverridables {
2019-11-15 21:00:48 +02:00
PackageName : "foo" ,
FileNameTemplate : defaultNameTemplate ,
2019-05-07 11:59:53 +02:00
} ,
2018-04-21 16:48:22 +02:00
} ,
2018-02-17 16:16:06 +02:00
} ,
} )
2019-10-05 19:58:46 +02:00
ctx . Version = "1.2.3"
2018-04-06 01:41:12 +02:00
ctx . Git = context . GitInfo {
CurrentTag : "v1.2.3" ,
}
2018-02-17 16:16:06 +02:00
for _ , goos := range [ ] string { "linux" , "darwin" } {
for _ , goarch := range [ ] string { "amd64" , "386" } {
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-02-17 16:16:06 +02:00
Name : "mybin" ,
2020-12-24 04:07:48 +02:00
Path : "testdata/testfile.txt" ,
2018-02-17 16:16:06 +02:00
Goarch : goarch ,
Goos : goos ,
Type : artifact . Binary ,
2019-05-07 11:59:53 +02:00
Extra : map [ string ] interface { } {
"ID" : "foo" ,
} ,
2018-02-17 16:16:06 +02:00
} )
}
}
2019-05-07 11:59:53 +02:00
require . Contains ( t , Pipe { } . Run ( ctx ) . Error ( ) , ` no packager registered for the format nope ` )
2018-02-17 16:16:06 +02:00
}
func TestRunPipe ( t * testing . T ) {
2020-12-12 18:27:35 +02:00
var folder = t . TempDir ( )
2018-02-17 16:16:06 +02:00
var dist = filepath . Join ( folder , "dist" )
2019-05-07 11:59:53 +02:00
require . NoError ( t , os . Mkdir ( dist , 0755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0755 ) )
2018-02-17 16:16:06 +02:00
var binPath = filepath . Join ( dist , "mybin" , "mybin" )
2020-12-12 18:27:35 +02:00
_ , err := os . Create ( binPath )
2019-05-07 11:59:53 +02:00
require . NoError ( t , err )
2018-02-17 16:16:06 +02:00
var ctx = context . New ( config . Project {
ProjectName : "mybin" ,
Dist : dist ,
2019-05-07 11:59:53 +02:00
NFPMs : [ ] config . NFPM {
{
2020-02-27 14:45:12 +02:00
ID : "someid" ,
2019-05-07 11:59:53 +02:00
Bindir : "/usr/bin" ,
Builds : [ ] string { "default" } ,
2020-08-23 00:00:14 +02:00
Formats : [ ] string { "deb" , "rpm" , "apk" } ,
2019-05-07 11:59:53 +02:00
Description : "Some description" ,
License : "MIT" ,
Maintainer : "me@me" ,
Vendor : "asdf" ,
Homepage : "https://goreleaser.github.io" ,
NFPMOverridables : config . NFPMOverridables {
2020-03-22 18:54:47 +02:00
FileNameTemplate : defaultNameTemplate + "-{{ .Release }}-{{ .Epoch }}" ,
2019-11-15 21:00:48 +02:00
PackageName : "foo" ,
Dependencies : [ ] string { "make" } ,
Recommends : [ ] string { "svn" } ,
Suggests : [ ] string { "bzr" } ,
2020-11-06 20:17:08 +02:00
Replaces : [ ] string { "fish" } ,
2019-11-15 21:00:48 +02:00
Conflicts : [ ] string { "git" } ,
EmptyFolders : [ ] string { "/var/log/foobar" } ,
2020-03-22 18:54:47 +02:00
Release : "10" ,
Epoch : "20" ,
2020-12-24 04:07:48 +02:00
Contents : [ ] * files . Content {
{
Source : "./testdata/testfile.txt" ,
Destination : "/usr/share/testfile.txt" ,
} ,
{
Source : "./testdata/testfile.txt" ,
Destination : "/etc/nope.conf" ,
Type : "config" ,
} ,
{
Source : "./testdata/testfile.txt" ,
Destination : "/etc/nope-rpm.conf" ,
Type : "config" ,
Packager : "rpm" ,
} ,
{
Source : "/etc/nope.conf" ,
Destination : "/etc/nope2.conf" ,
Type : "symlink" ,
} ,
2020-11-27 04:45:17 +02:00
} ,
2019-05-07 11:59:53 +02:00
Replacements : map [ string ] string {
"linux" : "Tux" ,
} ,
} ,
2018-02-17 23:30:26 +02:00
} ,
2018-02-17 16:16:06 +02:00
} ,
} )
2018-07-09 02:22:09 +02:00
ctx . Version = "1.0.0"
2018-02-24 22:59:08 +02:00
ctx . Git = context . GitInfo { CurrentTag : "v1.0.0" }
2018-02-17 16:16:06 +02:00
for _ , goos := range [ ] string { "linux" , "darwin" } {
for _ , goarch := range [ ] string { "amd64" , "386" } {
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-02-17 16:16:06 +02:00
Name : "mybin" ,
Path : binPath ,
Goarch : goarch ,
Goos : goos ,
Type : artifact . Binary ,
2019-05-07 11:59:53 +02:00
Extra : map [ string ] interface { } {
"ID" : "default" ,
} ,
2018-02-17 16:16:06 +02:00
} )
}
}
2019-05-07 11:59:53 +02:00
require . NoError ( t , Pipe { } . Run ( ctx ) )
2018-07-09 02:22:09 +02:00
var packages = ctx . Artifacts . Filter ( artifact . ByType ( artifact . LinuxPackage ) ) . List ( )
2020-08-23 00:00:14 +02:00
require . Len ( t , packages , 6 )
2018-07-09 02:22:09 +02:00
for _ , pkg := range packages {
2020-03-22 18:54:47 +02:00
var format = pkg . ExtraOr ( "Format" , "" ) . ( string )
require . NotEmpty ( t , format )
require . Equal ( t , pkg . Name , "mybin_1.0.0_Tux_" + pkg . Goarch + "-10-20." + format )
2020-02-27 14:45:12 +02:00
require . Equal ( t , pkg . ExtraOr ( "ID" , "" ) , "someid" )
2018-07-09 02:22:09 +02:00
}
2020-12-24 04:07:48 +02:00
require . Len ( t , ctx . Config . NFPMs [ 0 ] . Contents , 4 , "should not modify the config file list" )
2018-02-17 16:16:06 +02:00
}
func TestInvalidNameTemplate ( t * testing . T ) {
var ctx = & context . Context {
Parallelism : runtime . NumCPU ( ) ,
Artifacts : artifact . New ( ) ,
Config : config . Project {
2019-05-07 11:59:53 +02:00
NFPMs : [ ] config . NFPM {
{
2019-11-15 21:00:48 +02:00
NFPMOverridables : config . NFPMOverridables {
PackageName : "foo" ,
FileNameTemplate : "{{.Foo}" ,
} ,
Formats : [ ] string { "deb" } ,
Builds : [ ] string { "default" } ,
2019-05-07 11:59:53 +02:00
} ,
} ,
} ,
}
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2019-05-07 11:59:53 +02:00
Name : "mybin" ,
Goos : "linux" ,
Goarch : "amd64" ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
"ID" : "default" ,
} ,
} )
require . Contains ( t , Pipe { } . Run ( ctx ) . Error ( ) , ` template: tmpl:1: unexpected "}" in operand ` )
}
func TestNoBuildsFound ( t * testing . T ) {
var ctx = & context . Context {
Parallelism : runtime . NumCPU ( ) ,
Artifacts : artifact . New ( ) ,
Config : config . Project {
NFPMs : [ ] config . NFPM {
{
Formats : [ ] string { "deb" } ,
Builds : [ ] string { "nope" } ,
} ,
2018-02-17 16:16:06 +02:00
} ,
} ,
}
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-02-17 16:16:06 +02:00
Name : "mybin" ,
Goos : "linux" ,
Goarch : "amd64" ,
Type : artifact . Binary ,
2019-05-07 11:59:53 +02:00
Extra : map [ string ] interface { } {
"ID" : "default" ,
} ,
2018-02-17 16:16:06 +02:00
} )
2019-05-07 11:59:53 +02:00
require . EqualError ( t , Pipe { } . Run ( ctx ) , ` no linux binaries found for builds [nope] ` )
2018-02-17 16:16:06 +02:00
}
func TestCreateFileDoesntExist ( t * testing . T ) {
2020-12-12 18:27:35 +02:00
var folder = t . TempDir ( )
2018-02-17 16:16:06 +02:00
var dist = filepath . Join ( folder , "dist" )
2019-05-07 11:59:53 +02:00
require . NoError ( t , os . Mkdir ( dist , 0755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0755 ) )
2018-02-17 16:16:06 +02:00
var ctx = context . New ( config . Project {
2018-04-06 01:41:12 +02:00
Dist : dist ,
ProjectName : "asd" ,
2019-05-07 11:59:53 +02:00
NFPMs : [ ] config . NFPM {
{
Formats : [ ] string { "deb" , "rpm" } ,
Builds : [ ] string { "default" } ,
NFPMOverridables : config . NFPMOverridables {
2019-11-15 21:00:48 +02:00
PackageName : "foo" ,
2020-12-24 04:07:48 +02:00
Contents : [ ] * files . Content {
{
Source : "testdata/testfile.txt" ,
Destination : "/var/lib/test/testfile.txt" ,
} ,
2019-05-07 11:59:53 +02:00
} ,
2018-04-21 16:48:22 +02:00
} ,
2018-02-17 16:16:06 +02:00
} ,
} ,
} )
2019-10-05 19:58:46 +02:00
ctx . Version = "1.2.3"
2018-04-06 01:41:12 +02:00
ctx . Git = context . GitInfo {
CurrentTag : "v1.2.3" ,
}
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-02-17 16:16:06 +02:00
Name : "mybin" ,
Path : filepath . Join ( dist , "mybin" , "mybin" ) ,
Goos : "linux" ,
Goarch : "amd64" ,
Type : artifact . Binary ,
2019-05-07 11:59:53 +02:00
Extra : map [ string ] interface { } {
"ID" : "default" ,
} ,
2018-02-17 16:16:06 +02:00
} )
2020-11-12 15:48:49 +02:00
require . Contains ( t , Pipe { } . Run ( ctx ) . Error ( ) , ` dist/mybin/mybin": file does not exist ` )
2018-02-17 16:16:06 +02:00
}
2018-04-06 01:41:12 +02:00
func TestInvalidConfig ( t * testing . T ) {
2020-12-12 18:27:35 +02:00
var folder = t . TempDir ( )
2018-04-06 01:41:12 +02:00
var dist = filepath . Join ( folder , "dist" )
2019-05-07 11:59:53 +02:00
require . NoError ( t , os . Mkdir ( dist , 0755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0755 ) )
2018-04-06 01:41:12 +02:00
var ctx = context . New ( config . Project {
Dist : dist ,
2019-05-07 11:59:53 +02:00
NFPMs : [ ] config . NFPM {
{
Formats : [ ] string { "deb" } ,
Builds : [ ] string { "default" } ,
} ,
2018-04-06 01:41:12 +02:00
} ,
} )
2018-07-09 05:47:30 +02:00
ctx . Git . CurrentTag = "v1.2.3"
ctx . Version = "v1.2.3"
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-04-06 01:41:12 +02:00
Name : "mybin" ,
Path : filepath . Join ( dist , "mybin" , "mybin" ) ,
Goos : "linux" ,
Goarch : "amd64" ,
Type : artifact . Binary ,
2019-05-07 11:59:53 +02:00
Extra : map [ string ] interface { } {
"ID" : "default" ,
} ,
2018-04-06 01:41:12 +02:00
} )
2020-05-26 06:15:20 +02:00
require . Contains ( t , Pipe { } . Run ( ctx ) . Error ( ) , ` invalid nfpm config: package name must be provided ` )
2018-04-06 01:41:12 +02:00
}
2018-02-17 16:16:06 +02:00
func TestDefault ( t * testing . T ) {
var ctx = & context . Context {
Config : config . Project {
2019-11-15 21:00:48 +02:00
ProjectName : "foobar" ,
2019-12-27 19:51:40 +02:00
NFPMs : [ ] config . NFPM {
{ } ,
2019-05-07 11:59:53 +02:00
} ,
Builds : [ ] config . Build {
{ ID : "foo" } ,
{ ID : "bar" } ,
} ,
} ,
}
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . Equal ( t , "/usr/local/bin" , ctx . Config . NFPMs [ 0 ] . Bindir )
require . Equal ( t , [ ] string { "foo" , "bar" } , ctx . Config . NFPMs [ 0 ] . Builds )
2019-11-15 21:00:48 +02:00
require . Equal ( t , defaultNameTemplate , ctx . Config . NFPMs [ 0 ] . FileNameTemplate )
2019-12-27 19:51:40 +02:00
require . Equal ( t , ctx . Config . ProjectName , ctx . Config . NFPMs [ 0 ] . PackageName )
2019-11-15 21:00:48 +02:00
}
2020-12-24 04:07:48 +02:00
func TestDefaultDeprecatedOptions ( t * testing . T ) {
var ctx = & context . Context {
Config : config . Project {
ProjectName : "foobar" ,
NFPMs : [ ] config . NFPM {
{
NFPMOverridables : config . NFPMOverridables {
Files : map [ string ] string {
"testdata/testfile.txt" : "/bin/foo" ,
} ,
ConfigFiles : map [ string ] string {
"testdata/testfile.txt" : "/etc/foo.conf" ,
} ,
Symlinks : map [ string ] string {
"/etc/foo.conf" : "/etc/foov2.conf" ,
} ,
RPM : config . NFPMRPM {
GhostFiles : [ ] string { "/etc/ghost.conf" } ,
ConfigNoReplaceFiles : map [ string ] string {
"testdata/testfile.txt" : "/etc/foo_keep.conf" ,
} ,
} ,
2020-12-28 19:40:23 +02:00
Deb : config . NFPMDeb {
VersionMetadata : "beta1" ,
} ,
2020-12-24 04:07:48 +02:00
} ,
} ,
} ,
Builds : [ ] config . Build {
{ ID : "foo" } ,
{ ID : "bar" } ,
} ,
} ,
}
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . Equal ( t , "/usr/local/bin" , ctx . Config . NFPMs [ 0 ] . Bindir )
require . Equal ( t , [ ] string { "foo" , "bar" } , ctx . Config . NFPMs [ 0 ] . Builds )
require . ElementsMatch ( t , [ ] * files . Content {
{ Source : "testdata/testfile.txt" , Destination : "/bin/foo" } ,
{ Source : "testdata/testfile.txt" , Destination : "/etc/foo.conf" , Type : "config" } ,
{ Source : "/etc/foo.conf" , Destination : "/etc/foov2.conf" , Type : "symlink" } ,
{ Destination : "/etc/ghost.conf" , Type : "ghost" , Packager : "rpm" } ,
{ Source : "testdata/testfile.txt" , Destination : "/etc/foo_keep.conf" , Type : "config|noreplace" , Packager : "rpm" } ,
} , ctx . Config . NFPMs [ 0 ] . Contents )
require . Equal ( t , defaultNameTemplate , ctx . Config . NFPMs [ 0 ] . FileNameTemplate )
require . Equal ( t , ctx . Config . ProjectName , ctx . Config . NFPMs [ 0 ] . PackageName )
2020-12-28 19:40:23 +02:00
require . Equal ( t , "beta1" , ctx . Config . NFPMs [ 0 ] . VersionMetadata )
2020-12-24 04:07:48 +02:00
}
2019-11-15 21:00:48 +02:00
func TestDefaultSet ( t * testing . T ) {
var ctx = & context . Context {
Config : config . Project {
Builds : [ ] config . Build {
{ ID : "foo" } ,
{ ID : "bar" } ,
} ,
NFPMs : [ ] config . NFPM {
{
Builds : [ ] string { "foo" } ,
Bindir : "/bin" ,
NFPMOverridables : config . NFPMOverridables {
FileNameTemplate : "foo" ,
} ,
} ,
} ,
} ,
}
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . Equal ( t , "/bin" , ctx . Config . NFPMs [ 0 ] . Bindir )
require . Equal ( t , "foo" , ctx . Config . NFPMs [ 0 ] . FileNameTemplate )
2019-05-07 11:59:53 +02:00
require . Equal ( t , [ ] string { "foo" } , ctx . Config . NFPMs [ 0 ] . Builds )
2018-02-17 16:16:06 +02:00
}
2018-04-21 16:48:22 +02:00
func TestOverrides ( t * testing . T ) {
var ctx = & context . Context {
Config : config . Project {
2019-05-07 11:59:53 +02:00
NFPMs : [ ] config . NFPM {
{
Bindir : "/bin" ,
NFPMOverridables : config . NFPMOverridables {
2020-04-20 00:54:07 +02:00
FileNameTemplate : "foo" ,
2019-05-07 11:59:53 +02:00
} ,
Overrides : map [ string ] config . NFPMOverridables {
"deb" : {
2020-04-20 00:54:07 +02:00
FileNameTemplate : "bar" ,
2019-05-07 11:59:53 +02:00
} ,
2018-04-21 16:48:22 +02:00
} ,
} ,
} ,
} ,
}
2019-05-07 11:59:53 +02:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
merged , err := mergeOverrides ( ctx . Config . NFPMs [ 0 ] , "deb" )
require . NoError ( t , err )
require . Equal ( t , "/bin" , ctx . Config . NFPMs [ 0 ] . Bindir )
2020-04-20 00:54:07 +02:00
require . Equal ( t , "foo" , ctx . Config . NFPMs [ 0 ] . FileNameTemplate )
require . Equal ( t , "bar" , ctx . Config . NFPMs [ 0 ] . Overrides [ "deb" ] . FileNameTemplate )
require . Equal ( t , "bar" , merged . FileNameTemplate )
2018-04-21 16:48:22 +02:00
}
2019-05-07 12:18:35 +02:00
2020-11-05 23:47:55 +02:00
func TestDebSpecificConfig ( t * testing . T ) {
2020-12-12 18:27:35 +02:00
var folder = t . TempDir ( )
2020-11-05 23:47:55 +02:00
var dist = filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0755 ) )
var binPath = filepath . Join ( dist , "mybin" , "mybin" )
2020-12-12 18:27:35 +02:00
_ , err := os . Create ( binPath )
2020-11-05 23:47:55 +02:00
require . NoError ( t , err )
var ctx = context . New ( config . Project {
ProjectName : "mybin" ,
Dist : dist ,
NFPMs : [ ] config . NFPM {
{
ID : "someid" ,
Builds : [ ] string { "default" } ,
Formats : [ ] string { "deb" } ,
NFPMOverridables : config . NFPMOverridables {
PackageName : "foo" ,
2020-12-24 04:07:48 +02:00
Contents : [ ] * files . Content {
{
Source : "testdata/testfile.txt" ,
Destination : "/usr/share/testfile.txt" ,
} ,
2020-11-05 23:47:55 +02:00
} ,
Deb : config . NFPMDeb {
Signature : config . NFPMDebSignature {
KeyFile : "./testdata/privkey.gpg" ,
} ,
} ,
} ,
} ,
} ,
} )
ctx . Version = "1.0.0"
ctx . Git = context . GitInfo { CurrentTag : "v1.0.0" }
for _ , goos := range [ ] string { "linux" , "darwin" } {
for _ , goarch := range [ ] string { "amd64" , "386" } {
ctx . Artifacts . Add ( & artifact . Artifact {
Name : "mybin" ,
Path : binPath ,
Goarch : goarch ,
Goos : goos ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
"ID" : "default" ,
} ,
} )
}
}
t . Run ( "no passphrase set" , func ( t * testing . T ) {
require . Contains (
t ,
Pipe { } . Run ( ctx ) . Error ( ) ,
` key is encrypted but no passphrase was provided ` ,
)
} )
t . Run ( "general passphrase set" , func ( t * testing . T ) {
ctx . Env = map [ string ] string {
"NFPM_SOMEID_PASSPHRASE" : "hunter2" ,
}
require . NoError ( t , Pipe { } . Run ( ctx ) )
} )
t . Run ( "packager specific passphrase set" , func ( t * testing . T ) {
ctx . Env = map [ string ] string {
"NFPM_SOMEID_DEB_PASSPHRASE" : "hunter2" ,
}
require . NoError ( t , Pipe { } . Run ( ctx ) )
} )
}
func TestRPMSpecificConfig ( t * testing . T ) {
2020-12-12 18:27:35 +02:00
var folder = t . TempDir ( )
2020-11-05 23:47:55 +02:00
var dist = filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0755 ) )
var binPath = filepath . Join ( dist , "mybin" , "mybin" )
2020-12-12 18:27:35 +02:00
_ , err := os . Create ( binPath )
2020-11-05 23:47:55 +02:00
require . NoError ( t , err )
var ctx = context . New ( config . Project {
ProjectName : "mybin" ,
Dist : dist ,
NFPMs : [ ] config . NFPM {
{
ID : "someid" ,
Builds : [ ] string { "default" } ,
Formats : [ ] string { "rpm" } ,
NFPMOverridables : config . NFPMOverridables {
PackageName : "foo" ,
2020-12-24 04:07:48 +02:00
Contents : [ ] * files . Content {
{
Source : "testdata/testfile.txt" ,
Destination : "/usr/share/testfile.txt" ,
} ,
2020-11-05 23:47:55 +02:00
} ,
RPM : config . NFPMRPM {
Signature : config . NFPMRPMSignature {
KeyFile : "./testdata/privkey.gpg" ,
} ,
} ,
} ,
} ,
} ,
} )
ctx . Version = "1.0.0"
ctx . Git = context . GitInfo { CurrentTag : "v1.0.0" }
for _ , goos := range [ ] string { "linux" , "darwin" } {
for _ , goarch := range [ ] string { "amd64" , "386" } {
ctx . Artifacts . Add ( & artifact . Artifact {
Name : "mybin" ,
Path : binPath ,
Goarch : goarch ,
Goos : goos ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
"ID" : "default" ,
} ,
} )
}
}
t . Run ( "no passphrase set" , func ( t * testing . T ) {
require . Contains (
t ,
Pipe { } . Run ( ctx ) . Error ( ) ,
` key is encrypted but no passphrase was provided ` ,
)
} )
t . Run ( "general passphrase set" , func ( t * testing . T ) {
ctx . Env = map [ string ] string {
"NFPM_SOMEID_PASSPHRASE" : "hunter2" ,
}
require . NoError ( t , Pipe { } . Run ( ctx ) )
} )
t . Run ( "packager specific passphrase set" , func ( t * testing . T ) {
ctx . Env = map [ string ] string {
"NFPM_SOMEID_RPM_PASSPHRASE" : "hunter2" ,
}
require . NoError ( t , Pipe { } . Run ( ctx ) )
} )
}
func TestAPKSpecificConfig ( t * testing . T ) {
2020-12-12 18:27:35 +02:00
var folder = t . TempDir ( )
2020-11-05 23:47:55 +02:00
var dist = filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0755 ) )
var binPath = filepath . Join ( dist , "mybin" , "mybin" )
2020-12-12 18:27:35 +02:00
_ , err := os . Create ( binPath )
2020-11-05 23:47:55 +02:00
require . NoError ( t , err )
var ctx = context . New ( config . Project {
ProjectName : "mybin" ,
Dist : dist ,
NFPMs : [ ] config . NFPM {
{
ID : "someid" ,
Maintainer : "me@me" ,
Builds : [ ] string { "default" } ,
Formats : [ ] string { "apk" } ,
NFPMOverridables : config . NFPMOverridables {
PackageName : "foo" ,
2020-12-24 04:07:48 +02:00
Contents : [ ] * files . Content {
{
Source : "testdata/testfile.txt" ,
Destination : "/usr/share/testfile.txt" ,
} ,
2020-11-05 23:47:55 +02:00
} ,
APK : config . NFPMAPK {
Signature : config . NFPMAPKSignature {
KeyFile : "./testdata/rsa.priv" ,
} ,
} ,
} ,
} ,
} ,
} )
ctx . Version = "1.0.0"
ctx . Git = context . GitInfo { CurrentTag : "v1.0.0" }
for _ , goos := range [ ] string { "linux" , "darwin" } {
for _ , goarch := range [ ] string { "amd64" , "386" } {
ctx . Artifacts . Add ( & artifact . Artifact {
Name : "mybin" ,
Path : binPath ,
Goarch : goarch ,
Goos : goos ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
"ID" : "default" ,
} ,
} )
}
}
t . Run ( "no passphrase set" , func ( t * testing . T ) {
require . Contains (
t ,
Pipe { } . Run ( ctx ) . Error ( ) ,
` key is encrypted but no passphrase was provided ` ,
)
} )
t . Run ( "general passphrase set" , func ( t * testing . T ) {
ctx . Env = map [ string ] string {
"NFPM_SOMEID_PASSPHRASE" : "hunter2" ,
}
require . NoError ( t , Pipe { } . Run ( ctx ) )
} )
t . Run ( "packager specific passphrase set" , func ( t * testing . T ) {
ctx . Env = map [ string ] string {
"NFPM_SOMEID_APK_PASSPHRASE" : "hunter2" ,
}
require . NoError ( t , Pipe { } . Run ( ctx ) )
} )
}
2019-05-07 12:18:35 +02:00
func TestSeveralNFPMsWithTheSameID ( t * testing . T ) {
var ctx = & context . Context {
Config : config . Project {
NFPMs : [ ] config . NFPM {
{
ID : "a" ,
} ,
{
ID : "a" ,
} ,
} ,
} ,
}
2019-07-27 15:55:56 +02:00
require . EqualError ( t , Pipe { } . Default ( ctx ) , "found 2 nfpms with the ID 'a', please fix your config" )
2019-05-07 12:18:35 +02:00
}
2020-06-19 16:04:10 +02:00
func TestMeta ( t * testing . T ) {
2020-12-12 18:27:35 +02:00
var folder = t . TempDir ( )
2020-06-19 16:04:10 +02:00
var dist = filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0755 ) )
var binPath = filepath . Join ( dist , "mybin" , "mybin" )
2020-12-12 18:27:35 +02:00
_ , err := os . Create ( binPath )
2020-06-19 16:04:10 +02:00
require . NoError ( t , err )
var ctx = context . New ( config . Project {
ProjectName : "mybin" ,
Dist : dist ,
NFPMs : [ ] config . NFPM {
{
ID : "someid" ,
Bindir : "/usr/bin" ,
Builds : [ ] string { "default" } ,
Formats : [ ] string { "deb" , "rpm" } ,
Description : "Some description" ,
License : "MIT" ,
Maintainer : "me@me" ,
Vendor : "asdf" ,
Homepage : "https://goreleaser.github.io" ,
Meta : true ,
NFPMOverridables : config . NFPMOverridables {
FileNameTemplate : defaultNameTemplate + "-{{ .Release }}-{{ .Epoch }}" ,
PackageName : "foo" ,
Dependencies : [ ] string { "make" } ,
Recommends : [ ] string { "svn" } ,
Suggests : [ ] string { "bzr" } ,
2020-11-06 20:17:08 +02:00
Replaces : [ ] string { "fish" } ,
2020-06-19 16:04:10 +02:00
Conflicts : [ ] string { "git" } ,
EmptyFolders : [ ] string { "/var/log/foobar" } ,
Release : "10" ,
Epoch : "20" ,
2020-12-24 04:07:48 +02:00
Contents : [ ] * files . Content {
{
Source : "testdata/testfile.txt" ,
Destination : "/usr/share/testfile.txt" ,
} ,
{
Source : "./testdata/testfile.txt" ,
Destination : "/etc/nope.conf" ,
Type : "config" ,
} ,
{
Source : "./testdata/testfile.txt" ,
Destination : "/etc/nope-rpm.conf" ,
Type : "config" ,
Packager : "rpm" ,
} ,
2020-06-19 16:04:10 +02:00
} ,
Replacements : map [ string ] string {
"linux" : "Tux" ,
} ,
} ,
} ,
} ,
} )
ctx . Version = "1.0.0"
ctx . Git = context . GitInfo { CurrentTag : "v1.0.0" }
for _ , goos := range [ ] string { "linux" , "darwin" } {
for _ , goarch := range [ ] string { "amd64" , "386" } {
ctx . Artifacts . Add ( & artifact . Artifact {
Name : "mybin" ,
Path : binPath ,
Goarch : goarch ,
Goos : goos ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
"ID" : "default" ,
} ,
} )
}
}
require . NoError ( t , Pipe { } . Run ( ctx ) )
var packages = ctx . Artifacts . Filter ( artifact . ByType ( artifact . LinuxPackage ) ) . List ( )
require . Len ( t , packages , 4 )
for _ , pkg := range packages {
var format = pkg . ExtraOr ( "Format" , "" ) . ( string )
require . NotEmpty ( t , format )
require . Equal ( t , pkg . Name , "mybin_1.0.0_Tux_" + pkg . Goarch + "-10-20." + format )
require . Equal ( t , pkg . ExtraOr ( "ID" , "" ) , "someid" )
}
2020-12-24 04:07:48 +02:00
require . Len ( t , ctx . Config . NFPMs [ 0 ] . Contents , 3 , "should not modify the config file list" )
2020-06-19 16:04:10 +02:00
// ensure that no binaries added
for _ , pkg := range packages {
2020-12-24 04:07:48 +02:00
contents := pkg . ExtraOr ( "Files" , files . Contents { } ) . ( files . Contents )
for _ , f := range contents {
require . NotEqual ( t , "/usr/bin/mybin" , f . Destination , "binary file should not be added" )
2020-06-19 16:04:10 +02:00
}
}
}
2020-12-14 13:50:26 +02:00
func TestSkipSign ( t * testing . T ) {
folder , err := ioutil . TempDir ( "" , "archivetest" )
require . NoError ( t , err )
var dist = filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0755 ) )
var binPath = filepath . Join ( dist , "mybin" , "mybin" )
_ , err = os . Create ( binPath )
require . NoError ( t , err )
var ctx = context . New ( config . Project {
ProjectName : "mybin" ,
Dist : dist ,
NFPMs : [ ] config . NFPM {
{
ID : "someid" ,
Builds : [ ] string { "default" } ,
Formats : [ ] string { "deb" , "rpm" , "apk" } ,
NFPMOverridables : config . NFPMOverridables {
PackageName : "foo" ,
FileNameTemplate : defaultNameTemplate ,
2020-12-24 04:07:48 +02:00
Contents : [ ] * files . Content {
{
Source : "testdata/testfile.txt" ,
Destination : "/usr/share/testfile.txt" ,
} ,
2020-12-14 13:50:26 +02:00
} ,
Deb : config . NFPMDeb {
Signature : config . NFPMDebSignature {
KeyFile : "/does/not/exist.gpg" ,
} ,
} ,
RPM : config . NFPMRPM {
Signature : config . NFPMRPMSignature {
KeyFile : "/does/not/exist.gpg" ,
} ,
} ,
APK : config . NFPMAPK {
Signature : config . NFPMAPKSignature {
KeyFile : "/does/not/exist.gpg" ,
} ,
} ,
} ,
} ,
} ,
} )
ctx . Version = "1.0.0"
ctx . Git = context . GitInfo { CurrentTag : "v1.0.0" }
for _ , goos := range [ ] string { "linux" , "darwin" } {
for _ , goarch := range [ ] string { "amd64" , "386" } {
ctx . Artifacts . Add ( & artifact . Artifact {
Name : "mybin" ,
Path : binPath ,
Goarch : goarch ,
Goos : goos ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
"ID" : "default" ,
} ,
} )
}
}
t . Run ( "skip sign not set" , func ( t * testing . T ) {
require . Contains (
t ,
Pipe { } . Run ( ctx ) . Error ( ) ,
` 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 ` ,
)
} )
t . Run ( "skip sign set" , func ( t * testing . T ) {
ctx . SkipSign = true
require . NoError ( t , Pipe { } . Run ( ctx ) )
} )
}