Skip to content

Commit 8f67269

Browse files
authored
Fix unlucky xml writer tests (#3533)
1 parent d8360e4 commit 8f67269

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

test/DataCollectors/Microsoft.TestPlatform.Extensions.EventLogCollector.UnitTests/EventLogXmlWriterTests.cs

+18-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.Xml.Linq;
67

78
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
89
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -16,38 +17,35 @@ public class EventLogXmlWriterTests
1617
{
1718
private const string FileName = "Event Log.xml";
1819

19-
private readonly EventLog _eventLog;
20-
private readonly EventLogEntry _eventLogEntry;
21-
private readonly List<EventLogEntry> _eventLogEntries;
22-
private readonly Mock<IFileHelper> _mockFileHelper;
23-
24-
public EventLogXmlWriterTests()
25-
{
26-
_eventLog = new EventLog("Application");
27-
var count = _eventLog.Entries.Count;
28-
_eventLogEntry = _eventLog.Entries[count - 1];
29-
_eventLogEntries = new List<EventLogEntry>();
30-
_mockFileHelper = new Mock<IFileHelper>();
31-
}
32-
3320
[TestMethod]
3421
public void WriteEventLogEntriesToXmlFileShouldWriteToXmlFile()
3522
{
23+
var mockFileHelper = new Mock<IFileHelper>();
24+
var eventLogEntries = new List<EventLogEntry>();
3625
EventLogXmlWriter.WriteEventLogEntriesToXmlFile(
3726
FileName,
38-
_eventLogEntries,
39-
_mockFileHelper.Object);
27+
eventLogEntries,
28+
mockFileHelper.Object);
4029

41-
_mockFileHelper.Verify(x => x.WriteAllTextToFile(FileName, It.IsAny<string>()), Times.Once);
30+
mockFileHelper.Verify(x => x.WriteAllTextToFile(FileName, It.IsAny<string>()), Times.Once);
4231
}
4332

4433
[TestMethod]
4534
public void WriteEventLogEntriesToXmlFileShouldWriteLogEntryIfPresent()
4635
{
47-
_eventLogEntries.Add(_eventLogEntry);
36+
var eventLog = new EventLog("Application");
37+
var eventLogEntry = eventLog.Entries[eventLog.Entries.Count - 1];
38+
var eventLogEntries = new List<EventLogEntry> { eventLogEntry };
39+
40+
var mockFileHelper = new Mock<IFileHelper>();
41+
EventLogXmlWriter.WriteEventLogEntriesToXmlFile(FileName, eventLogEntries, mockFileHelper.Object);
4842

49-
EventLogXmlWriter.WriteEventLogEntriesToXmlFile(FileName, _eventLogEntries, _mockFileHelper.Object);
43+
// Serialize the message in case it contains any special character such as <, >, &, which the XML writer would escape
44+
// because otherwise the raw message and the message used to call WriteAllTextToFile won't match. E.g.
45+
// api-version=2020-07-01&format=json in raw message, becomes
46+
// api-version=2020-07-01&amp;format=json in the xml file.
47+
var serializedMessage = new XElement("t", eventLogEntry.Message).LastNode.ToString();
5048

51-
_mockFileHelper.Verify(x => x.WriteAllTextToFile(FileName, It.Is<string>(str => str.Contains(_eventLogEntry.Message))));
49+
mockFileHelper.Verify(x => x.WriteAllTextToFile(FileName, It.Is<string>(str => str.Contains(serializedMessage))));
5250
}
5351
}

0 commit comments

Comments
 (0)