PythonBindingsTests.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/UnitTest/TestTypes.h>
  9. #include <AzTest/Utils.h>
  10. #include <AzCore/IO/Path/Path.h>
  11. #include <AzCore/std/smart_ptr/make_shared.h>
  12. #include <PythonBindings.h>
  13. #include <ProjectManager_Test_Traits_Platform.h>
  14. #include <QDir>
  15. namespace O3DE::ProjectManager
  16. {
  17. class TestablePythonBindings
  18. : public PythonBindings
  19. {
  20. public:
  21. TestablePythonBindings(const AZ::IO::PathView& enginePath)
  22. : PythonBindings(enginePath)
  23. {
  24. }
  25. void TestOnStdOut(const char* msg)
  26. {
  27. PythonBindings::OnStdOut(msg);
  28. }
  29. void TestOnStdError(const char* msg)
  30. {
  31. PythonBindings::OnStdError(msg);
  32. }
  33. //! override with an implementation that won't do anything
  34. //! so we avoid modifying the manifest
  35. bool RemoveInvalidProjects() override
  36. {
  37. constexpr bool removalResult = true;
  38. return removalResult;
  39. }
  40. };
  41. class PythonBindingsTests
  42. : public ::UnitTest::LeakDetectionFixture
  43. , public AZ::Debug::TraceMessageBus::Handler
  44. {
  45. public:
  46. void SetUp() override
  47. {
  48. const AZStd::string engineRootPath{ AZ::Test::GetEngineRootPath() };
  49. m_pythonBindings = AZStd::make_unique<TestablePythonBindings>(AZ::IO::PathView(engineRootPath));
  50. }
  51. void TearDown() override
  52. {
  53. m_pythonBindings.reset();
  54. }
  55. //! AZ::Debug::TraceMessageBus
  56. bool OnOutput(const char*, const char* message) override
  57. {
  58. m_gatheredMessages.emplace_back(message);
  59. return true;
  60. }
  61. AZStd::unique_ptr<ProjectManager::TestablePythonBindings> m_pythonBindings;
  62. AZStd::vector<AZStd::string> m_gatheredMessages;
  63. };
  64. TEST_F(PythonBindingsTests, PythonBindings_Start_Python_Succeeds)
  65. {
  66. EXPECT_TRUE(m_pythonBindings->PythonStarted());
  67. }
  68. TEST_F(PythonBindingsTests, PythonBindings_Create_Project_Succeeds)
  69. {
  70. ASSERT_TRUE(m_pythonBindings->PythonStarted());
  71. auto templateResults = m_pythonBindings->GetProjectTemplates();
  72. ASSERT_TRUE(templateResults.IsSuccess());
  73. QVector<ProjectTemplateInfo> templates = templateResults.GetValue();
  74. ASSERT_FALSE(templates.isEmpty());
  75. // use the first registered template
  76. QString templatePath = templates.at(0).m_path;
  77. AZ::Test::ScopedAutoTempDirectory tempDir;
  78. ProjectInfo projectInfo;
  79. projectInfo.m_path = QDir::toNativeSeparators(QString(tempDir.GetDirectory()) + "/" + "TestProject");
  80. projectInfo.m_projectName = "TestProjectName";
  81. constexpr bool registerProject = false;
  82. auto result = m_pythonBindings->CreateProject(templatePath, projectInfo, registerProject);
  83. EXPECT_TRUE(result.IsSuccess());
  84. ProjectInfo resultProjectInfo = result.GetValue();
  85. EXPECT_EQ(projectInfo.m_path, resultProjectInfo.m_path);
  86. EXPECT_EQ(projectInfo.m_projectName, resultProjectInfo.m_projectName);
  87. }
  88. TEST_F(PythonBindingsTests, PythonBindings_Print_Percent_Does_Not_Crash)
  89. {
  90. bool testMessageFound = false;
  91. bool testErrorFound = false;
  92. const char* testMessage = "PythonTestMessage%";
  93. const char* testError = "ERROR:root:PythonTestError%";
  94. AZ::Debug::TraceMessageBus::Handler::BusConnect();
  95. m_pythonBindings->TestOnStdOut(testMessage);
  96. m_pythonBindings->TestOnStdError(testError);
  97. AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
  98. for (const auto& message : m_gatheredMessages)
  99. {
  100. if (message.contains(testMessage))
  101. {
  102. testMessageFound = true;
  103. }
  104. else if (message.contains(testError))
  105. {
  106. testErrorFound = true;
  107. }
  108. }
  109. EXPECT_TRUE(testMessageFound);
  110. EXPECT_TRUE(testErrorFound);
  111. }
  112. } // namespace O3DE::ProjectManager