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

allow CLICOLOR_FORCE env var to force CLI color output #6270

Merged
merged 12 commits into from
May 23, 2024

Conversation

cconverse711
Copy link
Contributor

This adds support for using the CLICOLOR_FORCE environment variable to force color output in Linux even when a tty is not used, mimicking behavior in CMake and other tools. The primary intent is to allow color to be output when cppcheck is run in CI, for example.

@firewave
Copy link
Collaborator

Thanks for your contribution. Please add a unit test for this.

@firewave
Copy link
Collaborator

Shouldn't this implement NO_COLOR as well?

@danmar
Copy link
Owner

danmar commented Apr 11, 2024

hmm I am not sure if a unit test would be possible. not if getenv is cached. maybe you want to uncache it it testing somehow. or another alternative is to write a test in cppcheck/test/cli/test-other.py

You could add a line in the releasenotes.txt also. And before we merge it it would not hurt if I add a ticket in trac.

lib/color.cpp Outdated Show resolved Hide resolved
@firewave
Copy link
Collaborator

hmm I am not sure if a unit test would be possible. not if getenv is cached. it might be better to write a test in cppcheck/test/cli/test-other.py

That's actually what I meant. It was still early. 😴

@cconverse711
Copy link
Contributor Author

Shouldn't this implement NO_COLOR as well?

According to the full linked "proposal", yes, however most of the other tools that support CLICOLOR_FORCE don't actually support NO_COLOR (it seems far less useful, imo). I'm happy to modify the update if you prefer, though.

@firewave
Copy link
Collaborator

According to the full linked "proposal", yes, however most of the other tools that support CLICOLOR_FORCE don't actually support NO_COLOR (it seems far less useful, imo). I'm happy to modify the update if you prefer, though.

I would prefer if you would do both while you are at it.

@danmar
Copy link
Owner

danmar commented Apr 12, 2024

I think your code and testing looks good.

Adding the NO_COLOR sounds easy technically, I am in favor of adding it also :-). But I am not sure how you would add the test for it - ideally the test would provide a tty terminal right?

@cconverse711
Copy link
Contributor Author

Thanks. I think I figured out a decent way to test using tty, let me know if you'd prefer to omit that.

test/cli/testutils.py Show resolved Hide resolved
test/cli/other_test.py Outdated Show resolved Hide resolved
lib/color.cpp Outdated Show resolved Hide resolved
Copy link
Owner

@danmar danmar left a comment

Choose a reason for hiding this comment

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

lgtm.

@firewave
Copy link
Collaborator

I still want to take a look this weekend. Something seems off to me but I cannot tell what.

return os;
}

std::string toString(Color c)
{
#ifndef _WIN32
std::stringstream ss;
std::ostringstream ss;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was about to comment that I would prefer if we could do this without a stream but it turns out this function is only called to generate a static lookup table so it is actually fine.

@firewave
Copy link
Collaborator

I still want to take a look this weekend. Something seems off to me but I cannot tell what.

Already commented on the "off" thing. Still need to take a look at the Python stuff.

@@ -71,6 +71,43 @@ def test_preprocessor_error(tmpdir):
assert exitcode != 0


ANSI_BOLD = "\x1b[1m"
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could prefix them with __ so they are private.


assert exitcode == 0
assert stderr
assert (ANSI_BOLD in stderr) == color_expected
Copy link
Collaborator

Choose a reason for hiding this comment

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

I usually prefer to check the exact output instead of just checking if something is contained. Makes it easier to understand what is actually returned. But as we are just verifying that the environment variables work it is fine.

@@ -72,8 +75,82 @@ def __lookup_cppcheck_exe():
return exe_path


def __run_subprocess_tty(args, env=None, cwd=None, timeout=None):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Still need to review this. Everything else LGTM.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I have not verified all the behavior of the calls in detail but it looks fine. Also it is only used by the added tests and the CI works so even if there is a small oversight it would be very limited.

raise
# EIO means EOF on some systems
readable.remove(fd)
else:
Copy link
Collaborator

Choose a reason for hiding this comment

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

try: -> except: -> else:? That seems extremely errorprone as it looks like an incorrectly intended block.

No need to change it.

@@ -72,8 +75,82 @@ def __lookup_cppcheck_exe():
return exe_path


def __run_subprocess_tty(args, env=None, cwd=None, timeout=None):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have not verified all the behavior of the calls in detail but it looks fine. Also it is only used by the added tests and the CI works so even if there is a small oversight it would be very limited.

@firewave firewave merged commit d518020 into danmar:main May 23, 2024
64 checks passed
@firewave
Copy link
Collaborator

Sorry the review took so long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants