test output resource

This commit is contained in:
Chris Brown
2015-01-31 23:53:38 +00:00
parent f1b2ea069d
commit 793c8a5019
4 changed files with 231 additions and 24 deletions

View File

@@ -10,28 +10,31 @@ import (
)
type FakeGitHub struct {
CreateReleaseStub func(release github.RepositoryRelease) (github.RepositoryRelease, error)
CreateReleaseStub func(release *github.RepositoryRelease) (*github.RepositoryRelease, error)
createReleaseMutex sync.RWMutex
createReleaseArgsForCall []struct {
release github.RepositoryRelease
release *github.RepositoryRelease
}
createReleaseReturns struct {
result1 github.RepositoryRelease
result1 *github.RepositoryRelease
result2 error
}
UploadReleaseAssetStub func(release github.RepositoryRelease, name string, file *os.File)
UploadReleaseAssetStub func(release *github.RepositoryRelease, name string, file *os.File) error
uploadReleaseAssetMutex sync.RWMutex
uploadReleaseAssetArgsForCall []struct {
release github.RepositoryRelease
release *github.RepositoryRelease
name string
file *os.File
}
uploadReleaseAssetReturns struct {
result1 error
}
}
func (fake *FakeGitHub) CreateRelease(release github.RepositoryRelease) (github.RepositoryRelease, error) {
func (fake *FakeGitHub) CreateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease, error) {
fake.createReleaseMutex.Lock()
fake.createReleaseArgsForCall = append(fake.createReleaseArgsForCall, struct {
release github.RepositoryRelease
release *github.RepositoryRelease
}{release})
fake.createReleaseMutex.Unlock()
if fake.CreateReleaseStub != nil {
@@ -47,30 +50,32 @@ func (fake *FakeGitHub) CreateReleaseCallCount() int {
return len(fake.createReleaseArgsForCall)
}
func (fake *FakeGitHub) CreateReleaseArgsForCall(i int) github.RepositoryRelease {
func (fake *FakeGitHub) CreateReleaseArgsForCall(i int) *github.RepositoryRelease {
fake.createReleaseMutex.RLock()
defer fake.createReleaseMutex.RUnlock()
return fake.createReleaseArgsForCall[i].release
}
func (fake *FakeGitHub) CreateReleaseReturns(result1 github.RepositoryRelease, result2 error) {
func (fake *FakeGitHub) CreateReleaseReturns(result1 *github.RepositoryRelease, result2 error) {
fake.CreateReleaseStub = nil
fake.createReleaseReturns = struct {
result1 github.RepositoryRelease
result1 *github.RepositoryRelease
result2 error
}{result1, result2}
}
func (fake *FakeGitHub) UploadReleaseAsset(release github.RepositoryRelease, name string, file *os.File) {
func (fake *FakeGitHub) UploadReleaseAsset(release *github.RepositoryRelease, name string, file *os.File) error {
fake.uploadReleaseAssetMutex.Lock()
fake.uploadReleaseAssetArgsForCall = append(fake.uploadReleaseAssetArgsForCall, struct {
release github.RepositoryRelease
release *github.RepositoryRelease
name string
file *os.File
}{release, name, file})
fake.uploadReleaseAssetMutex.Unlock()
if fake.UploadReleaseAssetStub != nil {
fake.UploadReleaseAssetStub(release, name, file)
return fake.UploadReleaseAssetStub(release, name, file)
} else {
return fake.uploadReleaseAssetReturns.result1
}
}
@@ -80,10 +85,17 @@ func (fake *FakeGitHub) UploadReleaseAssetCallCount() int {
return len(fake.uploadReleaseAssetArgsForCall)
}
func (fake *FakeGitHub) UploadReleaseAssetArgsForCall(i int) (github.RepositoryRelease, string, *os.File) {
func (fake *FakeGitHub) UploadReleaseAssetArgsForCall(i int) (*github.RepositoryRelease, string, *os.File) {
fake.uploadReleaseAssetMutex.RLock()
defer fake.uploadReleaseAssetMutex.RUnlock()
return fake.uploadReleaseAssetArgsForCall[i].release, fake.uploadReleaseAssetArgsForCall[i].name, fake.uploadReleaseAssetArgsForCall[i].file
}
func (fake *FakeGitHub) UploadReleaseAssetReturns(result1 error) {
fake.UploadReleaseAssetStub = nil
fake.uploadReleaseAssetReturns = struct {
result1 error
}{result1}
}
var _ resource.GitHub = new(FakeGitHub)

View File

@@ -1,8 +1,10 @@
package resource
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/github"
)
@@ -20,13 +22,41 @@ func NewOutCommand(github GitHub) *OutCommand {
func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, error) {
params := request.Params
release := &github.RepositoryRelease{}
name, err := c.fileContents(filepath.Join(sourceDir, request.Params.NamePath))
if err != nil {
return OutResponse{}, err
}
tag, err := c.fileContents(filepath.Join(sourceDir, request.Params.TagPath))
if err != nil {
return OutResponse{}, err
}
body := ""
if request.Params.BodyPath != "" {
body, err = c.fileContents(filepath.Join(sourceDir, request.Params.BodyPath))
if err != nil {
return OutResponse{}, err
}
}
release := &github.RepositoryRelease{
Name: github.String(name),
TagName: github.String(tag),
Body: github.String(body),
}
createdRelease, err := c.github.CreateRelease(release)
if err != nil {
return OutResponse{}, err
}
for _, filePath := range params.Globs {
for _, fileGlob := range params.Globs {
matches, err := filepath.Glob(filepath.Join(sourceDir, fileGlob))
if err != nil {
return OutResponse{}, err
}
for _, filePath := range matches {
file, err := os.Open(filePath)
if err != nil {
return OutResponse{}, err
@@ -40,6 +70,20 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err
file.Close()
}
}
return OutResponse{}, nil
return OutResponse{
Version: Version{
Tag: tag,
},
}, nil
}
func (c *OutCommand) fileContents(path string) (string, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return strings.TrimSpace(string(contents)), nil
}

138
out_command_test.go Normal file
View File

@@ -0,0 +1,138 @@
package resource_test
import (
"io/ioutil"
"os"
"path/filepath"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/concourse/github-release-resource"
"github.com/concourse/github-release-resource/fakes"
"github.com/google/go-github/github"
)
var _ = Describe("Out Command", func() {
var (
command *resource.OutCommand
githubClient *fakes.FakeGitHub
sourcesDir string
)
BeforeEach(func() {
var err error
githubClient = &fakes.FakeGitHub{}
command = resource.NewOutCommand(githubClient)
sourcesDir, err = ioutil.TempDir("", "github-release")
Ω(err).ShouldNot(HaveOccurred())
})
AfterEach(func() {
Ω(os.RemoveAll(sourcesDir)).Should(Succeed())
})
It("creates a release on GitHub", func() {
namePath := filepath.Join(sourcesDir, "name")
bodyPath := filepath.Join(sourcesDir, "body")
tagPath := filepath.Join(sourcesDir, "tag")
file(namePath, "v0.3.12")
file(bodyPath, "this is a great release")
file(tagPath, "0.3.12")
request := resource.OutRequest{
Params: resource.OutParams{
NamePath: "name",
BodyPath: "body",
TagPath: "tag",
},
}
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0)
Ω(*release.Name).Should(Equal("v0.3.12"))
Ω(*release.TagName).Should(Equal("0.3.12"))
Ω(*release.Body).Should(Equal("this is a great release"))
})
It("works without a body", func() {
namePath := filepath.Join(sourcesDir, "name")
tagPath := filepath.Join(sourcesDir, "tag")
file(namePath, "v0.3.12")
file(tagPath, "0.3.12")
request := resource.OutRequest{
Params: resource.OutParams{
NamePath: "name",
TagPath: "tag",
},
}
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.CreateReleaseCallCount()).Should(Equal(1))
release := githubClient.CreateReleaseArgsForCall(0)
Ω(*release.Name).Should(Equal("v0.3.12"))
Ω(*release.TagName).Should(Equal("0.3.12"))
Ω(*release.Body).Should(Equal(""))
})
It("uploads matching file globs", func() {
namePath := filepath.Join(sourcesDir, "name")
bodyPath := filepath.Join(sourcesDir, "body")
tagPath := filepath.Join(sourcesDir, "tag")
file(namePath, "v0.3.12")
file(bodyPath, "this is a great release")
file(tagPath, "0.3.12")
globMatching := filepath.Join(sourcesDir, "great-file.tgz")
globNotMatching := filepath.Join(sourcesDir, "bad-file.txt")
file(globMatching, "matching")
file(globNotMatching, "not matching")
githubClient.CreateReleaseStub = func(gh *github.RepositoryRelease) (*github.RepositoryRelease, error) {
gh.ID = github.Int(112)
return gh, nil
}
request := resource.OutRequest{
Params: resource.OutParams{
NamePath: "name",
BodyPath: "body",
TagPath: "tag",
Globs: []string{
"*.tgz",
},
},
}
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(githubClient.UploadReleaseAssetCallCount()).Should(Equal(1))
release, name, file := githubClient.UploadReleaseAssetArgsForCall(0)
Ω(*release.ID).Should(Equal(112))
Ω(name).Should(Equal("great-file.tgz"))
Ω(file.Name()).Should(Equal(filepath.Join(sourcesDir, "great-file.tgz")))
})
})
func file(path, contents string) {
Ω(ioutil.WriteFile(path, []byte(contents), 0644)).Should(Succeed())
}

13
resource_suite_test.go Normal file
View File

@@ -0,0 +1,13 @@
package resource_test
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestGithubReleaseResource(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Github Release Resource Suite")
}