-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: cmd/go: be consistent about not giving tests direct access to the terminal #34877
Comments
As a person who sometimes runs things on terminals that don't happen to support ANSI color, or non-terminals that do: I would very much like to see things not use "is a terminal" as a surrogate for "supports color output". If "we want special treatment when people are running tests", maybe we should have a |
FWIW, the distinction in general is very clearly documented in Right now in local directory mode the test runs with stdout/stderr both connected to the overall go command process's stdout, to make output appear immediately upon printing. We could I suppose connect it to a pipe that the go command would copy to the real stdout promptly. It's unclear how important that is and/or what it would break. |
Looking at #18153, what is going to break is colored output in local directory mode. |
I'm not tryng to get rid of the caching difference, though. I'm trying to remove the discrepancy of the access to the output terminal. Also note that said access to a terminal in "local directory mode" has never been documented.
It's a requirement for #29062, and I'm sure it will be an unfortunate limitation again in the future for
I kind of see your point there, but I'm not sure that this is a good argument to not make things consistent and better in |
I believe that correctness is more important than colors in a terminal.
|
#29062 can be fixed by hooking os.Exit internally; no need for pipes. |
In that case, it would leave only the consistency problem to discuss. Personal thoughts on the consistency issue:
Point 3 example (from fatih's color package): |
I think we're focusing too much on that issue. It was one of the reasons behind the proposal, but not the only one. I still think that the current setup is inconsistent and undesirable, even if we agree that
Sorry, but I strongly disagree there. Once this behavior is documented, there's no going back. What is your argument for locking in the terminal usage? You gave some reasons earlier, like people still being able to use colored output anyway. I replied to those in #34877 (comment). Ultimately I'm not one of the owners of |
First, it is already documented that there is a clear distinction between Second, we have had issues filed before that explicitly established the rule that the console is available in local directory mode. The fixes for those issues did not write it down, but surely people are depending on it, or they wouldn't have filed the issues in the first place (specifically #18153). I don't see any benefit here to compensate for breaking users. The original benefit stated was to implement the os.Exit(0) check, but there are other ways to do that, and it might not be a good idea anyway. What are the other benefits? |
Ping, anyone who wants to suggest other benefits besides ability to implement the os.Exit(0) check. Otherwise, I think this should be closed. |
The benefit I was trying to show earlier is that There's the argument that it's not worth breaking those users to make Having said that, I see that there's a tradeoff. I just don't agree that this proposal has no benefit beyond the |
I really feel like there's benefit to consistency as its own thing -- because if a test's behavior does in some way get affected by the console, it seems like this could cause very strange and hard to debug failures if people don't realize that's a thing. If I were just running I can see it being desireable to have console access available, or to not have it available, but it seems really weirdly brittle to have it be tied to the invocation of To put it another way: Say I really want to test a thing which genuinely DOES need console access to function. Is it even possible to express that? Should it be? |
I think it is perfectly fine for tests to have access to the console. Sometimes you do have manual tests that you need to interact with, perhaps selected by a command-line flag, and that is OK. You may think this is "bad tests" but not everyone agrees, and that's OK too.
|
@seebs, if your test is failing mysteriously with/without console access then it sounds like you're not printing a good error message in your test. That would be better to focus on, no? |
Based on the discussion and in particular #34877 (comment), this seems like a likely decline. Leaving open for a week for final comments. |
You know, that's a good point: It might be surprising that the behavior changes, but a good test should probably be clear enough about what went wrong that you can figure it out. |
No change in consensus. Declined. |
Summary
go test
run on a terminal currently runs tests withos.Stdout
pointing directly at said terminal.go test .
never gives access to a terminal, as any package arguments cause output buffering.At best, this leads to inconsistent and confusing behavior for test writers and users. At worst, this leads to tests behaving differently depending on whether or not their output is a terminal.
A test's output is simply text, and a terminal isn't guaranteed to be present. I propose that
go test
never give a test direct access to a terminal.Description
The decision to allow
go test
to pass through a real terminal to the tests was made explicit in #18153. The reasoning behind that issue and decision is that it's nice when tests run by a human on a terminal can be shown with colored output, without affecting when tests are run by non-humans.However, I think that was the wrong decision to make. First, it adds inconsistencies; many common commands like
go test .
orgo test ./...
won't give access to a real terminal because of howgo test
treats and buffers output from each test. Here is a quick demonstration:I also believe this sets a bad precedent. A test shouldn't care at all if its standard output is a terminal or not. If a test developer really wants prettier or colorful output, it should be based on explicit settings like
$TERM
or flags like-pretty
.Needing to give tests direct access to the terminal also limits what
go test
can do. For example, in #29062 we tried to capture stdout in all cases to see if a test abruptly calledos.Exit(0)
, which could result in no other tests being run without the user ever realising. This is impossible to do if we can't capture standard output. Even if this particular approach doesn't end up being approved, I still thinkgo test
should be able to inspect or manipulate test output in any way it needs./cc @bcmills @rsc @rhysd @bradfitz from previously linked issues
The text was updated successfully, but these errors were encountered: