PythonLoaderTests.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Component/Entity.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/std/containers/vector.h>
  12. #include <AzCore/std/sort.h>
  13. #include <AzTest/AzTest.h>
  14. #include <AzTest/Utils.h>
  15. #include <AzCore/UnitTest/TestTypes.h>
  16. #include <AzToolsFramework/API/PythonLoader.h>
  17. namespace UnitTest
  18. {
  19. class AzToolsFrameworkPythonLoaderFixture
  20. : public LeakDetectionFixture
  21. {
  22. protected:
  23. AZ::Test::ScopedAutoTempDirectory m_tempDirectory;
  24. static constexpr AZStd::string_view s_testEnginePath = "O3de_path";
  25. static constexpr const char* s_testEnginePathHashId = "1af80774";
  26. static constexpr const char* s_testPythonRootPath = ".o3de/3rdParty";
  27. };
  28. TEST_F(AzToolsFrameworkPythonLoaderFixture, TestGetPythonVenvPath_Valid)
  29. {
  30. auto testPythonRootPath = m_tempDirectory.GetDirectoryAsFixedMaxPath() / s_testPythonRootPath;
  31. auto testVenvRootPath = testPythonRootPath / "venv";
  32. AZ::IO::FixedMaxPath engineRoot{ s_testEnginePath };
  33. auto result = AzToolsFramework::EmbeddedPython::PythonLoader::GetPythonVenvPath(engineRoot, testVenvRootPath.c_str());
  34. AZ::IO::FixedMaxPath expectedPath = testVenvRootPath / s_testEnginePathHashId;
  35. EXPECT_TRUE(result == expectedPath);
  36. }
  37. TEST_F(AzToolsFrameworkPythonLoaderFixture, TestGetPythonVenvExecutablePath_Valid)
  38. {
  39. auto testPythonRootPath = m_tempDirectory.GetDirectoryAsFixedMaxPath() / s_testPythonRootPath;
  40. auto testVenvRootPath = testPythonRootPath / "venv";
  41. // Prepare the test venv pyvenv.cfg file in the expected location
  42. AZ::IO::FixedMaxPath tempVenvPath = testVenvRootPath / s_testEnginePathHashId;
  43. AZ::IO::SystemFile::CreateDir(tempVenvPath.c_str());
  44. AZ::IO::FixedMaxPath tempPyConfigFile = tempVenvPath / "pyvenv.cfg";
  45. AZStd::string testPython3rdPartyPath = "/home/user/python/";
  46. AZStd::string testPyConfigFileContent = AZStd::string::format("home = %s\n"
  47. "include-system-site-packages = false\n"
  48. "version = 3.10.13",
  49. testPython3rdPartyPath.c_str());
  50. AZ::Test::CreateTestFile(m_tempDirectory, tempPyConfigFile, testPyConfigFileContent);
  51. // Test the method
  52. AZ::IO::FixedMaxPath engineRoot{ s_testEnginePath };
  53. auto result = AzToolsFramework::EmbeddedPython::PythonLoader::GetPythonExecutablePath(engineRoot, testVenvRootPath.c_str());
  54. AZ::IO::FixedMaxPath expectedPath{ testPython3rdPartyPath };
  55. EXPECT_TRUE(result == expectedPath);
  56. }
  57. TEST_F(AzToolsFrameworkPythonLoaderFixture, TestReadPythonEggLinkPaths_Valid)
  58. {
  59. auto testPythonRootPath = m_tempDirectory.GetDirectoryAsFixedMaxPath() / s_testPythonRootPath;
  60. auto testVenvRootPath = testPythonRootPath / "venv";
  61. // Prepare the test folder and create dummy egg-link files
  62. AZ::IO::FixedMaxPath testRelativeSiteLibsPath = testVenvRootPath / s_testEnginePathHashId / O3DE_PYTHON_SITE_PACKAGE_SUBPATH;
  63. AZ::IO::FixedMaxPath testFullSiteLIbsPath = m_tempDirectory.GetDirectoryAsFixedMaxPath() / testRelativeSiteLibsPath;
  64. AZ::IO::SystemFile::CreateDir(testFullSiteLIbsPath.String().c_str());
  65. AZStd::vector<AZStd::string> expectedResults;
  66. expectedResults.emplace_back(testFullSiteLIbsPath.LexicallyNormal().Native());
  67. static constexpr AZStd::array<const char*, 3> testEggLinkPaths({ "/lib/path/one", "/lib/path/two", "/lib/path/three" });
  68. int index = 0;
  69. for (const char* testEggLinkPath : testEggLinkPaths)
  70. {
  71. ++index;
  72. AZStd::string testEggFileName = AZStd::string::format("test-%d.egg-link", index);
  73. const char* lineBreak = ((index % 2) == 0) ? "\n" : "\r\n";
  74. AZStd::string testEggFileContent = AZStd::string::format("%s%s.", testEggLinkPath, lineBreak);
  75. expectedResults.emplace_back(AZStd::string(testEggLinkPath));
  76. AZ::IO::FixedMaxPath testEggLinkNamePath = testRelativeSiteLibsPath / testEggFileName;
  77. AZ::Test::CreateTestFile(m_tempDirectory, testEggLinkNamePath, testEggFileContent);
  78. }
  79. // Test the method
  80. AZ::IO::FixedMaxPath engineRoot{ s_testEnginePath };
  81. AZStd::vector<AZStd::string> resultEggLinkPaths;
  82. AzToolsFramework::EmbeddedPython::PythonLoader::ReadPythonEggLinkPaths(
  83. engineRoot,
  84. [&resultEggLinkPaths](AZ::IO::PathView path)
  85. {
  86. resultEggLinkPaths.emplace_back(path.Native());
  87. },
  88. testVenvRootPath.c_str());
  89. // Sort the expected and actual lists
  90. AZStd::sort(expectedResults.begin(), expectedResults.end());
  91. AZStd::sort(resultEggLinkPaths.begin(), resultEggLinkPaths.end());
  92. EXPECT_EQ(expectedResults, resultEggLinkPaths);
  93. }
  94. } // namespace UnitTest