github.com/jasei/goreleaser@v0.62.4-0.20180312171904-62cb6a8963a6/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  		NFPM: config.NFPM{
    35  			Bindir:       "/usr/bin",
    36  			NameTemplate: defaultNameTemplate,
    37  			Formats:      []string{"nope"},
    38  			Files:        map[string]string{},
    39  		},
    40  	})
    41  	for _, goos := range []string{"linux", "darwin"} {
    42  		for _, goarch := range []string{"amd64", "386"} {
    43  			ctx.Artifacts.Add(artifact.Artifact{
    44  				Name:   "mybin",
    45  				Path:   "whatever",
    46  				Goarch: goarch,
    47  				Goos:   goos,
    48  				Type:   artifact.Binary,
    49  			})
    50  		}
    51  	}
    52  	assert.Contains(t, Pipe{}.Run(ctx).Error(), `no packager registered for the format nope`)
    53  }
    54  
    55  func TestRunPipe(t *testing.T) {
    56  	folder, err := ioutil.TempDir("", "archivetest")
    57  	assert.NoError(t, err)
    58  	var dist = filepath.Join(folder, "dist")
    59  	assert.NoError(t, os.Mkdir(dist, 0755))
    60  	assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
    61  	var binPath = filepath.Join(dist, "mybin", "mybin")
    62  	_, err = os.Create(binPath)
    63  	assert.NoError(t, err)
    64  	var ctx = context.New(config.Project{
    65  		ProjectName: "mybin",
    66  		Dist:        dist,
    67  		NFPM: config.NFPM{
    68  			Bindir:       "/usr/bin",
    69  			NameTemplate: defaultNameTemplate,
    70  			Formats:      []string{"deb", "rpm"},
    71  			Dependencies: []string{"make"},
    72  			Recommends:   []string{"svn"},
    73  			Suggests:     []string{"bzr"},
    74  			Conflicts:    []string{"git"},
    75  			Description:  "Some description",
    76  			License:      "MIT",
    77  			Maintainer:   "me@me",
    78  			Vendor:       "asdf",
    79  			Homepage:     "https://goreleaser.github.io",
    80  			Files: map[string]string{
    81  				"./testdata/testfile.txt": "/usr/share/testfile.txt",
    82  			},
    83  			ConfigFiles: map[string]string{
    84  				"./testdata/testfile.txt": "/etc/nope.conf",
    85  			},
    86  		},
    87  	})
    88  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
    89  	for _, goos := range []string{"linux", "darwin"} {
    90  		for _, goarch := range []string{"amd64", "386"} {
    91  			ctx.Artifacts.Add(artifact.Artifact{
    92  				Name:   "mybin",
    93  				Path:   binPath,
    94  				Goarch: goarch,
    95  				Goos:   goos,
    96  				Type:   artifact.Binary,
    97  			})
    98  		}
    99  	}
   100  	assert.NoError(t, Pipe{}.Run(ctx))
   101  	assert.Len(t, ctx.Config.NFPM.Files, 1, "should not modify the config file list")
   102  }
   103  
   104  func TestInvalidNameTemplate(t *testing.T) {
   105  	var ctx = &context.Context{
   106  		Parallelism: runtime.NumCPU(),
   107  		Artifacts:   artifact.New(),
   108  		Config: config.Project{
   109  			NFPM: config.NFPM{
   110  				NameTemplate: "{{.Foo}",
   111  				Formats:      []string{"deb"},
   112  			},
   113  		},
   114  	}
   115  	ctx.Artifacts.Add(artifact.Artifact{
   116  		Name:   "mybin",
   117  		Goos:   "linux",
   118  		Goarch: "amd64",
   119  		Type:   artifact.Binary,
   120  	})
   121  	assert.Contains(t, Pipe{}.Run(ctx).Error(), `template: {{.Foo}:1: unexpected "}" in operand`)
   122  }
   123  
   124  func TestCreateFileDoesntExist(t *testing.T) {
   125  	folder, err := ioutil.TempDir("", "archivetest")
   126  	assert.NoError(t, err)
   127  	var dist = filepath.Join(folder, "dist")
   128  	assert.NoError(t, os.Mkdir(dist, 0755))
   129  	assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
   130  	var ctx = context.New(config.Project{
   131  		Dist: dist,
   132  		NFPM: config.NFPM{
   133  			Formats: []string{"deb", "rpm"},
   134  			Files: map[string]string{
   135  				"testdata/testfile.txt": "/var/lib/test/testfile.txt",
   136  			},
   137  		},
   138  	})
   139  	ctx.Version = "1.0.0"
   140  	ctx.Artifacts.Add(artifact.Artifact{
   141  		Name:   "mybin",
   142  		Path:   filepath.Join(dist, "mybin", "mybin"),
   143  		Goos:   "linux",
   144  		Goarch: "amd64",
   145  		Type:   artifact.Binary,
   146  	})
   147  	assert.Contains(t, Pipe{}.Run(ctx).Error(), `dist/mybin/mybin: no such file or directory`)
   148  }
   149  
   150  func TestDefault(t *testing.T) {
   151  	var ctx = &context.Context{
   152  		Config: config.Project{
   153  			NFPM: config.NFPM{},
   154  		},
   155  	}
   156  	assert.NoError(t, Pipe{}.Default(ctx))
   157  	assert.Equal(t, "/usr/local/bin", ctx.Config.NFPM.Bindir)
   158  	assert.Equal(t, defaultNameTemplate, ctx.Config.NFPM.NameTemplate)
   159  }
   160  
   161  func TestDefaultSet(t *testing.T) {
   162  	var ctx = &context.Context{
   163  		Config: config.Project{
   164  			NFPM: config.NFPM{
   165  				Bindir:       "/bin",
   166  				NameTemplate: "foo",
   167  			},
   168  		},
   169  	}
   170  	assert.NoError(t, Pipe{}.Default(ctx))
   171  	assert.Equal(t, "/bin", ctx.Config.NFPM.Bindir)
   172  	assert.Equal(t, "foo", ctx.Config.NFPM.NameTemplate)
   173  }