forked from microsoft/vstest
/
EventLogXmlWriterTests.cs
51 lines (40 loc) · 2.03 KB
/
EventLogXmlWriterTests.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace Microsoft.TestPlatform.Extensions.EventLogCollector.UnitTests;
[TestClass]
public class EventLogXmlWriterTests
{
private const string FileName = "Event Log.xml";
[TestMethod]
public void WriteEventLogEntriesToXmlFileShouldWriteToXmlFile()
{
var mockFileHelper = new Mock<IFileHelper>();
var eventLogEntries = new List<EventLogEntry>();
EventLogXmlWriter.WriteEventLogEntriesToXmlFile(
FileName,
eventLogEntries,
mockFileHelper.Object);
mockFileHelper.Verify(x => x.WriteAllTextToFile(FileName, It.IsAny<string>()), Times.Once);
}
[TestMethod]
public void WriteEventLogEntriesToXmlFileShouldWriteLogEntryIfPresent()
{
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);
// 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&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(serializedMessage))));
}
}