|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + This file is part of the YUP library. |
| 5 | + Copyright (c) 2025 - [email protected] |
| 6 | +
|
| 7 | + YUP is an open source library subject to open-source licensing. |
| 8 | +
|
| 9 | + The code included in this file is provided under the terms of the ISC license |
| 10 | + http://www.isc.org/downloads/software-support-policy/isc-license. Permission |
| 11 | + to use, copy, modify, and/or distribute this software for any purpose with or |
| 12 | + without fee is hereby granted provided that the above copyright notice and |
| 13 | + this permission notice appear in all copies. |
| 14 | +
|
| 15 | + YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER |
| 16 | + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE |
| 17 | + DISCLAIMED. |
| 18 | +
|
| 19 | + ============================================================================== |
| 20 | +*/ |
| 21 | + |
| 22 | +#include <yup_python/yup_python.h> |
| 23 | + |
| 24 | +#include <gtest/gtest.h> |
| 25 | + |
| 26 | +#include <PythonStandardLibrary.h> |
| 27 | + |
| 28 | +using namespace yup; |
| 29 | + |
| 30 | +class ScriptPythonTest : public ::testing::Test |
| 31 | +{ |
| 32 | +protected: |
| 33 | + static File getPytestTestFolder() |
| 34 | + { |
| 35 | + return File (__FILE__) |
| 36 | + .getParentDirectory() |
| 37 | + .getParentDirectory() |
| 38 | + .getParentDirectory() |
| 39 | + .getChildFile ("python") |
| 40 | + .getChildFile ("tests"); |
| 41 | + } |
| 42 | + |
| 43 | + void SetUp() override |
| 44 | + { |
| 45 | + engine = std::make_unique<ScriptEngine> (ScriptEngine::prepareScriptingHome ( |
| 46 | + YUPApplication::getInstance()->getApplicationName(), |
| 47 | + File::getSpecialLocation (File::tempDirectory), |
| 48 | + [] (const char*) -> MemoryBlock |
| 49 | + { |
| 50 | + return { PythonStandardLibrary_data, PythonStandardLibrary_size }; |
| 51 | + })); |
| 52 | + } |
| 53 | + |
| 54 | + void TearDown() override |
| 55 | + { |
| 56 | + // Cleanup after each test |
| 57 | + } |
| 58 | + |
| 59 | + std::unique_ptr<ScriptEngine> engine; |
| 60 | +}; |
| 61 | + |
| 62 | +TEST_F (ScriptPythonTest, RunPythonTests) |
| 63 | +{ |
| 64 | + auto script = String (R"( |
| 65 | + import importlib |
| 66 | +
|
| 67 | + package = 'pytest' |
| 68 | +
|
| 69 | + try: |
| 70 | + importlib.import_module(package) |
| 71 | + except ImportError: |
| 72 | + import pip |
| 73 | + pip.main(['install', package, '--prefix', '{{root_path}}']) |
| 74 | + finally: |
| 75 | + globals()[package] = importlib.import_module(package) |
| 76 | +
|
| 77 | + pytest.main(['-x', '{{test_path}}', '-vv']) |
| 78 | + )") |
| 79 | + .dedentLines() |
| 80 | + .replace ("{{root_path}}", engine->getScriptingHome().getFullPathName()) |
| 81 | + .replace ("{{test_path}}", getPytestTestFolder().getFullPathName()); |
| 82 | + |
| 83 | + auto result = engine->runScript (script); |
| 84 | + |
| 85 | + EXPECT_TRUE (result.wasOk()) << "Pytest failed: " << result.getErrorMessage(); |
| 86 | +} |
0 commit comments