llvm.org GIT mirror llvm / c5caccf
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298514 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 3 years ago
1 changed file(s) with 22 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
2727 #endif
2828
2929 #ifdef LLVM_ON_UNIX
30 #include
3031 #include
3132 #endif
3233
327328 }
328329 }
329330
330 #ifndef LLVM_ON_WIN32
331 #ifdef LLVM_ON_UNIX
331332 TEST(Support, HomeDirectoryWithNoEnv) {
332 std::string Original;
333 char const *path = ::getenv("HOME");
334 // Don't try to test if we don't have something to compare against.
335 if (!path)
336 return;
337 Original = path;
333 std::string OriginalStorage;
334 char const *OriginalEnv = ::getenv("HOME");
335 if (OriginalEnv) {
336 // We're going to unset it, so make a copy and save a pointer to the copy
337 // so that we can reset it at the end of the test.
338 OriginalStorage = OriginalEnv;
339 OriginalEnv = OriginalStorage.c_str();
340 }
341
342 // Don't run the test if we have nothing to compare against.
343 struct passwd *pw = getpwuid(getuid());
344 if (!pw || !pw->pw_dir) return;
345
338346 ::unsetenv("HOME");
347 EXPECT_EQ(nullptr, ::getenv("HOME"));
348 std::string PwDir = pw->pw_dir;
339349
340350 SmallString<128> HomeDir;
341351 auto status = path::home_directory(HomeDir);
342352 EXPECT_TRUE(status);
343 EXPECT_EQ(Original, HomeDir);
344
345 // Now put the original environment variable back
346 ::setenv("HOME", Original.c_str(), 1);
353 EXPECT_EQ(PwDir, HomeDir);
354
355 // Now put the environment back to its original state (meaning that if it was
356 // unset before, we don't reset it).
357 if (OriginalEnv) ::setenv("HOME", OriginalEnv, 1);
347358 }
348359 #endif
349360