| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | using namespace System; |
| 6 | using namespace Xunit; |
| 7 | using namespace WixInternal::TestSupport; |
| 8 | |
| 9 | namespace DutilTests |
| 10 | { |
| 11 | public ref class DirUtil |
| 12 | { |
| 13 | public: |
| 14 | [Fact] |
| 15 | void DirUtilTest() |
| 16 | { |
| 17 | HRESULT hr = S_OK; |
| 18 | LPWSTR sczCurrentDir = NULL; |
| 19 | LPWSTR sczGuid = NULL; |
| 20 | LPWSTR sczFolder = NULL; |
| 21 | LPWSTR sczSubFolder = NULL; |
| 22 | |
| 23 | try |
| 24 | { |
| 25 | hr = GuidCreate(&sczGuid); |
| 26 | NativeAssert::Succeeded(hr, "Failed to create guid."); |
| 27 | |
| 28 | hr = DirGetCurrent(&sczCurrentDir, NULL); |
| 29 | NativeAssert::Succeeded(hr, "Failed to get current directory."); |
| 30 | |
| 31 | hr = PathConcat(sczCurrentDir, sczGuid, &sczFolder); |
| 32 | NativeAssert::Succeeded(hr, "Failed to combine current directory: '{0}' with Guid: '{1}'", sczCurrentDir, sczGuid); |
| 33 | |
| 34 | BOOL fExists = DirExists(sczFolder, NULL); |
| 35 | Assert::False(fExists == TRUE); |
| 36 | |
| 37 | hr = PathConcat(sczFolder, L"foo", &sczSubFolder); |
| 38 | NativeAssert::Succeeded(hr, "Failed to combine folder: '%ls' with subfolder: 'foo'", sczFolder); |
| 39 | |
| 40 | hr = DirEnsureExists(sczSubFolder, NULL); |
| 41 | NativeAssert::Succeeded(hr, "Failed to create multiple directories: %ls", sczSubFolder); |
| 42 | |
| 43 | // Test failure to delete non-empty folder. |
| 44 | hr = DirEnsureDelete(sczFolder, FALSE, FALSE); |
| 45 | Assert::Equal(HRESULT_FROM_WIN32(ERROR_DIR_NOT_EMPTY), hr); |
| 46 | |
| 47 | hr = DirEnsureDelete(sczSubFolder, FALSE, FALSE); |
| 48 | NativeAssert::Succeeded(hr, "Failed to delete single directory: %ls", sczSubFolder); |
| 49 | |
| 50 | // Put the directory back and we'll test deleting tree. |
| 51 | hr = DirEnsureExists(sczSubFolder, NULL); |
| 52 | NativeAssert::Succeeded(hr, "Failed to create single directory: %ls", sczSubFolder); |
| 53 | |
| 54 | hr = DirEnsureDelete(sczFolder, FALSE, TRUE); |
| 55 | NativeAssert::Succeeded(hr, "Failed to delete directory tree: %ls", sczFolder); |
| 56 | |
| 57 | // Finally, try to create "C:\" which would normally fail, but we want success |
| 58 | hr = DirEnsureExists(L"C:\\", NULL); |
| 59 | NativeAssert::Succeeded(hr, "Failed to create C:\\"); |
| 60 | } |
| 61 | finally |
| 62 | { |
| 63 | ReleaseStr(sczSubFolder); |
| 64 | ReleaseStr(sczFolder); |
| 65 | ReleaseStr(sczGuid); |
| 66 | ReleaseStr(sczCurrentDir); |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | } |