Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending redirection to stdout, stderr, stdin #822

Merged
merged 7 commits into from
Jun 7, 2019
Merged

Extending redirection to stdout, stderr, stdin #822

merged 7 commits into from
Jun 7, 2019

Conversation

jleni
Copy link
Contributor

@jleni jleni commented Feb 11, 2019

A couple of issues in the past have already requested the ability of setting stdio (in, out, err): #763 #658

In addition, there are some discussions about testing cobra based applications #770

This PR tries to address this by allowing redirection of I/O at the command level allowing for better unit testing.

	mockOut := bytes.NewBufferString("")
	mockErr := bytes.NewBufferString("")
	myCmd.SetIn(mockIn)
	myCmd.SetOut(mockOut)
        ....
        err = runMyCmd(myCmd, []string{})
        .... 
 	assert.Equal(t, expectedOut, myCmd.String())
	assert.Equal(t, expectedErr, myCmd.String())

stdin is also considered as some CLI may request data (such as passwords, confirmations) that may also need to be redirected when testing.

@CLAassistant
Copy link

CLAassistant commented Feb 11, 2019

CLA assistant check
All committers have signed the CLA.

command.go Show resolved Hide resolved
Copy link
Collaborator

@eparis eparis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix up the comment on SetOutput so it's clear that SetOutput and SetOut are the same thing now and I'll merge this.

@jleni
Copy link
Contributor Author

jleni commented Feb 13, 2019

I kept the old behavior (backwards compatibility) and updated the comment to indicate the newer alternative methods.

@syndbg
Copy link

syndbg commented Feb 20, 2019

It would be very nice if this gets merged :)

Testability of stdin is currently a huge issue. And as we know so far, stderr separation is also nice to have.

@jackzampolin
Copy link

@eparis We would love to get this merged as well so that we can migrate some of our tests to this format.

@jackzampolin
Copy link

@eparis Looks like this PR has some pretty broad support and a defined usecase! When do you anticipate this will be included in a release? 🙏

@jharshman
Copy link
Collaborator

This looks good to me but would be nice to have some tests.

@eparis
Copy link
Collaborator

eparis commented Mar 21, 2019

I guess I said I'd merge this a long time ago. Anyone willing to commit to writing a unit test like @jharshman mentioned?

@alessio
Copy link
Contributor

alessio commented Apr 30, 2019

@jharshman @eparis test cases have been added

@jharshman
Copy link
Collaborator

Thanks for adding some test cases. This looks good to me.

@alessio
Copy link
Contributor

alessio commented May 6, 2019

Bump (gently)

@alessio
Copy link
Contributor

alessio commented May 15, 2019

@jharshman @eparis @jamesdphillips All comments have been addressed

@alessio
Copy link
Contributor

alessio commented May 21, 2019

(gently) Bump

We'd appreciate if could review this again now that all comments have been addressed.

We really need this feature to make our life easier and avoid dirty hacks while writing test cases for our sub-commands. As a last resort, we could fork cobra though we're committed to first try everything to avoid such duplication of code and effort.

Thanks for considering.

@jamesdphillips @eparis @jharshman

@fedekunze
Copy link

is there anything blocking this PR from being reviewed again ?

@alessio
Copy link
Contributor

alessio commented Jun 5, 2019

@eparis bump (gently) :)

@spf13 spf13 merged commit f2b07da into spf13:master Jun 7, 2019
alessio added a commit to alessio/cobra that referenced this pull request Jun 24, 2019
alessio added a commit to alessio/cobra that referenced this pull request Aug 10, 2019
piotrkpc added a commit to piotrkpc/istio that referenced this pull request Jun 29, 2020
*cobra.Command `SetOutput` method is deprecated [0]. Replacing
with `SetOut` and `SetErr`.

[0]: spf13/cobra#822
istio-testing pushed a commit to istio/istio that referenced this pull request Jun 29, 2020
*cobra.Command `SetOutput` method is deprecated [0]. Replacing
with `SetOut` and `SetErr`.

[0]: spf13/cobra#822
jpmcb pushed a commit that referenced this pull request Oct 1, 2020
* Fix stderr printing functions

Follow-up of #822

* Errors go to stderr as per POSIX

* use PrintErrf() instead of extra call to Sprintf()

* Error messages should always be printed to os.Stderr.

* add test case for Print* redirection

Thanks: @bukowa for the patch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants