/ src / modules / powerrename / unittests / TestFileHelper.cpp
TestFileHelper.cpp
 1  #include "pch.h"
 2  #include "TestFileHelper.h"
 3  #include <iostream>
 4  #include <fstream>
 5  #include <Objbase.h>
 6  
 7  namespace fs = std::filesystem;
 8  
 9  CTestFileHelper::CTestFileHelper()
10  {
11      _CreateTempDirectory();
12  }
13  CTestFileHelper::~CTestFileHelper()
14  {
15      _DeleteTempDirectory();
16  }
17  
18  // Pass a relative path which will be appended to the temp directory path
19  bool CTestFileHelper::AddFile(_In_ const std::wstring path)
20  {
21      fs::path newFilePath = _tempDirectory;
22      newFilePath.append(path);
23      std::ofstream ofs(newFilePath);
24      ofs.close();
25      return true;
26  }
27  
28  // Pass a relative path which will be appended to the temp directory path
29  bool CTestFileHelper::AddFolder(_In_ const std::wstring path)
30  {
31      fs::path newFolderPath = _tempDirectory;
32      newFolderPath.append(path);
33      return fs::create_directory(fs::path(newFolderPath));
34  }
35  
36  fs::path CTestFileHelper::GetFullPath(_In_ const std::wstring path)
37  {
38      fs::path fullPath = _tempDirectory;
39      fullPath.append(path);
40      return fullPath;
41  }
42  
43  bool CTestFileHelper::PathExists(_In_ const std::wstring path)
44  {
45      fs::path fullPath = _tempDirectory;
46      fullPath.append(path);
47      return fs::exists(fullPath);
48  }
49  
50  bool CTestFileHelper::PathExistsCaseSensitive(_In_ const std::wstring path)
51  {
52      fs::path tempDirPath = fs::path(_tempDirectory);
53      for (const auto& entry : fs::directory_iterator(tempDirPath))
54      {
55          if (entry.path().filename().wstring() == path)
56          {
57              return true;
58          }
59      }
60      return false;
61  }
62  
63  bool CTestFileHelper::_CreateTempDirectory()
64  {
65      // Initialize to the temp directory
66      _tempDirectory = fs::temp_directory_path();
67  
68      // Create a unique folder name
69      GUID guid = { 0 };
70      CoCreateGuid(&guid);
71  
72      wchar_t uniqueName[MAX_PATH] = { 0 };
73      StringFromGUID2(guid, uniqueName, ARRAYSIZE(uniqueName));
74  
75      _tempDirectory.append(uniqueName);
76  
77      return fs::create_directory(_tempDirectory);
78  }
79  
80  void CTestFileHelper::_DeleteTempDirectory()
81  {
82      fs::remove_all(_tempDirectory);
83  }