Skip to content

Fixed #35308 -- Caught and logged OSError when launching formatters.#18597

Merged
nessita merged 1 commit intodjango:mainfrom
jacobtylerwalls:run-formatters-try-except
Nov 29, 2024
Merged

Fixed #35308 -- Caught and logged OSError when launching formatters.#18597
nessita merged 1 commit intodjango:mainfrom
jacobtylerwalls:run-formatters-try-except

Conversation

@jacobtylerwalls
Copy link
Copy Markdown
Member

Trac ticket number

ticket-35308

Branch description

Catch and log out OSError when black cannot be launched.

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

Copy link
Copy Markdown
Contributor

@nessita nessita left a comment

Choose a reason for hiding this comment

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

Thank you @jacobtylerwalls! Great start 🌟

I added a few comments to iterate a bit on the logger and tests.

Copy link
Copy Markdown
Contributor

@nessita nessita left a comment

Choose a reason for hiding this comment

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

Thank you @jacobtylerwalls, I loved the proposal to use stderr. Added some further comments.

@jacobtylerwalls jacobtylerwalls changed the title Fixed #35308 -- Caught and logged OSError when launching black. Fixed #35308 -- Caught and logged OSError when launching formatters. Oct 23, 2024
Copy link
Copy Markdown
Contributor

@nessita nessita left a comment

Choose a reason for hiding this comment

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

Almost there! Thank you!

@nessita
Copy link
Copy Markdown
Contributor

nessita commented Nov 22, 2024

@jacobtylerwalls Thank you for your work and patience on this one 🌟

I have replied to the outstanding comment: I wanted to push a solution myself, after attempting a rebase and squash, which resulted in many conflicts.

Do you think you'd have some time to squash all commits into one and see if you have some ideas for the comment above?

@jacobtylerwalls jacobtylerwalls force-pushed the run-formatters-try-except branch 2 times, most recently from 66dd051 to 08d9c77 Compare November 22, 2024 17:17
@nessita
Copy link
Copy Markdown
Contributor

nessita commented Nov 27, 2024

@jacobtylerwalls Thank you for the latest work, the PR looks in great shape.

I was thinking whether considering some unification for all the duplicated (new) tests make sense. Have you given this a thought? I can't decide whether the extra helper would make sense or be worth it. I toyed with this idea and came up with this option, but would like to know your thoughts:

from io import StringIO
from unittest import mock


class AssertFormatterFailedToLaunch:

    def __init__(self, test):
        self.out = StringIO()
        self.err = StringIO()
        self.test = test

    def __enter__(self):
        self.mocker = mock.patch("django.core.management.utils.shutil.which", return_value="nonexistent")
        self.mocker.start()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.mocker.stop()
        self.test.assertIn("Formatters failed to launch", self.err.getvalue())

Then each new test would be:

from user_commands.utils import AssertFormatterFailedToLaunch

    def test_failure_to_format(self):
        with AssertFormatterFailedToLaunch(self) as f:
            call_command(
                "startapp", "mynewapp", directory=self.test_dir, stdout=f.out, stderr=f.err
            )

@jacobtylerwalls
Copy link
Copy Markdown
Member Author

That's interesting, and thanks for raising it. I did have a feeling this was too repetitive.

Four of the tests are already in a MigrationTestBase providing specialized assertions, so I just pushed a possibility that tries to blend in.

I do need to be aware of the temporary migration module handling, so folding that in seemed nice, and then leaving one single test for startapp without a helper assertion didn't feel too verbose. I'm open to alternatives. How does this look to you?

@nessita
Copy link
Copy Markdown
Contributor

nessita commented Nov 28, 2024

That's interesting, and thanks for raising it. I did have a feeling this was too repetitive.

Four of the tests are already in a MigrationTestBase providing specialized assertions, so I just pushed a possibility that tries to blend in.

I do need to be aware of the temporary migration module handling, so folding that in seemed nice, and then leaving one single test for startapp without a helper assertion didn't feel too verbose. I'm open to alternatives. How does this look to you?

I loved this proposal. Since you were on board, I doubled down on this mixing the two approaches. I like the result, so I'm pushing these changes, and if you don't object, I'll be merging this when CI is green.

Thank you for indulging me!

@nessita nessita force-pushed the run-formatters-try-except branch from 2419559 to b0857f1 Compare November 28, 2024 21:42
Copy link
Copy Markdown
Member Author

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

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

I think this works well! I left an idea about removing a parameter, but I'm also perfectly happy for you to merge it as is. Thanks for iterating with me.

@jacobtylerwalls jacobtylerwalls force-pushed the run-formatters-try-except branch from b0857f1 to af0141e Compare November 28, 2024 22:41
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
@nessita nessita force-pushed the run-formatters-try-except branch from af0141e to 1136341 Compare November 29, 2024 01:03
Copy link
Copy Markdown
Contributor

@nessita nessita left a comment

Choose a reason for hiding this comment

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

Thank you @jacobtylerwalls, this looks great! 🌟

I pushed the tiniest cleanup and now, for sure, will merge when CI is green 💯

@nessita nessita merged commit 58cc912 into django:main Nov 29, 2024
@jacobtylerwalls jacobtylerwalls deleted the run-formatters-try-except branch November 29, 2024 12:26
ulgens added a commit to ulgens/django-blasphemy that referenced this pull request Mar 7, 2025
ulgens added a commit to ulgens/django-blasphemy that referenced this pull request Mar 7, 2025
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.

2 participants