| 1818 | )"; |
| 1819 | |
| 1820 | static void test_template_py(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect) { |
| 1821 | t.test(name, [&tmpl, &vars, &expect](testing & t) { |
| 1822 | // Prepare arguments |
| 1823 | std::string tmpl_json = json(tmpl).dump(); |
| 1824 | std::string vars_json = vars.dump(); |
| 1825 | |
| 1826 | #ifdef _WIN32 |
| 1827 | const char * python_executable = "python.exe"; |
| 1828 | #else |
| 1829 | const char * python_executable = "python3"; |
| 1830 | #endif |
| 1831 | |
| 1832 | const char * command_line[] = {python_executable, "-c", py_script.c_str(), tmpl_json.c_str(), vars_json.c_str(), NULL}; |
| 1833 | |
| 1834 | struct subprocess_s subprocess; |
| 1835 | int options = subprocess_option_combined_stdout_stderr |
| 1836 | | subprocess_option_no_window |
| 1837 | | subprocess_option_inherit_environment |
| 1838 | | subprocess_option_search_user_path; |
| 1839 | int result = subprocess_create(command_line, options, &subprocess); |
| 1840 | |
| 1841 | if (result != 0) { |
| 1842 | t.log("Failed to create subprocess, error code: " + std::to_string(result)); |
| 1843 | t.assert_true("subprocess creation", false); |
| 1844 | return; |
| 1845 | } |
| 1846 | |
| 1847 | // Read output |
| 1848 | std::string output; |
| 1849 | char buffer[1024]; |
| 1850 | FILE * p_stdout = subprocess_stdout(&subprocess); |
| 1851 | while (fgets(buffer, sizeof(buffer), p_stdout)) { |
| 1852 | output += buffer; |
| 1853 | } |
| 1854 | |
| 1855 | int process_return; |
| 1856 | subprocess_join(&subprocess, &process_return); |
| 1857 | subprocess_destroy(&subprocess); |
| 1858 | |
| 1859 | if (process_return != 0) { |
| 1860 | t.log("Python script failed with exit code: " + std::to_string(process_return)); |
| 1861 | t.log("Output: " + output); |
| 1862 | t.assert_true("python execution", false); |
| 1863 | return; |
| 1864 | } |
| 1865 | |
| 1866 | if (!t.assert_true("Template render mismatch", expect == output)) { |
| 1867 | t.log("Template: " + json(tmpl).dump()); |
| 1868 | t.log("Expected: " + json(expect).dump()); |
| 1869 | t.log("Python : " + json(output).dump()); |
| 1870 | } |
| 1871 | }); |
| 1872 | } |
| 1873 | |
| 1874 | static void test_template(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect) { |
| 1875 | if (g_python_mode) { |
no test coverage detected