|
1 |
| -import importlib.metadata |
2 | 1 | import os
|
3 |
| -import sys |
| 2 | +import tempfile |
4 | 3 | import unittest
|
5 | 4 |
|
6 | 5 | import pytest
|
7 | 6 |
|
8 | 7 | import autogen
|
9 | 8 | from autogen.code_utils import (
|
10 |
| - PATH_SEPARATOR, |
11 | 9 | UNKNOWN,
|
12 |
| - WIN32, |
13 | 10 | content_str,
|
14 | 11 | execute_code,
|
15 | 12 | extract_code,
|
|
21 | 18 | decide_use_docker,
|
22 | 19 | check_can_use_docker_or_throw,
|
23 | 20 | )
|
| 21 | +from conftest import skip_docker |
24 | 22 |
|
25 | 23 | KEY_LOC = "notebook"
|
26 | 24 | OAI_CONFIG_LIST = "OAI_CONFIG_LIST"
|
27 | 25 | here = os.path.abspath(os.path.dirname(__file__))
|
28 | 26 |
|
29 | 27 |
|
30 |
| -def is_package_installed(package_name): |
31 |
| - """Check if a package is installed. This is a preferred way to check if a |
32 |
| - package is installed or not than doing a try-catch around an import |
33 |
| - because it avoids name conflict with local modules and |
34 |
| - code execution in the imported module.""" |
35 |
| - try: |
36 |
| - importlib.metadata.version(package_name) |
37 |
| - return True |
38 |
| - except importlib.metadata.PackageNotFoundError: |
39 |
| - return False |
40 |
| - |
41 |
| - |
42 |
| -docker_package_installed = is_package_installed("docker") |
43 |
| - |
44 |
| - |
45 | 28 | # def test_find_code():
|
46 | 29 | # try:
|
47 | 30 | # import openai
|
@@ -313,86 +296,119 @@ def scrape(url):
|
313 | 296 | assert len(codeblocks) == 1 and codeblocks[0] == ("", "source setup.sh")
|
314 | 297 |
|
315 | 298 |
|
316 |
| -# skip if os is windows |
317 | 299 | @pytest.mark.skipif(
|
318 |
| - sys.platform in ["win32"] or (not is_docker_running() and not in_docker_container()), reason="docker is not running" |
| 300 | + skip_docker or not is_docker_running(), |
| 301 | + reason="docker is not running or requested to skip docker tests", |
319 | 302 | )
|
320 |
| -def test_execute_code(use_docker=None): |
321 |
| - try: |
322 |
| - import docker |
323 |
| - except ImportError as exc: |
324 |
| - print(exc) |
325 |
| - docker = None |
326 |
| - if use_docker is None: |
327 |
| - use_docker = docker is not None |
328 |
| - exit_code, msg, image = execute_code("print('hello world')", filename="tmp/codetest.py", use_docker=use_docker) |
329 |
| - assert exit_code == 0 and msg == "hello world\n", msg |
330 |
| - # read a file |
331 |
| - print(execute_code("with open('tmp/codetest.py', 'r') as f: a=f.read()", use_docker=use_docker)) |
332 |
| - # create a file |
333 |
| - exit_code, msg, image = execute_code( |
334 |
| - "with open('tmp/codetest.py', 'w') as f: f.write('b=1')", |
335 |
| - work_dir=f"{here}/my_tmp", |
336 |
| - filename="tmp2/codetest.py", |
337 |
| - use_docker=use_docker, |
338 |
| - ) |
339 |
| - assert exit_code and ( |
340 |
| - 'File "tmp2/codetest.py"'.replace("/", PATH_SEPARATOR) in msg |
341 |
| - or 'File ".\\tmp2/codetest.py' in msg # py3.8 + win32 |
342 |
| - ), msg |
343 |
| - print( |
344 |
| - execute_code( |
345 |
| - "with open('tmp/codetest.py', 'w') as f: f.write('b=1')", work_dir=f"{here}/my_tmp", use_docker=use_docker |
| 303 | +def test_execute_code(use_docker=True): |
| 304 | + # Test execute code and save the code to a file. |
| 305 | + with tempfile.TemporaryDirectory() as tempdir: |
| 306 | + filename = "temp_file_with_code.py" |
| 307 | + |
| 308 | + # execute code and save the code to a file. |
| 309 | + exit_code, msg, image = execute_code( |
| 310 | + "print('hello world')", |
| 311 | + filename=filename, |
| 312 | + work_dir=tempdir, |
| 313 | + use_docker=use_docker, |
346 | 314 | )
|
347 |
| - ) |
348 |
| - # execute code in a file |
349 |
| - print(execute_code(filename="tmp/codetest.py", use_docker=use_docker)) |
350 |
| - print(execute_code("python tmp/codetest.py", lang="sh", use_docker=use_docker)) |
351 |
| - # execute code for assertion error |
352 |
| - exit_code, msg, image = execute_code("assert 1==2", use_docker=use_docker) |
353 |
| - assert exit_code, msg |
354 |
| - assert 'File ""' in msg or 'File ".\\"' in msg # py3.8 + win32 |
355 |
| - # execute code which takes a long time |
356 |
| - exit_code, error, image = execute_code("import time; time.sleep(2)", timeout=1, use_docker=use_docker) |
357 |
| - assert exit_code and error == "Timeout" or WIN32 |
358 |
| - assert isinstance(image, str) or docker is None or os.path.exists("/.dockerenv") or use_docker is False |
| 315 | + assert exit_code == 0 and msg == "hello world\n", msg |
| 316 | + |
| 317 | + # read the file just saved |
| 318 | + exit_code, msg, image = execute_code( |
| 319 | + f"with open('{filename}', 'rt') as f: print(f.read())", |
| 320 | + use_docker=use_docker, |
| 321 | + work_dir=tempdir, |
| 322 | + ) |
| 323 | + assert exit_code == 0 and "print('hello world')" in msg, msg |
| 324 | + |
| 325 | + # execute code in a file |
| 326 | + exit_code, msg, image = execute_code( |
| 327 | + filename=filename, |
| 328 | + use_docker=use_docker, |
| 329 | + work_dir=tempdir, |
| 330 | + ) |
| 331 | + assert exit_code == 0 and msg == "hello world\n", msg |
| 332 | + |
| 333 | + # execute code in a file using shell command directly |
| 334 | + exit_code, msg, image = execute_code( |
| 335 | + f"python {filename}", |
| 336 | + lang="sh", |
| 337 | + use_docker=use_docker, |
| 338 | + work_dir=tempdir, |
| 339 | + ) |
| 340 | + assert exit_code == 0 and msg == "hello world\n", msg |
| 341 | + |
| 342 | + with tempfile.TemporaryDirectory() as tempdir: |
| 343 | + # execute code for assertion error |
| 344 | + exit_code, msg, image = execute_code( |
| 345 | + "assert 1==2", |
| 346 | + use_docker=use_docker, |
| 347 | + work_dir=tempdir, |
| 348 | + ) |
| 349 | + assert exit_code, msg |
| 350 | + assert "AssertionError" in msg |
| 351 | + assert 'File "' in msg or 'File ".\\"' in msg # py3.8 + win32 |
| 352 | + |
| 353 | + with tempfile.TemporaryDirectory() as tempdir: |
| 354 | + # execute code which takes a long time |
| 355 | + exit_code, error, image = execute_code( |
| 356 | + "import time; time.sleep(2)", |
| 357 | + timeout=1, |
| 358 | + use_docker=use_docker, |
| 359 | + work_dir=tempdir, |
| 360 | + ) |
| 361 | + assert exit_code and error == "Timeout" |
| 362 | + if use_docker is True: |
| 363 | + assert isinstance(image, str) |
359 | 364 |
|
360 | 365 |
|
361 | 366 | @pytest.mark.skipif(
|
362 |
| - sys.platform in ["win32"] or (not is_docker_running()) or in_docker_container(), |
363 |
| - reason="docker is not running or in docker container already", |
| 367 | + skip_docker or not is_docker_running(), |
| 368 | + reason="docker is not running or requested to skip docker tests", |
364 | 369 | )
|
365 | 370 | def test_execute_code_with_custom_filename_on_docker():
|
366 |
| - exit_code, msg, image = execute_code("print('hello world')", filename="tmp/codetest.py", use_docker=True) |
367 |
| - assert exit_code == 0 and msg == "hello world\n", msg |
368 |
| - assert image == "python:tmp_codetest.py" |
| 371 | + with tempfile.TemporaryDirectory() as tempdir: |
| 372 | + filename = "codetest.py" |
| 373 | + exit_code, msg, image = execute_code( |
| 374 | + "print('hello world')", |
| 375 | + filename=filename, |
| 376 | + use_docker=True, |
| 377 | + work_dir=tempdir, |
| 378 | + ) |
| 379 | + assert exit_code == 0 and msg == "hello world\n", msg |
| 380 | + assert image == "python:codetest.py" |
369 | 381 |
|
370 | 382 |
|
371 | 383 | @pytest.mark.skipif(
|
372 |
| - sys.platform in ["win32"] or (not is_docker_running()) or in_docker_container(), |
373 |
| - reason="docker is not running or in docker container already", |
| 384 | + skip_docker or not is_docker_running(), |
| 385 | + reason="docker is not running or requested to skip docker tests", |
374 | 386 | )
|
375 | 387 | def test_execute_code_with_misformed_filename_on_docker():
|
376 |
| - exit_code, msg, image = execute_code( |
377 |
| - "print('hello world')", filename="tmp/codetest.py (some extra information)", use_docker=True |
378 |
| - ) |
379 |
| - assert exit_code == 0 and msg == "hello world\n", msg |
380 |
| - assert image == "python:tmp_codetest.py__some_extra_information_" |
| 388 | + with tempfile.TemporaryDirectory() as tempdir: |
| 389 | + filename = "codetest.py (some extra information)" |
| 390 | + exit_code, msg, image = execute_code( |
| 391 | + "print('hello world')", |
| 392 | + filename=filename, |
| 393 | + use_docker=True, |
| 394 | + work_dir=tempdir, |
| 395 | + ) |
| 396 | + assert exit_code == 0 and msg == "hello world\n", msg |
| 397 | + assert image == "python:codetest.py__some_extra_information_" |
381 | 398 |
|
382 | 399 |
|
383 | 400 | def test_execute_code_raises_when_code_and_filename_are_both_none():
|
384 | 401 | with pytest.raises(AssertionError):
|
385 | 402 | execute_code(code=None, filename=None)
|
386 | 403 |
|
387 | 404 |
|
388 |
| -def test_execute_code_nodocker(): |
| 405 | +def test_execute_code_no_docker(): |
389 | 406 | test_execute_code(use_docker=False)
|
390 | 407 |
|
391 | 408 |
|
392 |
| -def test_execute_code_no_docker(): |
| 409 | +def test_execute_code_timeout_no_docker(): |
393 | 410 | exit_code, error, image = execute_code("import time; time.sleep(2)", timeout=1, use_docker=False)
|
394 |
| - if sys.platform != "win32": |
395 |
| - assert exit_code and error == "Timeout" |
| 411 | + assert exit_code and error == "Timeout" |
396 | 412 | assert image is None
|
397 | 413 |
|
398 | 414 |
|
|
0 commit comments