Skip to content

Commit

Permalink
fix: Fix Windows tests
Browse files Browse the repository at this point in the history
get conditional global hosts as windows does not allow 0.0.0.0 host
  • Loading branch information
sansyrox committed Feb 13, 2023
1 parent 16bdfb4 commit 707cc30
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 19 deletions.
9 changes: 5 additions & 4 deletions integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import signal
import socket
import subprocess
import sys
import time
from typing import List
import platform

import pytest
from helpers.network_helpers import get_network_host


def spawn_process(command: List[str]) -> subprocess.Popen:
if sys.platform.startswith("win32"):
if platform.system() == "Windows":
command[0] = "python"
process = subprocess.Popen(
command, shell=True, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
Expand All @@ -22,7 +23,7 @@ def spawn_process(command: List[str]) -> subprocess.Popen:


def kill_process(process: subprocess.Popen) -> None:
if sys.platform.startswith("win32"):
if platform.system() == "Windows":
process.send_signal(signal.CTRL_BREAK_EVENT)
process.kill()
return
Expand Down Expand Up @@ -84,7 +85,7 @@ def default_session():

@pytest.fixture(scope="session")
def global_session():
domain = "0.0.0.0"
domain = get_network_host()
port = 8080
os.environ["ROBYN_URL"] = domain
process = start_server(domain, port)
Expand Down
Empty file.
File renamed without changes.
14 changes: 14 additions & 0 deletions integration_tests/helpers/network_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import socket
import platform


def get_network_host():
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
# windows return ip address else return 0.0.0.0
# windows doesn't support 0.0.0.0

if platform.system() == "Windows":
return ip_address
else:
return "0.0.0.0"
7 changes: 5 additions & 2 deletions integration_tests/test_base_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import requests

from helpers.network_helpers import get_network_host


def test_default_url_index_request(default_session):
BASE_URL = "http://127.0.0.1:8080"
Expand All @@ -17,9 +19,10 @@ def test_local_index_request(session):


def test_global_index_request(global_session):
BASE_URL = "http://0.0.0.0:8080"
host = get_network_host()
BASE_URL = f"http://{host}:8080"
res = requests.get(f"{BASE_URL}")
assert os.getenv("ROBYN_URL") == "0.0.0.0"
assert os.getenv("ROBYN_URL") == f"{host}"
assert res.status_code == 200


Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_basic_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest

from http_methods_helpers import get
from helpers.http_methods_helpers import get


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_delete_requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from http_methods_helpers import delete
from helpers.http_methods_helpers import delete


@pytest.mark.parametrize("function_type", ["sync", "async"])
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_file_download.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from http_methods_helpers import get
from helpers.http_methods_helpers import get


@pytest.mark.parametrize("function_type", ["sync", "async"])
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_get_requests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from requests import Response

from http_methods_helpers import get
from helpers.http_methods_helpers import get


@pytest.mark.parametrize("function_type", ["sync", "async"])
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_middlewares.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from http_methods_helpers import get
from helpers.http_methods_helpers import get


@pytest.mark.skip(reason="Fix middleware request headers modification")
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_patch_requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from http_methods_helpers import patch
from helpers.http_methods_helpers import patch


@pytest.mark.parametrize("function_type", ["sync", "async"])
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_post_requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from http_methods_helpers import post
from helpers.http_methods_helpers import post


@pytest.mark.parametrize("function_type", ["sync", "async"])
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_put_requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from http_methods_helpers import put
from helpers.http_methods_helpers import put


@pytest.mark.parametrize("function_type", ["sync", "async"])
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_status_code.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from http_methods_helpers import get
from helpers.http_methods_helpers import get


def test_404_status_code(session):
Expand Down
3 changes: 2 additions & 1 deletion robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import multiprocessing as mp
import os
from platform import platform
import signal
import sys
from typing import Callable, List, Optional
Expand Down Expand Up @@ -123,7 +124,7 @@ def start(self, url: str = "127.0.0.1", port: int = 8080):

def init_processpool(socket):
process_pool = []
if sys.platform.startswith("win32"):
if platform.system() == "Windows":
spawn_process(
self.directories,
self.request_headers,
Expand Down
9 changes: 6 additions & 3 deletions robyn/dev_event_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import subprocess
import sys
import platform

from watchdog.events import FileSystemEventHandler

Expand All @@ -9,9 +9,9 @@ def __init__(self, file_name) -> None:
self.file_name = file_name
self.processes = []
self.python_alias = (
"python3" if not sys.platform.startswith("win32") else "python"
"python3" if not self._system_is_windows() else "python"
)
self.shell = True if sys.platform.startswith("win32") else False
self.shell = True if self._system_is_windows() else False

def start_server_first_time(self) -> None:
if self.processes:
Expand All @@ -24,6 +24,9 @@ def start_server_first_time(self) -> None:
)
)

def _system_is_windows(self) -> bool:
return platform.system() == "Windows"

def on_any_event(self, event) -> None:
"""
This function is a callback that will start a new server on every even change
Expand Down

0 comments on commit 707cc30

Please sign in to comment.