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
252
253
254
255
256
257
258
259
260
261
|
// 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.IO;
using System.Text;
using System.Text.RegularExpressions;
using Xunit;
/// <summary>
/// The LogVerifier can verify a log file for given regular expressions.
/// </summary>
public class LogVerifier
{
// Member Variables
private readonly string logFile;
/// <summary>
/// Prevent creation of LogVerifier without log file
/// </summary>
private LogVerifier()
{ }
/// <summary>
/// Constructor for log files where the exact file name is known.
/// </summary>
/// <param name="fileName">The full path to the log file</param>
public LogVerifier(string fileName)
{
if (null == fileName)
{
throw new ArgumentNullException("fileName");
}
if (!File.Exists(fileName))
{
throw new ArgumentException(String.Format(@"File doesn't exist:{0}", fileName), "fileName");
}
this.logFile = fileName;
}
/// <summary>
/// Constructor for log files where the exact file name is known.
/// </summary>
/// <param name="directory">The directory in which the log file is located.</param>
/// <param name="fileName">The name of the log file.</param>
public LogVerifier(string directory, string fileName)
: this(Path.Combine(directory, fileName))
{ }
/// <summary>
/// Scans a log file line by line until the regex pattern is matched or eof is reached.
/// This method would be used in the case where the log file is very large, the regex doesn't
/// span multiple lines, and only one match is required.
/// </summary>
/// <param name="regex">A regular expression</param>
/// <returns>True if a match is found, False otherwise.</returns>
public bool LineByLine(Regex regex)
{
string line;
StreamReader sr = new StreamReader(this.logFile);
// Read from a file stream line by line.
while ((line = sr.ReadLine()) != null)
{
if (regex.Match(line).Success)
{
sr.Close();
sr.Dispose();
return true;
}
}
return false;
}
/// <summary>
/// Scans a log file line by line until the regex pattern is matched or eof is reached.
/// This method would be used in the case where the log file is very large, the regex doesn't
/// span multiple lines, and only one match is required.
/// No RegexOptions are used and matches are case sensitive.
/// </summary>
/// <param name="regex">A regular expression string.</param>
/// <returns>True if a match is found, False otherwise.</returns>
public bool LineByLine(string regex)
{
return this.LineByLine(new Regex(regex));
}
/// <summary>
/// Scans a log file for matches to the regex.
/// </summary>
/// <param name="regex">A regular expression</param>
/// <returns>The number of matches</returns>
public int EntireFileAtOnce(Regex regex)
{
string logFileText = this.ReadLogFile();
return regex.Matches(logFileText).Count;
}
/// <summary>
/// Scans a log file for matches to the regex.
/// </summary>
/// <param name="match">A string to find.</param>
/// <returns>If the string is found.</returns>
public bool EntireFileAtOncestr(string match)
{
string logFileText = this.ReadLogFile();
return logFileText.Contains(match);
}
/// <summary>
/// Scans a log file for matches to the regex string.
/// Only the Multiline RegexOption is used and matches are case sensitive.
/// </summary>
/// <param name="regex">A regular expression</param>
/// <returns>The number of matches</returns>
public int EntireFileAtOnce(string regex)
{
return this.EntireFileAtOnce(new Regex(regex, RegexOptions.Multiline));
}
/// <summary>
/// Scans a log file for matches to the regex string.
/// </summary>
/// <param name="regex">A regular expression</param>
/// <param name="ignoreCase">Specify whether to perform case sensitive matches</param>
/// <returns>The number of matches</returns>
public int EntireFileAtOnce(string regex, bool ignoreCase)
{
if (!ignoreCase)
{
return this.EntireFileAtOnce(new Regex(regex, RegexOptions.Multiline));
}
else
{
return this.EntireFileAtOnce(new Regex(regex, RegexOptions.Multiline | RegexOptions.IgnoreCase));
}
}
/// <summary>
/// Search through the log and Assert.Fail() if a specified string is not found.
/// </summary>
/// <param name="match">Search expression</param>
/// <param name="ignoreCase">Perform case insensitive match</param>
public void AssertTextInLog(string match, bool ignoreCase)
{
Assert.True(this.EntireFileAtOncestr(match),
String.Format("The log does not contain a match for the {1}string \"{0}\" ", match, ignoreCase ? "case insensitive " : ""));
}
/// <summary>
/// Search through the log and Assert.Fail() if a specified string is not found.
/// </summary>
/// <param name="regex">Search expression</param>
/// <param name="ignoreCase">Perform case insensitive match</param>
public void AssertTextInLog(Regex regex, bool ignoreCase)
{
Assert.True(this.EntireFileAtOnce(regex) >= 1,
String.Format("The log does not contain a match to the regular expression \"{0}\" ", regex.ToString()));
}
/// <summary>
/// Search through the log and Assert.Fail() if a specified string is not found.
/// </summary>
/// <param name="regex">Search expression</param>
/// <param name="ignoreCase">Perform case insensitive match</param>
public void AssertTextInLog(string regex)
{
this.AssertTextInLog(regex, true);
}
/// <summary>
/// Search through the log and Assert.Fail() if a specified string is not found.
/// </summary>
/// <param name="regex">Search expression</param>
/// <param name="ignoreCase">Perform case insensitive match</param>
public void AssertTextInLog(Regex regex)
{
this.AssertTextInLog(regex, true);
}
/// <summary>
/// Search through the log and Assert.Fail() if a specified string is found.
/// </summary>
/// <param name="regex">Search expression</param>
/// <param name="ignoreCase">Perform case insensitive match</param>
public void AssertTextNotInLog(Regex regex, bool ignoreCase)
{
Assert.True(this.EntireFileAtOnce(regex) < 1,
String.Format("The log contain a match to the regular expression \"{0}\" ", regex.ToString()));
}
/// <summary>
/// Search through the log and Assert.Fail() if a specified string is not found.
/// </summary>
/// <param name="regex">Search expression</param>
/// <param name="ignoreCase">Perform case insensitive match</param>
public void AssertTextNotInLog(string regex, bool ignoreCase)
{
Assert.False(this.EntireFileAtOncestr(regex),
String.Format("The log does not contain a match to the regular expression \"{0}\" ", regex));
}
/// <summary>
/// Checks if a meesage is in a file
/// </summary>
/// <param name="logFileName">The full path to the log file</param>
/// <param name="message">Search expression</param>
/// <returns>True if the message was found, false otherwise</returns>
public static bool MessageInLogFile(string logFileName, string message)
{
LogVerifier logVerifier = new LogVerifier(logFileName);
return logVerifier.EntireFileAtOncestr(message);
}
/// <summary>
/// Checks if a meesage is in a file
/// </summary>
/// <param name="logFileName">The full path to the log file</param>
/// <param name="message">Search expression (regex)</param>
/// <returns>True if the message was found, false otherwise</returns>
public static bool MessageInLogFileRegex(string logFileName, string regexMessage)
{
LogVerifier logVerifier = new LogVerifier(logFileName);
return logVerifier.EntireFileAtOnce(regexMessage) > 0;
}
/// <summary>
/// Read in the entire log file at once.
/// </summary>
/// <returns>Contents of log file.</returns>
private string ReadLogFile()
{
// Retry a few times.
for (int retry = 0; ; ++retry)
{
try
{
using (StreamReader sr = new StreamReader(this.logFile))
{
return sr.ReadToEnd();
}
}
catch // we'll catch everything a few times until we give up.
{
if (retry > 4)
{
throw;
}
System.Threading.Thread.Sleep(1000);
}
}
}
}
}
|