env_ut.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <library/cpp/testing/common/env.h>
  2. #include <library/cpp/testing/common/scope.h>
  3. #include <library/cpp/testing/gtest/gtest.h>
  4. #include <util/folder/dirut.h>
  5. #include <util/stream/file.h>
  6. #include <util/system/env.h>
  7. #include <util/system/execpath.h>
  8. #include <util/system/fs.h>
  9. TEST(Runtime, ArcadiaSourceRoot) {
  10. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  11. {
  12. auto tmpDir = ::GetSystemTempDir();
  13. NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", tmpDir);
  14. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  15. EXPECT_EQ(tmpDir, ArcadiaSourceRoot());
  16. }
  17. {
  18. NTesting::TScopedEnvironment guard("ARCADIA_SOURCE_ROOT", "");
  19. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  20. EXPECT_FALSE(ArcadiaSourceRoot().empty());
  21. }
  22. }
  23. TEST(Runtime, BuildRoot) {
  24. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  25. {
  26. auto tmpDir = ::GetSystemTempDir();
  27. NTesting::TScopedEnvironment guard("ARCADIA_BUILD_ROOT", tmpDir);
  28. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  29. EXPECT_EQ(tmpDir, BuildRoot());
  30. }
  31. {
  32. NTesting::TScopedEnvironment guard("ARCADIA_BUILD_ROOT", "");
  33. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  34. EXPECT_FALSE(BuildRoot().empty());
  35. }
  36. }
  37. TEST(Runtime, BinaryPath) {
  38. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  39. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  40. EXPECT_TRUE(TFsPath(BinaryPath("library/cpp/testing/common/ut")).Exists());
  41. }
  42. TEST(Runtime, GetArcadiaTestsData) {
  43. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  44. {
  45. auto tmpDir = ::GetSystemTempDir();
  46. NTesting::TScopedEnvironment guard("ARCADIA_TESTS_DATA_DIR", tmpDir);
  47. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  48. EXPECT_EQ(tmpDir, GetArcadiaTestsData());
  49. }
  50. {
  51. NTesting::TScopedEnvironment guard("ARCADIA_TESTS_DATA_DIR", "");
  52. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  53. auto path = GetArcadiaTestsData();
  54. // it is not error if path is empty
  55. const bool ok = (path.empty() || GetBaseName(path) == "arcadia_tests_data");
  56. EXPECT_TRUE(ok);
  57. }
  58. }
  59. TEST(Runtime, GetWorkPath) {
  60. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  61. {
  62. auto tmpDir = ::GetSystemTempDir();
  63. NTesting::TScopedEnvironment guard("TEST_WORK_PATH", tmpDir);
  64. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  65. EXPECT_EQ(tmpDir, GetWorkPath());
  66. }
  67. {
  68. NTesting::TScopedEnvironment guard("TEST_WORK_PATH", "");
  69. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  70. EXPECT_TRUE(!GetWorkPath().empty());
  71. }
  72. }
  73. TEST(Runtime, GetOutputPath) {
  74. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  75. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  76. EXPECT_EQ(GetOutputPath().Basename(), "testing_out_stuff");
  77. }
  78. TEST(Runtime, GetRamDrivePath) {
  79. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  80. auto tmpDir = ::GetSystemTempDir();
  81. NTesting::TScopedEnvironment guard("YA_TEST_RAM_DRIVE_PATH", tmpDir);
  82. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  83. EXPECT_EQ(tmpDir, GetRamDrivePath());
  84. }
  85. TEST(Runtime, GetOutputRamDrivePath) {
  86. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", ""); // remove context filename
  87. auto tmpDir = ::GetSystemTempDir();
  88. NTesting::TScopedEnvironment guard("YA_TEST_OUTPUT_RAM_DRIVE_PATH", tmpDir);
  89. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  90. EXPECT_EQ(tmpDir, GetOutputRamDrivePath());
  91. }
  92. #ifdef _linux_
  93. TEST(Runtime, GdbPath) {
  94. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  95. EXPECT_TRUE(NFs::Exists(::GdbPath()));
  96. }
  97. #endif
  98. TString ReInitializeContext(TStringBuf data) {
  99. auto tmpDir = ::GetSystemTempDir();
  100. auto filename = tmpDir + "/context.json";
  101. TOFStream stream(filename);
  102. stream.Write(data.data(), data.size());
  103. stream.Finish();
  104. NTesting::TScopedEnvironment contextGuard("YA_TEST_CONTEXT_FILE", filename);
  105. Singleton<NPrivate::TTestEnv>()->ReInitialize();
  106. return filename;
  107. }
  108. TEST(Runtime, GetTestParam) {
  109. TString context = R"json({
  110. "runtime": {
  111. "test_params": {
  112. "a": "b",
  113. "c": "d"
  114. }
  115. }
  116. })json";
  117. auto filename = ReInitializeContext(context);
  118. EXPECT_EQ("b", GetTestParam("a"));
  119. EXPECT_EQ("d", GetTestParam("c"));
  120. EXPECT_EQ("", GetTestParam("e"));
  121. EXPECT_EQ("w", GetTestParam("e", "w"));
  122. Singleton<NPrivate::TTestEnv>()->AddTestParam("e", "e");
  123. EXPECT_EQ("e", GetTestParam("e"));
  124. }
  125. TEST(Runtime, WatchProcessCore) {
  126. TString context = R"json({
  127. "internal": {
  128. "core_search_file": "watch_core.txt"
  129. }
  130. })json";
  131. auto filename = ReInitializeContext(context);
  132. WatchProcessCore(1, "bin1", "pwd");
  133. WatchProcessCore(2, "bin1");
  134. StopProcessCoreWatching(2);
  135. TIFStream file("watch_core.txt");
  136. auto data = file.ReadAll();
  137. TString expected = R"json({"cmd":"add","pid":1,"binary_path":"bin1","cwd":"pwd"}
  138. {"cmd":"add","pid":2,"binary_path":"bin1"}
  139. {"cmd":"drop","pid":2}
  140. )json";
  141. EXPECT_EQ(expected, data);
  142. }
  143. TEST(Runtime, GlobalResources) {
  144. TString context = R"json({
  145. "resources": {
  146. "global": {
  147. "TOOL_NAME_RESOURCE_GLOBAL": "path"
  148. }
  149. }
  150. })json";
  151. auto filename = ReInitializeContext(context);
  152. EXPECT_EQ("path", GetGlobalResource("TOOL_NAME_RESOURCE_GLOBAL"));
  153. }