Skip to content

Commit

Permalink
wip: Tests for diagnostics command
Browse files Browse the repository at this point in the history
Don't currently work because `-c` doesn't seem to be honoured.
  • Loading branch information
dcarley committed Jun 13, 2018
1 parent 373ca9a commit dfed232
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions cmd/diagnostic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cmd_test

import (
"bytes"
"io/ioutil"
"os"
"os/exec"

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

var _ = Describe("Diagnostic", func() {
var (
config *os.File
command *exec.Cmd
)

BeforeEach(func() {
var err error
config, err = ioutil.TempFile("", "cmd_test")
Expect(err).ToNot(HaveOccurred())

command = exec.Command(pathCLI, "-c", config.Name(), "diagnostic")
})

AfterEach(func() {
Expect(os.Remove(config.Name())).To(Succeed())
})

Context("token and endpoint set in config file", func() {
BeforeEach(func() {
_, err := config.Write([]byte(`
endpoint: https://example.com/graphql
token: mytoken
`))
Expect(err).ToNot(HaveOccurred())
Expect(config.Close()).To(Succeed())
})

It("print success", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out).Should(gbytes.Say("GraphQL API endpoint: https://example.com/graphql"))
Eventually(session.Out).Should(gbytes.Say("OK, got a token."))
Eventually(session).Should(gexec.Exit(0))
})
})

Context("token not set in config file", func() {
BeforeEach(func() {
_, err := config.Write([]byte(`
endpoint: https://example.com
`))
Expect(err).ToNot(HaveOccurred())
Expect(config.Close()).To(Succeed())

var stdin bytes.Buffer
stdin.Write([]byte(`mytoken`))
command.Stdin = &stdin
})

It("prompt for token and print success", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out).Should(gbytes.Say("Please enter your CircleCI API token:"))
Eventually(session.Out).Should(gbytes.Say("OK, got a token."))
Eventually(session).Should(gexec.Exit(0))
})
})

Context("token set to empty string in config file", func() {
BeforeEach(func() {
_, err := config.Write([]byte(`
endpoint: https://example.com
token:
`))
Expect(err).ToNot(HaveOccurred())
Expect(config.Close()).To(Succeed())
})

It("print error", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err).Should(gbytes.Say("Please set a token!"))
Eventually(session).Should(gexec.Exit(1))
})
})
})

0 comments on commit dfed232

Please sign in to comment.