github.com/amane3/goreleaser@v0.182.0/internal/pipe/git/git_test.go (about)

     1  package git
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/amane3/goreleaser/internal/testlib"
    13  	"github.com/amane3/goreleaser/pkg/config"
    14  	"github.com/amane3/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  	var 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  	var 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  	var 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  	var 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  	var 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  	var 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  	var 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  	testlib.GitAdd(t)
    97  	testlib.GitCommit(t, "commit2")
    98  	testlib.GitTag(t, "v0.0.1")
    99  	require.NoError(t, ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
   100  	t.Run("all checks up", func(t *testing.T) {
   101  		err = Pipe{}.Run(context.New(config.Project{}))
   102  		require.Error(t, err)
   103  		require.Contains(t, err.Error(), "git is currently in a dirty state")
   104  	})
   105  	t.Run("skip validate is set", func(t *testing.T) {
   106  		ctx := context.New(config.Project{})
   107  		ctx.SkipValidate = true
   108  		err = Pipe{}.Run(ctx)
   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  		err = Pipe{}.Run(ctx)
   115  		testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   116  	})
   117  }
   118  
   119  func TestTagIsNotLastCommit(t *testing.T) {
   120  	testlib.Mktmp(t)
   121  	testlib.GitInit(t)
   122  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   123  	testlib.GitCommit(t, "commit3")
   124  	testlib.GitTag(t, "v0.0.1")
   125  	testlib.GitCommit(t, "commit4")
   126  	err := Pipe{}.Run(context.New(config.Project{}))
   127  	require.Error(t, err)
   128  	require.Contains(t, err.Error(), "git tag v0.0.1 was not made against commit")
   129  }
   130  
   131  func TestValidState(t *testing.T) {
   132  	testlib.Mktmp(t)
   133  	testlib.GitInit(t)
   134  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   135  	testlib.GitCommit(t, "commit3")
   136  	testlib.GitTag(t, "v0.0.1")
   137  	testlib.GitCommit(t, "commit4")
   138  	testlib.GitTag(t, "v0.0.2")
   139  	var ctx = context.New(config.Project{})
   140  	require.NoError(t, Pipe{}.Run(ctx))
   141  	require.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
   142  	require.Equal(t, "git@github.com:foo/bar.git", ctx.Git.URL)
   143  }
   144  
   145  func TestSnapshotNoTags(t *testing.T) {
   146  	testlib.Mktmp(t)
   147  	testlib.GitInit(t)
   148  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   149  	testlib.GitAdd(t)
   150  	testlib.GitCommit(t, "whatever")
   151  	var ctx = context.New(config.Project{})
   152  	ctx.Snapshot = true
   153  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   154  	require.Equal(t, fakeInfo.CurrentTag, ctx.Git.CurrentTag)
   155  }
   156  
   157  func TestSnapshotNoCommits(t *testing.T) {
   158  	testlib.Mktmp(t)
   159  	testlib.GitInit(t)
   160  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   161  	var ctx = context.New(config.Project{})
   162  	ctx.Snapshot = true
   163  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   164  	require.Equal(t, fakeInfo, ctx.Git)
   165  }
   166  
   167  func TestSnapshotWithoutRepo(t *testing.T) {
   168  	testlib.Mktmp(t)
   169  	var ctx = context.New(config.Project{})
   170  	ctx.Snapshot = true
   171  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   172  	require.Equal(t, fakeInfo, ctx.Git)
   173  }
   174  
   175  func TestSnapshotDirty(t *testing.T) {
   176  	var folder = testlib.Mktmp(t)
   177  	testlib.GitInit(t)
   178  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   179  	testlib.GitAdd(t)
   180  	testlib.GitCommit(t, "whatever")
   181  	testlib.GitTag(t, "v0.0.1")
   182  	require.NoError(t, ioutil.WriteFile(filepath.Join(folder, "foo"), []byte("foobar"), 0644))
   183  	var ctx = context.New(config.Project{})
   184  	ctx.Snapshot = true
   185  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   186  }
   187  
   188  func TestGitNotInPath(t *testing.T) {
   189  	var path = os.Getenv("PATH")
   190  	defer func() {
   191  		require.NoError(t, os.Setenv("PATH", path))
   192  	}()
   193  	require.NoError(t, os.Setenv("PATH", ""))
   194  	require.EqualError(t, Pipe{}.Run(context.New(config.Project{})), ErrNoGit.Error())
   195  }
   196  
   197  func TestTagFromCI(t *testing.T) {
   198  	testlib.Mktmp(t)
   199  	testlib.GitInit(t)
   200  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   201  	testlib.GitCommit(t, "commit1")
   202  	testlib.GitTag(t, "v0.0.1")
   203  	testlib.GitTag(t, "v0.0.2")
   204  
   205  	for _, tc := range []struct {
   206  		envs     map[string]string
   207  		expected string
   208  	}{
   209  		// It is not possible to concisely figure out the tag if a commit has more than one tags. Git always
   210  		// returns the tags in lexicographical order (ASC), which implies that we expect v0.0.1 here.
   211  		// More details: https://github.com/amane3/goreleaser/issues/1163
   212  		{expected: "v0.0.1"},
   213  		{
   214  			envs:     map[string]string{"GORELEASER_CURRENT_TAG": "v0.0.2"},
   215  			expected: "v0.0.2",
   216  		},
   217  	} {
   218  		for name, value := range tc.envs {
   219  			require.NoError(t, os.Setenv(name, value))
   220  		}
   221  
   222  		var ctx = &context.Context{
   223  			Config: config.Project{},
   224  		}
   225  		require.NoError(t, Pipe{}.Run(ctx))
   226  		require.Equal(t, tc.expected, ctx.Git.CurrentTag)
   227  
   228  		for name := range tc.envs {
   229  			require.NoError(t, os.Setenv(name, ""))
   230  		}
   231  	}
   232  }
   233  
   234  func TestCommitDate(t *testing.T) {
   235  	// round to seconds since this is expressed in a Unix timestamp
   236  	commitDate := time.Now().AddDate(-1, 0, 0).Round(1 * time.Second)
   237  
   238  	testlib.Mktmp(t)
   239  	testlib.GitInit(t)
   240  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   241  	testlib.GitCommitWithDate(t, "commit1", commitDate)
   242  	testlib.GitTag(t, "v0.0.1")
   243  	var ctx = &context.Context{
   244  		Config: config.Project{},
   245  	}
   246  	require.NoError(t, Pipe{}.Run(ctx))
   247  	require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
   248  	require.True(t, commitDate.Equal(ctx.Git.CommitDate), "commit date does not match expected")
   249  }