From f13ab5731a9dbbd0a47567e9736f39825a4fa381 Mon Sep 17 00:00:00 2001 From: Sup Yut Sum Date: Fri, 3 Apr 2015 21:14:29 +0800 Subject: [PATCH] Unit Test: Do not use SHFileOperation to delete directory to make faster Signed-off-by: Sup Yut Sum --- test/UnitTests/AutoTempDir.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/test/UnitTests/AutoTempDir.cpp b/test/UnitTests/AutoTempDir.cpp index 2a773a108..3169783ed 100644 --- a/test/UnitTests/AutoTempDir.cpp +++ b/test/UnitTests/AutoTempDir.cpp @@ -33,10 +33,39 @@ CAutoTempDir::CAutoTempDir() tempdir = szTempName; } +static void DeleteDirectoryRecursive(CString dir) +{ + WIN32_FIND_DATA ffd; + HANDLE hp = FindFirstFile(dir + _T("\\*"), &ffd); + do + { + if (!_tcscmp(ffd.cFileName, _T(".")) || !_tcscmp(ffd.cFileName, _T(".."))) + continue; + if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) + { + CString subdir = dir + _T("\\") + ffd.cFileName; + DeleteDirectoryRecursive(subdir); + } + else + { + CString file = dir + _T("\\") + ffd.cFileName; + bool failed = !DeleteFile(file); + if (failed && GetLastError() == ERROR_ACCESS_DENIED) + { + SetFileAttributes(file, GetFileAttributes(file) & ~FILE_ATTRIBUTE_READONLY); + failed = !DeleteFile(file); + } + } + } while(FindNextFile(hp, &ffd)); + FindClose(hp); + + RemoveDirectory(dir); +} + CAutoTempDir::~CAutoTempDir() { if (!tempdir.IsEmpty()) - CTGitPath(tempdir).Delete(false); + DeleteDirectoryRecursive(tempdir); } CString CAutoTempDir::GetTempDir() const -- 2.11.4.GIT