| 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 | namespace WixInternal.TestSupport |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Reflection; |
| 8 | using System.Runtime.CompilerServices; |
| 9 | |
| 10 | public class TestData |
| 11 | { |
| 12 | public static void CreateFile(string path, long size, bool fill = false) |
| 13 | { |
| 14 | // Ensure the directory exists. |
| 15 | path = Path.GetFullPath(path); |
| 16 | Directory.CreateDirectory(Path.GetDirectoryName(path)); |
| 17 | |
| 18 | using (var file = File.OpenWrite(path)) |
| 19 | { |
| 20 | if (fill) |
| 21 | { |
| 22 | var random = new Random(); |
| 23 | var bytes = new byte[4096]; |
| 24 | var generated = 0L; |
| 25 | |
| 26 | // Put fill bytes in the file so it doesn't compress trivially. |
| 27 | while (generated < size) |
| 28 | { |
| 29 | var generate = (int)Math.Min(size - generated, bytes.Length); |
| 30 | |
| 31 | random.NextBytes(bytes); |
| 32 | |
| 33 | file.Write(bytes, 0, generate); |
| 34 | |
| 35 | generated += generate; |
| 36 | } |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | file.SetLength(size); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public static string Get(params string[] paths) |
| 46 | { |
| 47 | var localPath = AppDomain.CurrentDomain.BaseDirectory; |
| 48 | return Path.Combine(localPath, Path.Combine(paths)); |
| 49 | } |
| 50 | |
| 51 | public static string GetUnitTestLogsFolder([CallerFilePath] string path = "", [CallerMemberName] string method = "") |
| 52 | { |
| 53 | var startingPath = AppDomain.CurrentDomain.BaseDirectory; |
| 54 | var buildPath = startingPath; |
| 55 | |
| 56 | while (!String.IsNullOrEmpty(buildPath)) |
| 57 | { |
| 58 | var folderName = Path.GetFileName(buildPath); |
| 59 | if (String.Equals("build", folderName, StringComparison.OrdinalIgnoreCase)) |
| 60 | { |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | buildPath = Path.GetDirectoryName(buildPath); |
| 65 | } |
| 66 | |
| 67 | if (String.IsNullOrEmpty(buildPath)) |
| 68 | { |
| 69 | 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."); |
| 70 | } |
| 71 | |
| 72 | var testLogsFolder = Path.Combine(buildPath, "logs", "UnitTests", $"{Path.GetFileNameWithoutExtension(path)}_{method}"); |
| 73 | Directory.CreateDirectory(testLogsFolder); |
| 74 | |
| 75 | return testLogsFolder; |
| 76 | } |
| 77 | } |
| 78 | } |