github.com/windmeup/goreleaser@v1.21.95/internal/pipe/gomod/gomod_test.go (about) 1 package gomod 2 3 import ( 4 "os" 5 "os/exec" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 "github.com/windmeup/goreleaser/internal/testctx" 12 "github.com/windmeup/goreleaser/internal/testlib" 13 "github.com/windmeup/goreleaser/pkg/config" 14 ) 15 16 func TestRun(t *testing.T) { 17 ctx := testctx.New() 18 require.NoError(t, Pipe{}.Default(ctx)) 19 require.NoError(t, Pipe{}.Run(ctx)) 20 require.Equal(t, "github.com/windmeup/goreleaser", ctx.ModulePath) 21 } 22 23 func TestRunGoWork(t *testing.T) { 24 dir := testlib.Mktmp(t) 25 require.NoError(t, os.WriteFile( 26 filepath.Join(dir, "go.mod"), 27 []byte("module a"), 28 0o666, 29 )) 30 require.NoError(t, os.Mkdir(filepath.Join(dir, "b"), 0o755)) 31 require.NoError(t, os.WriteFile( 32 filepath.Join(dir, "b", "go.mod"), 33 []byte("module a/b"), 34 0o666, 35 )) 36 require.NoError(t, os.WriteFile( 37 filepath.Join(dir, "go.work"), 38 []byte("use (\n\t.\n\tb\n)"), 39 0o666, 40 )) 41 out, err := exec.Command("go", "list", "-m").CombinedOutput() 42 require.NoError(t, err) 43 require.Equal(t, "a\na/b", strings.TrimSpace(string(out))) 44 ctx := testctx.New() 45 require.NoError(t, Pipe{}.Default(ctx)) 46 require.NoError(t, Pipe{}.Run(ctx)) 47 require.Equal(t, "a", ctx.ModulePath) 48 } 49 50 func TestRunCustomMod(t *testing.T) { 51 ctx := testctx.NewWithCfg(config.Project{ 52 GoMod: config.GoMod{ 53 Mod: "readonly", 54 }, 55 }) 56 require.NoError(t, Pipe{}.Default(ctx)) 57 require.NoError(t, Pipe{}.Run(ctx)) 58 require.Equal(t, "github.com/windmeup/goreleaser", ctx.ModulePath) 59 } 60 61 func TestCustomEnv(t *testing.T) { 62 bin := filepath.Join(t.TempDir(), "go.bin") 63 require.NoError(t, os.WriteFile( 64 bin, 65 []byte("#!/bin/sh\nenv | grep -qw FOO=bar"), 66 0o755, 67 )) 68 ctx := testctx.NewWithCfg(config.Project{ 69 GoMod: config.GoMod{ 70 GoBinary: bin, 71 Env: []string{"FOO=bar"}, 72 }, 73 }) 74 require.NoError(t, Pipe{}.Default(ctx)) 75 require.NoError(t, Pipe{}.Run(ctx)) 76 } 77 78 func TestRunOutsideGoModule(t *testing.T) { 79 dir := testlib.Mktmp(t) 80 require.NoError(t, os.WriteFile( 81 filepath.Join(dir, "main.go"), 82 []byte("package main\nfunc main() {println(0)}"), 83 0o666, 84 )) 85 ctx := testctx.New() 86 require.NoError(t, Pipe{}.Default(ctx)) 87 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 88 require.Empty(t, ctx.ModulePath) 89 } 90 91 func TestRunCommandError(t *testing.T) { 92 ctx := testctx.NewWithCfg(config.Project{ 93 GoMod: config.GoMod{ 94 GoBinary: "not-a-valid-binary", 95 }, 96 }) 97 require.EqualError( 98 t, 99 Pipe{}.Run(ctx), 100 `failed to get module path: exec: "not-a-valid-binary": executable file not found in $PATH: `, 101 ) 102 require.Empty(t, ctx.ModulePath) 103 } 104 105 func TestRunOldGoVersion(t *testing.T) { 106 bin := filepath.Join(t.TempDir(), "go.bin") 107 require.NoError(t, os.WriteFile( 108 bin, 109 []byte("#!/bin/sh\necho \"flag provided but not defined: -m\"\nexit 1"), 110 0o755, 111 )) 112 ctx := testctx.NewWithCfg(config.Project{ 113 GoMod: config.GoMod{ 114 GoBinary: bin, 115 }, 116 }) 117 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 118 require.Empty(t, ctx.ModulePath) 119 } 120 121 func TestDescription(t *testing.T) { 122 require.NotEmpty(t, Pipe{}.String()) 123 }