github.com/triarius/goreleaser@v1.12.5/internal/pipe/git/git_test.go (about)

     1  package git
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/triarius/goreleaser/internal/testlib"
    10  	"github.com/triarius/goreleaser/pkg/config"
    11  	"github.com/triarius/goreleaser/pkg/context"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestDescription(t *testing.T) {
    16  	require.NotEmpty(t, Pipe{}.String())
    17  }
    18  
    19  func TestNotAGitFolder(t *testing.T) {
    20  	testlib.Mktmp(t)
    21  	ctx := context.New(config.Project{})
    22  	require.EqualError(t, Pipe{}.Run(ctx), ErrNotRepository.Error())
    23  }
    24  
    25  func TestSingleCommit(t *testing.T) {
    26  	testlib.Mktmp(t)
    27  	testlib.GitInit(t)
    28  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
    29  	testlib.GitCommit(t, "commit1")
    30  	testlib.GitTag(t, "v0.0.1")
    31  	ctx := context.New(config.Project{})
    32  	require.NoError(t, Pipe{}.Run(ctx))
    33  	require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
    34  	require.Equal(t, "v0.0.1", ctx.Git.Summary)
    35  	require.Equal(t, "commit1", ctx.Git.TagSubject)
    36  	require.Equal(t, "commit1", ctx.Git.TagContents)
    37  }
    38  
    39  func TestAnnotatedTags(t *testing.T) {
    40  	testlib.Mktmp(t)
    41  	testlib.GitInit(t)
    42  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
    43  	testlib.GitCommit(t, "commit1")
    44  	testlib.GitAnnotatedTag(t, "v0.0.1", "first version\n\nlalalla\nlalal\nlah")
    45  	ctx := context.New(config.Project{})
    46  	require.NoError(t, Pipe{}.Run(ctx))
    47  	require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
    48  	require.Equal(t, "first version", ctx.Git.TagSubject)
    49  	require.Equal(t, "first version\n\nlalalla\nlalal\nlah", ctx.Git.TagContents)
    50  	require.Equal(t, "lalalla\nlalal\nlah", ctx.Git.TagBody)
    51  	require.Equal(t, "v0.0.1", ctx.Git.Summary)
    52  }
    53  
    54  func TestBranch(t *testing.T) {
    55  	testlib.Mktmp(t)
    56  	testlib.GitInit(t)
    57  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
    58  	testlib.GitCommit(t, "test-branch-commit")
    59  	testlib.GitTag(t, "test-branch-tag")
    60  	testlib.GitCheckoutBranch(t, "test-branch")
    61  	ctx := context.New(config.Project{})
    62  	require.NoError(t, Pipe{}.Run(ctx))
    63  	require.Equal(t, "test-branch", ctx.Git.Branch)
    64  	require.Equal(t, "test-branch-tag", ctx.Git.Summary)
    65  }
    66  
    67  func TestNoRemote(t *testing.T) {
    68  	testlib.Mktmp(t)
    69  	testlib.GitInit(t)
    70  	testlib.GitCommit(t, "commit1")
    71  	testlib.GitTag(t, "v0.0.1")
    72  	ctx := context.New(config.Project{})
    73  	require.EqualError(t, Pipe{}.Run(ctx), "couldn't get remote URL: fatal: No remote configured to list refs from.")
    74  }
    75  
    76  func TestNewRepository(t *testing.T) {
    77  	testlib.Mktmp(t)
    78  	testlib.GitInit(t)
    79  	ctx := context.New(config.Project{})
    80  	// TODO: improve this error handling
    81  	require.Contains(t, Pipe{}.Run(ctx).Error(), `fatal: ambiguous argument 'HEAD'`)
    82  }
    83  
    84  // TestNoTagsNoSnapshot covers the situation where a repository
    85  // only contains simple commits and no tags. In this case you have
    86  // to set the --snapshot flag otherwise an error is returned.
    87  func TestNoTagsNoSnapshot(t *testing.T) {
    88  	testlib.Mktmp(t)
    89  	testlib.GitInit(t)
    90  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
    91  	testlib.GitCommit(t, "first")
    92  	ctx := context.New(config.Project{})
    93  	ctx.Snapshot = false
    94  	require.EqualError(t, Pipe{}.Run(ctx), `git doesn't contain any tags. Either add a tag or use --snapshot`)
    95  }
    96  
    97  func TestDirty(t *testing.T) {
    98  	folder := testlib.Mktmp(t)
    99  	testlib.GitInit(t)
   100  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   101  	dummy, err := os.Create(filepath.Join(folder, "dummy"))
   102  	require.NoError(t, err)
   103  	require.NoError(t, dummy.Close())
   104  	testlib.GitAdd(t)
   105  	testlib.GitCommit(t, "commit2")
   106  	testlib.GitTag(t, "v0.0.1")
   107  	require.NoError(t, os.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0o644))
   108  	t.Run("all checks up", func(t *testing.T) {
   109  		err := Pipe{}.Run(context.New(config.Project{}))
   110  		require.Error(t, err)
   111  		require.Contains(t, err.Error(), "git is in a dirty state")
   112  	})
   113  	t.Run("skip validate is set", func(t *testing.T) {
   114  		ctx := context.New(config.Project{})
   115  		ctx.SkipValidate = true
   116  		testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   117  	})
   118  	t.Run("snapshot", func(t *testing.T) {
   119  		ctx := context.New(config.Project{})
   120  		ctx.Snapshot = true
   121  		testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   122  	})
   123  }
   124  
   125  func TestRemoteURLContainsWithUsernameAndToken(t *testing.T) {
   126  	testlib.Mktmp(t)
   127  	testlib.GitInit(t)
   128  	testlib.GitRemoteAdd(t, "https://gitlab-ci-token:SyYhsAghYFTvMoxw7GAg@gitlab.private.com/platform/base/poc/kink.git/releases/tag/v0.1.4")
   129  	testlib.GitAdd(t)
   130  	testlib.GitCommit(t, "commit2")
   131  	testlib.GitTag(t, "v0.0.1")
   132  	ctx := context.New(config.Project{})
   133  	require.NoError(t, Pipe{}.Run(ctx))
   134  }
   135  
   136  func TestRemoteURLContainsWithUsernameAndTokenWithInvalidURL(t *testing.T) {
   137  	testlib.Mktmp(t)
   138  	testlib.GitInit(t)
   139  	testlib.GitRemoteAdd(t, "https://gitlab-ci-token:SyYhsAghYFTvMoxw7GAggitlab.com/platform/base/poc/kink.git/releases/tag/v0.1.4")
   140  	testlib.GitAdd(t)
   141  	testlib.GitCommit(t, "commit2")
   142  	testlib.GitTag(t, "v0.0.1")
   143  	ctx := context.New(config.Project{})
   144  	require.Error(t, Pipe{}.Run(ctx))
   145  }
   146  
   147  func TestShallowClone(t *testing.T) {
   148  	folder := testlib.Mktmp(t)
   149  	require.NoError(
   150  		t,
   151  		exec.Command(
   152  			"git", "clone",
   153  			"--depth", "1",
   154  			"--branch", "v0.160.0",
   155  			"https://github.com/triarius/goreleaser",
   156  			folder,
   157  		).Run(),
   158  	)
   159  	t.Run("all checks up", func(t *testing.T) {
   160  		// its just a warning now
   161  		require.NoError(t, Pipe{}.Run(context.New(config.Project{})))
   162  	})
   163  	t.Run("skip validate is set", func(t *testing.T) {
   164  		ctx := context.New(config.Project{})
   165  		ctx.SkipValidate = true
   166  		testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   167  	})
   168  	t.Run("snapshot", func(t *testing.T) {
   169  		ctx := context.New(config.Project{})
   170  		ctx.Snapshot = true
   171  		testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   172  	})
   173  }
   174  
   175  func TestTagIsNotLastCommit(t *testing.T) {
   176  	testlib.Mktmp(t)
   177  	testlib.GitInit(t)
   178  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   179  	testlib.GitCommit(t, "commit3")
   180  	testlib.GitTag(t, "v0.0.1")
   181  	testlib.GitCommit(t, "commit4")
   182  	ctx := context.New(config.Project{})
   183  	err := Pipe{}.Run(ctx)
   184  	require.Error(t, err)
   185  	require.Contains(t, err.Error(), "git tag v0.0.1 was not made against commit")
   186  	require.Contains(t, ctx.Git.Summary, "v0.0.1-1-g") // commit not represented because it changes every test
   187  }
   188  
   189  func TestValidState(t *testing.T) {
   190  	testlib.Mktmp(t)
   191  	testlib.GitInit(t)
   192  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   193  	testlib.GitCommit(t, "commit3")
   194  	testlib.GitTag(t, "v0.0.1")
   195  	testlib.GitTag(t, "v0.0.2")
   196  	testlib.GitCommit(t, "commit4")
   197  	testlib.GitTag(t, "v0.0.3")
   198  	ctx := context.New(config.Project{})
   199  	require.NoError(t, Pipe{}.Run(ctx))
   200  	require.Equal(t, "v0.0.2", ctx.Git.PreviousTag)
   201  	require.Equal(t, "v0.0.3", ctx.Git.CurrentTag)
   202  	require.Equal(t, "git@github.com:foo/bar.git", ctx.Git.URL)
   203  }
   204  
   205  func TestSnapshotNoTags(t *testing.T) {
   206  	testlib.Mktmp(t)
   207  	testlib.GitInit(t)
   208  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   209  	testlib.GitAdd(t)
   210  	testlib.GitCommit(t, "whatever")
   211  	ctx := context.New(config.Project{})
   212  	ctx.Snapshot = true
   213  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   214  	require.Equal(t, fakeInfo.CurrentTag, ctx.Git.CurrentTag)
   215  	require.Empty(t, ctx.Git.PreviousTag)
   216  }
   217  
   218  func TestSnapshotNoCommits(t *testing.T) {
   219  	testlib.Mktmp(t)
   220  	testlib.GitInit(t)
   221  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   222  	ctx := context.New(config.Project{})
   223  	ctx.Snapshot = true
   224  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   225  	require.Equal(t, fakeInfo, ctx.Git)
   226  }
   227  
   228  func TestSnapshotWithoutRepo(t *testing.T) {
   229  	testlib.Mktmp(t)
   230  	ctx := context.New(config.Project{})
   231  	ctx.Snapshot = true
   232  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   233  	require.Equal(t, fakeInfo, ctx.Git)
   234  }
   235  
   236  func TestSnapshotDirty(t *testing.T) {
   237  	folder := testlib.Mktmp(t)
   238  	testlib.GitInit(t)
   239  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   240  	testlib.GitAdd(t)
   241  	testlib.GitCommit(t, "whatever")
   242  	testlib.GitTag(t, "v0.0.1")
   243  	require.NoError(t, os.WriteFile(filepath.Join(folder, "foo"), []byte("foobar"), 0o644))
   244  	ctx := context.New(config.Project{})
   245  	ctx.Snapshot = true
   246  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   247  	require.Equal(t, "v0.0.1", ctx.Git.Summary)
   248  }
   249  
   250  func TestGitNotInPath(t *testing.T) {
   251  	path := os.Getenv("PATH")
   252  	defer func() {
   253  		require.NoError(t, os.Setenv("PATH", path))
   254  	}()
   255  	require.NoError(t, os.Setenv("PATH", ""))
   256  	require.EqualError(t, Pipe{}.Run(context.New(config.Project{})), ErrNoGit.Error())
   257  }
   258  
   259  func TestTagFromCI(t *testing.T) {
   260  	testlib.Mktmp(t)
   261  	testlib.GitInit(t)
   262  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   263  	testlib.GitCommit(t, "commit1")
   264  	testlib.GitTag(t, "v0.0.1")
   265  	testlib.GitTag(t, "v0.0.2")
   266  
   267  	for _, tc := range []struct {
   268  		envs     map[string]string
   269  		expected string
   270  	}{
   271  		{expected: "v0.0.2"},
   272  		{
   273  			envs:     map[string]string{"GORELEASER_CURRENT_TAG": "v0.0.2"},
   274  			expected: "v0.0.2",
   275  		},
   276  	} {
   277  		for name, value := range tc.envs {
   278  			require.NoError(t, os.Setenv(name, value))
   279  		}
   280  
   281  		ctx := context.New(config.Project{})
   282  		require.NoError(t, Pipe{}.Run(ctx))
   283  		require.Equal(t, tc.expected, ctx.Git.CurrentTag)
   284  
   285  		for name := range tc.envs {
   286  			require.NoError(t, os.Setenv(name, ""))
   287  		}
   288  	}
   289  }
   290  
   291  func TestNoPreviousTag(t *testing.T) {
   292  	testlib.Mktmp(t)
   293  	testlib.GitInit(t)
   294  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   295  	testlib.GitCommit(t, "commit1")
   296  	testlib.GitTag(t, "v0.0.1")
   297  	ctx := context.New(config.Project{})
   298  	require.NoError(t, Pipe{}.Run(ctx))
   299  	require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
   300  	require.Empty(t, ctx.Git.PreviousTag, "should be empty")
   301  }
   302  
   303  func TestPreviousTagFromCI(t *testing.T) {
   304  	testlib.Mktmp(t)
   305  	testlib.GitInit(t)
   306  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   307  	testlib.GitCommit(t, "commit1")
   308  	testlib.GitTag(t, "v0.0.1")
   309  	testlib.GitCommit(t, "commit2")
   310  	testlib.GitTag(t, "v0.0.2")
   311  
   312  	for _, tc := range []struct {
   313  		envs     map[string]string
   314  		expected string
   315  	}{
   316  		{expected: "v0.0.1"},
   317  		{
   318  			envs:     map[string]string{"GORELEASER_PREVIOUS_TAG": "v0.0.2"},
   319  			expected: "v0.0.2",
   320  		},
   321  	} {
   322  		t.Run(tc.expected, func(t *testing.T) {
   323  			for name, value := range tc.envs {
   324  				require.NoError(t, os.Setenv(name, value))
   325  			}
   326  
   327  			ctx := context.New(config.Project{})
   328  			require.NoError(t, Pipe{}.Run(ctx))
   329  			require.Equal(t, tc.expected, ctx.Git.PreviousTag)
   330  
   331  			for name := range tc.envs {
   332  				require.NoError(t, os.Setenv(name, ""))
   333  			}
   334  		})
   335  	}
   336  }