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

Don't use color if NO_COLOR environment variable is set #1092

Merged
merged 5 commits into from
Jul 22, 2019
Merged

Don't use color if NO_COLOR environment variable is set #1092

merged 5 commits into from
Jul 22, 2019

Conversation

geieredgar
Copy link
Contributor

@geieredgar geieredgar commented Jul 22, 2019

Resolves #1073

@@ -48,6 +48,9 @@ def use_color(setting):
if setting not in COLOR_CHOICES:
raise InvalidColorSetting(setting)

if 'NO_COLOR' in os.environ:
Copy link
Member

Choose a reason for hiding this comment

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

this isn't quite right -- NO_COLOR= git commit ... should still have color

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@asottile Ok, I pushed a new commit

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

looks good -- mind adding a quick test in tests/color_test.py -- you can use either mock.patch.dict(os.environ, ...) or pre_commit.envcontext.envcontext to test the various cases -- probably just needs these three cases considered:

  • unset
  • set to empty string
  • set to non-empty string

@@ -48,7 +48,7 @@ def use_color(setting):
if setting not in COLOR_CHOICES:
raise InvalidColorSetting(setting)

if 'NO_COLOR' in os.environ:
if 'NO_COLOR' in os.environ and os.environ['NO_COLOR']:
Copy link
Member

Choose a reason for hiding this comment

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

if os.environ.get('NO_COLOR') would be ~slightly simpler

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I will change that.

@geieredgar
Copy link
Contributor Author

Ok, I will do that



def test_no_color_env_unset():
with mock.patch.dict(os.environ):
Copy link
Member

Choose a reason for hiding this comment

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

clear=True is what you want here I believe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wouldn't that remove all entries from os.environ? It would probably still work, though.

Copy link
Member

Choose a reason for hiding this comment

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

yes, and yes

I guess if you want to be more direct you could use:

with envcontext([('NO_COLOR', UNSET)]):
    ...

envcotext and UNSET come from pre_commit.envcontext

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I will change that because otherwise the coverage is reduced.

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

@asottile asottile merged commit 1874cea into pre-commit:master Jul 22, 2019
@asottile
Copy link
Member

thanks again! and thanks @saper for the issue!

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

Successfully merging this pull request may close these issues.

Globally disable color?
2 participants