From e5f43667727f397b2604601ddc416f9b78e18a97 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 11 Oct 2014 18:49:40 -0700 Subject: [PATCH] added unit tests for Heroku deploy --- plugin/deploy/git/git.go | 2 +- plugin/deploy/heroku/heroku.go | 50 ++++++++++++++++++++++ plugin/deploy/heroku/heroku_test.go | 66 +++++++++++++++++++++++++++++ plugin/publish/npm/npm_test.go | 13 ------ 4 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 plugin/deploy/heroku/heroku.go create mode 100644 plugin/deploy/heroku/heroku_test.go diff --git a/plugin/deploy/git/git.go b/plugin/deploy/git/git.go index 033d14892..298c502ae 100644 --- a/plugin/deploy/git/git.go +++ b/plugin/deploy/git/git.go @@ -13,7 +13,7 @@ const ( // 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')" + CmdGlobalUser = "git config --global user.name $(git --no-pager log -1 --pretty=format:'%an')" ) type Git struct { diff --git a/plugin/deploy/heroku/heroku.go b/plugin/deploy/heroku/heroku.go new file mode 100644 index 000000000..60cca50eb --- /dev/null +++ b/plugin/deploy/heroku/heroku.go @@ -0,0 +1,50 @@ +package heroku + +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 Heroku struct { + App string `yaml:"app,omitempty"` + Force bool `yaml:"force,omitempty"` + + Condition *condition.Condition `yaml:"when,omitempty"` +} + +func (h *Heroku) Write(f *buildfile.Buildfile) { + f.WriteCmdSilent(CmdRevParse) + f.WriteCmdSilent(CmdGlobalUser) + f.WriteCmdSilent(CmdGlobalEmail) + + // add heroku as a git remote + f.WriteCmd(fmt.Sprintf("git remote add heroku git@heroku.com:%s.git", h.App)) + + switch h.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 Heroku. + f.WriteCmd(fmt.Sprintf("git add -A")) + f.WriteCmd(fmt.Sprintf("git commit -m 'adding build artifacts'")) + f.WriteCmd(fmt.Sprintf("git push heroku HEAD:master --force")) + case false: + // otherwise we just do a standard git push + f.WriteCmd(fmt.Sprintf("git push heroku $COMMIT:master")) + } +} + +func (h *Heroku) GetCondition() *condition.Condition { + return h.Condition +} diff --git a/plugin/deploy/heroku/heroku_test.go b/plugin/deploy/heroku/heroku_test.go new file mode 100644 index 000000000..1a7ff70b0 --- /dev/null +++ b/plugin/deploy/heroku/heroku_test.go @@ -0,0 +1,66 @@ +package heroku + +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("Heroku Deploy", func() { + + g.It("Should set git.config", func() { + b := new(buildfile.Buildfile) + h := Heroku{ + App: "drone", + } + + h.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) + h := Heroku{ + App: "drone", + } + + h.Write(b) + out := b.String() + g.Assert(strings.Contains(out, "\ngit remote add heroku git@heroku.com:drone.git\n")).Equal(true) + }) + + g.It("Should push to remote", func() { + b := new(buildfile.Buildfile) + d := Heroku{ + App: "drone", + } + + d.Write(b) + out := b.String() + g.Assert(strings.Contains(out, "\ngit push heroku $COMMIT:master\n")).Equal(true) + }) + + g.It("Should force push to remote", func() { + b := new(buildfile.Buildfile) + h := Heroku{ + Force: true, + App: "drone", + } + + h.Write(b) + out := b.String() + g.Assert(strings.Contains(out, "\ngit add -A\n")).Equal(true) + g.Assert(strings.Contains(out, "\ngit commit -m 'adding build artifacts'\n")).Equal(true) + g.Assert(strings.Contains(out, "\ngit push heroku HEAD:master --force\n")).Equal(true) + }) + + }) +} diff --git a/plugin/publish/npm/npm_test.go b/plugin/publish/npm/npm_test.go index b0562c5d2..575f249b1 100644 --- a/plugin/publish/npm/npm_test.go +++ b/plugin/publish/npm/npm_test.go @@ -36,19 +36,6 @@ func Test_NPM(t *testing.T) { g.Assert(strings.Contains(out, "\nnpm config set")).Equal(false) }) - /* - n := NPM{ - Email: "foo@bar.com", - Username: "foo", - Password: "bar", - Force: true, - Registry: "", - Folder: "/path/to/repo", - Tag: "1.0.0", - AlwaysAuth: false, - } - */ - g.It("Should set force", func() { b := new(buildfile.Buildfile) n := NPM{