Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unlucky xml writer tests #3533

Merged
merged 1 commit into from Mar 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;

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

private readonly EventLog _eventLog;
private readonly EventLogEntry _eventLogEntry;
private readonly List<EventLogEntry> _eventLogEntries;
private readonly Mock<IFileHelper> _mockFileHelper;

public EventLogXmlWriterTests()
{
_eventLog = new EventLog("Application");
var count = _eventLog.Entries.Count;
_eventLogEntry = _eventLog.Entries[count - 1];
_eventLogEntries = new List<EventLogEntry>();
_mockFileHelper = new Mock<IFileHelper>();
}

[TestMethod]
public void WriteEventLogEntriesToXmlFileShouldWriteToXmlFile()
{
var mockFileHelper = new Mock<IFileHelper>();
var eventLogEntries = new List<EventLogEntry>();
EventLogXmlWriter.WriteEventLogEntriesToXmlFile(
FileName,
_eventLogEntries,
_mockFileHelper.Object);
eventLogEntries,
mockFileHelper.Object);

_mockFileHelper.Verify(x => x.WriteAllTextToFile(FileName, It.IsAny<string>()), Times.Once);
mockFileHelper.Verify(x => x.WriteAllTextToFile(FileName, It.IsAny<string>()), Times.Once);
}

[TestMethod]
public void WriteEventLogEntriesToXmlFileShouldWriteLogEntryIfPresent()
{
_eventLogEntries.Add(_eventLogEntry);
var eventLog = new EventLog("Application");
var eventLogEntry = eventLog.Entries[eventLog.Entries.Count - 1];
var eventLogEntries = new List<EventLogEntry> { eventLogEntry };

var mockFileHelper = new Mock<IFileHelper>();
EventLogXmlWriter.WriteEventLogEntriesToXmlFile(FileName, eventLogEntries, mockFileHelper.Object);

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

_mockFileHelper.Verify(x => x.WriteAllTextToFile(FileName, It.Is<string>(str => str.Contains(_eventLogEntry.Message))));
mockFileHelper.Verify(x => x.WriteAllTextToFile(FileName, It.Is<string>(str => str.Contains(serializedMessage))));
}
}