| 1 |
#import <XCTest/XCTest.h> |
| 2 |
#import <Python/Python.h> |
| 3 |
|
| 4 |
@interface TestbedTests : XCTestCase |
| 5 |
|
| 6 |
@end |
| 7 |
|
| 8 |
@implementation TestbedTests |
| 9 |
|
| 10 |
|
| 11 |
- (void)testPython { |
| 12 |
const char **argv; |
| 13 |
int exit_code; |
| 14 |
int failed; |
| 15 |
PyStatus status; |
| 16 |
PyPreConfig preconfig; |
| 17 |
PyConfig config; |
| 18 |
PyObject *app_packages_path; |
| 19 |
PyObject *method_args; |
| 20 |
PyObject *result; |
| 21 |
PyObject *site_module; |
| 22 |
PyObject *site_addsitedir_attr; |
| 23 |
PyObject *sys_module; |
| 24 |
PyObject *sys_path_attr; |
| 25 |
NSArray *test_args; |
| 26 |
NSString *python_home; |
| 27 |
NSString *path; |
| 28 |
wchar_t *wtmp_str; |
| 29 |
|
| 30 |
NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; |
| 31 |
|
| 32 |
// Set some other common environment indicators to disable color, as the |
| 33 |
// Xcode log can't display color. Stdout will report that it is *not* a |
| 34 |
// TTY. |
| 35 |
setenv("NO_COLOR", "1", true); |
| 36 |
setenv("PYTHON_COLORS", "0", true); |
| 37 |
|
| 38 |
if (getenv("GITHUB_ACTIONS")) { |
| 39 |
NSLog(@"Running in a GitHub Actions environment"); |
| 40 |
} |
| 41 |
// Arguments to pass into the test suite runner. |
| 42 |
// argv[0] must identify the process; any subsequent arg |
| 43 |
// will be handled as if it were an argument to `python -m test` |
| 44 |
// The processInfo arguments contain the binary that is running, |
| 45 |
// followed by the arguments defined in the test plan. This means: |
| 46 |
// run_module = test_args[1] |
| 47 |
// argv = ["Testbed"] + test_args[2:] |
| 48 |
test_args = [[NSProcessInfo processInfo] arguments]; |
| 49 |
if (test_args == NULL) { |
| 50 |
NSLog(@"Unable to identify test arguments."); |
| 51 |
} |
| 52 |
NSLog(@"Test arguments: %@", test_args); |
| 53 |
argv = malloc(sizeof(char *) * ([test_args count] - 1)); |
| 54 |
argv[0] = "Testbed"; |
| 55 |
for (int i = 1; i < [test_args count] - 1; i++) { |
| 56 |
argv[i] = [[test_args objectAtIndex:i+1] UTF8String]; |
| 57 |
} |
| 58 |
|
| 59 |
// Generate an isolated Python configuration. |
| 60 |
NSLog(@"Configuring isolated Python..."); |
| 61 |
PyPreConfig_InitIsolatedConfig(&preconfig); |
| 62 |
PyConfig_InitIsolatedConfig(&config); |
| 63 |
|
| 64 |
// Configure the Python interpreter: |
| 65 |
// Enforce UTF-8 encoding for stderr, stdout, file-system encoding and locale. |
| 66 |
// See https://docs.python.org/3/library/os.html#python-utf-8-mode. |
| 67 |
preconfig.utf8_mode = 1; |
| 68 |
// Use the system logger for stdout/err |
| 69 |
config.use_system_logger = 1; |
| 70 |
// Don't buffer stdio. We want output to appears in the log immediately |
| 71 |
config.buffered_stdio = 0; |
| 72 |
// Don't write bytecode; we can't modify the app bundle |
| 73 |
// after it has been signed. |
| 74 |
config.write_bytecode = 0; |
| 75 |
// Ensure that signal handlers are installed |
| 76 |
config.install_signal_handlers = 1; |
| 77 |
// Run the test module. |
| 78 |
config.run_module = Py_DecodeLocale([[test_args objectAtIndex:1] UTF8String], NULL); |
| 79 |
// For debugging - enable verbose mode. |
| 80 |
// config.verbose = 1; |
| 81 |
|
| 82 |
NSLog(@"Pre-initializing Python runtime..."); |
| 83 |
status = Py_PreInitialize(&preconfig); |
| 84 |
if (PyStatus_Exception(status)) { |
| 85 |
XCTFail(@"Unable to pre-initialize Python interpreter: %s", status.err_msg); |
| 86 |
PyConfig_Clear(&config); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// Set the home for the Python interpreter |
| 91 |
python_home = [NSString stringWithFormat:@"%@/python", resourcePath, nil]; |
| 92 |
NSLog(@"PythonHome: %@", python_home); |
| 93 |
wtmp_str = Py_DecodeLocale([python_home UTF8String], NULL); |
| 94 |
status = PyConfig_SetString(&config, &config.home, wtmp_str); |
| 95 |
if (PyStatus_Exception(status)) { |
| 96 |
XCTFail(@"Unable to set PYTHONHOME: %s", status.err_msg); |
| 97 |
PyConfig_Clear(&config); |
| 98 |
return; |
| 99 |
} |
| 100 |
PyMem_RawFree(wtmp_str); |
| 101 |
|
| 102 |
// Read the site config |
| 103 |
status = PyConfig_Read(&config); |
| 104 |
if (PyStatus_Exception(status)) { |
| 105 |
XCTFail(@"Unable to read site config: %s", status.err_msg); |
| 106 |
PyConfig_Clear(&config); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
NSLog(@"Configure argc/argv..."); |
| 111 |
status = PyConfig_SetBytesArgv(&config, [test_args count] - 1, (char**) argv); |
| 112 |
if (PyStatus_Exception(status)) { |
| 113 |
XCTFail(@"Unable to configure argc/argv: %s", status.err_msg); |
| 114 |
PyConfig_Clear(&config); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
NSLog(@"Initializing Python runtime..."); |
| 119 |
status = Py_InitializeFromConfig(&config); |
| 120 |
if (PyStatus_Exception(status)) { |
| 121 |
XCTFail(@"Unable to initialize Python interpreter: %s", status.err_msg); |
| 122 |
PyConfig_Clear(&config); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
// Add app_packages as a site directory. This both adds to sys.path, |
| 127 |
// and ensures that any .pth files in that directory will be executed. |
| 128 |
site_module = PyImport_ImportModule("site"); |
| 129 |
if (site_module == NULL) { |
| 130 |
XCTFail(@"Could not import site module"); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
site_addsitedir_attr = PyObject_GetAttrString(site_module, "addsitedir"); |
| 135 |
if (site_addsitedir_attr == NULL || !PyCallable_Check(site_addsitedir_attr)) { |
| 136 |
XCTFail(@"Could not access site.addsitedir"); |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
path = [NSString stringWithFormat:@"%@/app_packages", resourcePath, nil]; |
| 141 |
NSLog(@"App packages path: %@", path); |
| 142 |
wtmp_str = Py_DecodeLocale([path UTF8String], NULL); |
| 143 |
app_packages_path = PyUnicode_FromWideChar(wtmp_str, wcslen(wtmp_str)); |
| 144 |
if (app_packages_path == NULL) { |
| 145 |
XCTFail(@"Could not convert app_packages path to unicode"); |
| 146 |
return; |
| 147 |
} |
| 148 |
PyMem_RawFree(wtmp_str); |
| 149 |
|
| 150 |
method_args = Py_BuildValue("(O)", app_packages_path); |
| 151 |
if (method_args == NULL) { |
| 152 |
XCTFail(@"Could not create arguments for site.addsitedir"); |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
result = PyObject_CallObject(site_addsitedir_attr, method_args); |
| 157 |
if (result == NULL) { |
| 158 |
XCTFail(@"Could not add app_packages directory using site.addsitedir"); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
// Add test code to sys.path |
| 163 |
sys_module = PyImport_ImportModule("sys"); |
| 164 |
if (sys_module == NULL) { |
| 165 |
XCTFail(@"Could not import sys module"); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
sys_path_attr = PyObject_GetAttrString(sys_module, "path"); |
| 170 |
if (sys_path_attr == NULL) { |
| 171 |
XCTFail(@"Could not access sys.path"); |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
path = [NSString stringWithFormat:@"%@/app", resourcePath, nil]; |
| 176 |
NSLog(@"App path: %@", path); |
| 177 |
wtmp_str = Py_DecodeLocale([path UTF8String], NULL); |
| 178 |
failed = PyList_Insert(sys_path_attr, 0, PyUnicode_FromString([path UTF8String])); |
| 179 |
if (failed) { |
| 180 |
XCTFail(@"Unable to add app to sys.path"); |
| 181 |
return; |
| 182 |
} |
| 183 |
PyMem_RawFree(wtmp_str); |
| 184 |
|
| 185 |
// Ensure the working directory is the app folder. |
| 186 |
chdir([path UTF8String]); |
| 187 |
|
| 188 |
// Start the test suite. Print a separator to differentiate Python startup logs from app logs |
| 189 |
NSLog(@"---------------------------------------------------------------------------"); |
| 190 |
|
| 191 |
exit_code = Py_RunMain(); |
| 192 |
XCTAssertEqual(exit_code, 0, @"Test suite did not pass"); |
| 193 |
|
| 194 |
NSLog(@"---------------------------------------------------------------------------"); |
| 195 |
|
| 196 |
Py_Finalize(); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
@end |