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