github.com/elyscape/goreleaser@v0.66.1-0.20180515111211-5252f74ade63/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.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.GitCommit(t, "commit1") 33 testlib.GitTag(t, "v0.0.1") 34 var ctx = &context.Context{ 35 Config: config.Project{}, 36 } 37 assert.NoError(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 // TODO: improve this error handling 49 assert.Contains(t, Pipe{}.Run(ctx).Error(), `fatal: ambiguous argument 'HEAD'`) 50 } 51 52 func TestNoTagsSnapshot(t *testing.T) { 53 _, back := testlib.Mktmp(t) 54 defer back() 55 testlib.GitInit(t) 56 testlib.GitCommit(t, "first") 57 var ctx = context.New(config.Project{ 58 Snapshot: config.Snapshot{ 59 NameTemplate: "SNAPSHOT-{{.Commit}}", 60 }, 61 }) 62 ctx.Snapshot = true 63 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 64 assert.Contains(t, ctx.Version, "SNAPSHOT-") 65 } 66 67 func TestNoTagsSnapshotInvalidTemplate(t *testing.T) { 68 _, back := testlib.Mktmp(t) 69 defer back() 70 testlib.GitInit(t) 71 testlib.GitCommit(t, "first") 72 var ctx = context.New(config.Project{ 73 Snapshot: config.Snapshot{ 74 NameTemplate: "{{", 75 }, 76 }) 77 ctx.Snapshot = true 78 assert.EqualError(t, Pipe{}.Run(ctx), `failed to generate snapshot name: template: snapshot:1: unexpected unclosed action in command`) 79 } 80 81 // TestNoTagsNoSnapshot covers the situation where a repository 82 // only contains simple commits and no tags. In this case you have 83 // to set the --snapshot flag otherwise an error is returned. 84 func TestNoTagsNoSnapshot(t *testing.T) { 85 _, back := testlib.Mktmp(t) 86 defer back() 87 testlib.GitInit(t) 88 testlib.GitCommit(t, "first") 89 var ctx = context.New(config.Project{}) 90 ctx.Snapshot = false 91 assert.EqualError(t, Pipe{}.Run(ctx), `git doesn't contain any tags. Either add a tag or use --snapshot`) 92 } 93 94 func TestInvalidTagFormat(t *testing.T) { 95 _, back := testlib.Mktmp(t) 96 defer back() 97 testlib.GitInit(t) 98 testlib.GitCommit(t, "commit2") 99 testlib.GitTag(t, "sadasd") 100 var ctx = context.New(config.Project{}) 101 assert.EqualError(t, Pipe{}.Run(ctx), "sadasd is not in a valid version format") 102 assert.Equal(t, "sadasd", ctx.Git.CurrentTag) 103 } 104 105 func TestDirty(t *testing.T) { 106 folder, back := testlib.Mktmp(t) 107 defer back() 108 testlib.GitInit(t) 109 dummy, err := os.Create(filepath.Join(folder, "dummy")) 110 assert.NoError(t, err) 111 testlib.GitAdd(t) 112 testlib.GitCommit(t, "commit2") 113 testlib.GitTag(t, "v0.0.1") 114 assert.NoError(t, ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644)) 115 t.Run("all checks up", func(t *testing.T) { 116 err = Pipe{}.Run(context.New(config.Project{})) 117 assert.Error(t, err) 118 assert.Contains(t, err.Error(), "git is currently in a dirty state:") 119 }) 120 t.Run("skip validate is set", func(t *testing.T) { 121 ctx := context.New(config.Project{}) 122 ctx.SkipValidate = true 123 err = Pipe{}.Run(ctx) 124 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 125 }) 126 t.Run("snapshot", func(t *testing.T) { 127 ctx := context.New(config.Project{}) 128 ctx.Snapshot = true 129 err = Pipe{}.Run(ctx) 130 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 131 }) 132 } 133 134 func TestTagIsNotLastCommit(t *testing.T) { 135 _, back := testlib.Mktmp(t) 136 defer back() 137 testlib.GitInit(t) 138 testlib.GitCommit(t, "commit3") 139 testlib.GitTag(t, "v0.0.1") 140 testlib.GitCommit(t, "commit4") 141 err := Pipe{}.Run(context.New(config.Project{})) 142 assert.Error(t, err) 143 assert.Contains(t, err.Error(), "git tag v0.0.1 was not made against commit") 144 } 145 146 func TestValidState(t *testing.T) { 147 _, back := testlib.Mktmp(t) 148 defer back() 149 testlib.GitInit(t) 150 testlib.GitCommit(t, "commit3") 151 testlib.GitTag(t, "v0.0.1") 152 testlib.GitCommit(t, "commit4") 153 testlib.GitTag(t, "v0.0.2") 154 var ctx = context.New(config.Project{}) 155 assert.NoError(t, Pipe{}.Run(ctx)) 156 assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag) 157 } 158 159 func TestSnapshotNoTags(t *testing.T) { 160 _, back := testlib.Mktmp(t) 161 defer back() 162 testlib.GitInit(t) 163 testlib.GitAdd(t) 164 testlib.GitCommit(t, "whatever") 165 var ctx = context.New(config.Project{}) 166 ctx.Snapshot = true 167 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 168 assert.Equal(t, fakeInfo.CurrentTag, ctx.Git.CurrentTag) 169 } 170 171 func TestSnapshotNoCommits(t *testing.T) { 172 _, back := testlib.Mktmp(t) 173 defer back() 174 testlib.GitInit(t) 175 var ctx = context.New(config.Project{}) 176 ctx.Snapshot = true 177 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 178 assert.Equal(t, fakeInfo, ctx.Git) 179 } 180 181 func TestSnapshotWithoutRepo(t *testing.T) { 182 _, back := testlib.Mktmp(t) 183 defer back() 184 var ctx = context.New(config.Project{}) 185 ctx.Snapshot = true 186 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 187 assert.Equal(t, fakeInfo, ctx.Git) 188 } 189 190 func TestSnapshotDirty(t *testing.T) { 191 folder, back := testlib.Mktmp(t) 192 defer back() 193 testlib.GitInit(t) 194 testlib.GitAdd(t) 195 testlib.GitCommit(t, "whatever") 196 testlib.GitTag(t, "v0.0.1") 197 assert.NoError(t, ioutil.WriteFile(filepath.Join(folder, "foo"), []byte("foobar"), 0644)) 198 var ctx = context.New(config.Project{}) 199 ctx.Snapshot = true 200 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 201 } 202 203 func TestShortCommitHash(t *testing.T) { 204 _, back := testlib.Mktmp(t) 205 defer back() 206 testlib.GitInit(t) 207 testlib.GitCommit(t, "first") 208 var ctx = context.New(config.Project{ 209 Snapshot: config.Snapshot{ 210 NameTemplate: "{{.Commit}}", 211 }, 212 }) 213 ctx.Snapshot = true 214 ctx.Config.Git.ShortHash = true 215 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 216 assert.Len(t, ctx.Version, 7) 217 }