PythonLoaderTests.cpp 5.5 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_test3rdPartySubPath = ".o3de/3rdParty";
  27. };
  28. TEST_F(AzToolsFrameworkPythonLoaderFixture, TestGetPythonVenvPath_Valid)
  29. {
  30. auto test3rdPartyPath = m_tempDirectory.GetDirectoryAsFixedMaxPath() / s_test3rdPartySubPath;
  31. auto testVenvPath = test3rdPartyPath / "venv";
  32. AZ::IO::FixedMaxPath engineRoot{ s_testEnginePath };
  33. AZ::IO::SystemFile::CreateDir(test3rdPartyPath.String().c_str());
  34. //AZ::IO::FileIOBase::GetInstance()->CreatePath(test3rdPartyPath.String().c_str());
  35. auto result = AzToolsFramework::EmbeddedPython::PythonLoader::GetPythonVenvPath(test3rdPartyPath, engineRoot);
  36. AZ::IO::FixedMaxPath expectedPath = testVenvPath / s_testEnginePathHashId;
  37. EXPECT_TRUE(result == expectedPath);
  38. }
  39. TEST_F(AzToolsFrameworkPythonLoaderFixture, TestGetPythonVenvExecutablePath_Valid)
  40. {
  41. // Prepare the test venv pyvenv.cfg file in the expected location
  42. AZ::IO::FixedMaxPath tempVenvRelativePath = AZ::IO::FixedMaxPath(s_test3rdPartySubPath) / "venv/" / s_testEnginePathHashId;
  43. AZ::IO::FixedMaxPath tempVenvFullPath = m_tempDirectory.GetDirectoryAsFixedMaxPath() / tempVenvRelativePath;
  44. AZ::IO::SystemFile::CreateDir(tempVenvFullPath.String().c_str());
  45. AZ::IO::FixedMaxPath tempPyConfigFile = tempVenvRelativePath / "pyvenv.cfg";
  46. AZStd::string testPython3rdPartyPath = "/home/user/python/";
  47. AZStd::string testPyConfigFileContent = AZStd::string::format("home = %s\n"
  48. "include-system-site-packages = false\n"
  49. "version = 3.10.13",
  50. testPython3rdPartyPath.c_str());
  51. AZ::Test::CreateTestFile(m_tempDirectory, tempPyConfigFile, testPyConfigFileContent);
  52. // Test the method
  53. AZ::IO::FixedMaxPath engineRoot{ s_testEnginePath };
  54. AZ::IO::FixedMaxPath test3rdPartyRoot = m_tempDirectory.GetDirectoryAsFixedMaxPath() / s_test3rdPartySubPath;
  55. auto result = AzToolsFramework::EmbeddedPython::PythonLoader::GetPythonExecutablePath(test3rdPartyRoot, engineRoot);
  56. AZ::IO::FixedMaxPath expectedPath{ testPython3rdPartyPath };
  57. EXPECT_TRUE(result == expectedPath);
  58. }
  59. TEST_F(AzToolsFrameworkPythonLoaderFixture, TestReadPythonEggLinkPaths_Valid)
  60. {
  61. // Prepare the test folder and create dummy egg-link files
  62. AZ::IO::FixedMaxPath testRelativeSiteLibsPath = AZ::IO::FixedMaxPath(s_test3rdPartySubPath) / "venv" / 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 auto testEggLinkPaths = AZStd::to_array<const char*>({ "/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. AZ::IO::FixedMaxPath test3rdPartyRoot = m_tempDirectory.GetDirectoryAsFixedMaxPath() / s_test3rdPartySubPath;
  82. AZStd::vector<AZStd::string> resultEggLinkPaths;
  83. AzToolsFramework::EmbeddedPython::PythonLoader::ReadPythonEggLinkPaths(
  84. test3rdPartyRoot,
  85. engineRoot,
  86. [&resultEggLinkPaths](AZ::IO::PathView path)
  87. {
  88. resultEggLinkPaths.emplace_back(path.Native());
  89. } );
  90. // Sort the expected and actual lists
  91. AZStd::sort(expectedResults.begin(), expectedResults.end());
  92. AZStd::sort(resultEggLinkPaths.begin(), resultEggLinkPaths.end());
  93. EXPECT_EQ(expectedResults, resultEggLinkPaths);
  94. }
  95. } // namespace UnitTest