aboutsummaryrefslogtreecommitdiff
path: root/src/test/burn/WixTestTools/TestTool.cs
blob: b6d18ac93ae05e38d1960507975d00148ae8a78d (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// 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.

namespace WixTestTools
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using WixInternal.TestSupport;
    using Xunit;

    public class TestTool : ExternalExecutable
    {
        /// <summary>
        /// Constructor for a TestTool
        /// </summary>
        public TestTool()
            : this(null)
        {
        }

        /// <summary>
        /// Constructor for a TestTool
        /// </summary>
        /// <param name="toolFile">The full path to the tool. Eg. c:\bin\candle.exe</param>
        public TestTool(string toolFile)
            : base(toolFile)
        {
            this.PrintOutputToConsole = true;
        }

        /// <summary>
        /// The alternate expected exit code of the tool
        /// </summary>
        public int? AlternateExitCode { get; set; }

        /// <summary>
        /// The arguments to pass to the tool
        /// </summary>
        public virtual string Arguments { get; set; }

        /// <summary>
        /// Stores the errors that occurred when a run was checked against its expected results
        /// </summary>
        public List<string> Errors { get; set; }

        /// <summary>
        /// A list of Regex's that are expected to match stderr
        /// </summary>
        public List<Regex> ExpectedErrorRegexs { get; set; } = new List<Regex>();

        /// <summary>
        /// The expected error strings to stderr
        /// </summary>
        public List<string> ExpectedErrorStrings { get; set; } = new List<string>();

        /// <summary>
        /// The expected exit code of the tool
        /// </summary>
        public int? ExpectedExitCode { get; set; }

        /// <summary>
        /// A list of Regex's that are expected to match stdout
        /// </summary>
        public List<Regex> ExpectedOutputRegexs { get; set; } = new List<Regex>();

        /// <summary>
        /// The expected output strings to stdout
        /// </summary>
        public List<string> ExpectedOutputStrings { get; set; } = new List<string>();

        /// <summary>
        /// Print output from the tool execution to the console
        /// </summary>
        public bool PrintOutputToConsole { get; set; }

        /// <summary>
        /// The working directory of the tool
        /// </summary>
        public string WorkingDirectory { get; set; }

        /// <summary>
        /// Print the errors from the last run
        /// </summary>
        public void PrintErrors()
        {
            if (null != this.Errors)
            {
                Console.WriteLine("Errors:");

                foreach (string error in this.Errors)
                {
                    Console.WriteLine(error);
                }
            }
        }

        /// <summary>
        /// Run the tool
        /// </summary>
        /// <returns>The results of the run</returns>
        public ExternalExecutableResult Run()
        {
            return this.Run(true);
        }

        /// <summary>
        /// Run the tool
        /// </summary>
        /// <param name="exceptionOnError">Throw an exception if the expected results don't match the actual results</param>
        /// <exception cref="System.Exception">Thrown when the expected results don't match the actual results</exception>
        /// <returns>The results of the run</returns>
        public virtual ExternalExecutableResult Run(bool assertOnError)
        {
            var result = this.Run(this.Arguments, workingDirectory: this.WorkingDirectory ?? String.Empty);

            if (this.PrintOutputToConsole)
            {
                Console.WriteLine(FormatResult(result));
            }

            this.Errors = this.CheckResult(result);

            if (assertOnError && 0 < this.Errors.Count)
            {
                if (this.PrintOutputToConsole)
                {
                    this.PrintErrors();
                }

                WixAssert.StringCollectionEmpty(this.Errors);
            }

            return result;
        }

        /// <summary>
        /// Checks that the result from a run matches the expected results
        /// </summary>
        /// <param name="result">A result from a run</param>
        /// <returns>A list of errors</returns>
        public virtual List<string> CheckResult(ExternalExecutableResult result)
        {
            List<string> errors = new List<string>();

            // Verify that the expected return code matched the actual return code
            if (null != this.ExpectedExitCode && this.ExpectedExitCode != result.ExitCode &&
                (null == this.AlternateExitCode || this.AlternateExitCode != result.ExitCode))
            {
                errors.Add(String.Format("Expected exit code {0} did not match actual exit code {1}", this.ExpectedExitCode, result.ExitCode));
            }

            var standardErrorString = String.Join(Environment.NewLine, result.StandardError);

            // Verify that the expected error string are in stderr
            if (null != this.ExpectedErrorStrings)
            {
                foreach (string expectedString in this.ExpectedErrorStrings)
                {
                    if (!standardErrorString.Contains(expectedString))
                    {
                        errors.Add(String.Format("The text '{0}' was not found in stderr", expectedString));
                    }
                }
            }

            var standardOutputString = String.Join(Environment.NewLine, result.StandardOutput);

            // Verify that the expected output string are in stdout
            if (null != this.ExpectedOutputStrings)
            {
                foreach (string expectedString in this.ExpectedOutputStrings)
                {
                    if (!standardOutputString.Contains(expectedString))
                    {
                        errors.Add(String.Format("The text '{0}' was not found in stdout", expectedString));
                    }
                }
            }

            // Verify that the expected regular expressions match stderr
            if (null != this.ExpectedOutputRegexs)
            {
                foreach (Regex expectedRegex in this.ExpectedOutputRegexs)
                {
                    if (!expectedRegex.IsMatch(standardOutputString))
                    {
                        errors.Add(String.Format("Regex {0} did not match stdout", expectedRegex.ToString()));
                    }
                }
            }

            // Verify that the expected regular expressions match stdout
            if (null != this.ExpectedErrorRegexs)
            {
                foreach (Regex expectedRegex in this.ExpectedErrorRegexs)
                {
                    if (!expectedRegex.IsMatch(standardErrorString))
                    {
                        errors.Add(String.Format("Regex {0} did not match stderr", expectedRegex.ToString()));
                    }
                }
            }

            return errors;
        }

        /// <summary>
        /// Clears all of the expected results and resets them to the default values
        /// </summary>
        public virtual void SetDefaultExpectedResults()
        {
            this.ExpectedErrorRegexs = new List<Regex>();
            this.ExpectedErrorStrings = new List<string>();
            this.ExpectedExitCode = null;
            this.ExpectedOutputRegexs = new List<Regex>();
            this.ExpectedOutputStrings = new List<string>();
        }

        /// <summary>
        /// Returns a string with data contained in the result.
        /// </summary>
        /// <returns>A string</returns>
        private static string FormatResult(ExternalExecutableResult result)
        {
            var returnValue = new StringBuilder();
            returnValue.AppendLine();
            returnValue.AppendLine("----------------");
            returnValue.AppendLine("Tool run result:");
            returnValue.AppendLine("----------------");
            returnValue.AppendLine("Command:");
            returnValue.AppendLine($"\"{result.FileName}\" {result.Arguments}");
            returnValue.AppendLine();
            returnValue.AppendLine("Standard Output:");
            foreach (var line in result.StandardOutput ?? new string[0])
            {
                returnValue.AppendLine(line);
            }
            returnValue.AppendLine("Standard Error:");
            foreach (var line in result.StandardError ?? new string[0])
            {
                returnValue.AppendLine(line);
            }
            returnValue.AppendLine("Exit Code:");
            returnValue.AppendLine(Convert.ToString(result.ExitCode));
            returnValue.AppendLine("----------------");

            return returnValue.ToString();
        }
    }
}