diff options
author | Zachary Turner <zturner@google.com> | 2017-03-22 16:30:06 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2017-03-22 16:30:06 +0000 |
commit | d8350cf3fce4bb582e99634947f8fc4cbb7cb105 (patch) | |
tree | 4235d2ecf9b939aaad68ac76bd1ee402d904f4a1 /llvm/unittests/Support/Path.cpp | |
parent | a3cf70bfa0f11d7da90f11e148be8f9c285c79ac (diff) | |
download | bcm5719-llvm-d8350cf3fce4bb582e99634947f8fc4cbb7cb105.tar.gz bcm5719-llvm-d8350cf3fce4bb582e99634947f8fc4cbb7cb105.zip |
Make the home_directory test a little more resilient.
It's possible (albeit strange) for $HOME to intentionally
point somewhere other than the user's home directory as
reported by the password database. Our test shouldn't fail
in this case. This patch updates the test to pull directly
from the password database before unsetting $HOME, rather
than comparing the return value of home_directory() to the
original value of the environment variable.
llvm-svn: 298514
Diffstat (limited to 'llvm/unittests/Support/Path.cpp')
-rw-r--r-- | llvm/unittests/Support/Path.cpp | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index afa5a5a0d46..86ad57f3f3f 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -28,6 +28,7 @@ #endif #ifdef LLVM_ON_UNIX +#include <pwd.h> #include <sys/stat.h> #endif @@ -328,23 +329,33 @@ TEST(Support, HomeDirectory) { } } -#ifndef LLVM_ON_WIN32 +#ifdef LLVM_ON_UNIX TEST(Support, HomeDirectoryWithNoEnv) { - std::string Original; - char const *path = ::getenv("HOME"); - // Don't try to test if we don't have something to compare against. - if (!path) - return; - Original = path; + std::string OriginalStorage; + char const *OriginalEnv = ::getenv("HOME"); + if (OriginalEnv) { + // We're going to unset it, so make a copy and save a pointer to the copy + // so that we can reset it at the end of the test. + OriginalStorage = OriginalEnv; + OriginalEnv = OriginalStorage.c_str(); + } + + // Don't run the test if we have nothing to compare against. + struct passwd *pw = getpwuid(getuid()); + if (!pw || !pw->pw_dir) return; + ::unsetenv("HOME"); + EXPECT_EQ(nullptr, ::getenv("HOME")); + std::string PwDir = pw->pw_dir; SmallString<128> HomeDir; auto status = path::home_directory(HomeDir); EXPECT_TRUE(status); - EXPECT_EQ(Original, HomeDir); + EXPECT_EQ(PwDir, HomeDir); - // Now put the original environment variable back - ::setenv("HOME", Original.c_str(), 1); + // Now put the environment back to its original state (meaning that if it was + // unset before, we don't reset it). + if (OriginalEnv) ::setenv("HOME", OriginalEnv, 1); } #endif |