From a5c9124e1a7c6ab2d65b06f8543ef023e25e49a3 Mon Sep 17 00:00:00 2001
From: Andriy Ivaneyko <andriyivaneyko@gmail.com>
Date: Sun, 7 Jan 2024 13:11:02 -0500
Subject: [PATCH] 2882 Apply dynamic port fixture  for tests of cli, config and
 server events.

---
 tests/test_cli.py           | 108 +++++++++++++++++++-----------------
 tests/test_config.py        |   6 +-
 tests/test_server_events.py |  16 ++++--
 3 files changed, 72 insertions(+), 58 deletions(-)

diff --git a/tests/test_cli.py b/tests/test_cli.py
index 04ec0eded7..949f012d5e 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -14,6 +14,8 @@
 from sanic.__main__ import main
 from sanic.cli.inspector_client import InspectorClient
 
+from .conftest import get_port
+
 
 @pytest.fixture(scope="module", autouse=True)
 def tty():
@@ -58,16 +60,14 @@ def read_app_info(lines: List[str]):
     ),
 )
 def test_server_run(
-    appname: str,
-    extra: Optional[str],
-    caplog: pytest.LogCaptureFixture,
+    appname: str, extra: Optional[str], caplog: pytest.LogCaptureFixture, port
 ):
-    command = [appname]
+    command = [appname, f"-p={port}"]
     if extra:
         command.append(extra)
     lines = capture(command, caplog)
 
-    assert "Goin' Fast @ http://127.0.0.1:8000" in lines
+    assert f"Goin' Fast @ http://127.0.0.1:{port}" in lines
 
 
 @pytest.mark.parametrize(
@@ -77,17 +77,19 @@ def test_server_run(
         ["fake.server.create_app_with_args"],
     ),
 )
-def test_server_run_factory_with_args(caplog, command):
+def test_server_run_factory_with_args(caplog, command, port):
+    command.append(f"-p={port}")
     lines = capture(command, caplog)
 
     assert "target=fake.server.create_app_with_args" in lines
 
 
-def test_server_run_factory_with_args_arbitrary(caplog):
+def test_server_run_factory_with_args_arbitrary(caplog, port):
     command = [
         "fake.server.create_app_with_args",
         "--factory",
         "--foo=bar",
+        f"-p={port}",
     ]
     lines = capture(command, caplog)
 
@@ -112,16 +114,16 @@ def test_server_run_factory_with_args_arbitrary(caplog):
         ),
     ),
 )
-def test_tls_options(cmd: Tuple[str, ...], caplog):
+def test_tls_options(cmd: Tuple[str, ...], caplog, port):
     command = [
         "fake.server.app",
         *cmd,
-        "--port=9999",
+        f"--port={port}",
         "--debug",
         "--single-process",
     ]
     lines = capture(command, caplog)
-    assert "Goin' Fast @ https://127.0.0.1:9999" in lines
+    assert f"Goin' Fast @ https://127.0.0.1:{port}" in lines
 
 
 @pytest.mark.parametrize(
@@ -136,8 +138,8 @@ def test_tls_options(cmd: Tuple[str, ...], caplog):
         ("--tls-strict-host",),
     ),
 )
-def test_tls_wrong_options(cmd: Tuple[str, ...], caplog):
-    command = ["fake.server.app", *cmd, "-p=9999", "--debug"]
+def test_tls_wrong_options(cmd: Tuple[str, ...], caplog, port):
+    command = ["fake.server.app", *cmd, f"-p={port}", "--debug"]
     lines = capture(command, caplog)
 
     assert (
@@ -150,14 +152,15 @@ def test_tls_wrong_options(cmd: Tuple[str, ...], caplog):
 @pytest.mark.parametrize(
     "cmd",
     (
-        ("--host=localhost", "--port=9999"),
-        ("-H", "localhost", "-p", "9999"),
+        ("--host=localhost", "--port={port}"),
+        ("-H", "localhost", "-p", "{port}"),
     ),
 )
-def test_host_port_localhost(cmd: Tuple[str, ...], caplog):
+def test_host_port_localhost(cmd: Tuple[str, ...], caplog, port):
+    cmd = [c.format(port=str(port)) for c in cmd]
     command = ["fake.server.app", *cmd]
     lines = capture(command, caplog)
-    expected = "Goin' Fast @ http://localhost:9999"
+    expected = f"Goin' Fast @ http://localhost:{port}"
 
     assert expected in lines
 
@@ -166,28 +169,30 @@ def test_host_port_localhost(cmd: Tuple[str, ...], caplog):
     "cmd,expected",
     (
         (
-            ("--host=localhost", "--port=9999"),
-            "Goin' Fast @ http://localhost:9999",
+            ("--host=localhost", "--port={port}"),
+            "Goin' Fast @ http://localhost:{port}",
         ),
         (
-            ("-H", "localhost", "-p", "9999"),
-            "Goin' Fast @ http://localhost:9999",
+            ("-H", "localhost", "-p", "{port}"),
+            "Goin' Fast @ http://localhost:{port}",
         ),
         (
-            ("--host=127.0.0.127", "--port=9999"),
-            "Goin' Fast @ http://127.0.0.127:9999",
+            ("--host=127.0.0.1", "--port={port}"),
+            "Goin' Fast @ http://127.0.0.1:{port}",
         ),
         (
-            ("-H", "127.0.0.127", "-p", "9999"),
-            "Goin' Fast @ http://127.0.0.127:9999",
+            ("-H", "127.0.0.1", "-p", "{port}"),
+            "Goin' Fast @ http://127.0.0.1:{port}",
         ),
-        (("--host=::", "--port=9999"), "Goin' Fast @ http://[::]:9999"),
-        (("-H", "::", "-p", "9999"), "Goin' Fast @ http://[::]:9999"),
-        (("--host=::1", "--port=9999"), "Goin' Fast @ http://[::1]:9999"),
-        (("-H", "::1", "-p", "9999"), "Goin' Fast @ http://[::1]:9999"),
+        (("--host=::", "--port={port}"), "Goin' Fast @ http://[::]:{port}"),
+        (("-H", "::", "-p", "{port}"), "Goin' Fast @ http://[::]:{port}"),
+        (("--host=::1", "--port={port}"), "Goin' Fast @ http://[::1]:{port}"),
+        (("-H", "::1", "-p", "{port}"), "Goin' Fast @ http://[::1]:{port}"),
     ),
 )
-def test_host_port(cmd: Tuple[str, ...], expected: str, caplog):
+def test_host_port(cmd: Tuple[str, ...], expected: str, caplog, port):
+    cmd = [c.format(port=str(port)) for c in cmd]
+    expected = expected.format(port=str(port))
     command = ["fake.server.app", *cmd]
     lines = capture(command, caplog)
 
@@ -205,8 +210,8 @@ def test_host_port(cmd: Tuple[str, ...], expected: str, caplog):
         (4, ("-w", "4")),
     ),
 )
-def test_num_workers(num: int, cmd: Tuple[str, ...], caplog):
-    command = ["fake.server.app", *cmd]
+def test_num_workers(num: int, cmd: Tuple[str, ...], caplog, port):
+    command = ["fake.server.app", *cmd, f"-p={port}"]
     lines = capture(command, caplog)
 
     if num == 1:
@@ -218,8 +223,8 @@ def test_num_workers(num: int, cmd: Tuple[str, ...], caplog):
 
 
 @pytest.mark.parametrize("cmd", ("--debug",))
-def test_debug(cmd: str, caplog):
-    command = ["fake.server.app", cmd]
+def test_debug(cmd: str, caplog, port):
+    command = ["fake.server.app", cmd, f"-p={port}"]
     lines = capture(command, caplog)
     info = read_app_info(lines)
 
@@ -228,8 +233,8 @@ def test_debug(cmd: str, caplog):
 
 
 @pytest.mark.parametrize("cmd", ("--dev", "-d"))
-def test_dev(cmd: str, caplog):
-    command = ["fake.server.app", cmd]
+def test_dev(cmd: str, caplog, port):
+    command = ["fake.server.app", cmd, f"-p={port}"]
     lines = capture(command, caplog)
     info = read_app_info(lines)
 
@@ -238,13 +243,15 @@ def test_dev(cmd: str, caplog):
 
 
 @pytest.mark.parametrize("cmd", ("--auto-reload", "-r"))
-def test_auto_reload(cmd: str, caplog):
-    command = ["fake.server.app", cmd]
+def test_auto_reload(cmd: str, caplog, port):
+    command = ["fake.server.app", cmd, f"-p={port}"]
     lines = capture(command, caplog)
     info = read_app_info(lines)
 
-    assert info["debug"] is False
-    assert info["auto_reload"] is True
+    assert info["debug"] is False, f"Unexpected value of debug {info}"
+    assert (
+        info["auto_reload"] is True
+    ), f"Unexpected value of auto reload {info}"
 
 
 @pytest.mark.parametrize(
@@ -256,14 +263,18 @@ def test_auto_reload(cmd: str, caplog):
         ("--no-access-log", False),
     ),
 )
-def test_access_logs(cmd: str, expected: bool, caplog):
-    command = ["fake.server.app"]
+def test_access_logs(cmd: str, expected: bool, caplog, port):
+    command = ["fake.server.app", f"-p={port}"]
     if cmd:
         command.append(cmd)
     lines = capture(command, caplog)
+    print(lines)
     info = read_app_info(lines)
-
-    assert info["access_log"] is expected
+    if info["access_log"] != expected:
+        print(lines)
+    assert (
+        info["access_log"] is expected
+    ), f"Expected: {expected}. Received: {info}. Lines: {lines}"
 
 
 @pytest.mark.parametrize("cmd", ("--version", "-v"))
@@ -282,8 +293,8 @@ def test_version(cmd: str, caplog, capsys):
         ("--no-noisy-exceptions", False),
     ),
 )
-def test_noisy_exceptions(cmd: str, expected: bool, caplog):
-    command = ["fake.server.app", cmd]
+def test_noisy_exceptions(cmd: str, expected: bool, caplog, port):
+    command = ["fake.server.app", cmd, f"-p={port}"]
     lines = capture(command, caplog)
     info = read_app_info(lines)
 
@@ -346,10 +357,7 @@ def test_server_run_with_repl(caplog, capsys):
     )
 
     def run():
-        command = [
-            "fake.server.app",
-            "--repl",
-        ]
+        command = ["fake.server.app", "--repl", f"-p={get_port()}"]
         return capture(command, capsys=capsys)
 
     with patch("sanic.cli.app.is_atty", return_value=True):
@@ -359,5 +367,5 @@ def run():
     assert "Welcome to the Sanic interactive console" in result.err
     assert ">>> " in result.out
 
-    result = run()
+    run()
     assert record in caplog.record_tuples
diff --git a/tests/test_config.py b/tests/test_config.py
index a702bc880c..51abd6c4d6 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -17,6 +17,8 @@
 from sanic.constants import LocalCertCreator
 from sanic.exceptions import PyFileError
 
+from .conftest import get_port
+
 
 @contextmanager
 def temp_path():
@@ -309,12 +311,12 @@ async def _request(sanic, loop):
         app.stop()
 
     await app.create_server(
-        port=1341, access_log=False, return_asyncio_server=True
+        port=get_port(), access_log=False, return_asyncio_server=True
     )
     assert app.config.ACCESS_LOG is False
 
     await app.create_server(
-        port=1342, access_log=True, return_asyncio_server=True
+        port=get_port(), access_log=True, return_asyncio_server=True
     )
     assert app.config.ACCESS_LOG is True
 
diff --git a/tests/test_server_events.py b/tests/test_server_events.py
index 1e0c465392..df82350ae1 100644
--- a/tests/test_server_events.py
+++ b/tests/test_server_events.py
@@ -6,11 +6,13 @@
 
 import pytest
 
-from sanic_testing.testing import HOST, PORT
+from sanic_testing.testing import HOST
 
 from sanic import Blueprint
 from sanic.exceptions import BadRequest, SanicException
 
+from .conftest import get_port
+
 
 AVAILABLE_LISTENERS = [
     "before_server_start",
@@ -43,7 +45,9 @@ async def shutdown(app):
         app.stop()
 
     try:
-        random_name_app.run(HOST, PORT, single_process=True, **run_kwargs)
+        random_name_app.run(
+            HOST, get_port(), single_process=True, **run_kwargs
+        )
     except KeyboardInterrupt:
         pass
 
@@ -104,7 +108,7 @@ def test_all_listeners_as_convenience(app):
 
 
 @pytest.mark.asyncio
-async def test_trigger_before_events_create_server(app):
+async def test_trigger_before_events_create_server(app, port):
     class MySanicDb:
         pass
 
@@ -113,7 +117,7 @@ async def init_db(app, loop):
         app.ctx.db = MySanicDb()
 
     srv = await app.create_server(
-        debug=True, return_asyncio_server=True, port=PORT
+        debug=True, return_asyncio_server=True, port=port
     )
     await srv.startup()
     await srv.before_start()
@@ -198,13 +202,13 @@ async def after_stop(app, loop):
 
 
 @pytest.mark.asyncio
-async def test_missing_startup_raises_exception(app):
+async def test_missing_startup_raises_exception(app, port):
     @app.listener("before_server_start")
     async def init_db(app, loop):
         ...
 
     srv = await app.create_server(
-        debug=True, return_asyncio_server=True, port=PORT
+        debug=True, return_asyncio_server=True, port=port
     )
 
     with pytest.raises(SanicException):