From 2c711ea621c03e415dc9ad9e9f103e09372b8c50 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Thu, 28 Dec 2017 09:20:11 -0800 Subject: Add WixBuildTools.TestSupport with common testing functionality --- src/WixBuildTools.TestSupport/Builder.cs | 113 +++++++++++++++++++++ .../DisposableFileSystem.cs | 86 ++++++++++++++++ src/WixBuildTools.TestSupport/Pushd.cs | 46 +++++++++ src/WixBuildTools.TestSupport/TestData.cs | 16 +++ .../WixBuildTools.TestSupport.csproj | 21 ++++ 5 files changed, 282 insertions(+) create mode 100644 src/WixBuildTools.TestSupport/Builder.cs create mode 100644 src/WixBuildTools.TestSupport/DisposableFileSystem.cs create mode 100644 src/WixBuildTools.TestSupport/Pushd.cs create mode 100644 src/WixBuildTools.TestSupport/TestData.cs create mode 100644 src/WixBuildTools.TestSupport/WixBuildTools.TestSupport.csproj (limited to 'src') diff --git a/src/WixBuildTools.TestSupport/Builder.cs b/src/WixBuildTools.TestSupport/Builder.cs new file mode 100644 index 00000000..62439ff7 --- /dev/null +++ b/src/WixBuildTools.TestSupport/Builder.cs @@ -0,0 +1,113 @@ +// 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 WixBuildTools.TestSupport +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Text; + using WixToolset.Dtf.WindowsInstaller; + + public class Builder + { + public Builder(string sourceFolder, Type extensionType = null, string[] bindPaths = null) + { + this.SourceFolder = sourceFolder; + this.ExtensionType = extensionType; + this.BindPaths = bindPaths; + } + + public string[] BindPaths { get; } + + public Type ExtensionType { get; } + + public string SourceFolder { get; } + + public string[] BuildAndQuery(Action buildFunc, params string[] tables) + { + var sourceFiles = Directory.GetFiles(this.SourceFolder, "*.wxs"); + var wxlFiles = Directory.GetFiles(this.SourceFolder, "*.wxl"); + + using (var fs = new DisposableFileSystem()) + { + var intermediateFolder = fs.GetFolder(); + var outputPath = Path.Combine(intermediateFolder, @"bin\test.msi"); + + var args = new List + { + "build", + "-o", outputPath, + "-intermediateFolder", intermediateFolder, + }; + + if (this.ExtensionType != null) + { + args.Add("-ext"); + args.Add(Path.GetFullPath(new Uri(this.ExtensionType.Assembly.CodeBase).LocalPath)); + } + + args.AddRange(sourceFiles); + + foreach (var wxlFile in wxlFiles) + { + args.Add("-loc"); + args.Add(wxlFile); + } + + foreach (var bindPath in this.BindPaths) + { + args.Add("-bindpath"); + args.Add(bindPath); + } + + buildFunc(args.ToArray()); + + return this.Query(outputPath, tables); + } + } + + private string[] Query(string path, string[] tables) + { + var results = new List(); + + if (tables?.Length > 0) + { + var sb = new StringBuilder(); + using (var db = new Database(path)) + { + foreach (var table in tables) + { + using (var view = db.OpenView($"SELECT * FROM `{table}`")) + { + view.Execute(); + + Record record; + while ((record = view.Fetch()) != null) + { + sb.Clear(); + sb.AppendFormat("{0}:", table); + + using (record) + { + for (var i = 0; i < record.FieldCount; ++i) + { + if (i > 0) + { + sb.Append("\t"); + } + + sb.Append(record[i + 1]?.ToString()); + } + } + + results.Add(sb.ToString()); + } + } + } + } + } + + return results.ToArray(); + } + } +} diff --git a/src/WixBuildTools.TestSupport/DisposableFileSystem.cs b/src/WixBuildTools.TestSupport/DisposableFileSystem.cs new file mode 100644 index 00000000..28440f90 --- /dev/null +++ b/src/WixBuildTools.TestSupport/DisposableFileSystem.cs @@ -0,0 +1,86 @@ +// 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 WixBuildTools.TestSupport +{ + 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(); + + protected 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/WixBuildTools.TestSupport/Pushd.cs b/src/WixBuildTools.TestSupport/Pushd.cs new file mode 100644 index 00000000..d0545215 --- /dev/null +++ b/src/WixBuildTools.TestSupport/Pushd.cs @@ -0,0 +1,46 @@ +// 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 WixBuildTools.TestSupport +{ + 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/WixBuildTools.TestSupport/TestData.cs b/src/WixBuildTools.TestSupport/TestData.cs new file mode 100644 index 00000000..fde9db54 --- /dev/null +++ b/src/WixBuildTools.TestSupport/TestData.cs @@ -0,0 +1,16 @@ +// 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 WixBuildTools.TestSupport +{ + using System; + using System.IO; + + public class TestData + { + public static string Get(params string[] paths) + { + var localPath = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetCallingAssembly().CodeBase).LocalPath); + return Path.Combine(localPath, Path.Combine(paths)); + } + } +} diff --git a/src/WixBuildTools.TestSupport/WixBuildTools.TestSupport.csproj b/src/WixBuildTools.TestSupport/WixBuildTools.TestSupport.csproj new file mode 100644 index 00000000..e43a7753 --- /dev/null +++ b/src/WixBuildTools.TestSupport/WixBuildTools.TestSupport.csproj @@ -0,0 +1,21 @@ + + + + + + netstandard2.0 + + + + NU1701 + + + + + + + + + + + -- cgit v1.2.3-55-g6feb