main
cs 263 lines 8.67 KB
Raw
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.CoreIntegration
4 {
5 using System.Collections.Generic;
6 using System.Data;
7 using System.IO;
8 using System.Linq;
9 using WixInternal.Core.TestPackage;
10 using WixInternal.TestSupport;
11 using WixToolset.Data.WindowsInstaller;
12 using Xunit;
13
14 public class NakedFileFixture
15 {
16 [Fact]
17 public void CanBuildNakedFilesInComponentGroup()
18 {
19 var rows = BuildAndQueryComponentAndFileTables("ComponentGroup.wxs");
20
21 AssertFileComponentIds(2, rows);
22 }
23
24 [Fact]
25 public void CanBuildNakedFilesInFeature()
26 {
27 var rows = BuildAndQueryComponentAndFileTables("Feature.wxs");
28
29 AssertFileComponentIds(2, rows);
30 }
31
32 [Fact]
33 public void CanBuildNakedFilesInDirectory()
34 {
35 var rows = BuildAndQueryComponentAndFileTables("Directory.wxs");
36
37 AssertFileComponentIds(2, rows);
38 }
39
40 [Fact]
41 public void CanBuildNakedFilesInDirectoryRef()
42 {
43 var rows = BuildAndQueryComponentAndFileTables("DirectoryRef.wxs");
44
45 AssertFileComponentIds(2, rows);
46 }
47
48 [Fact]
49 public void CanBuildNakedFilesInFeatureRef()
50 {
51 var rows = BuildAndQueryComponentAndFileTables("FeatureRef.wxs");
52
53 AssertFileComponentIds(2, rows);
54 }
55
56 [Fact]
57 public void CanBuildNakedFilesInFeatureGroup()
58 {
59 var rows = BuildAndQueryComponentAndFileTables("FeatureGroup.wxs");
60
61 AssertFileComponentIds(2, rows);
62 }
63
64 [Fact]
65 public void CanBuildNakedFilesInFragments()
66 {
67 var rows = BuildAndQueryComponentAndFileTables("Fragment.wxs");
68
69 AssertFileComponentIds(2, rows);
70 }
71
72 [Fact]
73 public void CanBuildNakedFilesInStandardDirectory()
74 {
75 var rows = BuildAndQueryComponentAndFileTables("StandardDirectory.wxs");
76
77 AssertFileComponentIds(2, rows);
78 }
79
80 [Fact]
81 public void CanBuildNakedFilesInModule()
82 {
83 var rows = BuildAndQueryComponentAndFileTables("Module.wxs", isPackage: false);
84
85 AssertFileComponentIds(2, rows);
86
87 var expectedModuleComponents = new[]
88 {
89 "ModuleComponents:FILE1.E535B765_1019_4A4F_B3EA_AE28870E6D73\tMergeModule.E535B765_1019_4A4F_B3EA_AE28870E6D73\t1033",
90 "ModuleComponents:FILE2.E535B765_1019_4A4F_B3EA_AE28870E6D73\tMergeModule.E535B765_1019_4A4F_B3EA_AE28870E6D73\t1033",
91 };
92
93 Assert.Equal(expectedModuleComponents, rows.Where(row => row.StartsWith("ModuleComponents:")));
94 }
95
96 [Fact]
97 public void CanBuildNakedFilesWithConditions()
98 {
99 var rows = BuildAndQueryComponentAndFileTables("Condition.wxs");
100 var componentRows = rows.Where(row => row.StartsWith("Component:")).ToArray();
101
102 // Coincidentally, the files' ids are the same as the component conditions.
103 foreach (var componentRow in componentRows)
104 {
105 var columns = componentRow.Split(':', '\t');
106 Assert.Equal(columns[1], columns[5]);
107 }
108 }
109
110 [Fact]
111 public void CanBuildNakedFilesUnderPackage()
112 {
113 var rows = BuildAndQueryComponentAndFileTables("Package.wxs");
114 AssertFileComponentIds(4, rows);
115 }
116
117 [Fact]
118 public void CanBuildNakedFilesUnderPackageWithDefaultInstallFolder()
119 {
120 var rows = BuildAndQueryComponentAndFileTables("PackageWithDefaultInstallFolder.wxs");
121 AssertFileComponentIds(4, rows);
122 }
123
124 [Fact]
125 public void NakedFilesUnderPackageWithAuthoredFeatureAreOrphaned()
126 {
127 var messages = BuildAndQueryComponentAndFileTables("PackageWithoutDefaultFeature.wxs", isPackage: true, 267);
128 Assert.Equal(new[]
129 {
130 "267",
131 "267",
132 }, messages);
133 }
134
135 [Fact]
136 public void IllegalAttributesWhenNonNakedFailTheBuild()
137 {
138 var messages = BuildAndQueryComponentAndFileTables("BadAttributes.wxs", isPackage: true, 62);
139 Assert.Equal(new[]
140 {
141 "62",
142 "62",
143 "62",
144 "62",
145 }, messages);
146 }
147
148 [Fact]
149 public void CanBuildNakedFileFromWixlibComponentGroup()
150 {
151 var rows = BuildPackageWithWixlib("WixlibComponentGroup.wxs", "WixlibComponentGroupPackage.wxs");
152
153 AssertFileComponentIds(2, rows);
154 }
155
156 private static string[] BuildPackageWithWixlib(string wixlibSourcePath, string msiSourcePath)
157 {
158 var folder = TestData.Get("TestData", "NakedFile");
159
160 using (var fs = new DisposableFileSystem())
161 {
162 var baseFolder = fs.GetFolder();
163 var intermediateFolder = Path.Combine(baseFolder, "obj");
164 var binFolder = Path.Combine(baseFolder, "bin");
165 var wixlibPath = Path.Combine(binFolder, Path.ChangeExtension(wixlibSourcePath, ".wixlib"));
166
167 var result = WixRunner.Execute(new[]
168 {
169 "build",
170 Path.Combine(folder, wixlibSourcePath),
171 "-intermediateFolder", intermediateFolder,
172 "-bindpath", folder,
173 "-o", wixlibPath,
174 });
175
176 result.AssertSuccess();
177
178 var msiPath = Path.Combine(binFolder, "test.msi");
179
180 result = WixRunner.Execute(new[]
181 {
182 "build",
183 Path.Combine(folder, msiSourcePath),
184 wixlibPath,
185 "-intermediateFolder", intermediateFolder,
186 "-bindpath", folder,
187 "-o", msiPath,
188 });
189 result.AssertSuccess();
190
191 return Query.QueryDatabase(msiPath, new[] { "Component", "File" })
192 .OrderBy(s => s)
193 .ToArray();
194 }
195 }
196
197 private static string[] BuildAndQueryComponentAndFileTables(string file, bool isPackage = true, int? exitCode = null)
198 {
199 var folder = TestData.Get("TestData", "NakedFile");
200
201 using (var fs = new DisposableFileSystem())
202 {
203 var baseFolder = fs.GetFolder();
204 var intermediateFolder = Path.Combine(baseFolder, "obj");
205 var binFolder = Path.Combine(baseFolder, "bin");
206 var msiPath = Path.Combine(binFolder, isPackage ? "test.msi" : "test.msm");
207
208 var result = WixRunner.Execute(new[]
209 {
210 "build",
211 Path.Combine(folder, file),
212 "-intermediateFolder", intermediateFolder,
213 "-bindpath", folder,
214 "-o", msiPath,
215 });
216
217 if (exitCode.HasValue)
218 {
219 Assert.Equal(exitCode.Value, result.ExitCode);
220
221 return result.Messages.Select(m => m.Id.ToString()).ToArray();
222 }
223 else
224 {
225 result.AssertSuccess();
226
227 var tables = new List<string> { "Component", "File" };
228
229 if (!isPackage)
230 {
231 tables.Add("ModuleComponents");
232 }
233
234 return Query.QueryDatabase(msiPath, tables.ToArray())
235 .OrderBy(s => s)
236 .ToArray();
237 }
238 }
239 }
240
241 private static void AssertFileComponentIds(int fileCount, string[] rows)
242 {
243 var componentRows = rows.Where(row => row.StartsWith("Component:")).ToArray();
244 var fileRows = rows.Where(row => row.StartsWith("File:")).ToArray();
245
246 Assert.Equal(fileCount, componentRows.Length);
247 Assert.Equal(componentRows.Length, fileRows.Length);
248
249 // Component id == Component keypath == File id
250 foreach (var componentRow in componentRows)
251 {
252 var columns = componentRow.Split(':', '\t');
253 Assert.Equal(columns[1], columns[6]);
254 }
255
256 foreach (var fileRow in fileRows)
257 {
258 var columns = fileRow.Split(':', '\t');
259 Assert.Equal(columns[1], columns[2]);
260 }
261 }
262 }
263 }