Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orb validation and expansion. #8

Merged
merged 8 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package cmd_test

import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/ghttp"
)

var pathCLI string
Expand All @@ -24,3 +29,78 @@ func TestCmd(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Cmd Suite")
}

// Test helpers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


func appendPostHandler(server *ghttp.Server, authToken string, statusCode int, expectedRequestJson string, responseBody string) {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/"),
ghttp.VerifyHeader(http.Header{
"Authorization": []string{authToken},
}),
ghttp.VerifyContentType("application/json; charset=utf-8"),
// From Gomegas ghttp.VerifyJson to avoid the
// VerifyContentType("application/json") check
// that fails with "application/json; charset=utf-8"
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
req.Body.Close()
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(MatchJSON(expectedRequestJson), "JSON Mismatch")
},
ghttp.RespondWith(statusCode, `{ "data": `+responseBody+`}`),
),
)
}

type tmpFile struct {
RootDir string
Path string
File *os.File
}

func (f tmpFile) close() {
f.File.Close()
os.RemoveAll(f.RootDir)
}

func (f tmpFile) write(fileContent string) error {
_, err := f.File.Write([]byte(fileContent))

return err
}

func openTmpFile(path string) (tmpFile, error) {
var (
config tmpFile = tmpFile{}
err error
)

tmpDir, err := ioutil.TempDir("", "circleci-cli-test-")
if err != nil {
return config, err
}

config.RootDir = tmpDir
config.Path = filepath.Join(tmpDir, path)

err = os.MkdirAll(filepath.Dir(config.Path), 0700)
if err != nil {
return config, err
}

var file *os.File
file, err = os.OpenFile(
config.Path,
os.O_RDWR|os.O_CREATE,
0600,
)
if err != nil {
return config, err
}

config.File = file

return config, nil
}
90 changes: 9 additions & 81 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cmd_test

import (
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"

Expand All @@ -14,91 +12,23 @@ import (
"github.com/onsi/gomega/ghttp"
)

func appendPostHandler(server *ghttp.Server, authToken string, statusCode int, expectedRequestJson string, responseBody string) {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/"),
ghttp.VerifyHeader(http.Header{
"Authorization": []string{authToken},
}),
ghttp.VerifyContentType("application/json; charset=utf-8"),
// From Gomegas ghttp.VerifyJson to avoid the
// VerifyContentType("application/json") check
// that fails with "application/json; charset=utf-8"
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
req.Body.Close()
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(MatchJSON(expectedRequestJson), "JSON Mismatch")
},
ghttp.RespondWith(statusCode, `{ "data": `+responseBody+`}`),
),
)
}

type configYaml struct {
TempHome string
Path string
YamlFile *os.File
}

func openConfigYaml() (configYaml, error) {
var (
config configYaml = configYaml{}
err error
)

const (
configDir = ".circleci"
configFile = "config.yaml"
)

tempHome, err := ioutil.TempDir("", "circleci-cli-test-")
if err != nil {
return config, err
}

err = os.Mkdir(filepath.Join(tempHome, configDir), 0700)
if err != nil {
return config, err
}

config.Path = filepath.Join(tempHome, configDir, configFile)

var file *os.File
file, err = os.OpenFile(
config.Path,
os.O_RDWR|os.O_CREATE,
0600,
)
if err != nil {
return config, err
}

config.YamlFile = file

return config, nil
}

var _ = Describe("Config", func() {
Describe("with an api and config.yml", func() {
var (
testServer *ghttp.Server
config configYaml
config tmpFile
)

BeforeEach(func() {
var err error
config, err = openConfigYaml()
config, err = openTmpFile(filepath.Join(".circleci", "config.yaml"))
Expect(err).ToNot(HaveOccurred())

testServer = ghttp.NewServer()
})

AfterEach(func() {
config.YamlFile.Close()
os.RemoveAll(config.TempHome)

config.close()
testServer.Close()
})

Expand All @@ -119,7 +49,7 @@ var _ = Describe("Config", func() {
})

It("works", func() {
_, err := config.YamlFile.Write([]byte(`some config`))
err := config.write(`some config`)
Expect(err).ToNot(HaveOccurred())

gqlResponse := `{
Expand Down Expand Up @@ -147,7 +77,7 @@ var _ = Describe("Config", func() {
})

It("prints errors if invalid", func() {
_, err := config.YamlFile.Write([]byte(`some config`))
err := config.write(`some config`)
Expect(err).ToNot(HaveOccurred())

gqlResponse := `{
Expand All @@ -174,8 +104,7 @@ var _ = Describe("Config", func() {
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err).Should(gbytes.Say("Error:"))
Eventually(session.Err).Should(gbytes.Say("-- invalid_config"))
Eventually(session).Should(gexec.Exit(255))

Eventually(session).ShouldNot(gexec.Exit(0))
})
})

Expand All @@ -196,7 +125,7 @@ var _ = Describe("Config", func() {
})

It("works", func() {
_, err := config.YamlFile.Write([]byte(`some config`))
err := config.write(`some config`)
Expect(err).ToNot(HaveOccurred())

gqlResponse := `{
Expand Down Expand Up @@ -224,7 +153,7 @@ var _ = Describe("Config", func() {
})

It("prints errors if invalid", func() {
_, err := config.YamlFile.Write([]byte(`some config`))
err := config.write(`some config`)
Expect(err).ToNot(HaveOccurred())

gqlResponse := `{
Expand Down Expand Up @@ -253,8 +182,7 @@ var _ = Describe("Config", func() {
Eventually(session.Err).Should(gbytes.Say("Error:"))
Eventually(session.Err).Should(gbytes.Say("-- error1,"))
Eventually(session.Err).Should(gbytes.Say("-- error2,"))
Eventually(session).Should(gexec.Exit(255))

Eventually(session).ShouldNot(gexec.Exit(0))
})
})
})
Expand Down
Loading