UnitTestUtils.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <native/unittests/UnitTestUtils.h>
  9. #include <QElapsedTimer>
  10. #include <QTextStream>
  11. #include <QCoreApplication>
  12. #include <AzCore/IO/Path/Path.h>
  13. namespace AssetProcessorBuildTarget
  14. {
  15. //! This function returns the build system target name
  16. AZStd::string_view GetBuildTargetName()
  17. {
  18. #if !defined (LY_CMAKE_TARGET)
  19. #error "LY_CMAKE_TARGET must be defined in order to add this source file to a CMake executable target"
  20. #endif
  21. return AZStd::string_view{ LY_CMAKE_TARGET };
  22. }
  23. }
  24. namespace UnitTestUtils
  25. {
  26. void SleepForMinimumFileSystemTime()
  27. {
  28. // note that on Mac, the file system has a resolution of 1 second, and since we're using modtime for a bunch of things,
  29. // not the actual hash files, we have to wait different amount depending on the OS.
  30. #ifdef AZ_PLATFORM_WINDOWS
  31. int milliseconds = 1;
  32. #else
  33. int milliseconds = 1001;
  34. #endif
  35. AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(milliseconds));
  36. }
  37. bool CreateDummyFileAZ(AZ::IO::PathView fullPathToFile, AZStd::string_view contents)
  38. {
  39. AZ::IO::FileIOStream stream(fullPathToFile.FixedMaxPathString().c_str(), AZ::IO::OpenMode::ModeWrite|AZ::IO::OpenMode::ModeCreatePath);
  40. if (!stream.IsOpen())
  41. {
  42. return false;
  43. }
  44. stream.Write(contents.size(), contents.data());
  45. stream.Close();
  46. return true;
  47. }
  48. bool CreateDummyFile(const QString& fullPathToFile, QString contents)
  49. {
  50. QFileInfo fi(fullPathToFile);
  51. QDir fp(fi.path());
  52. fp.mkpath(".");
  53. QFile writer(fullPathToFile);
  54. if (!writer.open(QFile::WriteOnly))
  55. {
  56. return false;
  57. }
  58. if (!contents.isEmpty())
  59. {
  60. QTextStream ts(&writer);
  61. ts.setCodec("UTF-8");
  62. ts << contents;
  63. }
  64. return true;
  65. }
  66. bool BlockUntil(bool& varToWatch, int millisecondsMax)
  67. {
  68. QElapsedTimer limit;
  69. limit.start();
  70. varToWatch = false;
  71. while ((!varToWatch) && (limit.elapsed() < millisecondsMax))
  72. {
  73. QCoreApplication::processEvents(QEventLoop::AllEvents, 10);
  74. }
  75. // and then once more, so that any queued events as a result of the above finish.
  76. QCoreApplication::processEvents(QEventLoop::AllEvents, 10);
  77. return (varToWatch);
  78. }
  79. }