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

main: fix replacing color codes in tracebacks #335

Merged
merged 7 commits into from Aug 1, 2021
Merged

Conversation

FFY00
Copy link
Member

@FFY00 FFY00 commented Jul 28, 2021

Signed-off-by: Filipe Laíns lains@riseup.net

Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Copy link
Contributor

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

Can we add a test to validate the change?

@FFY00 FFY00 mentioned this pull request Jul 28, 2021
@FFY00
Copy link
Member Author

FFY00 commented Jul 28, 2021

I think adding a test for this that would actually be helpful in development would be very tricky, and I feel like it might not be worth the time. The code that we would be covering would only generate formatting errors, not functional ones which would prevent people from working.
How do you test this kind of stuff in tox?

@gaborbernat
Copy link
Contributor

Why would it be tricky? If now the contract is that the output is nicely colored, why would a test validating that be not worth it? tox threats output similarly. We run the code in a subprocess, and then assert the stdout/stderr contains the right series of bytes. If you can run it without needing a new subprocess you can assert on capsys/capfd and do the same. Doesn't have to test color output for all functional cases, just one run that covers all cases. Bugs related to the coloring helper function can be tested by directly calling the helper function from your test.

src/build/__main__.py Outdated Show resolved Hide resolved
src/build/__main__.py Outdated Show resolved Hide resolved
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Copy link
Contributor

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

In general looks good, let's just add an environmetn variable to enable color mode 😊

for name, code in _STYLES.items():
message = message.replace(f'[{name}]', code if color else '')
print(message)
if not sys.stdout.isatty() or 'NO_COLOR' in os.environ:
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that we have an environment variable to disable color, however can we have also one that force enables it? (e.g. tox 4 cannot forward the tty state into a subprocess yet, so is handy in such cases to force color mode). Perhaps FORCE_COLOR? And if neither set then check the tty state 😊

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. What happens with both FORCE_COLOR and NO_COLOR?

Copy link
Contributor

Choose a reason for hiding this comment

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

In my implementation NO_COLOR wins, but 🤷

Copy link
Contributor

Choose a reason for hiding this comment

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

See e.g. how this would be used https://github.com/tox-dev/tox/blob/rewrite/tox.ini#L79 (this shows how mypy handles it).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't empty values translate as false for these flags?

Copy link
Member Author

Choose a reason for hiding this comment

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

https://no-color.org/ says the mere presence enables the behavior, the value is irrelevant. I think we should have the same behavior in FORCE_COLOR.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a fan of having a FORCE_COLOR too, as often on GHA (which supports color) things can be missing color. I think no-color.org went overboard - it's a pretty well established practice for an empty environment variable to be the same as unsetting it. It's not always easy to unset an environment variable, and the disconnect there is irritating. But I'd follow what the spec dictates. :(

Copy link
Member Author

@FFY00 FFY00 Jul 29, 2021

Choose a reason for hiding this comment

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

I understand the concern and share a similar opinion. IMO, coloring should be disabled if NO_COLOR is present and is not no/false. But given that it is already a pretty well established standard, I think the best option is to follow what is defined.

Signed-off-by: Filipe Laíns <lains@riseup.net>
src/build/__main__.py Outdated Show resolved Hide resolved
@layday
Copy link
Member

layday commented Jul 29, 2021

It's not entirely clear what this is fixing. Are you trying to avoid replacing "[dim]" and "[reset]" in the traceback itself?

@FFY00
Copy link
Member Author

FFY00 commented Jul 29, 2021

Yes, as @henryiii pointed out.

Copy link
Contributor

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

src/build/__main__.py Outdated Show resolved Hide resolved
@FFY00 FFY00 enabled auto-merge (rebase) July 29, 2021 13:07
@FFY00 FFY00 force-pushed the fix-tb-color branch 2 times, most recently from 73b44db to 7ec79db Compare July 29, 2021 15:34
@FFY00
Copy link
Member Author

FFY00 commented Jul 31, 2021

The tests are failing on Windows and I don't really get why. Since I don't have any Windows setup right now, it might be a little while before I am able to set one up and debug the issues. If anyone is able to look into it, I would really appreciate it.
@henryiii you use Windows, right? Perhaps you could help out if you have time.

@FFY00
Copy link
Member Author

FFY00 commented Aug 1, 2021

It was colorama. It's a bit weird since this test was passing previously.

Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
@FFY00 FFY00 merged commit 7ed0a6b into pypa:main Aug 1, 2021
@henryiii
Copy link
Contributor

henryiii commented Aug 2, 2021

The tests are failing on Windows and I don't really get why. Since I don't have any Windows setup right now, it might be a little while before I am able to set one up and debug the issues. If anyone is able to look into it, I would really appreciate it.
@henryiii you use Windows, right? Perhaps you could help out if you have time.

Sorry, wasn't around a computer much this weekend. I technically have a Windows box I boot up once in a while when I'm stuck debugging Windows issues in various packages. I'm perfectly fine to keep that a secret, though. 😅

inmantaci pushed a commit to inmanta/inmanta-core that referenced this pull request Aug 6, 2021
Bumps [build](https://github.com/pypa/build) from 0.5.1 to 0.6.0.post1.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/pypa/build/blob/main/CHANGELOG.rst">build's changelog</a>.</em></p>
<blockquote>
<h1>0.6.0.post1 (05-08-2021)</h1>
<ul>
<li>Fix compability with Python 3.6 and 3.7 (<code>PR [#339](https://github.com/pypa/build/issues/339)</code><em>, Fixes <code>[#338](https://github.com/pypa/build/issues/338)</code></em>)</li>
</ul>
<p>.. _PR <a href="https://github-redirect.dependabot.com/pypa/build/issues/339">#339</a>: <a href="https://github-redirect.dependabot.com/pypa/build/pull/339">pypa/build#339</a>
.. _<a href="https://github-redirect.dependabot.com/pypa/build/issues/338">#338</a>: <a href="https://github-redirect.dependabot.com/pypa/build/issues/338">pypa/build#338</a></p>
<h1>0.6.0 (02-08-2021)</h1>
<ul>
<li>Improved output (<code>PR [#333](https://github.com/pypa/build/issues/333)</code><em>, Fixes <code>[#142](https://github.com/pypa/build/issues/142)</code></em>)</li>
<li>The CLI now honnors <code>NO_COLOR</code>_ (<code>PR [#333](https://github.com/pypa/build/issues/333)</code>_)</li>
<li>The CLI can now be forced to colorize the output by setting the <code>FORCE_COLOR</code> environment variable (<code>PR [#335](https://github.com/pypa/build/issues/335)</code>_)</li>
<li>Added logging to <code>build</code> and <code>build.env</code> (<code>PR [#333](https://github.com/pypa/build/issues/333)</code>_)</li>
<li>Switch to a TOML v1 compliant parser (<code>PR [#336](https://github.com/pypa/build/issues/336)</code><em>, Fixes <code>[#308](https://github.com/pypa/build/issues/308)</code></em>)</li>
</ul>
<h2>Breaking Changes</h2>
<ul>
<li>Dropped support for Python 2 and 3.5.</li>
</ul>
<p>.. _PR <a href="https://github-redirect.dependabot.com/pypa/build/issues/333">#333</a>: <a href="https://github-redirect.dependabot.com/pypa/build/pull/333">pypa/build#333</a>
.. _PR <a href="https://github-redirect.dependabot.com/pypa/build/issues/335">#335</a>: <a href="https://github-redirect.dependabot.com/pypa/build/pull/335">pypa/build#335</a>
.. _PR <a href="https://github-redirect.dependabot.com/pypa/build/issues/336">#336</a>: <a href="https://github-redirect.dependabot.com/pypa/build/pull/336">pypa/build#336</a>
.. _<a href="https://github-redirect.dependabot.com/pypa/build/issues/142">#142</a>: <a href="https://github-redirect.dependabot.com/pypa/build/issues/142">pypa/build#142</a>
.. _<a href="https://github-redirect.dependabot.com/pypa/build/issues/308">#308</a>: <a href="https://github-redirect.dependabot.com/pypa/build/issues/308">pypa/build#308</a>
.. _NO_COLOR: <a href="https://no-color.org">https://no-color.org</a></p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/pypa/build/commit/018a6f81cca1b26d9bf754eb91a64667c3847d28"><code>018a6f8</code></a> release 0.6.0.post1</li>
<li><a href="https://github.com/pypa/build/commit/84412dbbb29f08fdc0040ce75c81107843f7c496"><code>84412db</code></a> changelog: add Python 3.6 and 3.7 compability fix</li>
<li><a href="https://github.com/pypa/build/commit/c3f9e6d8c11c6ca3f71159a0347de171395cddd9"><code>c3f9e6d</code></a> build: restore compatibility with Python 3.6 and 3.7</li>
<li><a href="https://github.com/pypa/build/commit/720d69dadc1641769be89e3630b12b207e157ca3"><code>720d69d</code></a> tests: enable pytest strict mode</li>
<li><a href="https://github.com/pypa/build/commit/4cb0c1ec375aeff3e681855fdb37cf8f47e0fa93"><code>4cb0c1e</code></a> setup.cfg: fix typo in version</li>
<li><a href="https://github.com/pypa/build/commit/48c68979d95f070c093125b663dbd074399b5619"><code>48c6897</code></a> release 0.6.0</li>
<li><a href="https://github.com/pypa/build/commit/612dde4a4d538d1f0547ee524667b11534cba2cd"><code>612dde4</code></a> build: switch to tomli for TOML v1 compliant parser</li>
<li><a href="https://github.com/pypa/build/commit/7ed0a6bf06e05e6ccc72a99fe8194ebb9a2f1d20"><code>7ed0a6b</code></a> main: add line between backend output and missing dependencies error</li>
<li><a href="https://github.com/pypa/build/commit/79e777452cf3a75392b858f5ec41fc41eb4403b7"><code>79e7774</code></a> main: refactor color detection</li>
<li><a href="https://github.com/pypa/build/commit/03ad5d4af2cb23c13ab2de4672c359afbb99ab6b"><code>03ad5d4</code></a> main: add FORCE_COLOR</li>
<li>Additional commits viewable in <a href="https://github.com/pypa/build/compare/0.5.1...0.6.0.post1">compare view</a></li>
</ul>
</details>
<br />

[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=build&package-manager=pip&previous-version=0.5.1&new-version=0.6.0.post1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)

</details>
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.

None yet

4 participants