From 30a1836965ceee4f215965fbcb239b308474a024 Mon Sep 17 00:00:00 2001 From: Tim Jarratt Date: Mon, 19 Oct 2015 23:21:16 -0700 Subject: [PATCH] Only prompt for interface when the terminal is a TTY ... otherwise we shouldn't bother trying to prompt the user, because the environment running the command can't be expected to allow the user to respond. --- arguments/parser.go | 6 ++++++ arguments/parser_test.go | 11 +++++++++++ integration/integration_test.go | 5 ++++- scripts/test.sh | 3 ++- terminal/ui.go | 4 +++- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/arguments/parser.go b/arguments/parser.go index 042bfe7..a26f1ab 100644 --- a/arguments/parser.go +++ b/arguments/parser.go @@ -64,6 +64,12 @@ func (argParser *argumentParser) ParseArguments(args ...string) ParsedArguments } func (parser *argumentParser) PromptUserForInterfaceName(filepath string) string { + if !(parser.ui.TerminalIsTTY()) { + parser.ui.WriteLine("Cowardly refusing to prompt user for an interface name in a non-tty environment") + parser.failHandler("Perhaps you meant to invoke counterfeiter with more than one argument?") + return "" + } + parser.ui.WriteLine("Which interface to counterfeit?") interfacesInPackage := parser.interfaceLocator.GetInterfacesFromFilePath(filepath) diff --git a/arguments/parser_test.go b/arguments/parser_test.go index 0ec0b39..1ebe2f6 100644 --- a/arguments/parser_test.go +++ b/arguments/parser_test.go @@ -75,6 +75,17 @@ var _ = Describe("parsing arguments", func() { interfaceLocator.GetInterfacesFromFilePathReturns([]string{"Foo", "Bar"}) ui.ReadLineFromStdinReturns("1") + ui.TerminalIsTTYReturns(true) + }) + + Context("but the connecting terminal is not a TTY", func() { + BeforeEach(func() { + ui.TerminalIsTTYReturns(false) + }) + + It("should invoke the fail handler", func() { + Expect(failWasCalled).To(BeTrue()) + }) }) It("prompts the user for which interface they want", func() { diff --git a/integration/integration_test.go b/integration/integration_test.go index c78e3d7..7d6df0e 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -117,7 +117,10 @@ func startCounterfeiterWithStdinPipe(workingDir string, stdin io.Reader, args .. cmd := exec.Command(pathToCounterfeiter, args...) cmd.Stdin = stdin cmd.Dir = workingDir - cmd.Env = []string{"GOPATH=" + absPathWithSymlinks} + cmd.Env = []string{ + "GOPATH=" + absPathWithSymlinks, + "COUNTERFEITER_INTERACTIVE=1", + } session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) Expect(err).ToNot(HaveOccurred()) diff --git a/scripts/test.sh b/scripts/test.sh index 33777e9..7a2948f 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -21,6 +21,7 @@ $counterfeiter /tmp/symlinked_fixtures Something >/dev/null go build ./fixtures/... -go test -race -v ./... +go test -race -v . ./arguments ./integration rm /tmp/symlinked_fixtures +rm -rf fixtures/fixtures diff --git a/terminal/ui.go b/terminal/ui.go index edec9de..db9e4a7 100644 --- a/terminal/ui.go +++ b/terminal/ui.go @@ -22,7 +22,9 @@ func NewUI() UI { type ui struct{} func (ui *ui) TerminalIsTTY() bool { - return terminal.IsTerminal(int(os.Stdin.Fd())) + isTTY := terminal.IsTerminal(int(os.Stdin.Fd())) + hasOverride := os.Getenv("COUNTERFEITER_INTERACTIVE") == "1" + return isTTY || hasOverride } func (ui *ui) ReadLineFromStdin() string {