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

No additional newline with --stdout flag. #257

Merged

Conversation

magnunm
Copy link
Collaborator

@magnunm magnunm commented Jan 19, 2022

Fixes #250

@akaihola
Copy link
Owner

The test_stdout_path_resolution() unit test now fails as expected. And actually in that unit test we can see that the behavior was indeed wrong!

I'm also noticing that we didn't really have tests for the output behavior of --stdout. I think we need them, both for output to a TTY with and without Pygments, and for non-TTY output. Any idea how to set up those three cases?

@akaihola
Copy link
Owner

akaihola commented Jan 19, 2022

Ah, and the colorize test failure is probably due to a change in a new version of Pygments, and I'm fixing that in #258.

Actually @magnunm would you like to become a collaborator on the repository and review the fix in #258?

After merging #258, you should rebase your branch on master, and the colorization test should then pass again.


Also, by a lucky chance, the colorization test answers my question about testing TTY output (tty is True or False in test cases):

def test_colorize(text, lexer, tty, expect):
    """``colorize()`` produces correct highlighted terminal output"""
    with patch("sys.stdout.isatty", Mock(return_value=tty)):

        result = with_pygments.colorize(text, lexer)
    assert result in expect

@magnunm
Copy link
Collaborator Author

magnunm commented Jan 19, 2022

Will see what I can do about the --stdout tests!

@magnunm magnunm force-pushed the additional-newline-with-stdout-flag branch from e26309d to 5ffbf0b Compare January 23, 2022 14:04
@magnunm magnunm force-pushed the additional-newline-with-stdout-flag branch 3 times, most recently from 548ed94 to 56626f3 Compare January 23, 2022 17:45
@magnunm
Copy link
Collaborator Author

magnunm commented Jan 23, 2022

@akaihola Made an attempt in 56626f3, will look at it again later. Do you understand the Python <= 3.8 failure?
The pygments case also seems flaky, the two different variants are what I get running the whole test-suite and just the test_main.py... Maybe it is better to expect=highlight(...?

@akaihola
Copy link
Owner

akaihola commented Feb 2, 2022

Could you rebase on master since I now merged #258?

I'll take a look at the py<3.8 test failures next. Maybe a context manager syntax compatibility issue?

Comment on lines 811 to 818
with (
patch("sys.stdout.isatty", Mock(return_value=tty)),
patch(
"darker.__main__._import_pygments",
Mock(
return_value=darker.__main__._import_pygments(),
side_effect=None if with_pygments else ImportError(),
),
Copy link
Owner

Choose a reason for hiding this comment

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

Parenthesized context managers are a recent addition to Python syntax. We should support older versions using this old uglier syntax (made even worse by Black):

Suggested change
with (
patch("sys.stdout.isatty", Mock(return_value=tty)),
patch(
"darker.__main__._import_pygments",
Mock(
return_value=darker.__main__._import_pygments(),
side_effect=None if with_pygments else ImportError(),
),
with patch("sys.stdout.isatty", Mock(return_value=tty)), patch(
"darker.__main__._import_pygments",
Mock(
return_value=darker.__main__._import_pygments(),
side_effect=None if with_pygments else ImportError(),

@magnunm magnunm force-pushed the additional-newline-with-stdout-flag branch from 56626f3 to c10b39d Compare February 5, 2022 11:02
@akaihola akaihola added the bug Something isn't working label Feb 6, 2022
@akaihola akaihola added this to the 1.4.0 milestone Feb 6, 2022
Because `print()` adds a newline to the end of its string argument, the
output with `--stdout` contained an additional newline compared to
the result of modifying the file.
Unit-test for the `print_source` function used when the `--stdout`
option is applied. To test the behaviour with/without pygments the
importing of pygments was extracted to a function.
@magnunm magnunm force-pushed the additional-newline-with-stdout-flag branch from c10b39d to 66f38fb Compare February 6, 2022 17:54
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 6, 2022

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.41%.

Quality metrics Before After Change
Complexity 8.99 🙂 8.81 🙂 -0.18 👍
Method Length 98.48 🙂 96.78 🙂 -1.70 👍
Working memory 12.07 😞 12.00 😞 -0.07 👍
Quality 55.38% 🙂 55.79% 🙂 0.41% 👍
Other metrics Before After Change
Lines 1114 1170 56
Changed files Quality Before Quality After Quality Change
src/darker/main.py 41.05% 😞 41.99% 😞 0.94% 👍
src/darker/tests/test_main.py 67.95% 🙂 67.85% 🙂 -0.10% 👎

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
src/darker/main.py main 32 😞 416.97 ⛔ 15 😞 18.17% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
src/darker/tests/test_main.py test_main 6 ⭐ 433.96 ⛔ 25 ⛔ 30.26% 😞 Try splitting into smaller methods. Extract out complex expressions
src/darker/main.py _reformat_single_file 11 🙂 216 ⛔ 19 ⛔ 35.04% 😞 Try splitting into smaller methods. Extract out complex expressions
src/darker/tests/test_main.py test_format_edited_parts 0 ⭐ 195.99 😞 15 😞 50.84% 🙂 Try splitting into smaller methods. Extract out complex expressions
src/darker/tests/test_main.py test_reformat_single_file 0 ⭐ 113 🙂 18 ⛔ 56.42% 🙂 Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Copy link
Owner

@akaihola akaihola left a comment

Choose a reason for hiding this comment

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

This looks great, ready to merge in my opinion!

@akaihola akaihola merged commit c56dd8f into akaihola:master Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

Successfully merging this pull request may close these issues.

Output with --stdout flag contains an additional newline compared to without the flag
2 participants