Bazel client: add and use blaze::GetPathEnv()

Use GetPathEnv() instead of GetEnv() for envvars
with paths.

On Linux/macOS/POSIX, GetPathEnv and GetEnv do the
same.

On Windows, GetPathEnv removes the UNC prefix from
the result, calls AsWindowsPath, then converts
backslashes to forward slashes. (As callers expect
the result.)

Fixes https://github.com/bazelbuild/bazel/issues/7705

Closes #7707.

PiperOrigin-RevId: 238236143
diff --git a/src/test/cpp/bazel_startup_options_test.cc b/src/test/cpp/bazel_startup_options_test.cc
index 18467a3..27d692f 100644
--- a/src/test/cpp/bazel_startup_options_test.cc
+++ b/src/test/cpp/bazel_startup_options_test.cc
@@ -32,7 +32,7 @@
     // This knowingly ignores the possibility of these environment variables
     // being unset because we expect our test runner to set them in all cases.
     // Otherwise, we'll crash here, but this keeps our code simpler.
-    old_test_tmpdir_ = GetEnv("TEST_TMPDIR");
+    old_test_tmpdir_ = GetPathEnv("TEST_TMPDIR");
 
     ReinitStartupOptions();
   }
diff --git a/src/test/cpp/blaze_util_windows_test.cc b/src/test/cpp/blaze_util_windows_test.cc
index f577b48..47d0aa9 100644
--- a/src/test/cpp/blaze_util_windows_test.cc
+++ b/src/test/cpp/blaze_util_windows_test.cc
@@ -96,27 +96,28 @@
   }
 
 TEST(BlazeUtilWindowsTest, TestGetEnv) {
-  ASSERT_ENVVAR_UNSET("DOES_not_EXIST");
+#define _STR(x) #x
+#define STR(x) _STR(x)
+  const char* envvar = "BAZEL_TEST_" STR(__LINE__);
+#undef STR
+#undef _STR
 
-  string actual(GetEnv("TEST_SRCDIR"));
-  ASSERT_NE(actual, "");
+  ASSERT_TRUE(SetEnvironmentVariableA(envvar, "A\\B c"));
+  ASSERT_EQ(GetEnv(envvar), "A\\B c");
+}
 
-  std::replace(actual.begin(), actual.end(), '/', '\\');
-  ASSERT_NE(actual.find(":\\"), string::npos);
+TEST(BlazeUtilWindowsTest, TestGetPathEnv) {
+#define _STR(x) #x
+#define STR(x) _STR(x)
+  const char* envvar = "BAZEL_TEST_" STR(__LINE__);
+#undef STR
+#undef _STR
 
-  ASSERT_ENVVAR_UNSET("Bazel_TEST_Key1");
-  ASSERT_TRUE(::SetEnvironmentVariableA("Bazel_TEST_Key1", "some_VALUE"));
-  ASSERT_ENVVAR("Bazel_TEST_Key1", "some_VALUE");
-  ASSERT_TRUE(::SetEnvironmentVariableA("Bazel_TEST_Key1", NULL));
+  ASSERT_TRUE(SetEnvironmentVariableA(envvar, "A\\B c"));
+  ASSERT_EQ(GetPathEnv(envvar), "A/B c");
 
-  string long_string(MAX_PATH, 'a');
-  string long_key = string("Bazel_TEST_Key2_") + long_string;
-  string long_value = string("Bazel_TEST_Value2_") + long_string;
-
-  ASSERT_ENVVAR_UNSET(long_key.c_str());
-  ASSERT_TRUE(::SetEnvironmentVariableA(long_key.c_str(), long_value.c_str()));
-  ASSERT_ENVVAR(long_key, long_value);
-  ASSERT_TRUE(::SetEnvironmentVariableA(long_key.c_str(), NULL));
+  ASSERT_TRUE(SetEnvironmentVariableA(envvar, "\\\\?\\A:\\B c"));
+  ASSERT_EQ(GetPathEnv(envvar), "A:/B c");
 }
 
 TEST(BlazeUtilWindowsTest, TestSetEnv) {
diff --git a/src/test/cpp/option_processor_test.cc b/src/test/cpp/option_processor_test.cc
index 461b425..e9f09cb 100644
--- a/src/test/cpp/option_processor_test.cc
+++ b/src/test/cpp/option_processor_test.cc
@@ -30,7 +30,7 @@
  protected:
   OptionProcessorTest()
       : workspace_(
-            blaze_util::JoinPath(blaze::GetEnv("TEST_TMPDIR"), "testdir")),
+            blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "testdir")),
         cwd_("cwd"),
         workspace_layout_(new WorkspaceLayout()) {}
 
diff --git a/src/test/cpp/rc_file_test.cc b/src/test/cpp/rc_file_test.cc
index 25baaaa..bfbdbea 100644
--- a/src/test/cpp/rc_file_test.cc
+++ b/src/test/cpp/rc_file_test.cc
@@ -41,11 +41,11 @@
 class RcFileTest : public ::testing::Test {
  protected:
   RcFileTest()
-      : workspace_(
-            blaze_util::JoinPath(blaze::GetEnv("TEST_TMPDIR"), "workspace")),
-        cwd_(blaze_util::JoinPath(blaze::GetEnv("TEST_TMPDIR"), "cwd")),
+      : workspace_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"),
+                                        "workspace")),
+        cwd_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "cwd")),
         binary_dir_(
-            blaze_util::JoinPath(blaze::GetEnv("TEST_TMPDIR"), "bazeldir")),
+            blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "bazeldir")),
         binary_path_(blaze_util::JoinPath(binary_dir_, "bazel")),
         workspace_layout_(new WorkspaceLayout()) {}
 
diff --git a/src/test/cpp/rc_options_test.cc b/src/test/cpp/rc_options_test.cc
index daaba7f..418639e 100644
--- a/src/test/cpp/rc_options_test.cc
+++ b/src/test/cpp/rc_options_test.cc
@@ -34,8 +34,7 @@
 class RcOptionsTest : public ::testing::Test {
  protected:
   RcOptionsTest()
-      : test_file_dir_(blaze::GetEnv("TEST_TMPDIR")),
-        workspace_layout_() {}
+      : test_file_dir_(blaze::GetPathEnv("TEST_TMPDIR")), workspace_layout_() {}
 
   const string test_file_dir_;
   const WorkspaceLayout workspace_layout_;
diff --git a/src/test/cpp/startup_options_test.cc b/src/test/cpp/startup_options_test.cc
index 9a0b01c..0c2cf2b 100644
--- a/src/test/cpp/startup_options_test.cc
+++ b/src/test/cpp/startup_options_test.cc
@@ -47,7 +47,7 @@
     // being unset because we expect our test runner to set them in all cases.
     // Otherwise, we'll crash here, but this keeps our code simpler.
     old_home_ = GetHomeDir();
-    old_test_tmpdir_ = GetEnv("TEST_TMPDIR");
+    old_test_tmpdir_ = GetPathEnv("TEST_TMPDIR");
 
     ReinitStartupOptions();
   }
diff --git a/src/test/cpp/util/logging_test.cc b/src/test/cpp/util/logging_test.cc
index 7891a07..dce3623 100644
--- a/src/test/cpp/util/logging_test.cc
+++ b/src/test/cpp/util/logging_test.cc
@@ -40,7 +40,7 @@
     // Set the value of $TMP first, because CaptureStderr retrieves a temp
     // directory path and on Windows, the corresponding function (GetTempPathA)
     // reads $TMP.
-    blaze::SetEnv("TMP", blaze::GetEnv("TEST_TMPDIR"));
+    blaze::SetEnv("TMP", blaze::GetPathEnv("TEST_TMPDIR"));
   }
   void TearDown() { blaze_util::SetLogHandler(nullptr); }
 };
@@ -530,7 +530,7 @@
 TEST(LoggingDeathTest,
      BazelLogHandler_CustomStream_BazelDiePrintsToStderrAndCustomStream) {
   std::string logfile =
-      blaze_util::JoinPath(blaze::GetEnv("TEST_TMPDIR"), "logfile");
+      blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), "logfile");
 
   ASSERT_EXIT(
       {
diff --git a/src/test/cpp/workspace_layout_test.cc b/src/test/cpp/workspace_layout_test.cc
index 4e17d64..865005a 100644
--- a/src/test/cpp/workspace_layout_test.cc
+++ b/src/test/cpp/workspace_layout_test.cc
@@ -27,10 +27,10 @@
 
 class WorkspaceLayoutTest : public ::testing::Test {
  protected:
-  WorkspaceLayoutTest() :
-      build_root_(blaze_util::JoinPath(
-          blaze::GetEnv("TEST_TMPDIR"), "build_root")),
-      workspace_layout_(new WorkspaceLayout()) {}
+  WorkspaceLayoutTest()
+      : build_root_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"),
+                                         "build_root")),
+        workspace_layout_(new WorkspaceLayout()) {}
 
   void SetUp() override {
     ASSERT_TRUE(blaze_util::MakeDirectories(build_root_, 0755));