-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from circleci/CIRCLE-11693/moar_tests
Circle 11693/moar tests
- Loading branch information
Showing
4 changed files
with
145 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters