diff options
Diffstat (limited to 'src/test/wixcop/WixCopFixture.cs')
-rw-r--r-- | src/test/wixcop/WixCopFixture.cs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/test/wixcop/WixCopFixture.cs b/src/test/wixcop/WixCopFixture.cs new file mode 100644 index 00000000..12863959 --- /dev/null +++ b/src/test/wixcop/WixCopFixture.cs | |||
@@ -0,0 +1,107 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.IO; | ||
4 | using System.Linq; | ||
5 | using WixBuildTools.TestSupport; | ||
6 | using WixCop.CommandLine; | ||
7 | using WixCop.Interfaces; | ||
8 | using WixToolset.Core; | ||
9 | using WixToolset.Core.TestPackage; | ||
10 | using WixToolset.Extensibility; | ||
11 | using WixToolset.Extensibility.Services; | ||
12 | using Xunit; | ||
13 | |||
14 | namespace WixCopTests | ||
15 | { | ||
16 | public class WixCopFixture | ||
17 | { | ||
18 | [Fact] | ||
19 | public void CanConvertSingleFile() | ||
20 | { | ||
21 | const string beforeFileName = "SingleFile.wxs"; | ||
22 | const string afterFileName = "ConvertedSingleFile.wxs"; | ||
23 | var folder = TestData.Get(@"TestData\SingleFile"); | ||
24 | |||
25 | using (var fs = new DisposableFileSystem()) | ||
26 | { | ||
27 | var baseFolder = fs.GetFolder(true); | ||
28 | var targetFile = Path.Combine(baseFolder, beforeFileName); | ||
29 | File.Copy(Path.Combine(folder, beforeFileName), Path.Combine(baseFolder, beforeFileName)); | ||
30 | |||
31 | var runner = new WixCopRunner | ||
32 | { | ||
33 | FixErrors = true, | ||
34 | SearchPatterns = | ||
35 | { | ||
36 | targetFile, | ||
37 | }, | ||
38 | }; | ||
39 | |||
40 | var result = runner.Execute(out var messages); | ||
41 | |||
42 | Assert.Equal(2, result); | ||
43 | |||
44 | var actualLines = File.ReadAllLines(targetFile); | ||
45 | var expectedLines = File.ReadAllLines(Path.Combine(folder, afterFileName)); | ||
46 | Assert.Equal(expectedLines, actualLines); | ||
47 | |||
48 | var runner2 = new WixCopRunner | ||
49 | { | ||
50 | FixErrors = true, | ||
51 | SearchPatterns = | ||
52 | { | ||
53 | targetFile, | ||
54 | }, | ||
55 | }; | ||
56 | |||
57 | var result2 = runner2.Execute(out var messages2); | ||
58 | |||
59 | Assert.Equal(0, result2); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | private class WixCopRunner | ||
64 | { | ||
65 | public bool FixErrors { get; set; } | ||
66 | |||
67 | public List<string> SearchPatterns { get; } = new List<string>(); | ||
68 | |||
69 | public int Execute(out List<string> messages) | ||
70 | { | ||
71 | var argList = new List<string>(); | ||
72 | if (this.FixErrors) | ||
73 | { | ||
74 | argList.Add("-f"); | ||
75 | } | ||
76 | |||
77 | foreach (string searchPattern in this.SearchPatterns) | ||
78 | { | ||
79 | argList.Add(searchPattern); | ||
80 | } | ||
81 | |||
82 | return WixCopRunner.Execute(argList.ToArray(), out messages); | ||
83 | } | ||
84 | |||
85 | public static int Execute(string[] args, out List<string> messages) | ||
86 | { | ||
87 | var listener = new TestMessageListener(); | ||
88 | |||
89 | var serviceProvider = new WixToolsetServiceProvider(); | ||
90 | serviceProvider.AddService<IMessageListener>((x, y) => listener); | ||
91 | serviceProvider.AddService<IWixCopCommandLineParser>((x, y) => new WixCopCommandLineParser(x)); | ||
92 | |||
93 | var result = Execute(serviceProvider, args); | ||
94 | |||
95 | var messaging = serviceProvider.GetService<IMessaging>(); | ||
96 | messages = listener.Messages.Select(x => messaging.FormatMessage(x)).ToList(); | ||
97 | return result; | ||
98 | } | ||
99 | |||
100 | public static int Execute(IServiceProvider serviceProvider, string[] args) | ||
101 | { | ||
102 | var wixcop = new WixCop.Program(); | ||
103 | return wixcop.Run(serviceProvider, args); | ||
104 | } | ||
105 | } | ||
106 | } | ||
107 | } | ||