@joebigelow / wix-1 / commits / f905838a

Add TestData.CreateFile and dedupe TestSupport code from Core.Native tests

Rob Mensching committed Aug 1, 2022 at 19:26 UTC f905838a6398e2d5083145acf279968506ac900b
7 files changed +36 -152
src/internal/WixBuildTools.TestSupport/TestData.cs
+33
@@ -9,6 +9,39 @@ namespace WixBuildTools.TestSupport
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 = Path.GetDirectoryName(new Uri(Assembly.GetCallingAssembly().CodeBase).LocalPath);
src/test/burn/WixToolsetTest.BurnE2E/Utilities/TestExeTool.cs
+1 -1
@@ -7,7 +7,7 @@ namespace WixTestTools
7
8 public class TestExeTool : TestTool
9 {
10 - private static readonly string TestExePath32 = Path.Combine(TestData.Get(), "win-x86", "TestExe.exe");
10 + private static readonly string TestExePath32 = TestData.Get("win-x86", "TestExe.exe");
11
12 public TestExeTool()
13 : base(TestExePath32)
src/wix/test/WixToolsetTest.Core.Native/CabinetFixture.cs
+1 -1
@@ -4,9 +4,9 @@ namespace WixToolsetTest.CoreNative
4 {
5 using System.IO;
6 using System.Linq;
7 + using WixBuildTools.TestSupport;
8 using WixToolset.Core.Native;
9 using WixToolset.Data;
9 - using WixToolsetTest.CoreNative.Utility;
10 using Xunit;
11
12 public class CabinetFixture
src/wix/test/WixToolsetTest.Core.Native/CertificateHashesFixture.cs
+1 -1
@@ -3,8 +3,8 @@
3 namespace WixToolsetTest.CoreNative
4 {
5 using System.Linq;
6 + using WixBuildTools.TestSupport;
7 using WixToolset.Core.Native;
7 - using WixToolsetTest.CoreNative.Utility;
8 using Xunit;
9
10 public class CertificateHashesFixture
src/wix/test/WixToolsetTest.Core.Native/Utility/DisposableFileSystem.cs deleted
-86
@@ -1,86 +0,0 @@
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 WixToolsetTest.CoreNative.Utility
4 -{
5 - using System;
6 - using System.Collections.Generic;
7 - using System.IO;
8 -
9 - public class DisposableFileSystem : IDisposable
10 - {
11 - protected bool Disposed { get; private set; }
12 -
13 - private List<string> CleanupPaths { get; } = new List<string>();
14 -
15 - public string GetFile(bool create = false)
16 - {
17 - var path = Path.GetTempFileName();
18 -
19 - if (!create)
20 - {
21 - File.Delete(path);
22 - }
23 -
24 - this.CleanupPaths.Add(path);
25 -
26 - return path;
27 - }
28 -
29 - public string GetFolder(bool create = false)
30 - {
31 - var path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
32 -
33 - if (create)
34 - {
35 - Directory.CreateDirectory(path);
36 - }
37 -
38 - this.CleanupPaths.Add(path);
39 -
40 - return path;
41 - }
42 -
43 -
44 - #region // IDisposable
45 -
46 - public void Dispose()
47 - {
48 - this.Dispose(true);
49 - GC.SuppressFinalize(this);
50 - }
51 -
52 - protected virtual void Dispose(bool disposing)
53 - {
54 - if (this.Disposed)
55 - {
56 - return;
57 - }
58 -
59 - if (disposing)
60 - {
61 - foreach (var path in this.CleanupPaths)
62 - {
63 - try
64 - {
65 - if (File.Exists(path))
66 - {
67 - File.Delete(path);
68 - }
69 - else if (Directory.Exists(path))
70 - {
71 - Directory.Delete(path, true);
72 - }
73 - }
74 - catch
75 - {
76 - // Best effort delete, so ignore any failures.
77 - }
78 - }
79 - }
80 -
81 - this.Disposed = true;
82 - }
83 -
84 - #endregion
85 - }
86 -}
src/wix/test/WixToolsetTest.Core.Native/Utility/Pushd.cs deleted
-46
@@ -1,46 +0,0 @@
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 WixToolsetTest.CoreNative.Utility
4 -{
5 - using System;
6 - using System.IO;
7 -
8 - public class Pushd : IDisposable
9 - {
10 - protected bool Disposed { get; private set; }
11 -
12 - public Pushd(string path)
13 - {
14 - this.PreviousDirectory = Directory.GetCurrentDirectory();
15 -
16 - Directory.SetCurrentDirectory(path);
17 - }
18 -
19 - public string PreviousDirectory { get; }
20 -
21 - #region // IDisposable
22 -
23 - public void Dispose()
24 - {
25 - this.Dispose(true);
26 - GC.SuppressFinalize(this);
27 - }
28 -
29 - protected virtual void Dispose(bool disposing)
30 - {
31 - if (this.Disposed)
32 - {
33 - return;
34 - }
35 -
36 - if (disposing)
37 - {
38 - Directory.SetCurrentDirectory(this.PreviousDirectory);
39 - }
40 -
41 - this.Disposed = true;
42 - }
43 -
44 - #endregion
45 - }
46 -}
src/wix/test/WixToolsetTest.Core.Native/Utility/TestData.cs deleted
-17
@@ -1,17 +0,0 @@
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 WixToolsetTest.CoreNative.Utility
4 -{
5 - using System;
6 - using System.IO;
7 -
8 - public class TestData
9 - {
10 - public static string LocalPath => Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
11 -
12 - public static string Get(params string[] paths)
13 - {
14 - return Path.Combine(LocalPath, Path.Combine(paths));
15 - }
16 - }
17 -}