Skip to content

Commit

Permalink
Mvdb/harvest command use argparse (#527)
Browse files Browse the repository at this point in the history
* use parser.add_argument() instead of make_option()

* default values for options are already defined in the parser

* verbosity=4 is now not supported anymore (cannot remove arg with argparse)

* do not define requires_system_checks for versions lower than 1.9

* verbosity default is 3

* requires_model_validation is useful for 1.6 and below
  • Loading branch information
Maxime Vdb authored and gabrielfalcao committed Sep 19, 2016
1 parent fb0f24f commit 6d68a72
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 125 deletions.
15 changes: 4 additions & 11 deletions lettuce/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import os import os
import sys import sys
import traceback import traceback
import warnings
try: try:
from imp import reload from imp import reload
except ImportError: except ImportError:
Expand Down Expand Up @@ -125,17 +124,11 @@ def __init__(self, base_path, scenarios=None,
from lettuce.plugins import dots as output from lettuce.plugins import dots as output
elif verbosity is 2: elif verbosity is 2:
from lettuce.plugins import scenario_names as output from lettuce.plugins import scenario_names as output
else: elif verbosity is 3:
if verbosity is 4: if no_color:
from lettuce.plugins import shell_output as output
else:
from lettuce.plugins import colored_shell_output as output from lettuce.plugins import colored_shell_output as output
msg = ('Deprecated in lettuce 2.2.21. Use verbosity 3 without '
'--no-color flag instead of verbosity 4')
warnings.warn(msg, DeprecationWarning)
elif verbosity is 3:
if no_color:
from lettuce.plugins import shell_output as output
else:
from lettuce.plugins import colored_shell_output as output


self.random = random self.random = random


Expand Down
219 changes: 105 additions & 114 deletions lettuce/django/management/commands/harvest.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
import os import os
import sys import sys
import traceback import traceback
import django
from distutils.version import StrictVersion from distutils.version import StrictVersion
from optparse import make_option
import django
from django.conf import settings from django.conf import settings
from django.core.management import call_command from django.core.management import call_command
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.test.utils import setup_test_environment from django.test.utils import setup_test_environment, teardown_test_environment
from django.test.utils import teardown_test_environment


from lettuce import Runner from lettuce import Runner
from lettuce import registry from lettuce import registry
Expand All @@ -35,110 +34,103 @@
from lettuce.django.server import LettuceServerException from lettuce.django.server import LettuceServerException




DJANGO_VERSION = StrictVersion(django.get_version())


class Command(BaseCommand): class Command(BaseCommand):
help = u'Run lettuce tests all along installed apps' help = u'Run lettuce tests all along installed apps'
args = '[PATH to feature file or folder]' args = '[PATH to feature file or folder]'
requires_model_validation = requires_system_checks = False

option_list = BaseCommand.option_list + (
make_option('-a', '--apps', action='store', dest='apps', default='',
help='Run ONLY the django apps that are listed here. Comma separated'),


make_option('-A', '--avoid-apps', action='store', dest='avoid_apps', default='', if DJANGO_VERSION < StrictVersion('1.7'):
help='AVOID running the django apps that are listed here. Comma separated'), requires_model_validation = False

else:
make_option('-S', '--no-server', action='store_true', dest='no_server', default=False, requires_system_checks = False
help="will not run django's builtin HTTP server"),

def add_arguments(self, parser):
make_option('--nothreading', action='store_false', dest='use_threading', default=True, parser.set_defaults(verbosity=3) # default verbosity is 3
help='Tells Django to NOT use threading.'), parser.add_argument(

'-a', '--apps', action='store', dest='apps', default='',
make_option('-T', '--test-server', action='store_true', dest='test_database', help='Run ONLY the django apps that are listed here. Comma separated'
)
parser.add_argument(
'-A', '--avoid-apps', action='store', dest='avoid_apps', default='',
help='AVOID running the django apps that are listed here. Comma separated'
)
parser.add_argument(
'-S', '--no-server', action='store_true', dest='no_server', default=False,
help="will not run django's builtin HTTP server"
)
parser.add_argument(
'--nothreading', action='store_false', dest='use_threading', default=True,
help='Tells Django to NOT use threading.'
)
parser.add_argument(
'-T', '--test-server', action='store_true', dest='test_database',
default=getattr(settings, "LETTUCE_USE_TEST_DATABASE", False), default=getattr(settings, "LETTUCE_USE_TEST_DATABASE", False),
help="will run django's builtin HTTP server using the test databases"), help="will run django's builtin HTTP server using the test databases"

)
make_option('-P', '--port', type='int', dest='port', parser.add_argument(
help="the port in which the HTTP server will run at"), '-P', '--port', type=int, dest='port',

help="the port in which the HTTP server will run at"
make_option('-d', '--debug-mode', action='store_true', dest='debug', default=False, )
help="when put together with builtin HTTP server, forces django to run with settings.DEBUG=True"), parser.add_argument(

'-d', '--debug-mode', action='store_true', dest='debug', default=False,
make_option('-s', '--scenarios', action='store', dest='scenarios', default=None, help="when put together with builtin HTTP server, forces django to run with settings.DEBUG=True"
help='Comma separated list of scenarios to run'), )

parser.add_argument(
make_option("-t", "--tag", '-s', '--scenarios', action='store', dest='scenarios', default=None,
dest="tags", help='Comma separated list of scenarios to run'
type="str", )
action='append', parser.add_argument(
default=None, "-t", "--tag", dest="tags", type=str, action='append', default=None,
help='Tells lettuce to run the specified tags only; ' help='Tells lettuce to run the specified tags only; '
'can be used multiple times to define more tags' 'can be used multiple times to define more tags'
'(prefixing tags with "-" will exclude them and ' '(prefixing tags with "-" will exclude them and '
'prefixing with "~" will match approximate words)'), 'prefixing with "~" will match approximate words)'

)
make_option('--with-xunit', action='store_true', dest='enable_xunit', default=False, parser.add_argument(
help='Output JUnit XML test results to a file'), '--with-xunit', action='store_true', dest='enable_xunit', default=False,

help='Output JUnit XML test results to a file'
make_option('--smtp-queue', action='store_true', dest='smtp_queue', default=False, )
help='Use smtp for mail queue (usefull with --no-server option'), parser.add_argument(

'--smtp-queue', action='store_true', dest='smtp_queue', default=False,
make_option('--xunit-file', action='store', dest='xunit_file', default=None, help='Use smtp for mail queue (usefull with --no-server option'
help='Write JUnit XML to this file. Defaults to lettucetests.xml'), )

parser.add_argument(
make_option('--with-subunit', '--xunit-file', action='store', dest='xunit_file', default=None,
action='store_true', help='Write JUnit XML to this file. Defaults to lettucetests.xml'
dest='enable_subunit', )
default=False, parser.add_argument(
help='Output Subunit test results to a file'), '--with-subunit', action='store_true', dest='enable_subunit',

default=False, help='Output Subunit test results to a file'
make_option('--subunit-file', )
action='store', parser.add_argument(
dest='subunit_file', '--subunit-file', action='store', dest='subunit_file', default=None,
default=None, help='Write Subunit to this file. Defaults to subunit.bin'
help='Write Subunit to this file. Defaults to subunit.bin'), )

parser.add_argument(
make_option('--with-jsonreport', '--with-jsonreport', action='store_true', dest='enable_jsonreport',
action='store_true', default=False, help='Output JSON test results to a file'
dest='enable_jsonreport', )
default=False, parser.add_argument(
help='Output JSON test results to a file'), '--jsonreport-file', action='store', dest='jsonreport_file',

default=None, help='Write JSON report to this file. Defaults to lettucetests.json'
make_option('--jsonreport-file', )
action='store', parser.add_argument(
dest='jsonreport_file', "--failfast", dest="failfast", default=False,
default=None, action="store_true", help='Stop running in the first failure'
help='Write JSON report to this file. Defaults to lettucetests.json'), )

parser.add_argument(
make_option("--failfast", dest="failfast", default=False, "--pdb", dest="auto_pdb", default=False, action="store_true",
action="store_true", help='Stop running in the first failure'), help='Launches an interactive debugger upon error'

)
make_option("--pdb", dest="auto_pdb", default=False, if DJANGO_VERSION < StrictVersion('1.7'):
action="store_true", help='Launches an interactive debugger upon error'),

)

def create_parser(self, prog_name, subcommand):
parser = super(Command, self).create_parser(prog_name, subcommand)
parser.remove_option('-v')
help_text = ('Verbosity level; 0=no output, 1=only dots, 2=only '
'scenario names, 3=normal output, 4=normal output '
'(colorful, deprecated)')
parser.add_option('-v', '--verbosity',
action='store',
dest='verbosity',
default='3',
type='choice',
choices=map(str, range(5)),
help=help_text)
if StrictVersion(django.get_version()) < StrictVersion('1.7'):
# Django 1.7 introduces the --no-color flag. We must add the flag # Django 1.7 introduces the --no-color flag. We must add the flag
# to be compatible with older django versions # to be compatible with older django versions
parser.add_option('--no-color', parser.add_argument(
action='store_true', '--no-color', action='store_true', dest='no_color',
dest='no_color', default=False, help="Don't colorize the command output."
default=False, )
help="Don't colorize the command output.")
return parser


def get_paths(self, args, apps_to_run, apps_to_avoid): def get_paths(self, args, apps_to_run, apps_to_avoid):
if args: if args:
Expand All @@ -156,18 +148,17 @@ def get_paths(self, args, apps_to_run, apps_to_avoid):
def handle(self, *args, **options): def handle(self, *args, **options):
setup_test_environment() setup_test_environment()


verbosity = int(options.get('verbosity', 3)) verbosity = options['verbosity']
no_color = int(options.get('no_color', False)) no_color = options.get('no_color', False)
apps_to_run = tuple(options.get('apps', '').split(",")) apps_to_run = tuple(options['apps'].split(","))
apps_to_avoid = tuple(options.get('avoid_apps', '').split(",")) apps_to_avoid = tuple(options['avoid_apps'].split(","))
run_server = not options.get('no_server', False) run_server = not options['no_server']
test_database = options.get('test_database', False) test_database = options['test_database']
smtp_queue = options.get('smtp_queue', False) smtp_queue = options['smtp_queue']
tags = options.get('tags', None) tags = options['tags']
failfast = options.get('failfast', False) failfast = options['failfast']
auto_pdb = options.get('auto_pdb', False) auto_pdb = options['auto_pdb']
threading = options.get('use_threading', True) threading = options['use_threading']
with_summary = options.get('summary_display', False)


if test_database: if test_database:
migrate_south = getattr(settings, "SOUTH_TESTS_MIGRATE", True) migrate_south = getattr(settings, "SOUTH_TESTS_MIGRATE", True)
Expand All @@ -183,7 +174,7 @@ def handle(self, *args, **options):
self._testrunner.setup_test_environment() self._testrunner.setup_test_environment()
self._old_db_config = self._testrunner.setup_databases() self._old_db_config = self._testrunner.setup_databases()


if StrictVersion(django.get_version()) < StrictVersion('1.7'): if DJANGO_VERSION < StrictVersion('1.7'):
call_command('syncdb', verbosity=0, interactive=False,) call_command('syncdb', verbosity=0, interactive=False,)
if migrate_south: if migrate_south:
call_command('migrate', verbosity=0, interactive=False,) call_command('migrate', verbosity=0, interactive=False,)
Expand Down

0 comments on commit 6d68a72

Please sign in to comment.