From e21aa5961d4f496d1b5e05b6afe580df7373ad4c Mon Sep 17 00:00:00 2001 From: Dennis Tomas Date: Tue, 1 Aug 2017 16:07:35 +0200 Subject: [PATCH 1/3] Allow setting the live server port for Django >= 1.11.2. --- pytest_django/fixtures.py | 15 +++++++++++---- pytest_django/live_server_helper.py | 10 +++++++++- tests/test_fixtures.py | 19 ++++++++++++++++--- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 692e0614a..ebb2a08d3 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -302,10 +302,17 @@ 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.') + elif django.VERSION < (1, 11, 2): + request.config.warn('D001', 'Specifying a live server port is not supported ' + 'in Django >= 1.11 < 1.11.2.') if not addr: if django.VERSION < (1, 11): diff --git a/pytest_django/live_server_helper.py b/pytest_django/live_server_helper.py index a220639b5..b07d0148e 100644 --- a/pytest_django/live_server_helper.py +++ b/pytest_django/live_server_helper.py @@ -37,9 +37,17 @@ def __init__(self, addr): host, possible_ports = parse_addr(addr) self.thread = LiveServerThread(host, possible_ports, **liveserver_kwargs) - else: + elif django.VERSION < (1, 11, 2): host = addr self.thread = LiveServerThread(host, **liveserver_kwargs) + else: + 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( ALLOWED_HOSTS={'append': host}, diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index d03a7d6fb..154b0f892 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -319,8 +319,8 @@ def test_serve_static_dj17_without_staticfiles_app(self, live_server, with pytest.raises(HTTPError): urlopen(live_server + '/static/a_file.txt').read() - @pytest.mark.skipif(get_django_version() < (1, 11), - reason='Django >= 1.11 required') + @pytest.mark.skipif(get_django_version() < (1, 11) or get_django_version() >= (1, 11, 2), + reason='Django >= 1.11 < 1.11.2 required') def test_specified_port_error_message_django_111(self, django_testdir): django_testdir.create_test_module(""" def test_with_live_server(live_server): @@ -329,7 +329,20 @@ def test_with_live_server(live_server): result = django_testdir.runpytest_subprocess('--liveserver=localhost:1234') result.stdout.fnmatch_lines([ - '*Specifying a live server port is not supported in Django 1.11. This ' + '*Specifying a live server port is not supported in Django >= 1.11 < 1.11.2.' + ]) + + @pytest.mark.skipif(get_django_version() < (1, 11), + reason='Django >= 1.11 required') + 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-2345') + result.stdout.fnmatch_lines([ + '*Specifying multiple live server ports is not supported in Django 1.11. This ' 'will be an error in a future pytest-django release.*' ]) From 14fa29430909bb9aaf7c11db5f9c75a59694befe Mon Sep 17 00:00:00 2001 From: Dennis Tomas Date: Sun, 19 Nov 2017 12:19:24 +0100 Subject: [PATCH 2/3] Removed warning when specifying live server port with Django >= 1.11 < 1.11.2. --- pytest_django/fixtures.py | 3 --- pytest_django/live_server_helper.py | 3 --- tests/test_fixtures.py | 13 ------------- 3 files changed, 19 deletions(-) diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index ebb2a08d3..917ac4e7d 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -310,9 +310,6 @@ def live_server(request): 'Specifying multiple live server ports is not supported ' 'in Django 1.11. This will be an error in a future ' 'pytest-django release.') - elif django.VERSION < (1, 11, 2): - request.config.warn('D001', 'Specifying a live server port is not supported ' - 'in Django >= 1.11 < 1.11.2.') if not addr: if django.VERSION < (1, 11): diff --git a/pytest_django/live_server_helper.py b/pytest_django/live_server_helper.py index b07d0148e..5d2464ed0 100644 --- a/pytest_django/live_server_helper.py +++ b/pytest_django/live_server_helper.py @@ -37,9 +37,6 @@ def __init__(self, addr): host, possible_ports = parse_addr(addr) self.thread = LiveServerThread(host, possible_ports, **liveserver_kwargs) - elif django.VERSION < (1, 11, 2): - host = addr - self.thread = LiveServerThread(host, **liveserver_kwargs) else: try: host, port = addr.split(':') diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index 154b0f892..a535333a7 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -319,19 +319,6 @@ def test_serve_static_dj17_without_staticfiles_app(self, live_server, with pytest.raises(HTTPError): urlopen(live_server + '/static/a_file.txt').read() - @pytest.mark.skipif(get_django_version() < (1, 11) or get_django_version() >= (1, 11, 2), - reason='Django >= 1.11 < 1.11.2 required') - def test_specified_port_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.stdout.fnmatch_lines([ - '*Specifying a live server port is not supported in Django >= 1.11 < 1.11.2.' - ]) - @pytest.mark.skipif(get_django_version() < (1, 11), reason='Django >= 1.11 required') def test_specified_port_range_error_message_django_111(self, django_testdir): From b4d0690d21808abcd959668b82ef77820a32e83e Mon Sep 17 00:00:00 2001 From: Dennis Tomas Date: Mon, 20 Nov 2017 15:46:19 +0100 Subject: [PATCH 3/3] Added test for specifying a live server port with Django >= 1.11.2. --- tests/test_fixtures.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index a535333a7..10ceac35c 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -6,6 +6,8 @@ from __future__ import with_statement +import socket + import pytest from django.db import connection, transaction @@ -333,6 +335,23 @@ def test_with_live_server(live_server): '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'