mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-12 08:23:48 +02:00
unit tests for git deploy plugin
This commit is contained in:
parent
8e3cf23d52
commit
82a7ea857d
56
plugin/deploy/git/git.go
Normal file
56
plugin/deploy/git/git.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package git
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/drone/drone/plugin/condition"
|
||||||
|
"github.com/drone/drone/shared/build/buildfile"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Gommand to the current commit hash
|
||||||
|
CmdRevParse = "COMMIT=$(git rev-parse HEAD)"
|
||||||
|
|
||||||
|
// Command to set the git user and email based on the
|
||||||
|
// individual that made the commit.
|
||||||
|
CmdGlobalEmail = "git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')"
|
||||||
|
CmdGlobalUser = "git config --global user.name $(git --no-pager log -1 --pretty=format:'%an')"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Git struct {
|
||||||
|
Target string `yaml:"target,omitempty"`
|
||||||
|
Force bool `yaml:"force,omitempty"`
|
||||||
|
Branch string `yaml:"branch,omitempty"`
|
||||||
|
|
||||||
|
Condition *condition.Condition `yaml:"when,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Git) Write(f *buildfile.Buildfile) {
|
||||||
|
f.WriteCmdSilent(CmdRevParse)
|
||||||
|
f.WriteCmdSilent(CmdGlobalUser)
|
||||||
|
f.WriteCmdSilent(CmdGlobalEmail)
|
||||||
|
|
||||||
|
// add target as a git remote
|
||||||
|
f.WriteCmd(fmt.Sprintf("git remote add deploy %s", g.Target))
|
||||||
|
|
||||||
|
dest := g.Branch
|
||||||
|
if len(dest) == 0 {
|
||||||
|
dest = "master"
|
||||||
|
}
|
||||||
|
|
||||||
|
switch g.Force {
|
||||||
|
case true:
|
||||||
|
// this is useful when the there are artifacts generated
|
||||||
|
// by the build script, such as less files converted to css,
|
||||||
|
// that need to be deployed to git remote.
|
||||||
|
f.WriteCmd(fmt.Sprintf("git add -A"))
|
||||||
|
f.WriteCmd(fmt.Sprintf("git commit -m 'add build artifacts'"))
|
||||||
|
f.WriteCmd(fmt.Sprintf("git push deploy HEAD:%s --force", dest))
|
||||||
|
case false:
|
||||||
|
// otherwise we just do a standard git push
|
||||||
|
f.WriteCmd(fmt.Sprintf("git push deploy $COMMIT:%s", dest))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Git) GetCondition() *condition.Condition {
|
||||||
|
return g.Condition
|
||||||
|
}
|
78
plugin/deploy/git/git_test.go
Normal file
78
plugin/deploy/git/git_test.go
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package git
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/drone/drone/shared/build/buildfile"
|
||||||
|
"github.com/franela/goblin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Git(t *testing.T) {
|
||||||
|
|
||||||
|
g := goblin.Goblin(t)
|
||||||
|
g.Describe("Git Deploy", func() {
|
||||||
|
|
||||||
|
g.It("Should set git.config", func() {
|
||||||
|
b := new(buildfile.Buildfile)
|
||||||
|
d := Git{
|
||||||
|
Target: "git://foo.com/bar/baz.git",
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Write(b)
|
||||||
|
out := b.String()
|
||||||
|
g.Assert(strings.Contains(out, CmdRevParse)).Equal(true)
|
||||||
|
g.Assert(strings.Contains(out, CmdGlobalUser)).Equal(true)
|
||||||
|
g.Assert(strings.Contains(out, CmdGlobalEmail)).Equal(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should add remote", func() {
|
||||||
|
b := new(buildfile.Buildfile)
|
||||||
|
d := Git{
|
||||||
|
Target: "git://foo.com/bar/baz.git",
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Write(b)
|
||||||
|
out := b.String()
|
||||||
|
g.Assert(strings.Contains(out, "\ngit remote add deploy git://foo.com/bar/baz.git\n")).Equal(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should push to remote", func() {
|
||||||
|
b := new(buildfile.Buildfile)
|
||||||
|
d := Git{
|
||||||
|
Target: "git://foo.com/bar/baz.git",
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Write(b)
|
||||||
|
out := b.String()
|
||||||
|
g.Assert(strings.Contains(out, "\ngit push deploy $COMMIT:master\n")).Equal(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should push to alternate branch", func() {
|
||||||
|
b := new(buildfile.Buildfile)
|
||||||
|
d := Git{
|
||||||
|
Branch: "foo",
|
||||||
|
Target: "git://foo.com/bar/baz.git",
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Write(b)
|
||||||
|
out := b.String()
|
||||||
|
g.Assert(strings.Contains(out, "\ngit push deploy $COMMIT:foo\n")).Equal(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should force push to remote", func() {
|
||||||
|
b := new(buildfile.Buildfile)
|
||||||
|
d := Git{
|
||||||
|
Force: true,
|
||||||
|
Target: "git://foo.com/bar/baz.git",
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Write(b)
|
||||||
|
out := b.String()
|
||||||
|
g.Assert(strings.Contains(out, "\ngit add -A\n")).Equal(true)
|
||||||
|
g.Assert(strings.Contains(out, "\ngit commit -m 'add build artifacts'\n")).Equal(true)
|
||||||
|
g.Assert(strings.Contains(out, "\ngit push deploy HEAD:master --force\n")).Equal(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user