aboutsummaryrefslogtreecommitdiff
path: root/src/internal/WixInternal.TestSupport/TestData.cs
blob: 05691e8c0e2e5c7eb4479b6a716cc01d9502a034 (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
71
72
73
74
75
76
77
78
// 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.

namespace WixInternal.TestSupport
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Runtime.CompilerServices;

    public class TestData
    {
        public static void CreateFile(string path, long size, bool fill = false)
        {
            // Ensure the directory exists.
            path = Path.GetFullPath(path);
            Directory.CreateDirectory(Path.GetDirectoryName(path));

            using (var file = File.OpenWrite(path))
            {
                if (fill)
                {
                    var random = new Random();
                    var bytes = new byte[4096];
                    var generated = 0L;

                    // Put fill bytes in the file so it doesn't compress trivially.
                    while (generated < size)
                    {
                        var generate = (int)Math.Min(size - generated, bytes.Length);

                        random.NextBytes(bytes);

                        file.Write(bytes, 0, generate);

                        generated += generate;
                    }
                }
                else
                {
                    file.SetLength(size);
                }
            }
        }

        public static string Get(params string[] paths)
        {
            var localPath = Path.GetDirectoryName(new Uri(Assembly.GetCallingAssembly().CodeBase).LocalPath);
            return Path.Combine(localPath, Path.Combine(paths));
        }

        public static string GetUnitTestLogsFolder([CallerFilePath] string path = "", [CallerMemberName] string method = "")
        {
            var startingPath = Path.GetDirectoryName(new Uri(Assembly.GetCallingAssembly().CodeBase).LocalPath);
            var buildPath = startingPath;

            while (!String.IsNullOrEmpty(buildPath))
            {
                var folderName = Path.GetFileName(buildPath);
                if (String.Equals("build", folderName, StringComparison.OrdinalIgnoreCase))
                {
                    break;
                }

                buildPath = Path.GetDirectoryName(buildPath);
            }

            if (String.IsNullOrEmpty(buildPath))
            {
                throw new InvalidOperationException($"Could not find the 'build' folder in the test path: {startingPath}. Cannot get test logs folder without being able to find the build folder.");
            }

            var testLogsFolder = Path.Combine(buildPath, "logs", "UnitTests", $"{Path.GetFileNameWithoutExtension(path)}_{method}");
            Directory.CreateDirectory(testLogsFolder);

            return testLogsFolder;
        }
    }
}