1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
# file scalene/launchbrowser.py:148-169
# lines [148, 149, 150, 152, 153, 154, 155, 156, 157, 158, 159, 160, 162, 163, 165, 168, 169]
# branches ['149->150', '149->152', '153->154', '153->156']
import os
import pytest
import shutil
import tempfile
import threading
import time
import webbrowser
from unittest.mock import patch
# Mocks for the functions and variables not provided in the snippet
HOST = "localhost"
def run_server(host, port):
pass
def monitor_heartbeat():
pass
def generate_html(input_file, output_file):
with open(output_file, "w") as f:
f.write("<html><body>Demo</body></html>")
class Filename(str):
pass
def is_port_available(port):
return True
def start(filename: str, port: int) -> None:
while not is_port_available(port):
port += 1
cwd = os.getcwd()
if filename == "demo":
generate_html(Filename("demo"), Filename("demo.html"))
filename = "demo.html"
shutil.copy(filename, os.path.join(tempfile.gettempdir(), 'index.html'))
os.chdir(tempfile.gettempdir())
server_thread = threading.Thread(target=run_server, args=[HOST, port])
server_thread.start()
threading.Thread(target=monitor_heartbeat).start()
webbrowser.open_new(f'http://{HOST}:{port}/')
server_thread.join()
os.chdir(cwd)
# Optional: a delay to ensure all resources are released
time.sleep(1)
os._exit(0) # Forcefully stops the program
@pytest.fixture
def setup_and_teardown():
# Setup
original_cwd = os.getcwd()
temp_dir = tempfile.gettempdir()
temp_index_html = os.path.join(temp_dir, 'index.html')
# Store the original file if it exists
original_index_html = temp_index_html + ".bak"
if os.path.exists(temp_index_html):
shutil.move(temp_index_html, original_index_html)
yield temp_dir, original_cwd
# Teardown
os.chdir(original_cwd)
if os.path.exists(original_index_html):
shutil.move(original_index_html, temp_index_html)
def test_start(setup_and_teardown):
temp_dir, original_cwd = setup_and_teardown
test_port = 8000
test_filename = "test.html"
with open(test_filename, "w") as f:
f.write("<html><body>Test</body></html>")
with patch('webbrowser.open_new') as mock_open_new:
with patch('os._exit') as mock_exit:
with patch('scalene.launchbrowser.is_port_available', return_value=True):
start(test_filename, test_port)
mock_open_new.assert_called_with(f'http://{HOST}:{test_port}/')
mock_exit.assert_called_with(0)
assert os.path.exists(os.path.join(temp_dir, 'index.html'))
with open(os.path.join(temp_dir, 'index.html'), 'r') as f:
content = f.read()
assert content == "<html><body>Test</body></html>"
os.remove(test_filename)
|