github.phpd.cn/goreleaser/goreleaser@v0.92.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  
     9  	"github.com/goreleaser/goreleaser/internal/testlib"
    10  	"github.com/goreleaser/goreleaser/pkg/config"
    11  	"github.com/goreleaser/goreleaser/pkg/context"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestDescription(t *testing.T) {
    16  	assert.NotEmpty(t, Pipe{}.String())
    17  }
    18  
    19  func TestNotAGitFolder(t *testing.T) {
    20  	_, back := testlib.Mktmp(t)
    21  	defer back()
    22  	var ctx = &context.Context{
    23  		Config: config.Project{},
    24  	}
    25  	assert.EqualError(t, Pipe{}.Run(ctx), ErrNotRepository.Error())
    26  }
    27  
    28  func TestSingleCommit(t *testing.T) {
    29  	_, back := testlib.Mktmp(t)
    30  	defer back()
    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  	assert.NoError(t, Pipe{}.Run(ctx))
    39  	assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
    40  }
    41  
    42  func TestNoRemote(t *testing.T) {
    43  	_, back := testlib.Mktmp(t)
    44  	defer back()
    45  	testlib.GitInit(t)
    46  	testlib.GitCommit(t, "commit1")
    47  	testlib.GitTag(t, "v0.0.1")
    48  	var ctx = &context.Context{
    49  		Config: config.Project{},
    50  	}
    51  	assert.EqualError(t, Pipe{}.Run(ctx), "couldn't get remote URL: fatal: No remote configured to list refs from.")
    52  }
    53  
    54  func TestNewRepository(t *testing.T) {
    55  	_, back := testlib.Mktmp(t)
    56  	defer back()
    57  	testlib.GitInit(t)
    58  	var ctx = &context.Context{
    59  		Config: config.Project{},
    60  	}
    61  	// TODO: improve this error handling
    62  	assert.Contains(t, Pipe{}.Run(ctx).Error(), `fatal: ambiguous argument 'HEAD'`)
    63  }
    64  
    65  func TestNoTagsSnapshot(t *testing.T) {
    66  	_, back := testlib.Mktmp(t)
    67  	defer back()
    68  	testlib.GitInit(t)
    69  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
    70  	testlib.GitCommit(t, "first")
    71  	var ctx = context.New(config.Project{
    72  		Snapshot: config.Snapshot{
    73  			NameTemplate: "SNAPSHOT-{{.Commit}}",
    74  		},
    75  	})
    76  	ctx.Snapshot = true
    77  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    78  	assert.Contains(t, ctx.Version, "SNAPSHOT-")
    79  }
    80  
    81  func TestNoTagsSnapshotInvalidTemplate(t *testing.T) {
    82  	_, back := testlib.Mktmp(t)
    83  	defer back()
    84  	testlib.GitInit(t)
    85  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
    86  	testlib.GitCommit(t, "first")
    87  	var ctx = context.New(config.Project{
    88  		Snapshot: config.Snapshot{
    89  			NameTemplate: "{{",
    90  		},
    91  	})
    92  	ctx.Snapshot = true
    93  	assert.EqualError(t, Pipe{}.Run(ctx), `failed to generate snapshot name: template: tmpl:1: unexpected unclosed action in command`)
    94  }
    95  
    96  // TestNoTagsNoSnapshot covers the situation where a repository
    97  // only contains simple commits and no tags. In this case you have
    98  // to set the --snapshot flag otherwise an error is returned.
    99  func TestNoTagsNoSnapshot(t *testing.T) {
   100  	_, back := testlib.Mktmp(t)
   101  	defer back()
   102  	testlib.GitInit(t)
   103  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   104  	testlib.GitCommit(t, "first")
   105  	var ctx = context.New(config.Project{})
   106  	ctx.Snapshot = false
   107  	assert.EqualError(t, Pipe{}.Run(ctx), `git doesn't contain any tags. Either add a tag or use --snapshot`)
   108  }
   109  
   110  func TestInvalidTagFormat(t *testing.T) {
   111  	_, back := testlib.Mktmp(t)
   112  	defer back()
   113  	testlib.GitInit(t)
   114  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   115  	testlib.GitCommit(t, "commit2")
   116  	testlib.GitTag(t, "sadasd")
   117  	var ctx = context.New(config.Project{})
   118  	assert.EqualError(t, Pipe{}.Run(ctx), "sadasd is not in a valid version format")
   119  	assert.Equal(t, "sadasd", ctx.Git.CurrentTag)
   120  }
   121  
   122  func TestDirty(t *testing.T) {
   123  	folder, back := testlib.Mktmp(t)
   124  	defer back()
   125  	testlib.GitInit(t)
   126  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   127  	dummy, err := os.Create(filepath.Join(folder, "dummy"))
   128  	assert.NoError(t, err)
   129  	testlib.GitAdd(t)
   130  	testlib.GitCommit(t, "commit2")
   131  	testlib.GitTag(t, "v0.0.1")
   132  	assert.NoError(t, ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
   133  	t.Run("all checks up", func(t *testing.T) {
   134  		err = Pipe{}.Run(context.New(config.Project{}))
   135  		assert.Error(t, err)
   136  		assert.Contains(t, err.Error(), "git is currently in a dirty state:")
   137  	})
   138  	t.Run("skip validate is set", func(t *testing.T) {
   139  		ctx := context.New(config.Project{})
   140  		ctx.SkipValidate = true
   141  		err = Pipe{}.Run(ctx)
   142  		testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   143  	})
   144  	t.Run("snapshot", func(t *testing.T) {
   145  		ctx := context.New(config.Project{})
   146  		ctx.Snapshot = true
   147  		err = Pipe{}.Run(ctx)
   148  		testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   149  	})
   150  }
   151  
   152  func TestTagIsNotLastCommit(t *testing.T) {
   153  	_, back := testlib.Mktmp(t)
   154  	defer back()
   155  	testlib.GitInit(t)
   156  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   157  	testlib.GitCommit(t, "commit3")
   158  	testlib.GitTag(t, "v0.0.1")
   159  	testlib.GitCommit(t, "commit4")
   160  	err := Pipe{}.Run(context.New(config.Project{}))
   161  	assert.Error(t, err)
   162  	assert.Contains(t, err.Error(), "git tag v0.0.1 was not made against commit")
   163  }
   164  
   165  func TestValidState(t *testing.T) {
   166  	_, back := testlib.Mktmp(t)
   167  	defer back()
   168  	testlib.GitInit(t)
   169  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   170  	testlib.GitCommit(t, "commit3")
   171  	testlib.GitTag(t, "v0.0.1")
   172  	testlib.GitCommit(t, "commit4")
   173  	testlib.GitTag(t, "v0.0.2")
   174  	var ctx = context.New(config.Project{})
   175  	assert.NoError(t, Pipe{}.Run(ctx))
   176  	assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
   177  	assert.Equal(t, "git@github.com:foo/bar.git", ctx.Git.URL)
   178  }
   179  
   180  func TestSnapshotNoTags(t *testing.T) {
   181  	_, back := testlib.Mktmp(t)
   182  	defer back()
   183  	testlib.GitInit(t)
   184  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   185  	testlib.GitAdd(t)
   186  	testlib.GitCommit(t, "whatever")
   187  	var ctx = context.New(config.Project{})
   188  	ctx.Snapshot = true
   189  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   190  	assert.Equal(t, fakeInfo.CurrentTag, ctx.Git.CurrentTag)
   191  }
   192  
   193  func TestSnapshotNoCommits(t *testing.T) {
   194  	_, back := testlib.Mktmp(t)
   195  	defer back()
   196  	testlib.GitInit(t)
   197  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   198  	var ctx = context.New(config.Project{})
   199  	ctx.Snapshot = true
   200  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   201  	assert.Equal(t, fakeInfo, ctx.Git)
   202  }
   203  
   204  func TestSnapshotWithoutRepo(t *testing.T) {
   205  	_, back := testlib.Mktmp(t)
   206  	defer back()
   207  	var ctx = context.New(config.Project{})
   208  	ctx.Snapshot = true
   209  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   210  	assert.Equal(t, fakeInfo, ctx.Git)
   211  }
   212  
   213  func TestSnapshotDirty(t *testing.T) {
   214  	folder, back := testlib.Mktmp(t)
   215  	defer back()
   216  	testlib.GitInit(t)
   217  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   218  	testlib.GitAdd(t)
   219  	testlib.GitCommit(t, "whatever")
   220  	testlib.GitTag(t, "v0.0.1")
   221  	assert.NoError(t, ioutil.WriteFile(filepath.Join(folder, "foo"), []byte("foobar"), 0644))
   222  	var ctx = context.New(config.Project{})
   223  	ctx.Snapshot = true
   224  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   225  }
   226  
   227  func TestShortCommitHash(t *testing.T) {
   228  	_, back := testlib.Mktmp(t)
   229  	defer back()
   230  	testlib.GitInit(t)
   231  	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
   232  	testlib.GitCommit(t, "first")
   233  	var ctx = context.New(config.Project{
   234  		Snapshot: config.Snapshot{
   235  			NameTemplate: "{{.Commit}}",
   236  		},
   237  	})
   238  	ctx.Snapshot = true
   239  	ctx.Config.Git.ShortHash = true
   240  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   241  	assert.Len(t, ctx.Version, 7)
   242  }
   243  
   244  func TestGitNotInPath(t *testing.T) {
   245  	var path = os.Getenv("PATH")
   246  	defer func() {
   247  		assert.NoError(t, os.Setenv("PATH", path))
   248  	}()
   249  	assert.NoError(t, os.Setenv("PATH", ""))
   250  	assert.EqualError(t, Pipe{}.Run(context.New(config.Project{})), ErrNoGit.Error())
   251  }