diff options
-rw-r--r-- | base/file_util_unittest.cc | 31 | ||||
-rw-r--r-- | base/file_util_win.cc | 4 |
2 files changed, 31 insertions, 4 deletions
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc index b1f9fed..073dbae 100644 --- a/base/file_util_unittest.cc +++ b/base/file_util_unittest.cc @@ -8,6 +8,7 @@ #include <windows.h> #include <shellapi.h> #include <shlobj.h> +#include <tchar.h> #endif #include <fstream> @@ -773,7 +774,35 @@ TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) { EXPECT_TRUE(file_util::PathExists(dir_name_to)); EXPECT_TRUE(file_util::PathExists(file_name_to)); } -#endif + +TEST_F(FileUtilTest, GetTempDirTest) { + static const TCHAR* kTmpKey = _T("TMP"); + static const TCHAR* kTmpValues[] = { + _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\") + }; + // Save the original $TMP. + size_t original_tmp_size; + TCHAR* original_tmp; + ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey)); + // original_tmp may be NULL. + + for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) { + FilePath path; + ::_tputenv_s(kTmpKey, kTmpValues[i]); + file_util::GetTempDir(&path); + EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] << + " result=" << path.value(); + } + + // Restore the original $TMP. + if (original_tmp) { + ::_tputenv_s(kTmpKey, original_tmp); + free(original_tmp); + } else { + ::_tputenv_s(kTmpKey, _T("")); + } +} +#endif // OS_WIN TEST_F(FileUtilTest, CreateTemporaryFileTest) { FilePath temp_files[3]; diff --git a/base/file_util_win.cc b/base/file_util_win.cc index 0acaf5f..972a7b8 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -428,9 +428,7 @@ bool GetTempDir(FilePath* path) { // TODO(evanm): the old behavior of this function was to always strip the // trailing slash. We duplicate this here, but it shouldn't be necessary // when everyone is using the appropriate FilePath APIs. - std::wstring path_str(temp_path); - TrimTrailingSeparator(&path_str); - *path = FilePath(path_str); + *path = FilePath(temp_path).StripTrailingSeparators(); return true; } |