aboutsummaryrefslogtreecommitdiff
path: root/src/libs/dutil/test/DUtilUnitTest/DirUtilTests.cpp
blob: 3475af873cbdc50746613b6c9343105f8488655e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 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.

#include "precomp.h"

using namespace System;
using namespace Xunit;
using namespace WixInternal::TestSupport;

namespace DutilTests
{
    public ref class DirUtil
    {
    public:
        [Fact]
        void DirUtilTest()
        {
            HRESULT hr = S_OK;
            LPWSTR sczCurrentDir = NULL;
            LPWSTR sczGuid = NULL;
            LPWSTR sczFolder = NULL;
            LPWSTR sczSubFolder = NULL;

            try
            {
                hr = GuidCreate(&sczGuid);
                NativeAssert::Succeeded(hr, "Failed to create guid.");

                hr = DirGetCurrent(&sczCurrentDir, NULL);
                NativeAssert::Succeeded(hr, "Failed to get current directory.");

                hr = PathConcat(sczCurrentDir, sczGuid, &sczFolder);
                NativeAssert::Succeeded(hr, "Failed to combine current directory: '{0}' with Guid: '{1}'", sczCurrentDir, sczGuid);

                BOOL fExists = DirExists(sczFolder, NULL);
                Assert::False(fExists == TRUE);

                hr = PathConcat(sczFolder, L"foo", &sczSubFolder);
                NativeAssert::Succeeded(hr, "Failed to combine folder: '%ls' with subfolder: 'foo'", sczFolder);

                hr = DirEnsureExists(sczSubFolder, NULL);
                NativeAssert::Succeeded(hr, "Failed to create multiple directories: %ls", sczSubFolder);

                // Test failure to delete non-empty folder.
                hr = DirEnsureDelete(sczFolder, FALSE, FALSE);
                Assert::Equal(HRESULT_FROM_WIN32(ERROR_DIR_NOT_EMPTY), hr);

                hr = DirEnsureDelete(sczSubFolder, FALSE, FALSE);
                NativeAssert::Succeeded(hr, "Failed to delete single directory: %ls", sczSubFolder);

                // Put the directory back and we'll test deleting tree.
                hr = DirEnsureExists(sczSubFolder, NULL);
                NativeAssert::Succeeded(hr, "Failed to create single directory: %ls", sczSubFolder);

                hr = DirEnsureDelete(sczFolder, FALSE, TRUE);
                NativeAssert::Succeeded(hr, "Failed to delete directory tree: %ls", sczFolder);

                // Finally, try to create "C:\" which would normally fail, but we want success
                hr = DirEnsureExists(L"C:\\", NULL);
                NativeAssert::Succeeded(hr, "Failed to create C:\\");
            }
            finally
            {
                ReleaseStr(sczSubFolder);
                ReleaseStr(sczFolder);
                ReleaseStr(sczGuid);
                ReleaseStr(sczCurrentDir);
            }
        }
    };
}