From f905838a6398e2d5083145acf279968506ac900b Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Mon, 1 Aug 2022 19:26:38 -0700 Subject: Add TestData.CreateFile and dedupe TestSupport code from Core.Native tests --- src/internal/WixBuildTools.TestSupport/TestData.cs | 33 +++++++++ .../Utilities/TestExeTool.cs | 2 +- .../WixToolsetTest.Core.Native/CabinetFixture.cs | 2 +- .../CertificateHashesFixture.cs | 2 +- .../Utility/DisposableFileSystem.cs | 86 ---------------------- .../WixToolsetTest.Core.Native/Utility/Pushd.cs | 46 ------------ .../WixToolsetTest.Core.Native/Utility/TestData.cs | 17 ----- 7 files changed, 36 insertions(+), 152 deletions(-) delete mode 100644 src/wix/test/WixToolsetTest.Core.Native/Utility/DisposableFileSystem.cs delete mode 100644 src/wix/test/WixToolsetTest.Core.Native/Utility/Pushd.cs delete mode 100644 src/wix/test/WixToolsetTest.Core.Native/Utility/TestData.cs diff --git a/src/internal/WixBuildTools.TestSupport/TestData.cs b/src/internal/WixBuildTools.TestSupport/TestData.cs index 309e9038..fc1ae4cc 100644 --- a/src/internal/WixBuildTools.TestSupport/TestData.cs +++ b/src/internal/WixBuildTools.TestSupport/TestData.cs @@ -9,6 +9,39 @@ namespace WixBuildTools.TestSupport 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); diff --git a/src/test/burn/WixToolsetTest.BurnE2E/Utilities/TestExeTool.cs b/src/test/burn/WixToolsetTest.BurnE2E/Utilities/TestExeTool.cs index a02299d7..07442cd3 100644 --- a/src/test/burn/WixToolsetTest.BurnE2E/Utilities/TestExeTool.cs +++ b/src/test/burn/WixToolsetTest.BurnE2E/Utilities/TestExeTool.cs @@ -7,7 +7,7 @@ namespace WixTestTools public class TestExeTool : TestTool { - private static readonly string TestExePath32 = Path.Combine(TestData.Get(), "win-x86", "TestExe.exe"); + private static readonly string TestExePath32 = TestData.Get("win-x86", "TestExe.exe"); public TestExeTool() : base(TestExePath32) diff --git a/src/wix/test/WixToolsetTest.Core.Native/CabinetFixture.cs b/src/wix/test/WixToolsetTest.Core.Native/CabinetFixture.cs index 0d5d777d..c566339a 100644 --- a/src/wix/test/WixToolsetTest.Core.Native/CabinetFixture.cs +++ b/src/wix/test/WixToolsetTest.Core.Native/CabinetFixture.cs @@ -4,9 +4,9 @@ namespace WixToolsetTest.CoreNative { using System.IO; using System.Linq; + using WixBuildTools.TestSupport; using WixToolset.Core.Native; using WixToolset.Data; - using WixToolsetTest.CoreNative.Utility; using Xunit; public class CabinetFixture diff --git a/src/wix/test/WixToolsetTest.Core.Native/CertificateHashesFixture.cs b/src/wix/test/WixToolsetTest.Core.Native/CertificateHashesFixture.cs index beb5224c..9f8a31f6 100644 --- a/src/wix/test/WixToolsetTest.Core.Native/CertificateHashesFixture.cs +++ b/src/wix/test/WixToolsetTest.Core.Native/CertificateHashesFixture.cs @@ -3,8 +3,8 @@ namespace WixToolsetTest.CoreNative { using System.Linq; + using WixBuildTools.TestSupport; using WixToolset.Core.Native; - using WixToolsetTest.CoreNative.Utility; using Xunit; public class CertificateHashesFixture diff --git a/src/wix/test/WixToolsetTest.Core.Native/Utility/DisposableFileSystem.cs b/src/wix/test/WixToolsetTest.Core.Native/Utility/DisposableFileSystem.cs deleted file mode 100644 index c9957247..00000000 --- a/src/wix/test/WixToolsetTest.Core.Native/Utility/DisposableFileSystem.cs +++ /dev/null @@ -1,86 +0,0 @@ -// 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 WixToolsetTest.CoreNative.Utility -{ - using System; - using System.Collections.Generic; - using System.IO; - - public class DisposableFileSystem : IDisposable - { - protected bool Disposed { get; private set; } - - private List CleanupPaths { get; } = new List(); - - public string GetFile(bool create = false) - { - var path = Path.GetTempFileName(); - - if (!create) - { - File.Delete(path); - } - - this.CleanupPaths.Add(path); - - return path; - } - - public string GetFolder(bool create = false) - { - var path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); - - if (create) - { - Directory.CreateDirectory(path); - } - - this.CleanupPaths.Add(path); - - return path; - } - - - #region // IDisposable - - public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize(this); - } - - protected virtual void Dispose(bool disposing) - { - if (this.Disposed) - { - return; - } - - if (disposing) - { - foreach (var path in this.CleanupPaths) - { - try - { - if (File.Exists(path)) - { - File.Delete(path); - } - else if (Directory.Exists(path)) - { - Directory.Delete(path, true); - } - } - catch - { - // Best effort delete, so ignore any failures. - } - } - } - - this.Disposed = true; - } - - #endregion - } -} diff --git a/src/wix/test/WixToolsetTest.Core.Native/Utility/Pushd.cs b/src/wix/test/WixToolsetTest.Core.Native/Utility/Pushd.cs deleted file mode 100644 index 91700c2f..00000000 --- a/src/wix/test/WixToolsetTest.Core.Native/Utility/Pushd.cs +++ /dev/null @@ -1,46 +0,0 @@ -// 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 WixToolsetTest.CoreNative.Utility -{ - using System; - using System.IO; - - public class Pushd : IDisposable - { - protected bool Disposed { get; private set; } - - public Pushd(string path) - { - this.PreviousDirectory = Directory.GetCurrentDirectory(); - - Directory.SetCurrentDirectory(path); - } - - public string PreviousDirectory { get; } - - #region // IDisposable - - public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize(this); - } - - protected virtual void Dispose(bool disposing) - { - if (this.Disposed) - { - return; - } - - if (disposing) - { - Directory.SetCurrentDirectory(this.PreviousDirectory); - } - - this.Disposed = true; - } - - #endregion - } -} diff --git a/src/wix/test/WixToolsetTest.Core.Native/Utility/TestData.cs b/src/wix/test/WixToolsetTest.Core.Native/Utility/TestData.cs deleted file mode 100644 index cd9c6318..00000000 --- a/src/wix/test/WixToolsetTest.Core.Native/Utility/TestData.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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 WixToolsetTest.CoreNative.Utility -{ - using System; - using System.IO; - - public class TestData - { - public static string LocalPath => Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath); - - public static string Get(params string[] paths) - { - return Path.Combine(LocalPath, Path.Combine(paths)); - } - } -} -- cgit v1.2.3-55-g6feb