File size: 1,706 Bytes
26bead7
 
 
 
 
 
 
 
 
 
 
cc7a977
26bead7
cc7a977
 
26bead7
 
 
 
 
 
 
 
 
 
 
 
 
cc7a977
 
 
26bead7
 
 
 
 
 
 
cc7a977
26bead7
 
cc7a977
26bead7
 
 
 
 
 
 
 
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
"""Testing module for the AI Python C Extensions Generator application."""

import io
import sys
from logging import getLogger


_logger = getLogger(__name__)


# Define a function to write outputs to a file with a given filename.
def write_file(data, path):
    """Write data to a file with the specified filename."""
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w") as file:
        file.write(data)


# Define a function to execute Python code and capture its output.
def execute_python(code):
    """Execute the given Python code and capture its output."""
    try:
        _logger.info('EXECUTING TEST CODE...')
        # Redirect stdout to capture the output of the executed code.
        output = io.StringIO()
        sys.stdout = output
        # Execute the provided code in a clean global namespace.
        exec(code, {})
    except Exception as ex:
        _logger.error(f"ERROR WHILE EXECUTING TEST CODE:\n{ex}")
        return f"An error occurred while executing test code:\n{ex}"
    finally:
        # Restore original stdout.
        sys.stdout = sys.__stdout__
    return output.getvalue()


# Extension testing function.
def test_extension(usage_code, compile_path):
    """Test the C extension executing the code and capture its output."""
    try:  # Write the code to file.
        write_file(usage_code, compile_path / "usage_example.py")
    except Exception as ex:
        return f"An error occurred while writing test file:\n{ex}"
    try:
        # Execute the code and capture the output.
        output = execute_python(usage_code)
    except Exception as ex:
        return f"An error occurred while testing the extension:\n{ex}"
    return output