<commit_before>package command_factory_test

import (
	"errors"
	"io"

	"github.com/dajulia3/cli"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	config_package "github.com/pivotal-cf-experimental/lattice-cli/config"
	"github.com/pivotal-cf-experimental/lattice-cli/config/persister"
	"github.com/pivotal-cf-experimental/lattice-cli/output"
	"github.com/pivotal-cf-experimental/lattice-cli/test_helpers"

	"github.com/pivotal-cf-experimental/lattice-cli/config/command_factory"
)

var _ = Describe("CommandFactory", func() {
	Describe("setApiEndpoint", func() {
		var (
			stdinReader      *io.PipeReader
			stdinWriter      *io.PipeWriter
			outputBuffer     *gbytes.Buffer
			setTargetCommand cli.Command
			config           *config_package.Config
		)

		BeforeEach(func() {
			stdinReader, stdinWriter = io.Pipe()
			outputBuffer = gbytes.NewBuffer()
			config = config_package.New(persister.NewFakePersister())

			commandFactory := command_factory.NewConfigCommandFactory(config, stdinReader, output.New(outputBuffer))
			setTargetCommand = commandFactory.MakeSetTargetCommand()
		})

		Describe("targetCommand", func() {

			It("sets the api, username, password from the target specified", func() {
				setTargetCommandDone := test_helpers.AsyncExecuteCommandWithArgs(setTargetCommand, []string{"myapi.com"})

				Eventually(outputBuffer).Should(test_helpers.Say("Username: "))
				stdinWriter.Write([]byte("testusername\n"))
				Eventually(outputBuffer).Should(test_helpers.Say("Password: "))
				stdinWriter.Write([]byte("testpassword\n"))

				<-setTargetCommandDone

				Expect(config.Target()).To(Equal("myapi.com"))
				Expect(config.Receptor()).To(Equal("http://testusername:testpassword@receptor.myapi.com"))
				Expect(outputBuffer).To(test_helpers.Say("Api Location Set"))
			})

			It("does not update the config if error on reading username", func() {
				config.SetTarget("oldtarget.com")
				config.SetLogin("olduser", "oldpass")

				setTargetCommandDone := test_helpers.AsyncExecuteCommandWithArgs(setTargetCommand, []string{"myapi.com"})

				Eventually(outputBuffer).Should(test_helpers.Say("Username:"))
				stdinWriter.Close()

				Consistently(outputBuffer).ShouldNot(test_helpers.Say("Api Location Set"))
				<-setTargetCommandDone

				Expect(config.Receptor()).To(Equal("http://olduser:oldpass@receptor.oldtarget.com"))
			})

			It("does not update the config if error on reading password", func() {
				config.SetTarget("oldtarget.com")
				config.SetLogin("olduser", "oldpass")

				commandDone := test_helpers.AsyncExecuteCommandWithArgs(setTargetCommand, []string{"myapi.com"})

				Eventually(outputBuffer).Should(test_helpers.Say("Username: "))
				stdinWriter.Write([]byte("testusername\n"))
				Eventually(outputBuffer).Should(test_helpers.Say("Password:"))
				stdinWriter.Close()

				<-commandDone

				Consistently(outputBuffer).ShouldNot(test_helpers.Say("Api Location Set"))
				Expect(config.Receptor()).To(Equal("http://olduser:oldpass@receptor.oldtarget.com"))
			})

			It("does not set a username or password if none are passed in", func() {

				commandDone := test_helpers.AsyncExecuteCommandWithArgs(setTargetCommand, []string{"myapi.com"})

				Eventually(outputBuffer).Should(test_helpers.Say("Username: "))
				stdinWriter.Write([]byte("\n"))

				Eventually(outputBuffer).Should(test_helpers.Say("Password: "))
				stdinWriter.Write([]byte("\n"))

				<-commandDone

				Expect(config.Target()).To(Equal("myapi.com"))
				Expect(config.Receptor()).To(Equal("http://receptor.myapi.com"))
				Expect(outputBuffer).To(test_helpers.Say("Api Location Set"))
			})

			It("returns an error if the target is blank", func() {
				test_helpers.ExecuteCommandWithArgs(setTargetCommand, []string{""})

				Expect(outputBuffer).To(test_helpers.Say("Incorrect Usage: Target required."))
			})

			It("bubbles errors from setting the target", func() {
				fakePersister := persister.NewFakePersisterWithError(errors.New("FAILURE setting api"))

				commandFactory := command_factory.NewConfigCommandFactory(config_package.New(fakePersister), stdinReader, output.New(outputBuffer))
				setTargetCommand = commandFactory.MakeSetTargetCommand()

				commandDone := test_helpers.AsyncExecuteCommandWithArgs(setTargetCommand, []string{"myapi.com"})

				Eventually(outputBuffer).Should(test_helpers.Say("Username: "))
				stdinWriter.Write([]byte("\n"))
				Eventually(outputBuffer).Should(test_helpers.Say("Password: "))
				stdinWriter.Write([]byte("\n"))

				<-commandDone
				Eventually(outputBuffer).Should(test_helpers.Say("FAILURE setting api"))
			})
		})

	})
})<commit_msg>assert that the command returns in tests<commit_after>package command_factory_test

import (
	"errors"
	"io"

	"github.com/dajulia3/cli"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	config_package "github.com/pivotal-cf-experimental/lattice-cli/config"
	"github.com/pivotal-cf-experimental/lattice-cli/config/persister"
	"github.com/pivotal-cf-experimental/lattice-cli/output"
	"github.com/pivotal-cf-experimental/lattice-cli/test_helpers"

	"github.com/pivotal-cf-experimental/lattice-cli/config/command_factory"
)

var _ = Describe("CommandFactory", func() {
	Describe("setApiEndpoint", func() {
		var (
			stdinReader      *io.PipeReader
			stdinWriter      *io.PipeWriter
			outputBuffer     *gbytes.Buffer
			setTargetCommand cli.Command
			config           *config_package.Config
		)

		BeforeEach(func() {
			stdinReader, stdinWriter = io.Pipe()
			outputBuffer = gbytes.NewBuffer()
			config = config_package.New(persister.NewFakePersister())

			commandFactory := command_factory.NewConfigCommandFactory(config, stdinReader, output.New(outputBuffer))
			setTargetCommand = commandFactory.MakeSetTargetCommand()
		})

		Describe("targetCommand", func() {

			It("sets the api, username, password from the target specified", func() {
				commandFinishChan := test_helpers.AsyncExecuteCommandWithArgs(setTargetCommand, []string{"myapi.com"})

				Eventually(outputBuffer).Should(test_helpers.Say("Username: "))
				stdinWriter.Write([]byte("testusername\n"))
				Eventually(outputBuffer).Should(test_helpers.Say("Password: "))
				stdinWriter.Write([]byte("<PASSWORD>"))

				Eventually(commandFinishChan).Should(BeClosed())

				Expect(config.Target()).To(Equal("myapi.com"))
				Expect(config.Receptor()).To(Equal("http://testusername:testpassword@receptor.myapi.com"))
				Expect(outputBuffer).To(test_helpers.Say("Api Location Set"))
			})

			It("does not update the config if error on reading username", func() {
				config.SetTarget("oldtarget.com")
				config.SetLogin("olduser", "oldpass")

				commandFinishChan := test_helpers.AsyncExecuteCommandWithArgs(setTargetCommand, []string{"myapi.com"})

				Eventually(outputBuffer).Should(test_helpers.Say("Username:"))
				stdinWriter.Close()

				Consistently(outputBuffer).ShouldNot(test_helpers.Say("Api Location Set"))
				Eventually(commandFinishChan).Should(BeClosed())

				Expect(config.Receptor()).To(Equal("http://olduser:oldpass@receptor.oldtarget.com"))
			})

			It("does not update the config if error on reading password", func() {
				config.SetTarget("oldtarget.com")
				config.SetLogin("olduser", "oldpass")

				commandFinishChan := test_helpers.AsyncExecuteCommandWithArgs(setTargetCommand, []string{"myapi.com"})

				Eventually(outputBuffer).Should(test_helpers.Say("Username: "))
				stdinWriter.Write([]byte("testusername\n"))
				Eventually(outputBuffer).Should(test_helpers.Say("Password:"))
				stdinWriter.Close()

				Eventually(commandFinishChan).Should(BeClosed())

				Consistently(outputBuffer).ShouldNot(test_helpers.Say("Api Location Set"))
				Expect(config.Receptor()).To(Equal("http://olduser:oldpass@receptor.oldtarget.com"))
			})

			It("does not set a username or password if none are passed in", func() {
				commandFinishChan := test_helpers.AsyncExecuteCommandWithArgs(setTargetCommand, []string{"myapi.com"})

				Eventually(outputBuffer).Should(test_helpers.Say("Username: "))
				stdinWriter.Write([]byte("\n"))

				Eventually(outputBuffer).Should(test_helpers.Say("Password: "))
				stdinWriter.Write([]byte("\n"))

				Eventually(commandFinishChan).Should(BeClosed())

				Expect(config.Target()).To(Equal("myapi.com"))
				Expect(config.Receptor()).To(Equal("http://receptor.myapi.com"))
				Expect(outputBuffer).To(test_helpers.Say("Api Location Set"))
			})

			It("returns an error if the target is blank", func() {
				test_helpers.ExecuteCommandWithArgs(setTargetCommand, []string{""})

				Expect(outputBuffer).To(test_helpers.Say("Incorrect Usage: Target required."))
			})

			It("bubbles errors from setting the target", func() {
				fakePersister := persister.NewFakePersisterWithError(errors.New("FAILURE setting api"))

				commandFactory := command_factory.NewConfigCommandFactory(config_package.New(fakePersister), stdinReader, output.New(outputBuffer))
				setTargetCommand = commandFactory.MakeSetTargetCommand()

				commandFinishChan := test_helpers.AsyncExecuteCommandWithArgs(setTargetCommand, []string{"myapi.com"})

				Eventually(outputBuffer).Should(test_helpers.Say("Username: "))
				stdinWriter.Write([]byte("\n"))
				Eventually(outputBuffer).Should(test_helpers.Say("Password: "))
				stdinWriter.Write([]byte("\n"))

				Eventually(commandFinishChan).Should(BeClosed())

				Eventually(outputBuffer).Should(test_helpers.Say("FAILURE setting api"))
			})
		})

	})
})
{'fraction_non_alphanumeric': 0.14705882352941177, 'fraction_numerical': 0.0002100840336134454, 'mean_word_length': 5.347333333333333, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4622433', 'n_tokens_mistral': 3392, 'n_tokens_neox': 2874, 'n_words': 384}