Debian Codegraph @

iOS/testbed/iOSTestbedTests/iOSTestbedTests.m

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