Skip to content

Commit

Permalink
wip: Diagnostics tests use HOME instead
Browse files Browse the repository at this point in the history
Because of the bug with `-c` not being honoured. This is probably better
anyway because it allows us to exercise more of the config file
handling. But it requires too much setup to use in the tests for other
commands.
  • Loading branch information
dcarley committed Jun 13, 2018
1 parent dfed232 commit b02cf77
Showing 1 changed file with 65 additions and 43 deletions.
108 changes: 65 additions & 43 deletions cmd/diagnostic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cmd_test

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -14,80 +16,100 @@ import (

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

BeforeEach(func() {
var err error
config, err = ioutil.TempFile("", "cmd_test")
tempHome, err = ioutil.TempDir("", "circleci-cli-test-")
Expect(err).ToNot(HaveOccurred())

command = exec.Command(pathCLI, "-c", config.Name(), "diagnostic")
command = exec.Command(pathCLI, "diagnostic")
command.Env = append(os.Environ(),
fmt.Sprintf("HOME=%s", tempHome),
)
})

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

Context("token and endpoint set in config file", func() {
Describe("no 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())
var stdin bytes.Buffer
stdin.Write([]byte(`mytoken`))
command.Stdin = &stdin
})

It("print success", func() {
It("prompt for token and use default endpoint", 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("Please enter your CircleCI API token:"))
Eventually(session.Out).Should(gbytes.Say("GraphQL API endpoint: https://circleci.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() {
Describe("existing config file", func() {
var config *os.File

BeforeEach(func() {
_, err := config.Write([]byte(`
endpoint: https://example.com
`))
Expect(err).ToNot(HaveOccurred())
Expect(config.Close()).To(Succeed())
const (
configDir = ".circleci"
configFile = "cli.yml"
)

var stdin bytes.Buffer
stdin.Write([]byte(`mytoken`))
command.Stdin = &stdin
Expect(os.Mkdir(filepath.Join(tempHome, configDir), 0700)).To(Succeed())

var err error
config, err = os.OpenFile(
filepath.Join(tempHome, configDir, configFile),
os.O_RDWR|os.O_CREATE,
0600,
)
Expect(err).ToNot(HaveOccurred())
})

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))
Describe("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 set to empty string in config file", func() {
BeforeEach(func() {
_, err := config.Write([]byte(`
endpoint: https://example.com
Context("token set to empty string in config file", func() {
BeforeEach(func() {
_, err := config.Write([]byte(`
endpoint: https://example.com/graphql
token:
`))
Expect(err).ToNot(HaveOccurred())
Expect(config.Close()).To(Succeed())
})
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))
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.Out).Should(gbytes.Say("GraphQL API endpoint: https://example.com/graphql"))
Eventually(session).Should(gexec.Exit(1))
})
})
})
})

0 comments on commit b02cf77

Please sign in to comment.