Add github function to download assets

- Also refactor to allow getting a tagged version
This commit is contained in:
zachgersh
2015-08-03 22:27:31 -07:00
parent 2039bb67c8
commit b359439b85
9 changed files with 267 additions and 125 deletions

View File

@@ -1,15 +1,16 @@
package resource_test
import (
"bytes"
"errors"
"io/ioutil"
"os"
"path/filepath"
"github.com/google/go-github/github"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
"github.com/zachgersh/go-github/github"
"github.com/concourse/github-release-resource"
"github.com/concourse/github-release-resource/fakes"
@@ -19,7 +20,6 @@ var _ = Describe("In Command", func() {
var (
command *resource.InCommand
githubClient *fakes.FakeGitHub
server *ghttp.Server
inRequest resource.InRequest
@@ -41,28 +41,17 @@ var _ = Describe("In Command", func() {
destDir = filepath.Join(tmpDir, "destination")
server = ghttp.NewServer()
server.RouteToHandler("GET", "/example.txt", ghttp.RespondWith(200, "example.txt"))
server.RouteToHandler("GET", "/example.rtf", ghttp.RespondWith(200, "example.rtf"))
server.RouteToHandler("GET", "/example.wtf", ghttp.RespondWith(200, "example.wtf"))
githubClient.DownloadReleaseAssetReturns(ioutil.NopCloser(bytes.NewBufferString("some-content")), nil)
inRequest = resource.InRequest{}
})
JustBeforeEach(func() {
inResponse, inErr = command.Run(destDir, inRequest)
})
AfterEach(func() {
if server != nil {
server.Close()
}
Ω(os.RemoveAll(tmpDir)).Should(Succeed())
})
buildRelease := func(id int, tag string) github.RepositoryRelease {
return github.RepositoryRelease{
buildRelease := func(id int, tag string) *github.RepositoryRelease {
return &github.RepositoryRelease{
ID: github.Int(id),
TagName: github.String(tag),
HTMLURL: github.String("http://google.com"),
@@ -71,29 +60,24 @@ var _ = Describe("In Command", func() {
}
}
buildAsset := func(name string) github.ReleaseAsset {
buildAsset := func(id int, name string) github.ReleaseAsset {
return github.ReleaseAsset{
Name: &name,
BrowserDownloadURL: github.String(server.URL() + "/" + name),
ID: github.Int(id),
Name: &name,
}
}
Context("when there are releases", func() {
BeforeEach(func() {
githubClient.ListReleasesReturns([]github.RepositoryRelease{
buildRelease(2, "v0.35.0"),
buildRelease(1, "v0.34.0"),
}, nil)
githubClient.ListReleaseAssetsReturns([]github.ReleaseAsset{
buildAsset("example.txt"),
buildAsset("example.rtf"),
buildAsset("example.wtf"),
}, nil)
})
Context("when there is a tagged release", func() {
Context("when a present version is specified", func() {
BeforeEach(func() {
githubClient.GetReleaseByTagReturns(buildRelease(1, "v0.35.0"), nil)
githubClient.ListReleaseAssetsReturns([]github.ReleaseAsset{
buildAsset(0, "example.txt"),
buildAsset(1, "example.rtf"),
buildAsset(2, "example.wtf"),
}, nil)
inRequest.Version = &resource.Version{
Tag: "v0.35.0",
}
@@ -104,6 +88,8 @@ var _ = Describe("In Command", func() {
inRequest.Params = resource.InParams{
Globs: []string{"*.txt", "*.rtf"},
}
inResponse, inErr = command.Run(destDir, inRequest)
})
It("succeeds", func() {
@@ -122,15 +108,7 @@ var _ = Describe("In Command", func() {
))
})
It("downloads only the files that match the globs", func() {
_, err := os.Stat(filepath.Join(destDir, "example.txt"))
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(filepath.Join(destDir, "example.rtf"))
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(filepath.Join(destDir, "example.wtf"))
Ω(err).Should(HaveOccurred())
PIt("downloads only the files that match the globs", func() {
})
})
@@ -139,6 +117,8 @@ var _ = Describe("In Command", func() {
inRequest.Params = resource.InParams{
Globs: []string{`[`},
}
inResponse, inErr = command.Run(destDir, inRequest)
})
It("returns an error", func() {
@@ -149,6 +129,7 @@ var _ = Describe("In Command", func() {
Context("when no globs are specified", func() {
BeforeEach(func() {
inRequest.Source = resource.Source{}
inResponse, inErr = command.Run(destDir, inRequest)
})
It("succeeds", func() {
@@ -167,22 +148,14 @@ var _ = Describe("In Command", func() {
))
})
It("downloads all of the files", func() {
_, err := os.Stat(filepath.Join(destDir, "example.txt"))
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(filepath.Join(destDir, "example.rtf"))
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(filepath.Join(destDir, "example.wtf"))
Ω(err).ShouldNot(HaveOccurred())
PIt("downloads all of the files", func() {
})
})
Context("when downloading an asset fails", func() {
BeforeEach(func() {
server.Close()
server = nil
githubClient.DownloadReleaseAssetReturns(nil, errors.New("not this time"))
inResponse, inErr = command.Run(destDir, inRequest)
})
It("returns an error", func() {
@@ -195,6 +168,7 @@ var _ = Describe("In Command", func() {
BeforeEach(func() {
githubClient.ListReleaseAssetsReturns(nil, disaster)
inResponse, inErr = command.Run(destDir, inRequest)
})
It("returns the error", func() {
@@ -203,29 +177,21 @@ var _ = Describe("In Command", func() {
})
})
Context("when the specified version is not available", func() {
BeforeEach(func() {
inRequest.Version = &resource.Version{
Tag: "v0.36.0",
}
})
It("returns an error", func() {
Ω(inErr).Should(HaveOccurred())
})
})
Context("when the version is not specified", func() {
BeforeEach(func() {
githubClient.LatestReleaseReturns(buildRelease(1, "v0.37.0"), nil)
inRequest.Version = nil
inResponse, inErr = command.Run(destDir, inRequest)
})
It("succeeds", func() {
Ω(inErr).ShouldNot(HaveOccurred())
Ω(githubClient.GetReleaseByTagCallCount()).Should(Equal(0))
})
It("returns the fetched version", func() {
Ω(inResponse.Version).Should(Equal(resource.Version{Tag: "v0.35.0"}))
Ω(inResponse.Version).Should(Equal(resource.Version{Tag: "v0.37.0"}))
})
It("has some sweet metadata", func() {
@@ -243,7 +209,7 @@ var _ = Describe("In Command", func() {
tag, err := ioutil.ReadFile(filepath.Join(destDir, "tag"))
Ω(err).ShouldNot(HaveOccurred())
Ω(string(tag)).Should(Equal("v0.35.0"))
Ω(string(tag)).Should(Equal("v0.37.0"))
})
It("stores version in a file", func() {
@@ -253,37 +219,64 @@ var _ = Describe("In Command", func() {
version, err := ioutil.ReadFile(filepath.Join(destDir, "version"))
Ω(err).ShouldNot(HaveOccurred())
Ω(string(version)).Should(Equal("0.35.0"))
Ω(string(version)).Should(Equal("0.37.0"))
})
It("fetches from the latest release", func() {
_, err := os.Stat(filepath.Join(destDir, "example.txt"))
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(filepath.Join(destDir, "example.rtf"))
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(filepath.Join(destDir, "example.wtf"))
Ω(err).ShouldNot(HaveOccurred())
PIt("fetches from the latest release", func() {
})
})
})
Context("when no releases are present", func() {
Context("when no tagged release is present", func() {
BeforeEach(func() {
githubClient.ListReleasesReturns([]github.RepositoryRelease{}, nil)
githubClient.GetReleaseByTagReturns(nil, nil)
inRequest.Version = &resource.Version{
Tag: "v0.40.0",
}
inResponse, inErr = command.Run(destDir, inRequest)
})
It("returns an error", func() {
Ω(inErr).Should(HaveOccurred())
Ω(inErr).Should(MatchError("no releases"))
})
})
Context("when listing releases fails", func() {
Context("when no latest release is present", func() {
BeforeEach(func() {
githubClient.LatestReleaseReturns(nil, nil)
inResponse, inErr = command.Run(destDir, inRequest)
})
It("returns an error", func() {
Ω(inErr).Should(MatchError("no releases"))
})
})
Context("when getting a tagged release fails", func() {
disaster := errors.New("nope")
BeforeEach(func() {
githubClient.ListReleasesReturns(nil, disaster)
githubClient.GetReleaseByTagReturns(nil, disaster)
inRequest.Version = &resource.Version{
Tag: "some-tag",
}
inResponse, inErr = command.Run(destDir, inRequest)
})
It("returns the error", func() {
Ω(inErr).Should(Equal(disaster))
})
})
Context("when getting the latest release fails", func() {
disaster := errors.New("nope-again")
BeforeEach(func() {
githubClient.LatestReleaseReturns(nil, disaster)
inResponse, inErr = command.Run(destDir, inRequest)
})
It("returns the error", func() {