gitee.com/mirrors_opencollective/goreleaser@v0.45.0/pipeline/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/config"
    10  	"github.com/goreleaser/goreleaser/context"
    11  	"github.com/goreleaser/goreleaser/internal/testlib"
    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.Error(t, Pipe{}.Run(ctx))
    26  }
    27  
    28  func TestSingleCommit(t *testing.T) {
    29  	_, back := testlib.Mktmp(t)
    30  	defer back()
    31  	testlib.GitInit(t)
    32  	testlib.GitCommit(t, "commit1")
    33  	testlib.GitTag(t, "v0.0.1")
    34  	var ctx = &context.Context{
    35  		Config: config.Project{},
    36  	}
    37  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    38  	assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
    39  }
    40  
    41  func TestNewRepository(t *testing.T) {
    42  	_, back := testlib.Mktmp(t)
    43  	defer back()
    44  	testlib.GitInit(t)
    45  	var ctx = &context.Context{
    46  		Config: config.Project{},
    47  	}
    48  	assert.Error(t, Pipe{}.Run(ctx))
    49  }
    50  
    51  func TestNoTagsSnapshot(t *testing.T) {
    52  	_, back := testlib.Mktmp(t)
    53  	defer back()
    54  	testlib.GitInit(t)
    55  	testlib.GitCommit(t, "first")
    56  	var ctx = &context.Context{
    57  		Config: config.Project{
    58  			Snapshot: config.Snapshot{
    59  				NameTemplate: "SNAPSHOT-{{.Commit}}",
    60  			},
    61  		},
    62  		Snapshot: true,
    63  		Publish:  false,
    64  	}
    65  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    66  	assert.Contains(t, ctx.Version, "SNAPSHOT-")
    67  }
    68  
    69  func TestNoTagsSnapshotInvalidTemplate(t *testing.T) {
    70  	_, back := testlib.Mktmp(t)
    71  	defer back()
    72  	testlib.GitInit(t)
    73  	testlib.GitCommit(t, "first")
    74  	var ctx = &context.Context{
    75  		Config: config.Project{
    76  			Snapshot: config.Snapshot{
    77  				NameTemplate: "{{",
    78  			},
    79  		},
    80  		Snapshot: true,
    81  		Publish:  false,
    82  	}
    83  	assert.Error(t, Pipe{}.Run(ctx))
    84  }
    85  
    86  // TestNoTagsNoSnapshot covers the situation where a repository
    87  // only contains simple commits and no tags. In this case you have
    88  // to set the --snapshot flag otherwise an error is returned.
    89  func TestNoTagsNoSnapshot(t *testing.T) {
    90  	_, back := testlib.Mktmp(t)
    91  	defer back()
    92  	testlib.GitInit(t)
    93  	testlib.GitCommit(t, "first")
    94  	var ctx = &context.Context{
    95  		Config: config.Project{
    96  			Snapshot: config.Snapshot{
    97  				NameTemplate: "SNAPSHOT-{{.Commit}}",
    98  			},
    99  		},
   100  		Snapshot: false,
   101  		Publish:  false,
   102  	}
   103  	assert.Error(t, Pipe{}.Run(ctx))
   104  }
   105  
   106  func TestInvalidTagFormat(t *testing.T) {
   107  	_, back := testlib.Mktmp(t)
   108  	defer back()
   109  	testlib.GitInit(t)
   110  	testlib.GitCommit(t, "commit2")
   111  	testlib.GitTag(t, "sadasd")
   112  	var ctx = &context.Context{
   113  		Config:   config.Project{},
   114  		Validate: true,
   115  	}
   116  	assert.EqualError(t, Pipe{}.Run(ctx), "sadasd is not in a valid version format")
   117  	assert.Equal(t, "sadasd", ctx.Git.CurrentTag)
   118  }
   119  
   120  func TestDirty(t *testing.T) {
   121  	folder, back := testlib.Mktmp(t)
   122  	defer back()
   123  	testlib.GitInit(t)
   124  	dummy, err := os.Create(filepath.Join(folder, "dummy"))
   125  	assert.NoError(t, err)
   126  	testlib.GitAdd(t)
   127  	testlib.GitCommit(t, "commit2")
   128  	testlib.GitTag(t, "v0.0.1")
   129  	assert.NoError(t, ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
   130  	var ctx = &context.Context{
   131  		Config:   config.Project{},
   132  		Validate: true,
   133  	}
   134  	err = Pipe{}.Run(ctx)
   135  	assert.Error(t, err)
   136  	assert.Contains(t, err.Error(), "git is currently in a dirty state:")
   137  }
   138  
   139  func TestTagIsNotLastCommit(t *testing.T) {
   140  	_, back := testlib.Mktmp(t)
   141  	defer back()
   142  	testlib.GitInit(t)
   143  	testlib.GitCommit(t, "commit3")
   144  	testlib.GitTag(t, "v0.0.1")
   145  	testlib.GitCommit(t, "commit4")
   146  	var ctx = &context.Context{
   147  		Config:   config.Project{},
   148  		Validate: true,
   149  	}
   150  	err := Pipe{}.Run(ctx)
   151  	assert.Error(t, err)
   152  	assert.Contains(t, err.Error(), "git tag v0.0.1 was not made against commit")
   153  }
   154  
   155  func TestValidState(t *testing.T) {
   156  	_, back := testlib.Mktmp(t)
   157  	defer back()
   158  	testlib.GitInit(t)
   159  	testlib.GitCommit(t, "commit3")
   160  	testlib.GitTag(t, "v0.0.1")
   161  	testlib.GitCommit(t, "commit4")
   162  	testlib.GitTag(t, "v0.0.2")
   163  	var ctx = &context.Context{
   164  		Config:   config.Project{},
   165  		Validate: true,
   166  	}
   167  	assert.NoError(t, Pipe{}.Run(ctx))
   168  	assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
   169  }
   170  
   171  func TestNoValidate(t *testing.T) {
   172  	_, back := testlib.Mktmp(t)
   173  	defer back()
   174  	testlib.GitInit(t)
   175  	testlib.GitAdd(t)
   176  	testlib.GitCommit(t, "commit5")
   177  	testlib.GitTag(t, "v0.0.1")
   178  	testlib.GitCommit(t, "commit6")
   179  	var ctx = &context.Context{
   180  		Config:   config.Project{},
   181  		Validate: false,
   182  	}
   183  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   184  }
   185  
   186  func TestSnapshot(t *testing.T) {
   187  	_, back := testlib.Mktmp(t)
   188  	defer back()
   189  	testlib.GitInit(t)
   190  	testlib.GitAdd(t)
   191  	testlib.GitCommit(t, "whatever")
   192  	var ctx = &context.Context{
   193  		Config:   config.Project{},
   194  		Validate: true,
   195  		Snapshot: true,
   196  	}
   197  	assert.NoError(t, Pipe{}.Run(ctx))
   198  }