From d782655ff45bf6124d440d7a0a672528c2554367 Mon Sep 17 00:00:00 2001 From: Nicolas ROCHE Date: Wed, 7 Aug 2019 10:43:37 +0200 Subject: [PATCH] data: raise a CommandError if import-site fails when roles are missing (#35289) --- combo/data/management/commands/import_site.py | 4 ++-- tests/test_import_export.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/combo/data/management/commands/import_site.py b/combo/data/management/commands/import_site.py index c0aaf630..509a5377 100644 --- a/combo/data/management/commands/import_site.py +++ b/combo/data/management/commands/import_site.py @@ -17,7 +17,7 @@ import json import sys -from django.core.management.base import BaseCommand +from django.core.management.base import BaseCommand, CommandError from django.utils.encoding import force_text from combo.data.utils import import_site, MissingGroups @@ -45,4 +45,4 @@ class Command(BaseCommand): if_empty=options['if_empty'], clean=options['clean']) except MissingGroups as e: - sys.stderr.write(force_text(e) + '\n') + raise CommandError(e) diff --git a/tests/test_import_export.py b/tests/test_import_export.py index 3bfef3ce..9661c361 100644 --- a/tests/test_import_export.py +++ b/tests/test_import_export.py @@ -10,6 +10,7 @@ import pytest from django.contrib.auth.models import Group from django.core.files import File from django.core.management import call_command +from django.core.management.base import CommandError from django.utils.encoding import force_bytes, force_text from django.utils.six import BytesIO, StringIO @@ -161,6 +162,13 @@ def test_group_restrictions_import_export(app, some_data): assert excinfo.value.names == ['A Group'] + with pytest.raises(CommandError, match='Missing groups: A Group'): + with tempfile.NamedTemporaryFile() as f: + f.write(force_bytes(output)) + f.flush() + call_command('import_site', f.name, clean=True) + assert Page.objects.count() == 0 + group = Group(name='A Group') group.save()