github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/internal/pipe/gomod/gomod_proxy_test.go (about)

     1  package gomod
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"testing"
     9  
    10  	"github.com/goreleaser/goreleaser/internal/testlib"
    11  	"github.com/goreleaser/goreleaser/pkg/config"
    12  	"github.com/goreleaser/goreleaser/pkg/context"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestGoModProxy(t *testing.T) {
    17  	t.Run("goreleaser", func(t *testing.T) {
    18  		dir := testlib.Mktmp(t)
    19  		dist := filepath.Join(dir, "dist")
    20  		ctx := context.New(config.Project{
    21  			Dist: dist,
    22  			GoMod: config.GoMod{
    23  				Proxy:    true,
    24  				GoBinary: "go",
    25  			},
    26  			Builds: []config.Build{
    27  				{
    28  					ID:     "foo",
    29  					Goos:   []string{runtime.GOOS},
    30  					Goarch: []string{runtime.GOARCH},
    31  					Main:   ".",
    32  					Dir:    ".",
    33  				},
    34  			},
    35  		})
    36  		ctx.Git.CurrentTag = "v0.161.1"
    37  
    38  		ctx.ModulePath = "github.com/goreleaser/goreleaser"
    39  
    40  		fakeGoModAndSum(t, ctx.ModulePath)
    41  		require.NoError(t, ProxyPipe{}.Run(ctx))
    42  		requireGoMod(t, ctx.ModulePath, ctx.Git.CurrentTag)
    43  		requireMainGo(t, ctx.ModulePath)
    44  		require.Equal(t, ctx.ModulePath, ctx.Config.Builds[0].Main)
    45  		require.Equal(t, ".", ctx.Config.Builds[0].UnproxiedMain)
    46  		require.Equal(t, filepath.Join(dist, "proxy", "foo"), ctx.Config.Builds[0].Dir)
    47  		require.Equal(t, ".", ctx.Config.Builds[0].UnproxiedDir)
    48  		require.Equal(t, ctx.ModulePath, ctx.ModulePath)
    49  	})
    50  
    51  	t.Run("nfpm", func(t *testing.T) {
    52  		dir := testlib.Mktmp(t)
    53  		dist := filepath.Join(dir, "dist")
    54  		ctx := context.New(config.Project{
    55  			Dist: dist,
    56  			GoMod: config.GoMod{
    57  				Proxy:    true,
    58  				GoBinary: "go",
    59  			},
    60  			Builds: []config.Build{
    61  				{
    62  					ID:     "foo",
    63  					Goos:   []string{runtime.GOOS},
    64  					Goarch: []string{runtime.GOARCH},
    65  					Main:   "./cmd/nfpm",
    66  				},
    67  			},
    68  		})
    69  		ctx.Git.CurrentTag = "v2.3.1"
    70  
    71  		ctx.ModulePath = "github.com/goreleaser/nfpm/v2"
    72  		fakeGoModAndSum(t, ctx.ModulePath)
    73  		require.NoError(t, ProxyPipe{}.Run(ctx))
    74  		requireGoMod(t, ctx.ModulePath, ctx.Git.CurrentTag)
    75  		requireMainGo(t, ctx.ModulePath+"/cmd/nfpm")
    76  		require.Equal(t, ctx.ModulePath+"/cmd/nfpm", ctx.Config.Builds[0].Main)
    77  		require.Equal(t, filepath.Join(dist, "proxy", "foo"), ctx.Config.Builds[0].Dir)
    78  		require.Equal(t, ctx.ModulePath, ctx.ModulePath)
    79  	})
    80  
    81  	// this repo does not have a go.sum file, which is ok, a project might not have any dependencies
    82  	t.Run("no go.sum", func(t *testing.T) {
    83  		dir := testlib.Mktmp(t)
    84  		dist := filepath.Join(dir, "dist")
    85  		ctx := context.New(config.Project{
    86  			Dist: dist,
    87  			GoMod: config.GoMod{
    88  				Proxy:    true,
    89  				GoBinary: "go",
    90  			},
    91  			Builds: []config.Build{
    92  				{
    93  					ID:     "foo",
    94  					Goos:   []string{runtime.GOOS},
    95  					Goarch: []string{runtime.GOARCH},
    96  				},
    97  			},
    98  		})
    99  		ctx.Git.CurrentTag = "v0.0.1"
   100  
   101  		ctx.ModulePath = "github.com/goreleaser/example-mod-proxy"
   102  		fakeGoMod(t, ctx.ModulePath)
   103  		require.NoError(t, ProxyPipe{}.Run(ctx))
   104  		requireGoMod(t, ctx.ModulePath, ctx.Git.CurrentTag)
   105  		requireMainGo(t, ctx.ModulePath)
   106  		require.Equal(t, ctx.ModulePath, ctx.Config.Builds[0].Main)
   107  		require.Equal(t, filepath.Join(dist, "proxy", "foo"), ctx.Config.Builds[0].Dir)
   108  		require.Equal(t, ctx.ModulePath, ctx.ModulePath)
   109  	})
   110  
   111  	t.Run("no perms", func(t *testing.T) {
   112  		for file, mode := range map[string]os.FileMode{
   113  			"go.mod":          0o500,
   114  			"go.sum":          0o500,
   115  			"main.go":         0o500,
   116  			"../../../go.sum": 0o300,
   117  		} {
   118  			t.Run(file, func(t *testing.T) {
   119  				dir := testlib.Mktmp(t)
   120  				dist := filepath.Join(dir, "dist")
   121  				ctx := context.New(config.Project{
   122  					Dist: dist,
   123  					GoMod: config.GoMod{
   124  						Proxy:    true,
   125  						GoBinary: "go",
   126  					},
   127  					Builds: []config.Build{
   128  						{
   129  							ID:     "foo",
   130  							Goos:   []string{runtime.GOOS},
   131  							Goarch: []string{runtime.GOARCH},
   132  						},
   133  					},
   134  				})
   135  				ctx.Git.CurrentTag = "v0.161.1"
   136  
   137  				ctx.ModulePath = "github.com/goreleaser/goreleaser"
   138  
   139  				fakeGoModAndSum(t, ctx.ModulePath)
   140  				require.NoError(t, ProxyPipe{}.Run(ctx)) // should succeed at first
   141  
   142  				// change perms of a file and run again, which should now fail on that file.
   143  				require.NoError(t, os.Chmod(filepath.Join(dist, "proxy", "foo", file), mode))
   144  				require.ErrorAs(t, ProxyPipe{}.Run(ctx), &ErrProxy{})
   145  			})
   146  		}
   147  	})
   148  
   149  	t.Run("goreleaser with main.go", func(t *testing.T) {
   150  		dir := testlib.Mktmp(t)
   151  		dist := filepath.Join(dir, "dist")
   152  		ctx := context.New(config.Project{
   153  			Dist: dist,
   154  			GoMod: config.GoMod{
   155  				Proxy:    true,
   156  				GoBinary: "go",
   157  			},
   158  			Builds: []config.Build{
   159  				{
   160  					ID:     "foo",
   161  					Goos:   []string{runtime.GOOS},
   162  					Goarch: []string{runtime.GOARCH},
   163  					Main:   "main.go",
   164  				},
   165  			},
   166  		})
   167  		ctx.Git.CurrentTag = "v0.161.1"
   168  
   169  		ctx.ModulePath = "github.com/goreleaser/goreleaser"
   170  
   171  		fakeGoModAndSum(t, ctx.ModulePath)
   172  		require.NoError(t, ProxyPipe{}.Run(ctx))
   173  		requireGoMod(t, ctx.ModulePath, ctx.Git.CurrentTag)
   174  		requireMainGo(t, ctx.ModulePath)
   175  		require.Equal(t, ctx.ModulePath, ctx.Config.Builds[0].Main)
   176  		require.Equal(t, filepath.Join(dist, "proxy", "foo"), ctx.Config.Builds[0].Dir)
   177  		require.Equal(t, ctx.ModulePath, ctx.ModulePath)
   178  	})
   179  }
   180  
   181  func TestProxyDescription(t *testing.T) {
   182  	require.NotEmpty(t, ProxyPipe{}.String())
   183  }
   184  
   185  func TestSkip(t *testing.T) {
   186  	t.Run("skip false gomod.proxy", func(t *testing.T) {
   187  		require.True(t, ProxyPipe{}.Skip(context.New(config.Project{})))
   188  	})
   189  
   190  	t.Run("skip snapshot", func(t *testing.T) {
   191  		ctx := context.New(config.Project{
   192  			GoMod: config.GoMod{
   193  				Proxy: true,
   194  			},
   195  		})
   196  		ctx.ModulePath = "github.com/goreleaser/goreleaser"
   197  		ctx.Snapshot = true
   198  		require.True(t, ProxyPipe{}.Skip(ctx))
   199  	})
   200  
   201  	t.Run("skip not a go module", func(t *testing.T) {
   202  		ctx := context.New(config.Project{
   203  			GoMod: config.GoMod{
   204  				Proxy: true,
   205  			},
   206  		})
   207  		ctx.ModulePath = ""
   208  		require.True(t, ProxyPipe{}.Skip(ctx))
   209  	})
   210  
   211  	t.Run("dont skip", func(t *testing.T) {
   212  		ctx := context.New(config.Project{
   213  			GoMod: config.GoMod{
   214  				Proxy: true,
   215  			},
   216  		})
   217  		ctx.ModulePath = "github.com/goreleaser/goreleaser"
   218  		require.False(t, ProxyPipe{}.Skip(ctx))
   219  	})
   220  }
   221  
   222  func requireGoMod(tb testing.TB, module, version string) {
   223  	tb.Helper()
   224  
   225  	mod, err := os.ReadFile("dist/proxy/foo/go.mod")
   226  	require.NoError(tb, err)
   227  	require.Contains(tb, string(mod), fmt.Sprintf(`module foo
   228  
   229  go 1.17
   230  
   231  require %s %s
   232  `, module, version))
   233  }
   234  
   235  func requireMainGo(tb testing.TB, module string) {
   236  	tb.Helper()
   237  
   238  	main, err := os.ReadFile("dist/proxy/foo/main.go")
   239  	require.NoError(tb, err)
   240  	require.Equal(tb, fmt.Sprintf(`
   241  // +build main
   242  package main
   243  
   244  import _ "%s"
   245  `, module), string(main))
   246  }
   247  
   248  func fakeGoModAndSum(tb testing.TB, module string) {
   249  	tb.Helper()
   250  
   251  	fakeGoMod(tb, module)
   252  	require.NoError(tb, os.WriteFile("go.sum", []byte("\n"), 0o666))
   253  }
   254  
   255  func fakeGoMod(tb testing.TB, module string) {
   256  	tb.Helper()
   257  	require.NoError(tb, os.WriteFile("go.mod", []byte(fmt.Sprintf("module %s\n", module)), 0o666))
   258  }