mirror of
https://github.com/microsoft/autogen.git
synced 2026-04-20 03:02:16 -04:00
* fix #1069 * uncomment skips * use importlib to check package * better code --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import importlib.metadata
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
@@ -22,6 +23,21 @@ OAI_CONFIG_LIST = "OAI_CONFIG_LIST"
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
def is_package_installed(package_name):
|
||||
"""Check if a package is installed. This is a preferred way to check if a
|
||||
package is installed or not than doing a try-catch around an import
|
||||
because it avoids name conflict with local modules and
|
||||
code execution in the imported module."""
|
||||
try:
|
||||
importlib.metadata.version(package_name)
|
||||
return True
|
||||
except importlib.metadata.PackageNotFoundError:
|
||||
return False
|
||||
|
||||
|
||||
docker_package_installed = is_package_installed("docker")
|
||||
|
||||
|
||||
# def test_find_code():
|
||||
# try:
|
||||
# import openai
|
||||
@@ -293,10 +309,6 @@ def scrape(url):
|
||||
assert len(codeblocks) == 1 and codeblocks[0] == ("", "source setup.sh")
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.platform in ["darwin"],
|
||||
reason="do not run on MacOS",
|
||||
)
|
||||
def test_execute_code(use_docker=None):
|
||||
try:
|
||||
import docker
|
||||
@@ -338,15 +350,27 @@ def test_execute_code(use_docker=None):
|
||||
assert isinstance(image, str) or docker is None or os.path.exists("/.dockerenv") or use_docker is False
|
||||
|
||||
|
||||
@pytest.mark.skipif(docker_package_installed is False, reason="docker package not installed")
|
||||
def test_execute_code_with_custom_filename_on_docker():
|
||||
exit_code, msg, image = execute_code("print('hello world')", filename="tmp/codetest.py", use_docker=True)
|
||||
assert exit_code == 0 and msg == "hello world\n", msg
|
||||
assert image == "python:tmp_codetest.py"
|
||||
|
||||
|
||||
@pytest.mark.skipif(docker_package_installed is False, reason="docker package not installed")
|
||||
def test_execute_code_with_misformed_filename_on_docker():
|
||||
exit_code, msg, image = execute_code(
|
||||
"print('hello world')", filename="tmp/codetest.py (some extra information)", use_docker=True
|
||||
)
|
||||
assert exit_code == 0 and msg == "hello world\n", msg
|
||||
assert image == "python:tmp_codetest.py__some_extra_information_"
|
||||
|
||||
|
||||
def test_execute_code_raises_when_code_and_filename_are_both_none():
|
||||
with pytest.raises(AssertionError):
|
||||
execute_code(code=None, filename=None)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.platform in ["darwin"],
|
||||
reason="do not run on MacOS",
|
||||
)
|
||||
def test_execute_code_nodocker():
|
||||
test_execute_code(use_docker=False)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user