aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/Path.cpp
diff options
context:
space:
mode:
authorPawel Bylica <chfast@gmail.com>2015-11-06 23:21:49 +0000
committerPawel Bylica <chfast@gmail.com>2015-11-06 23:21:49 +0000
commitb43221439c6e57c90dd231cba0c12ace0aee73c7 (patch)
treeafda6be221219c5228f24f5d530ef386a9f959c3 /llvm/unittests/Support/Path.cpp
parent472d937db78109b67629eb6827424810c8002e8c (diff)
downloadllvm-b43221439c6e57c90dd231cba0c12ace0aee73c7.zip
llvm-b43221439c6e57c90dd231cba0c12ace0aee73c7.tar.gz
llvm-b43221439c6e57c90dd231cba0c12ace0aee73c7.tar.bz2
[Support] Use GetTempDir to get the temporary dir path on Windows.
Summary: In general GetTempDir follows the same logic as the replaced code: checks env variables TMP, TEMP, USERPROFILE in order. However, it also perform other checks like making separators native (\), making the path absolute, etc. This change fixes FileSystemTest.CreateDir unittest that had been failing when run from Unix-like shell on Windows (Unix-like path separator (/) used in env variables). Reviewers: chapuni, rafael, aaron.ballman Subscribers: rafael, llvm-commits Differential Revision: http://reviews.llvm.org/D14231 llvm-svn: 252366
Diffstat (limited to 'llvm/unittests/Support/Path.cpp')
-rw-r--r--llvm/unittests/Support/Path.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp
index a7a6a4a..ebfd266e 100644
--- a/llvm/unittests/Support/Path.cpp
+++ b/llvm/unittests/Support/Path.cpp
@@ -351,6 +351,76 @@ TEST(Support, UserCacheDirectory) {
}
}
+TEST(Support, TempDirectory) {
+ SmallString<32> TempDir;
+ path::system_temp_directory(false, TempDir);
+ EXPECT_TRUE(!TempDir.empty());
+ TempDir.clear();
+ path::system_temp_directory(true, TempDir);
+ EXPECT_TRUE(!TempDir.empty());
+}
+
+static std::string path2regex(std::string Path) {
+ size_t Pos = 0;
+ while ((Pos = Path.find('\\', Pos)) != std::string::npos) {
+ Path.replace(Pos, 1, "\\\\");
+ Pos += 2;
+ }
+ return Path;
+}
+
+/// Helper for running temp dir test in separated process. See below.
+#define EXPECT_TEMP_DIR(prepare, expected) \
+ EXPECT_EXIT( \
+ { \
+ prepare; \
+ SmallString<300> TempDir; \
+ path::system_temp_directory(true, TempDir); \
+ raw_os_ostream(std::cerr) << TempDir; \
+ std::exit(0); \
+ }, \
+ ::testing::ExitedWithCode(0), path2regex(expected))
+
+#ifdef LLVM_ON_WIN32
+TEST(SupportDeathTest, TempDirectoryOnWindows) {
+ // In this test we want to check how system_temp_directory responds to
+ // different values of specific env vars. To prevent corrupting env vars of
+ // the current process all checks are done in separated processes.
+ EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:\\OtherFolder"), "C:\\OtherFolder");
+ EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"C:/Unix/Path/Seperators"),
+ "C:\\Unix\\Path\\Seperators");
+ EXPECT_TEMP_DIR(_wputenv_s(L"TMP", L"Local Path"), ".+\\Local Path$");
+ EXPECT_TEMP_DIR(
+ _wputenv_s(L"TMP", L"C:\\2\x03C0r-\x00B5\x00B3\\\x2135\x2080"),
+ "C:\\2\xCF\x80r-\xC2\xB5\xC2\xB3\\\xE2\x84\xB5\xE2\x82\x80");
+
+ // Test $TMP empty, $TEMP set.
+ EXPECT_TEMP_DIR(
+ {
+ _wputenv_s(L"TMP", L"");
+ _wputenv_s(L"TEMP", L"C:\\Valid\\Path");
+ },
+ "C:\\Valid\\Path");
+
+ // Test evn var / path with 260 chars.
+ SmallString<270> Expected{"C:\\Temp\\AB\\123456789"};
+ while (Expected.size() < 260)
+ Expected.append("\\DirNameWith19Charss");
+ ASSERT_EQ(260, Expected.size());
+ EXPECT_TEMP_DIR(_putenv_s("TMP", Expected.c_str()), Expected.c_str());
+
+ // Test evn var 261 chars.
+ Expected.append("X");
+ ASSERT_EQ(261, Expected.size());
+ EXPECT_TEMP_DIR(
+ {
+ _putenv_s("TMP", Expected.c_str());
+ _wputenv_s(L"TEMP", L"C:\\Short\\Path");
+ },
+ "C:\\Short\\Path");
+}
+#endif
+
class FileSystemTest : public testing::Test {
protected:
/// Unique temporary directory in which all created filesystem entities must