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

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