Opened 3 weeks ago

Closed 2 weeks ago

Last modified 4 days ago

#36680 closed Bug (fixed)

Formatters run on `TemplateCommand` make `admin_scripts` tests brittle

Reported by: Natalia Bidart Owned by: Jacob Walls
Component: Core (Management commands) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Some admin_scripts tests, such as those for startproject, assert literal file contents generated from templates. Because TemplateCommand runs formatters before exiting (run_formatters(...)), the output can differ depending on environment details such as temporary directory path length or whether black is installed.

For example, when black reformats long lines during parallel test runs, strings in the generated manage.py file are wrapped in parentheses, causing assertion failures.

Tests should not depend on the exact output of formatters. Mocking or disabling them during test runs would make the suite more stable.

Change History (15)

comment:1 by Natalia Bidart, 3 weeks ago

Has patch: set

comment:2 by Jacob Walls, 3 weeks ago

Triage Stage: UnreviewedAccepted

comment:3 by Natalia Bidart, 3 weeks ago

Owner: changed from Natalia Bidart to Jacob Walls

comment:4 by Natalia Bidart, 3 weeks ago

Triage Stage: AcceptedReady for checkin

comment:5 by Natalia Bidart, 3 weeks ago

Triage Stage: Ready for checkinAccepted

comment:6 by Natalia Bidart, 3 weeks ago

Patch needs improvement: set

There are other tests failures that need to be logged at.

comment:7 by Jacob Walls, 3 weeks ago

Patch needs improvement: unset

I expect some more discussion about the merit of the approach, but I pushed some edits that should handle those failures.

comment:8 by Natalia Bidart, 2 weeks ago

Triage Stage: AcceptedReady for checkin

comment:9 by Jacob Walls <jacobtylerwalls@…>, 2 weeks ago

Resolution: fixed
Status: assignedclosed

In 6436ec32:

Fixed #36680 -- Parametrized formatter discovery in AdminScriptTestCase.

comment:10 by Natalia <124304+nessita@…>, 2 weeks ago

In 32ef9579:

[6.0.x] Fixed #36680 -- Parametrized formatter discovery in AdminScriptTestCase.

Backport of 6436ec321073bf0622af815e0af08f54c97f9b30 from main.

comment:11 by GitHub <noreply@…>, 2 weeks ago

In 3939cd27:

Refs #36680 -- Fixed admin_scripts tests crash when black is not installed.

Regression in 6436ec321073bf0622af815e0af08f54c97f9b30.

comment:12 by Natalia <124304+nessita@…>, 2 weeks ago

In 11cfb57:

[6.0.x] Refs #36680 -- Fixed admin_scripts tests crash when black is not installed.

Regression in 6436ec321073bf0622af815e0af08f54c97f9b30.
Backport of 3939cd279569fde44f557d79f20bb5b1a02440af from main.

comment:13 by Hal Blackburn, 10 days ago

I realise this ticket is closed, but I ran into this issue again (despite it being fixed) when I was working on the PR for ticket:36704. I found several admin_scripts tests were failing on a clean checkout after following the repo's instructions to run tests. The fix here didn't stop the tests failing when black is on the PATH in more than one place. (e.g. when it's installed globally via pipx or a system package manager). I've got a PR to extend the current fix to handle multiple instances of black on the PATH, but I've not discussed here ahead of time as it was a small thing to fix while working on the other ticket: https://github.com/django/django/pull/20054

comment:14 by Jacob Walls <jacobtylerwalls@…>, 4 days ago

In e78420c2:

Refs #36680 -- Avoided manipulating PATH in AdminScriptTestCase.

This mostly reverts 6436ec321073bf0622af815e0af08f54c97f9b30,
which was fragile. Instead, if black is present, we use it to format the
expected and actual results, instead of hard-coding the expected
formatted value.

Co-authored-by: Natalia <124304+nessita@…>

comment:15 by Jacob Walls <jacobtylerwalls@…>, 4 days ago

In a26b660c:

Refs #36680 -- Avoided manipulating PATH in AdminScriptTestCase.

This mostly reverts 6436ec321073bf0622af815e0af08f54c97f9b30,
which was fragile. Instead, if black is present, we use it to format the
expected and actual results, instead of hard-coding the expected
formatted value.

Co-authored-by: Natalia <124304+nessita@…>

Note: See TracTickets for help on using tickets.
Back to Top