test_helpers.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import contextlib
  2. import socket
  3. import time
  4. import webbrowser
  5. from pathlib import Path
  6. from nicegui import helpers
  7. TEST_DIR = Path(__file__).parent
  8. def test_is_port_open():
  9. with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
  10. sock.bind(('127.0.0.1', 0)) # port = 0 => the OS chooses a port for us
  11. sock.listen(1)
  12. host, port = sock.getsockname()
  13. assert not helpers.is_port_open(host, port), 'after closing the socket, the port should be free'
  14. with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
  15. sock.bind(('127.0.0.1', port))
  16. sock.listen(1)
  17. assert helpers.is_port_open(host, port), 'after opening the socket, the port should be detected'
  18. def test_is_port_open_on_bad_ip():
  19. assert not helpers.is_port_open('1.2', 0), 'should not be able to connect to a bad IP'
  20. def test_schedule_browser(monkeypatch):
  21. called_with_url = None
  22. def mock_webbrowser_open(url):
  23. nonlocal called_with_url
  24. called_with_url = url
  25. monkeypatch.setattr(webbrowser, 'open', mock_webbrowser_open)
  26. with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
  27. sock.bind(('127.0.0.1', 0))
  28. host, port = sock.getsockname()
  29. _, cancel_event = helpers.schedule_browser(host, port)
  30. try:
  31. # port bound, but not opened yet
  32. assert called_with_url is None
  33. sock.listen()
  34. # port opened
  35. time.sleep(1)
  36. assert called_with_url == f'http://{host}:{port}/'
  37. finally:
  38. cancel_event.set()
  39. def test_canceling_schedule_browser(monkeypatch):
  40. called_with_url = None
  41. def mock_webbrowser_open(url):
  42. nonlocal called_with_url
  43. called_with_url = url
  44. monkeypatch.setattr(webbrowser, 'open', mock_webbrowser_open)
  45. # find a free port ...
  46. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  47. sock.bind(('127.0.0.1', 0))
  48. sock.listen(1)
  49. host, port = sock.getsockname()
  50. # ... and close it so schedule_browser does not launch the browser
  51. sock.close()
  52. thread, cancel_event = helpers.schedule_browser(host, port)
  53. time.sleep(0.2)
  54. cancel_event.set()
  55. time.sleep(0.2)
  56. assert not thread.is_alive()
  57. assert called_with_url is None
  58. def test_is_file():
  59. assert helpers.is_file(TEST_DIR / 'test_helpers.py')
  60. assert helpers.is_file(str(TEST_DIR / 'test_helpers.py'))
  61. assert not helpers.is_file(TEST_DIR / 'nonexistent_file')
  62. assert not helpers.is_file(str(TEST_DIR / 'nonexistent_file'))
  63. assert not helpers.is_file('data:image/png;base64,...')
  64. assert not helpers.is_file(None)
  65. assert not helpers.is_file('x' * 100_000), 'a very long filepath should not lead to OSError 63'
  66. assert not helpers.is_file('http://nicegui.io/logo.png')