Skip to content

Commit

Permalink
Merge pull request #16 from circleci/CIRCLE-11693/moar_tests
Browse files Browse the repository at this point in the history
Circle 11693/moar tests
  • Loading branch information
marcomorain authored Jun 13, 2018
2 parents 1557454 + b02cf77 commit 108a94e
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 17 deletions.
13 changes: 0 additions & 13 deletions circleci_cli_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,8 @@ import (

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

var pathCLI string

var _ = BeforeSuite(func() {
var err error
pathCLI, err = gexec.Build("github.com/circleci/circleci-cli")
Ω(err).ShouldNot(HaveOccurred())
})

var _ = AfterSuite(func() {
gexec.CleanupBuildArtifacts()
})

func TestCircleciCli(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "CircleciCli Suite")
Expand Down
26 changes: 26 additions & 0 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd_test

import (
"testing"

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

var pathCLI string

var _ = BeforeSuite(func() {
var err error
pathCLI, err = gexec.Build("github.com/circleci/circleci-cli")
Ω(err).ShouldNot(HaveOccurred())
})

var _ = AfterSuite(func() {
gexec.CleanupBuildArtifacts()
})

func TestCmd(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Cmd Suite")
}
115 changes: 115 additions & 0 deletions cmd/diagnostic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package cmd_test

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

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

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

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

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

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

Describe("no config file", func() {
BeforeEach(func() {
var stdin bytes.Buffer
stdin.Write([]byte(`mytoken`))
command.Stdin = &stdin
})

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("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))
})
})

Describe("existing config file", func() {
var config *os.File

BeforeEach(func() {
const (
configDir = ".circleci"
configFile = "cli.yml"
)

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())
})

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/graphql
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.Out).Should(gbytes.Say("GraphQL API endpoint: https://example.com/graphql"))
Eventually(session).Should(gexec.Exit(1))
})
})
})
})
8 changes: 4 additions & 4 deletions main_test.go → cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main_test
package cmd_test

import (
"os/exec"
Expand All @@ -9,15 +9,15 @@ import (
"github.com/onsi/gomega/gexec"
)

var _ = Describe("Main", func() {
var _ = Describe("Root", func() {
Describe("when run with --help", func() {
It("return exit code 0 with help message", func() {
command := exec.Command(pathCLI, "--help")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
Eventually(session.Out).Should(gbytes.Say("Usage:"))
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out).Should(gbytes.Say("Usage:"))
Eventually(session).Should(gexec.Exit(0))
})
})
})

0 comments on commit 108a94e

Please sign in to comment.