github.com/droot/goreleaser@v0.66.2-0.20180420030140-c2db5fb17157/pipeline/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/goreleaser/goreleaser/config"
    11  	"github.com/goreleaser/goreleaser/context"
    12  	"github.com/goreleaser/goreleaser/internal/artifact"
    13  	"github.com/goreleaser/goreleaser/internal/testlib"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestDescription(t *testing.T) {
    18  	assert.NotEmpty(t, Pipe{}.String())
    19  }
    20  
    21  func TestRunPipeNoFormats(t *testing.T) {
    22  	var ctx = &context.Context{
    23  		Git: context.GitInfo{
    24  			CurrentTag: "v1.0.0",
    25  		},
    26  		Config:      config.Project{},
    27  		Parallelism: runtime.NumCPU(),
    28  	}
    29  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    30  }
    31  
    32  func TestRunPipeInvalidFormat(t *testing.T) {
    33  	var ctx = context.New(config.Project{
    34  		ProjectName: "nope",
    35  		NFPM: config.NFPM{
    36  			Bindir:       "/usr/bin",
    37  			NameTemplate: defaultNameTemplate,
    38  			Formats:      []string{"nope"},
    39  			Files:        map[string]string{},
    40  		},
    41  	})
    42  	ctx.Git = context.GitInfo{
    43  		CurrentTag: "v1.2.3",
    44  	}
    45  	for _, goos := range []string{"linux", "darwin"} {
    46  		for _, goarch := range []string{"amd64", "386"} {
    47  			ctx.Artifacts.Add(artifact.Artifact{
    48  				Name:   "mybin",
    49  				Path:   "whatever",
    50  				Goarch: goarch,
    51  				Goos:   goos,
    52  				Type:   artifact.Binary,
    53  			})
    54  		}
    55  	}
    56  	assert.Contains(t, Pipe{}.Run(ctx).Error(), `no packager registered for the format nope`)
    57  }
    58  
    59  func TestRunPipe(t *testing.T) {
    60  	folder, err := ioutil.TempDir("", "archivetest")
    61  	assert.NoError(t, err)
    62  	var dist = filepath.Join(folder, "dist")
    63  	assert.NoError(t, os.Mkdir(dist, 0755))
    64  	assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
    65  	var binPath = filepath.Join(dist, "mybin", "mybin")
    66  	_, err = os.Create(binPath)
    67  	assert.NoError(t, err)
    68  	var ctx = context.New(config.Project{
    69  		ProjectName: "mybin",
    70  		Dist:        dist,
    71  		NFPM: config.NFPM{
    72  			Bindir:       "/usr/bin",
    73  			NameTemplate: defaultNameTemplate,
    74  			Formats:      []string{"deb", "rpm"},
    75  			Dependencies: []string{"make"},
    76  			Recommends:   []string{"svn"},
    77  			Suggests:     []string{"bzr"},
    78  			Conflicts:    []string{"git"},
    79  			Description:  "Some description",
    80  			License:      "MIT",
    81  			Maintainer:   "me@me",
    82  			Vendor:       "asdf",
    83  			Homepage:     "https://goreleaser.github.io",
    84  			Files: map[string]string{
    85  				"./testdata/testfile.txt": "/usr/share/testfile.txt",
    86  			},
    87  			ConfigFiles: map[string]string{
    88  				"./testdata/testfile.txt": "/etc/nope.conf",
    89  			},
    90  		},
    91  	})
    92  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
    93  	for _, goos := range []string{"linux", "darwin"} {
    94  		for _, goarch := range []string{"amd64", "386"} {
    95  			ctx.Artifacts.Add(artifact.Artifact{
    96  				Name:   "mybin",
    97  				Path:   binPath,
    98  				Goarch: goarch,
    99  				Goos:   goos,
   100  				Type:   artifact.Binary,
   101  			})
   102  		}
   103  	}
   104  	assert.NoError(t, Pipe{}.Run(ctx))
   105  	assert.Len(t, ctx.Config.NFPM.Files, 1, "should not modify the config file list")
   106  }
   107  
   108  func TestInvalidNameTemplate(t *testing.T) {
   109  	var ctx = &context.Context{
   110  		Parallelism: runtime.NumCPU(),
   111  		Artifacts:   artifact.New(),
   112  		Config: config.Project{
   113  			NFPM: config.NFPM{
   114  				NameTemplate: "{{.Foo}",
   115  				Formats:      []string{"deb"},
   116  			},
   117  		},
   118  	}
   119  	ctx.Artifacts.Add(artifact.Artifact{
   120  		Name:   "mybin",
   121  		Goos:   "linux",
   122  		Goarch: "amd64",
   123  		Type:   artifact.Binary,
   124  	})
   125  	assert.Contains(t, Pipe{}.Run(ctx).Error(), `template: {{.Foo}:1: unexpected "}" in operand`)
   126  }
   127  
   128  func TestCreateFileDoesntExist(t *testing.T) {
   129  	folder, err := ioutil.TempDir("", "archivetest")
   130  	assert.NoError(t, err)
   131  	var dist = filepath.Join(folder, "dist")
   132  	assert.NoError(t, os.Mkdir(dist, 0755))
   133  	assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
   134  	var ctx = context.New(config.Project{
   135  		Dist:        dist,
   136  		ProjectName: "asd",
   137  		NFPM: config.NFPM{
   138  			Formats: []string{"deb", "rpm"},
   139  			Files: map[string]string{
   140  				"testdata/testfile.txt": "/var/lib/test/testfile.txt",
   141  			},
   142  		},
   143  	})
   144  	ctx.Git = context.GitInfo{
   145  		CurrentTag: "v1.2.3",
   146  	}
   147  	ctx.Artifacts.Add(artifact.Artifact{
   148  		Name:   "mybin",
   149  		Path:   filepath.Join(dist, "mybin", "mybin"),
   150  		Goos:   "linux",
   151  		Goarch: "amd64",
   152  		Type:   artifact.Binary,
   153  	})
   154  	assert.Contains(t, Pipe{}.Run(ctx).Error(), `dist/mybin/mybin: file does not exist`)
   155  }
   156  
   157  func TestInvalidConfig(t *testing.T) {
   158  	folder, err := ioutil.TempDir("", "archivetest")
   159  	assert.NoError(t, err)
   160  	var dist = filepath.Join(folder, "dist")
   161  	assert.NoError(t, os.Mkdir(dist, 0755))
   162  	assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
   163  	var ctx = context.New(config.Project{
   164  		Dist: dist,
   165  		NFPM: config.NFPM{
   166  			Formats: []string{"deb"},
   167  		},
   168  	})
   169  	ctx.Artifacts.Add(artifact.Artifact{
   170  		Name:   "mybin",
   171  		Path:   filepath.Join(dist, "mybin", "mybin"),
   172  		Goos:   "linux",
   173  		Goarch: "amd64",
   174  		Type:   artifact.Binary,
   175  	})
   176  	assert.Contains(t, Pipe{}.Run(ctx).Error(), `invalid nfpm config: package name cannot be empty`)
   177  }
   178  
   179  func TestDefault(t *testing.T) {
   180  	var ctx = &context.Context{
   181  		Config: config.Project{
   182  			NFPM: config.NFPM{},
   183  		},
   184  	}
   185  	assert.NoError(t, Pipe{}.Default(ctx))
   186  	assert.Equal(t, "/usr/local/bin", ctx.Config.NFPM.Bindir)
   187  	assert.Equal(t, defaultNameTemplate, ctx.Config.NFPM.NameTemplate)
   188  }
   189  
   190  func TestDefaultSet(t *testing.T) {
   191  	var ctx = &context.Context{
   192  		Config: config.Project{
   193  			NFPM: config.NFPM{
   194  				Bindir:       "/bin",
   195  				NameTemplate: "foo",
   196  			},
   197  		},
   198  	}
   199  	assert.NoError(t, Pipe{}.Default(ctx))
   200  	assert.Equal(t, "/bin", ctx.Config.NFPM.Bindir)
   201  	assert.Equal(t, "foo", ctx.Config.NFPM.NameTemplate)
   202  }