github.com/tomsquest/goreleaser@v0.34.3-0.20171008022654-7d6ef4d338b3/pipeline/build/build_test.go (about)

     1  package build
     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/buildtarget"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  var emptyEnv []string
    17  
    18  func TestPipeDescription(t *testing.T) {
    19  	assert.NotEmpty(t, Pipe{}.Description())
    20  }
    21  
    22  func TestRun(t *testing.T) {
    23  	assert.NoError(t, run(buildtarget.Runtime, []string{"go", "list", "./..."}, emptyEnv))
    24  }
    25  
    26  func TestRunInvalidCommand(t *testing.T) {
    27  	assert.Error(t, run(buildtarget.Runtime, []string{"gggggo", "nope"}, emptyEnv))
    28  }
    29  
    30  func TestBuild(t *testing.T) {
    31  	var config = config.Project{
    32  		Builds: []config.Build{
    33  			{
    34  				Binary: "testing",
    35  				Flags:  "-n",
    36  				Env:    []string{"BLAH=1"},
    37  			},
    38  		},
    39  	}
    40  	var ctx = context.New(config)
    41  	assert.NoError(t, doBuild(ctx, ctx.Config.Builds[0], buildtarget.Runtime))
    42  }
    43  
    44  func TestRunFullPipe(t *testing.T) {
    45  	folder, err := ioutil.TempDir("", "goreleasertest")
    46  	assert.NoError(t, err)
    47  	var binary = filepath.Join(folder, "testing")
    48  	var pre = filepath.Join(folder, "pre")
    49  	var post = filepath.Join(folder, "post")
    50  	var config = config.Project{
    51  		Dist: folder,
    52  		Builds: []config.Build{
    53  			{
    54  				Binary:  "testing",
    55  				Flags:   "-v",
    56  				Ldflags: "-X main.test=testing",
    57  				Hooks: config.Hooks{
    58  					Pre:  "touch " + pre,
    59  					Post: "touch " + post,
    60  				},
    61  				Goos: []string{
    62  					runtime.GOOS,
    63  				},
    64  				Goarch: []string{
    65  					runtime.GOARCH,
    66  				},
    67  			},
    68  		},
    69  	}
    70  	assert.NoError(t, Pipe{}.Run(context.New(config)))
    71  	assert.True(t, exists(binary), binary)
    72  	assert.True(t, exists(pre), pre)
    73  	assert.True(t, exists(post), post)
    74  }
    75  
    76  func TestRunPipeFormatBinary(t *testing.T) {
    77  	folder, err := ioutil.TempDir("", "goreleasertest")
    78  	assert.NoError(t, err)
    79  	var binary = filepath.Join(folder, "binary-testing")
    80  	var config = config.Project{
    81  		ProjectName: "testing",
    82  		Dist:        folder,
    83  		Builds: []config.Build{
    84  			{
    85  				Binary: "testing",
    86  				Goos: []string{
    87  					runtime.GOOS,
    88  				},
    89  				Goarch: []string{
    90  					runtime.GOARCH,
    91  				},
    92  			},
    93  		},
    94  		Archive: config.Archive{
    95  			Format:       "binary",
    96  			NameTemplate: "binary-{{.Binary}}",
    97  		},
    98  	}
    99  	assert.NoError(t, Pipe{}.Run(context.New(config)))
   100  	assert.True(t, exists(binary))
   101  }
   102  
   103  func TestRunPipeArmBuilds(t *testing.T) {
   104  	folder, err := ioutil.TempDir("", "goreleasertest")
   105  	assert.NoError(t, err)
   106  	var binary = filepath.Join(folder, "armtesting")
   107  	var config = config.Project{
   108  		Dist: folder,
   109  		Builds: []config.Build{
   110  			{
   111  				Binary:  "armtesting",
   112  				Flags:   "-v",
   113  				Ldflags: "-X main.test=armtesting",
   114  				Goos: []string{
   115  					"linux",
   116  				},
   117  				Goarch: []string{
   118  					"arm",
   119  					"arm64",
   120  				},
   121  				Goarm: []string{
   122  					"6",
   123  				},
   124  			},
   125  		},
   126  	}
   127  	assert.NoError(t, Pipe{}.Run(context.New(config)))
   128  	assert.True(t, exists(binary), binary)
   129  }
   130  
   131  func TestBuildFailed(t *testing.T) {
   132  	var config = config.Project{
   133  		Builds: []config.Build{
   134  			{
   135  				Flags: "-flag-that-dont-exists-to-force-failure",
   136  				Goos: []string{
   137  					runtime.GOOS,
   138  				},
   139  				Goarch: []string{
   140  					runtime.GOARCH,
   141  				},
   142  			},
   143  		},
   144  	}
   145  	assert.Error(t, Pipe{}.Run(context.New(config)))
   146  }
   147  
   148  func TestRunPipeWithInvalidOS(t *testing.T) {
   149  	var config = config.Project{
   150  		Builds: []config.Build{
   151  			{
   152  				Flags: "-v",
   153  				Goos: []string{
   154  					"windows",
   155  				},
   156  				Goarch: []string{
   157  					"arm",
   158  				},
   159  			},
   160  		},
   161  	}
   162  	assert.NoError(t, Pipe{}.Run(context.New(config)))
   163  }
   164  
   165  func TestRunInvalidNametemplate(t *testing.T) {
   166  	for _, format := range []string{"tar.gz", "zip", "binary"} {
   167  		var config = config.Project{
   168  			ProjectName: "nameeeee",
   169  			Builds: []config.Build{
   170  				{
   171  					Binary: "namet{{.est}",
   172  					Flags:  "-v",
   173  					Goos: []string{
   174  						runtime.GOOS,
   175  					},
   176  					Goarch: []string{
   177  						runtime.GOARCH,
   178  					},
   179  				},
   180  			},
   181  			Archive: config.Archive{
   182  				Format:       format,
   183  				NameTemplate: "{{.Binary}",
   184  			},
   185  		}
   186  		assert.Error(t, Pipe{}.Run(context.New(config)))
   187  	}
   188  }
   189  
   190  func TestRunInvalidLdflags(t *testing.T) {
   191  	var config = config.Project{
   192  		Builds: []config.Build{
   193  			{
   194  				Binary:  "nametest",
   195  				Flags:   "-v",
   196  				Ldflags: "-s -w -X main.version={{.Version}",
   197  				Goos: []string{
   198  					runtime.GOOS,
   199  				},
   200  				Goarch: []string{
   201  					runtime.GOARCH,
   202  				},
   203  			},
   204  		},
   205  	}
   206  	assert.Error(t, Pipe{}.Run(context.New(config)))
   207  }
   208  
   209  func TestRunPipeFailingHooks(t *testing.T) {
   210  	var config = config.Project{
   211  		Builds: []config.Build{
   212  			{
   213  				Hooks: config.Hooks{},
   214  				Goos: []string{
   215  					runtime.GOOS,
   216  				},
   217  				Goarch: []string{
   218  					runtime.GOARCH,
   219  				},
   220  			},
   221  		},
   222  	}
   223  	var ctx = context.New(config)
   224  	t.Run("pre-hook", func(t *testing.T) {
   225  		ctx.Config.Builds[0].Hooks.Pre = "exit 1"
   226  		assert.Error(t, Pipe{}.Run(ctx))
   227  	})
   228  	t.Run("post-hook", func(t *testing.T) {
   229  		ctx.Config.Builds[0].Hooks.Post = "exit 1"
   230  		assert.Error(t, Pipe{}.Run(ctx))
   231  	})
   232  }
   233  
   234  func exists(file string) bool {
   235  	_, err := os.Stat(file)
   236  	return !os.IsNotExist(err)
   237  }