Skip to content

Commit

Permalink
Support for setting the live server port (pytest-dev#500)
Browse files Browse the repository at this point in the history
* Allow setting the live server port for Django >= 1.11.2.
* Removed warning when specifying live server port with Django >= 1.11 < 1.11.2.
* Added test for specifying a live server port with Django >= 1.11.2.
  • Loading branch information
dtomas authored and timb07 committed May 26, 2018
1 parent c9a0936 commit d8c6d13
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
12 changes: 8 additions & 4 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,14 @@ def live_server(request):
addr = (request.config.getvalue('liveserver') or
os.getenv('DJANGO_LIVE_TEST_SERVER_ADDRESS'))

if addr and django.VERSION >= (1, 11) and ':' in addr:
request.config.warn('D001', 'Specifying a live server port is not supported '
'in Django 1.11. This will be an error in a future '
'pytest-django release.')
if addr and ':' in addr:
if django.VERSION >= (1, 11):
ports = addr.split(':')[1]
if '-' in ports or ',' in ports:
request.config.warn('D001',
'Specifying multiple live server ports is not supported '
'in Django 1.11. This will be an error in a future '
'pytest-django release.')

if not addr:
if django.VERSION < (1, 11):
Expand Down
7 changes: 6 additions & 1 deletion pytest_django/live_server_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ def __init__(self, addr):
self.thread = LiveServerThread(host, possible_ports,
**liveserver_kwargs)
else:
host = addr
try:
host, port = addr.split(':')
except ValueError:
host = addr
else:
liveserver_kwargs['port'] = int(port)
self.thread = LiveServerThread(host, **liveserver_kwargs)

self._live_server_modified_settings = modify_settings(
Expand Down
25 changes: 22 additions & 3 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from __future__ import with_statement

import socket

import pytest

from django.db import connection, transaction
Expand Down Expand Up @@ -321,18 +323,35 @@ def test_serve_static_dj17_without_staticfiles_app(self, live_server,

@pytest.mark.skipif(get_django_version() < (1, 11),
reason='Django >= 1.11 required')
def test_specified_port_error_message_django_111(self, django_testdir):
def test_specified_port_range_error_message_django_111(self, django_testdir):
django_testdir.create_test_module("""
def test_with_live_server(live_server):
pass
""")

result = django_testdir.runpytest_subprocess('--liveserver=localhost:1234')
result = django_testdir.runpytest_subprocess('--liveserver=localhost:1234-2345')
result.stdout.fnmatch_lines([
'*Specifying a live server port is not supported in Django 1.11. This '
'*Specifying multiple live server ports is not supported in Django 1.11. This '
'will be an error in a future pytest-django release.*'
])

@pytest.mark.skipif(get_django_version() < (1, 11, 2),
reason='Django >= 1.11.2 required')
def test_specified_port_django_111(self, django_testdir):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(('', 0))
__, port = sock.getsockname()
finally:
sock.close()

django_testdir.create_test_module("""
def test_with_live_server(live_server):
assert live_server.port == %d
""" % port)

django_testdir.runpytest_subprocess('--liveserver=localhost:%s' % port)


@pytest.mark.django_project(extra_settings="""
AUTH_USER_MODEL = 'app.MyCustomUser'
Expand Down

0 comments on commit d8c6d13

Please sign in to comment.