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 potential trx logger NRE #4240

Merged
merged 2 commits into from Jan 3, 2023
Merged
Show file tree
Hide file tree
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
34 changes: 17 additions & 17 deletions src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs
Expand Up @@ -477,28 +477,29 @@ private void ReserveTrxFilePath()
private string AcquireTrxFileNamePath(out bool shouldOverwrite)
{
TPDebug.Assert(IsInitialized, "Logger is not initialized");
TPDebug.Assert(_parametersDictionary is not null, "_parametersDictionary is null");

shouldOverwrite = false;
var isLogFileNameParameterExists = _parametersDictionary.TryGetValue(TrxLoggerConstants.LogFileNameKey, out string? logFileNameValue) && !logFileNameValue.IsNullOrWhiteSpace();
var isLogFilePrefixParameterExists = _parametersDictionary.TryGetValue(TrxLoggerConstants.LogFilePrefixKey, out string? logFilePrefixValue) && !logFilePrefixValue.IsNullOrWhiteSpace();

string? filePath = null;

if (isLogFilePrefixParameterExists)
if (_parametersDictionary is not null)
{
if (_parametersDictionary.TryGetValue(DefaultLoggerParameterNames.TargetFramework, out var framework) && framework != null)
var isLogFileNameParameterExists = _parametersDictionary.TryGetValue(TrxLoggerConstants.LogFileNameKey, out string? logFileNameValue) && !logFileNameValue.IsNullOrWhiteSpace();
var isLogFilePrefixParameterExists = _parametersDictionary.TryGetValue(TrxLoggerConstants.LogFilePrefixKey, out string? logFilePrefixValue) && !logFilePrefixValue.IsNullOrWhiteSpace();
if (isLogFilePrefixParameterExists)
{
framework = NuGetFramework.Parse(framework).GetShortFolderName();
logFilePrefixValue = logFilePrefixValue + "_" + framework;
}
if (_parametersDictionary.TryGetValue(DefaultLoggerParameterNames.TargetFramework, out var framework) && framework != null)
{
framework = NuGetFramework.Parse(framework).GetShortFolderName();
logFilePrefixValue = logFilePrefixValue + "_" + framework;
}

filePath = _trxFileHelper.GetNextTimestampFileName(_testResultsDirPath, logFilePrefixValue + _trxFileExtension, "_yyyyMMddHHmmss");
}
else if (isLogFileNameParameterExists)
{
filePath = Path.Combine(_testResultsDirPath, logFileNameValue!);
shouldOverwrite = true;
filePath = _trxFileHelper.GetNextTimestampFileName(_testResultsDirPath, logFilePrefixValue + _trxFileExtension, "_yyyyMMddHHmmss");
}
else if (isLogFileNameParameterExists)
{
filePath = Path.Combine(_testResultsDirPath, logFileNameValue!);
shouldOverwrite = true;
}
}

filePath ??= SetDefaultTrxFilePath();
Expand Down Expand Up @@ -746,12 +747,11 @@ private void UpdateTestEntries(Guid executionId, Guid parentExecutionId, ITestEl
private TrxLoggerObjectModel.TestOutcome ChangeTestOutcomeIfNecessary(TrxLoggerObjectModel.TestOutcome outcome)
{
TPDebug.Assert(IsInitialized, "Logger is not initialized");
TPDebug.Assert(_parametersDictionary is not null, "_parametersDictionary is null");

// If no tests discovered/executed and TreatNoTestsAsError was set to True
// We will return ResultSummary as Failed
// Note : we only send the value of TreatNoTestsAsError if it is "True"
if (TotalTestCount == 0 && _parametersDictionary.ContainsKey(ObjectModelConstants.TreatNoTestsAsError))
if (TotalTestCount == 0 && _parametersDictionary?.ContainsKey(ObjectModelConstants.TreatNoTestsAsError) == true)
{
outcome = TrxLoggerObjectModel.TestOutcome.Failed;
}
Expand Down
Expand Up @@ -857,6 +857,17 @@ public void IntializeShouldThrowExceptionIfBothPrefixAndNameProvided()
Assert.ThrowsException<ArgumentException>(() => _testableTrxLogger.Initialize(_events.Object, _parameters));
}

[TestMethod]
public void SkipInitializeDictionaryShouldNotFail()
{
var logger = new TestableTrxLogger();
logger.Initialize(_events.Object, Path.GetTempPath());
var testRunCompleteEventArgs = CreateTestRunCompleteEventArgs();
logger.TestRunCompleteHandler(new object(), testRunCompleteEventArgs);
Assert.IsTrue(File.Exists(logger.TrxFile));
File.Delete(logger.TrxFile);
}

private void ValidateTestIdAndNameInTrx()
{
TestCase testCase = CreateTestCase("TestCase");
Expand Down