diff options
author | Rob Mensching <rob@firegiant.com> | 2017-12-28 09:20:11 -0800 |
---|---|---|
committer | Rob Mensching <rob@firegiant.com> | 2017-12-28 09:20:11 -0800 |
commit | 2c711ea621c03e415dc9ad9e9f103e09372b8c50 (patch) | |
tree | 415b00719d8f016e6edbd867947510d753224040 /src | |
parent | 9a829ccb7b8cbc3432356f5b8deba081d64e3cb5 (diff) | |
download | wix-2c711ea621c03e415dc9ad9e9f103e09372b8c50.tar.gz wix-2c711ea621c03e415dc9ad9e9f103e09372b8c50.tar.bz2 wix-2c711ea621c03e415dc9ad9e9f103e09372b8c50.zip |
Add WixBuildTools.TestSupport with common testing functionality
Diffstat (limited to 'src')
-rw-r--r-- | src/WixBuildTools.TestSupport/Builder.cs | 113 | ||||
-rw-r--r-- | src/WixBuildTools.TestSupport/DisposableFileSystem.cs | 86 | ||||
-rw-r--r-- | src/WixBuildTools.TestSupport/Pushd.cs | 46 | ||||
-rw-r--r-- | src/WixBuildTools.TestSupport/TestData.cs | 16 | ||||
-rw-r--r-- | src/WixBuildTools.TestSupport/WixBuildTools.TestSupport.csproj | 21 |
5 files changed, 282 insertions, 0 deletions
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 @@ | |||
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 | } | ||
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 @@ | |||
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 | } | ||
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 @@ | |||
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 | } | ||
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 @@ | |||
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 | } | ||
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 @@ | |||
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> | ||