Refs #33476 -- Added warning to optimizemigration/squashmigrations commands when black cannot be applied.

This commit is contained in:
Mariusz Felisiak 2022-02-23 07:29:15 +01:00 committed by GitHub
parent 7fd2deb3e8
commit d11944be34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 8 deletions

View File

@ -1,3 +1,4 @@
import shutil
import sys
from django.apps import apps
@ -111,6 +112,13 @@ class Command(BaseCommand):
" See the comment at the top of the optimized migration for "
"details."
)
if shutil.which("black"):
self.stdout.write(
self.style.WARNING(
"Optimized migration couldn't be formatted using the "
'"black" command. You can call it manually.'
)
)
with open(writer.path, "w", encoding="utf-8") as fh:
fh.write(migration_file_string)
run_formatters([writer.path])

View File

@ -1,4 +1,5 @@
import os
import shutil
from django.apps import apps
from django.conf import settings
@ -244,6 +245,13 @@ class Command(BaseCommand):
" See the comment at the top of the squashed migration for "
"details."
)
if shutil.which("black"):
self.stdout.write(
self.style.WARNING(
"Squashed migration couldn't be formatted using the "
'"black" command. You can call it manually.'
)
)
def find_migration(self, loader, app_label, name):
try:

View File

@ -2729,6 +2729,12 @@ class SquashMigrationsTests(MigrationTestBase):
"0001_squashed_0002_second.py",
)
self.assertTrue(os.path.exists(squashed_migration_file))
black_warning = ""
if HAS_BLACK:
black_warning = (
"Squashed migration couldn't be formatted using the "
'"black" command. You can call it manually.\n'
)
self.assertEqual(
out.getvalue(),
f"Will squash the following migrations:\n"
@ -2746,7 +2752,8 @@ class SquashMigrationsTests(MigrationTestBase):
f" Your migrations contained functions that must be manually copied "
f"over,\n"
f" as we could not safely copy their implementation.\n"
f" See the comment at the top of the squashed migration for details.\n",
f" See the comment at the top of the squashed migration for details.\n"
+ black_warning,
)
@ -2905,15 +2912,21 @@ class OptimizeMigrationTests(MigrationTestBase):
with open(optimized_migration_file) as fp:
content = fp.read()
self.assertIn("replaces = [", content)
black_warning = ""
if HAS_BLACK:
black_warning = (
"Optimized migration couldn't be formatted using the "
'"black" command. You can call it manually.\n'
)
self.assertEqual(
out.getvalue(),
f"Optimizing from 3 operations to 2 operations.\n"
f"Manual porting required\n"
f" Your migrations contained functions that must be manually copied over,"
f"\n"
f" as we could not safely copy their implementation.\n"
f" See the comment at the top of the optimized migration for details.\n"
f"Optimized migration {optimized_migration_file}\n",
"Optimizing from 3 operations to 2 operations.\n"
"Manual porting required\n"
" Your migrations contained functions that must be manually copied over,\n"
" as we could not safely copy their implementation.\n"
" See the comment at the top of the optimized migration for details.\n"
+ black_warning
+ f"Optimized migration {optimized_migration_file}\n",
)
def test_fails_squash_migration_manual_porting(self):