@joebigelow / wix-1 / commits / 2c711ea6

Add WixBuildTools.TestSupport with common testing functionality

Rob Mensching committed Dec 28, 2017 at 09:20 UTC 2c711ea621c03e415dc9ad9e9f103e09372b8c50
7 files changed +298 -7
WixBuildTools.sln
+15 -7
@@ -1,13 +1,13 @@
1 Microsoft Visual Studio Solution File, Format Version 12.00
2 # Visual Studio 15
3 -VisualStudioVersion = 15.0.26730.3
3 +VisualStudioVersion = 15.0.27130.2003
4 MinimumVisualStudioVersion = 15.0.26124.0
5 -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{00E50075-E896-42D1-AC30-2D2E7D129FB9}"
6 -EndProject
5 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WixBuildTools.XsdGen", "src\WixBuildTools.XsdGen\WixBuildTools.XsdGen.csproj", "{E89E52C9-A4A1-4174-A1B1-3B72975E6ED6}"
6 EndProject
7 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WixBuildTools.MsgGen", "src\WixBuildTools.MsgGen\WixBuildTools.MsgGen.csproj", "{DB6EF6F3-51B1-4214-9A14-D501C23F6FA4}"
8 EndProject
9 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WixBuildTools.TestSupport", "src\WixBuildTools.TestSupport\WixBuildTools.TestSupport.csproj", "{6C57EF2C-979A-4106-A9E5-FE342810619A}"
10 +EndProject
11 Global
12 GlobalSection(SolutionConfigurationPlatforms) = preSolution
13 Debug|Any CPU = Debug|Any CPU
@@ -42,14 +42,22 @@ Global
42 {DB6EF6F3-51B1-4214-9A14-D501C23F6FA4}.Release|x64.Build.0 = Release|Any CPU
43 {DB6EF6F3-51B1-4214-9A14-D501C23F6FA4}.Release|x86.ActiveCfg = Release|Any CPU
44 {DB6EF6F3-51B1-4214-9A14-D501C23F6FA4}.Release|x86.Build.0 = Release|Any CPU
45 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
46 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Debug|Any CPU.Build.0 = Debug|Any CPU
47 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Debug|x64.ActiveCfg = Debug|Any CPU
48 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Debug|x64.Build.0 = Debug|Any CPU
49 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Debug|x86.ActiveCfg = Debug|Any CPU
50 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Debug|x86.Build.0 = Debug|Any CPU
51 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Release|Any CPU.ActiveCfg = Release|Any CPU
52 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Release|Any CPU.Build.0 = Release|Any CPU
53 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Release|x64.ActiveCfg = Release|Any CPU
54 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Release|x64.Build.0 = Release|Any CPU
55 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Release|x86.ActiveCfg = Release|Any CPU
56 + {6C57EF2C-979A-4106-A9E5-FE342810619A}.Release|x86.Build.0 = Release|Any CPU
57 EndGlobalSection
58 GlobalSection(SolutionProperties) = preSolution
59 HideSolutionNode = FALSE
60 EndGlobalSection
49 - GlobalSection(NestedProjects) = preSolution
50 - {E89E52C9-A4A1-4174-A1B1-3B72975E6ED6} = {00E50075-E896-42D1-AC30-2D2E7D129FB9}
51 - {DB6EF6F3-51B1-4214-9A14-D501C23F6FA4} = {00E50075-E896-42D1-AC30-2D2E7D129FB9}
52 - EndGlobalSection
61 GlobalSection(ExtensibilityGlobals) = postSolution
62 SolutionGuid = {83E9E075-B440-471A-9C37-9D84BA0AE3E0}
63 EndGlobalSection
nuget.config
+1
@@ -2,6 +2,7 @@
2 <configuration>
3 <packageSources>
4 <clear />
5 + <add key="wixtoolset-dtf" value="https://ci.appveyor.com/nuget/wixtoolset-dtf" />
6 <add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
7 </packageSources>
8 </configuration>
\ No newline at end of file
src/WixBuildTools.TestSupport/Builder.cs new
+113
@@ -0,0 +1,113 @@
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 WixBuildTools.TestSupport
4 +{
5 + using System;
6 + using System.Collections.Generic;
7 + using System.IO;
8 + using System.Text;
9 + using WixToolset.Dtf.WindowsInstaller;
10 +
11 + public class Builder
12 + {
13 + public Builder(string sourceFolder, Type extensionType = null, string[] bindPaths = null)
14 + {
15 + this.SourceFolder = sourceFolder;
16 + this.ExtensionType = extensionType;
17 + this.BindPaths = bindPaths;
18 + }
19 +
20 + public string[] BindPaths { get; }
21 +
22 + public Type ExtensionType { get; }
23 +
24 + public string SourceFolder { get; }
25 +
26 + public string[] BuildAndQuery(Action<string[]> buildFunc, params string[] tables)
27 + {
28 + var sourceFiles = Directory.GetFiles(this.SourceFolder, "*.wxs");
29 + var wxlFiles = Directory.GetFiles(this.SourceFolder, "*.wxl");
30 +
31 + using (var fs = new DisposableFileSystem())
32 + {
33 + var intermediateFolder = fs.GetFolder();
34 + var outputPath = Path.Combine(intermediateFolder, @"bin\test.msi");
35 +
36 + var args = new List<string>
37 + {
38 + "build",
39 + "-o", outputPath,
40 + "-intermediateFolder", intermediateFolder,
41 + };
42 +
43 + if (this.ExtensionType != null)
44 + {
45 + args.Add("-ext");
46 + args.Add(Path.GetFullPath(new Uri(this.ExtensionType.Assembly.CodeBase).LocalPath));
47 + }
48 +
49 + args.AddRange(sourceFiles);
50 +
51 + foreach (var wxlFile in wxlFiles)
52 + {
53 + args.Add("-loc");
54 + args.Add(wxlFile);
55 + }
56 +
57 + foreach (var bindPath in this.BindPaths)
58 + {
59 + args.Add("-bindpath");
60 + args.Add(bindPath);
61 + }
62 +
63 + buildFunc(args.ToArray());
64 +
65 + return this.Query(outputPath, tables);
66 + }
67 + }
68 +
69 + private string[] Query(string path, string[] tables)
70 + {
71 + var results = new List<string>();
72 +
73 + if (tables?.Length > 0)
74 + {
75 + var sb = new StringBuilder();
76 + using (var db = new Database(path))
77 + {
78 + foreach (var table in tables)
79 + {
80 + using (var view = db.OpenView($"SELECT * FROM `{table}`"))
81 + {
82 + view.Execute();
83 +
84 + Record record;
85 + while ((record = view.Fetch()) != null)
86 + {
87 + sb.Clear();
88 + sb.AppendFormat("{0}:", table);
89 +
90 + using (record)
91 + {
92 + for (var i = 0; i < record.FieldCount; ++i)
93 + {
94 + if (i > 0)
95 + {
96 + sb.Append("\t");
97 + }
98 +
99 + sb.Append(record[i + 1]?.ToString());
100 + }
101 + }
102 +
103 + results.Add(sb.ToString());
104 + }
105 + }
106 + }
107 + }
108 + }
109 +
110 + return results.ToArray();
111 + }
112 + }
113 +}
src/WixBuildTools.TestSupport/DisposableFileSystem.cs new
+86
@@ -0,0 +1,86 @@
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 WixBuildTools.TestSupport
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 + protected 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/WixBuildTools.TestSupport/Pushd.cs new
+46
@@ -0,0 +1,46 @@
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 WixBuildTools.TestSupport
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/WixBuildTools.TestSupport/TestData.cs new
+16
@@ -0,0 +1,16 @@
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 WixBuildTools.TestSupport
4 +{
5 + using System;
6 + using System.IO;
7 +
8 + public class TestData
9 + {
10 + public static string Get(params string[] paths)
11 + {
12 + var localPath = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetCallingAssembly().CodeBase).LocalPath);
13 + return Path.Combine(localPath, Path.Combine(paths));
14 + }
15 + }
16 +}
src/WixBuildTools.TestSupport/WixBuildTools.TestSupport.csproj new
+21
@@ -0,0 +1,21 @@
1 +<?xml version="1.0" encoding="utf-8"?>
2 +<!-- 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. -->
3 +
4 +<Project Sdk="Microsoft.NET.Sdk">
5 + <PropertyGroup>
6 + <TargetFramework>netstandard2.0</TargetFramework>
7 + </PropertyGroup>
8 +
9 + <PropertyGroup>
10 + <NoWarn>NU1701</NoWarn>
11 + </PropertyGroup>
12 +
13 + <ItemGroup>
14 + <PackageReference Include="WixToolset.Dtf.Resources" Version="4.0.*" />
15 + <PackageReference Include="WixToolset.Dtf.WindowsInstaller" Version="4.0.*" />
16 + </ItemGroup>
17 +
18 + <ItemGroup>
19 + <PackageReference Include="Nerdbank.GitVersioning" Version="2.0.41" PrivateAssets="all" />
20 + </ItemGroup>
21 +</Project>