|
1 | 1 | import os
|
2 | 2 | import socket
|
3 | 3 | import subprocess
|
4 |
| -import sys |
5 | 4 | import tempfile
|
6 | 5 | from unittest.mock import MagicMock, patch
|
7 | 6 |
|
@@ -430,23 +429,94 @@ def _new_isdir(path):
|
430 | 429 | expected_args = [
|
431 | 430 | "run",
|
432 | 431 | "--host",
|
433 |
| - "localhost", |
| 432 | + "127.0.0.1", |
434 | 433 | "--port",
|
435 | 434 | str(12345), # Check the mocked port
|
436 | 435 | "--path",
|
437 | 436 | temp_dir,
|
438 |
| - # "--log-path", |
439 |
| - # os.path.join(str(config.db_log_path), "chroma.log"), |
440 | 437 | ]
|
441 |
| - assert os.path.isfile(args[0]) and "chroma" in args[0], ( |
| 438 | + assert "chroma" in args[0], ( |
442 | 439 | f"{args[0]} should be the path to the `chroma` executable."
|
443 | 440 | )
|
444 | 441 | assert tuple(args[1:]) == tuple(expected_args)
|
445 |
| - assert kwargs["stdout"] == subprocess.DEVNULL |
446 |
| - assert kwargs["stderr"] == sys.stderr |
| 442 | + assert kwargs["stdout"] == subprocess.PIPE |
| 443 | + assert kwargs["stderr"] == subprocess.PIPE |
447 | 444 | assert "ANONYMIZED_TELEMETRY" in kwargs["env"]
|
448 | 445 |
|
449 |
| - MockWaitForServer.assert_called_once_with("localhost", 12345) |
| 446 | + MockWaitForServer.assert_called_once_with("127.0.0.1", 12345) |
| 447 | + |
| 448 | + assert process == mock_process |
| 449 | + mock_makedirs.assert_called_once_with(config.db_log_path) |
| 450 | + |
| 451 | + |
| 452 | +@pytest.mark.asyncio |
| 453 | +async def test_start_server_windows(): |
| 454 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 455 | + |
| 456 | + def _new_isdir(path): |
| 457 | + if str(temp_dir) in str(path): |
| 458 | + return True |
| 459 | + return False |
| 460 | + |
| 461 | + def _new_isfile(path): |
| 462 | + if "/bin/" in path: |
| 463 | + return False |
| 464 | + return True |
| 465 | + |
| 466 | + # Mock subprocess.Popen |
| 467 | + with ( |
| 468 | + patch("asyncio.create_subprocess_exec") as MockCreateProcess, |
| 469 | + patch("asyncio.sleep"), |
| 470 | + patch("socket.socket") as MockSocket, |
| 471 | + patch("vectorcode.common.wait_for_server") as MockWaitForServer, |
| 472 | + patch("os.path.isdir") as mock_isdir, |
| 473 | + patch("os.path.isfile") as mock_isfile, |
| 474 | + patch("os.makedirs") as mock_makedirs, |
| 475 | + ): |
| 476 | + mock_isdir.side_effect = _new_isdir |
| 477 | + mock_isfile.side_effect = _new_isfile |
| 478 | + # Mock socket to return a specific port |
| 479 | + mock_socket = MagicMock() |
| 480 | + mock_socket.getsockname.return_value = ("localhost", 12345) # Mock port |
| 481 | + MockSocket.return_value.__enter__.return_value = mock_socket |
| 482 | + |
| 483 | + # Mock the process object |
| 484 | + mock_process = MagicMock() |
| 485 | + mock_process.returncode = 0 # Simulate successful execution |
| 486 | + MockCreateProcess.return_value = mock_process |
| 487 | + |
| 488 | + # Create a config object |
| 489 | + config = Config( |
| 490 | + host="localhost", |
| 491 | + port=8000, |
| 492 | + db_path=temp_dir, |
| 493 | + project_root=temp_dir, |
| 494 | + ) |
| 495 | + |
| 496 | + # Call start_server |
| 497 | + process = await start_server(config) |
| 498 | + |
| 499 | + # Assert that asyncio.create_subprocess_exec was called with the correct arguments |
| 500 | + MockCreateProcess.assert_called_once() |
| 501 | + args, kwargs = MockCreateProcess.call_args |
| 502 | + expected_args = [ |
| 503 | + "run", |
| 504 | + "--host", |
| 505 | + "127.0.0.1", |
| 506 | + "--port", |
| 507 | + str(12345), # Check the mocked port |
| 508 | + "--path", |
| 509 | + temp_dir, |
| 510 | + ] |
| 511 | + assert "chroma" in args[0], ( |
| 512 | + f"{args[0]} should be the path to the `chroma` executable." |
| 513 | + ) |
| 514 | + assert tuple(args[1:]) == tuple(expected_args) |
| 515 | + assert kwargs["stdout"] == subprocess.PIPE |
| 516 | + assert kwargs["stderr"] == subprocess.PIPE |
| 517 | + assert "ANONYMIZED_TELEMETRY" in kwargs["env"] |
| 518 | + |
| 519 | + MockWaitForServer.assert_called_once_with("127.0.0.1", 12345) |
450 | 520 |
|
451 | 521 | assert process == mock_process
|
452 | 522 | mock_makedirs.assert_called_once_with(config.db_log_path)
|
|
0 commit comments