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

Adding log prefixkey to html logger #2204

Merged
merged 6 commits into from Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -51,7 +51,12 @@ public static class Constants
/// <summary>
/// Log file parameter key
/// </summary>
public const string LogFileNameKey = "LogFileName";
public const string LogFileNameKey = "LogFileName";

/// <summary>
/// Log file prefix parameter key
/// </summary>
public const string LogFilePrefixKey = "LogFilePrefix";

public static readonly TestProperty ExecutionIdProperty = TestProperty.Register("ExecutionId", ExecutionIdPropertyIdentifier, typeof(Guid), TestPropertyAttributes.Hidden, typeof(TestResult));

Expand Down
23 changes: 17 additions & 6 deletions src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs
Expand Up @@ -22,6 +22,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Extensions.HtmlLogger

using HtmlResource = Resources.Resources;
using HtmlLoggerConstants = Constants;
using NuGet.Frameworks;

/// <summary>
/// Logger for generating Html.
Expand Down Expand Up @@ -153,7 +154,7 @@ public void TestMessageHandler(object sender, TestRunMessageEventArgs e)
switch (e.Level)
{
case TestMessageLevel.Informational:
if(TestRunDetails.RunLevelMessageInformational == null)
if (TestRunDetails.RunLevelMessageInformational == null)
{
TestRunDetails.RunLevelMessageInformational = new List<string>();
}
Expand Down Expand Up @@ -273,11 +274,21 @@ public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
PassPercentage = (PassedTests * 100) / TotalTests,
TotalRunTime = GetFormattedDurationString(e.ElapsedTimeInRunningTests),
};
var isLogFileNameParameterExists = parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFileNameKey,
out string logFileNameValue);
if (isLogFileNameParameterExists && !string.IsNullOrWhiteSpace(logFileNameValue))

if (this.parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && !string.IsNullOrWhiteSpace(logFilePrefixValue))
{
vagisha-nidhi marked this conversation as resolved.
Show resolved Hide resolved

var framework = this.parametersDictionary[DefaultLoggerParameterNames.TargetFramework] ?? string.Empty;
framework = NuGetFramework.Parse(framework).GetShortFolderName();
hvinett marked this conversation as resolved.
Show resolved Hide resolved
logFilePrefixValue = logFilePrefixValue.Replace(".html", string.Empty) + "_" + framework + DateTime.Now.ToString("_yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + $".{HtmlLoggerConstants.HtmlFileExtension}";
hvinett marked this conversation as resolved.
Show resolved Hide resolved
this.HtmlFilePath = Path.Combine(TestResultsDirPath, logFilePrefixValue);
}
else
{
HtmlFilePath = Path.Combine(TestResultsDirPath, logFileNameValue);
if (parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFileNameKey, out string logFileNameValue) && !string.IsNullOrWhiteSpace(logFileNameValue))
{
HtmlFilePath = Path.Combine(TestResultsDirPath, logFileNameValue);
}
}

PopulateHtmlFile();
Expand Down Expand Up @@ -410,7 +421,7 @@ internal string GetFormattedDurationString(TimeSpan duration)
time.Add(duration.Milliseconds + "ms");
}
}
}
}

return time.Count == 0 ? "< 1ms" : string.Join(" ", time);
}
Expand Down
Expand Up @@ -401,7 +401,7 @@ public void TestCompleteHandlerShouldKeepTackOfSummary()
}

[TestMethod]
public void TestCompleteHandlerShouldCreateCustumHtmlFileNameIfParameterDirectoryIsNull()
public void TestCompleteHandlerShouldCreateCustumHtmlFileNamewithLogFileNameKey()
{
var parameters = new Dictionary<string, string>();
parameters[HtmlLoggerConstants.LogFileNameKey] = null;
Expand All @@ -417,6 +417,62 @@ public void TestCompleteHandlerShouldCreateCustumHtmlFileNameIfParameterDirector
Assert.IsTrue(this.htmlLogger.HtmlFilePath.Contains("TestResult"));
}

[TestMethod]
public void TestCompleteHandlerShouldCreateCustumHtmlFileNameWithLogPrefix()
{
var parameters = new Dictionary<string, string>();
parameters[HtmlLoggerConstants.LogFilePrefixKey] = "sample.html";
parameters[DefaultLoggerParameterNames.TestRunDirectory] = "dsa";
parameters[DefaultLoggerParameterNames.TargetFramework] = "net451";

var testCase1 = CreateTestCase("TestCase1");
var result1 = new ObjectModel.TestResult(testCase1) { Outcome = TestOutcome.Failed };
var resultEventArg1 = new Mock<TestResultEventArgs>(result1);
this.htmlLogger.TestResultHandler(new object(), resultEventArg1.Object);

this.htmlLogger.Initialize(new Mock<TestLoggerEvents>().Object, parameters);
this.htmlLogger.TestRunCompleteHandler(new object(), new TestRunCompleteEventArgs(null, false, true, null, null, TimeSpan.Zero));
Assert.IsTrue(this.htmlLogger.HtmlFilePath.Contains("sample_net451"));
}

[TestMethod]
public void TestCompleteHandlerShouldCreateCustumHtmlFileNameWithLogPrefixNull()
{
var parameters = new Dictionary<string, string>();
parameters[HtmlLoggerConstants.LogFilePrefixKey] = null;
parameters[DefaultLoggerParameterNames.TestRunDirectory] = "dsa";
parameters[DefaultLoggerParameterNames.TargetFramework] = "net451";

var testCase1 = CreateTestCase("TestCase1");
var result1 = new ObjectModel.TestResult(testCase1) { Outcome = TestOutcome.Failed };
var resultEventArg1 = new Mock<TestResultEventArgs>(result1);

this.mockFileHelper.Setup(x => x.GetStream(It.IsAny<string>(), FileMode.Create, FileAccess.ReadWrite)).Callback<string, FileMode, FileAccess>((x, y, z) =>
{
}).Returns(new Mock<Stream>().Object);

this.htmlLogger.TestResultHandler(new object(), resultEventArg1.Object);
this.htmlLogger.TestRunCompleteHandler(new object(), new TestRunCompleteEventArgs(null, false, true, null, null, TimeSpan.Zero));

this.mockFileHelper.Verify(x => x.GetStream(It.IsAny<string>(), FileMode.Create, FileAccess.ReadWrite), Times.Once);
}

[TestMethod]
public void TestCompleteHandlerShouldThrowExceptionWithLogPrefixIfTargetFrameworkKeyIsNotPresent()
{
var parameters = new Dictionary<string, string>();
parameters[HtmlLoggerConstants.LogFilePrefixKey] = "sample.html";
parameters[DefaultLoggerParameterNames.TestRunDirectory] = "dsa";
var testCase1 = CreateTestCase("TestCase1");
var result1 = new ObjectModel.TestResult(testCase1) { Outcome = TestOutcome.Failed };
var resultEventArg1 = new Mock<TestResultEventArgs>(result1);
this.htmlLogger.TestResultHandler(new object(), resultEventArg1.Object);

this.htmlLogger.Initialize(new Mock<TestLoggerEvents>().Object, parameters);

Assert.ThrowsException<KeyNotFoundException>(() => this.htmlLogger.TestRunCompleteHandler(new object(), new TestRunCompleteEventArgs(null, false, true, null, null, TimeSpan.Zero)));
}

[TestMethod]
public void TestCompleteHandlerShouldCreateFileCorrectly()
{
Expand Down