Skip to content

Commit

Permalink
Only prompt for interface when the terminal is a TTY
Browse files Browse the repository at this point in the history
... 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.
  • Loading branch information
tjarratt authored and Tim Jarratt committed Oct 20, 2015
1 parent 5712b80 commit 30a1836
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions arguments/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions arguments/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion terminal/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 30a1836

Please sign in to comment.