Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ internal sealed partial class TrxReportEngine
private readonly CancellationToken _cancellationToken;
private readonly int _exitCode;
private readonly IFileSystem _fileSystem;
private readonly bool _isCopyingFileAllowed;

public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, ICommandLineOptions commandLineOptionsService, IConfiguration configuration, IClock clock, Dictionary<IExtension, List<SessionFileArtifact>> artifactsByExtension, ITestFramework testFrameworkAdapter, DateTimeOffset testStartTime, int exitCode, CancellationToken cancellationToken, bool isCopyingFileAllowed = true)
public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, ICommandLineOptions commandLineOptionsService, IConfiguration configuration, IClock clock, Dictionary<IExtension, List<SessionFileArtifact>> artifactsByExtension, ITestFramework testFrameworkAdapter, DateTimeOffset testStartTime, int exitCode, CancellationToken cancellationToken)
{
_testApplicationModuleInfo = testApplicationModuleInfo;
_environment = environment;
Expand All @@ -101,7 +100,6 @@ public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testAp
_cancellationToken = cancellationToken;
_exitCode = exitCode;
_fileSystem = fileSystem;
_isCopyingFileAllowed = isCopyingFileAllowed;
}

public async Task<(string FileName, string? Warning)> GenerateReportAsync(TestNodeUpdateMessage[] testNodeUpdateMessages, string testHostCrashInfo = "", bool isTestHostCrashed = false)
Expand Down Expand Up @@ -147,7 +145,7 @@ public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testAp

string trxOutcome = isTestHostCrashed || _exitCode != ExitCodes.Success || hasFailedTests ? "Failed" : "Completed";

await AddResultSummaryAsync(testRun, trxOutcome, runDeploymentRoot, testHostCrashInfo, _exitCode, summaryCounts, isTestHostCrashed).ConfigureAwait(false);
AddResultSummary(testRun, trxOutcome, runDeploymentRoot, testHostCrashInfo, _exitCode, summaryCounts, isTestHostCrashed);

// will need catch Unauthorized access
document.Add(testRun);
Expand Down Expand Up @@ -224,13 +222,13 @@ public async Task AddArtifactsAsync(FileInfo trxFile, Dictionary<IExtension, Lis
resultSummary.Add(collectorDataEntries);
}

await AddArtifactsToCollectionAsync(artifacts, collectorDataEntries, runDeploymentRoot).ConfigureAwait(false);
AddArtifactsToCollection(artifacts, collectorDataEntries, runDeploymentRoot);

using FileStream fs = File.OpenWrite(trxFile.FullName);
await document.SaveAsync(fs, SaveOptions.None, _cancellationToken).ConfigureAwait(false);
}

private async Task AddArtifactsToCollectionAsync(Dictionary<IExtension, List<SessionFileArtifact>> artifacts, XElement collectorDataEntries, string runDeploymentRoot)
private void AddArtifactsToCollection(Dictionary<IExtension, List<SessionFileArtifact>> artifacts, XElement collectorDataEntries, string runDeploymentRoot)
{
foreach (KeyValuePair<IExtension, List<SessionFileArtifact>> extensionArtifacts in artifacts)
{
Expand All @@ -250,13 +248,13 @@ private async Task AddArtifactsToCollectionAsync(Dictionary<IExtension, List<Ses

foreach (SessionFileArtifact artifact in extensionArtifacts.Value)
{
string href = await CopyArtifactIntoTrxDirectoryAndReturnHrefValueAsync(artifact.FileInfo, runDeploymentRoot).ConfigureAwait(false);
string href = CopyArtifactIntoTrxDirectoryAndReturnHrefValue(artifact.FileInfo, runDeploymentRoot);
uriAttachments.Add(new XElement(NamespaceUri + "UriAttachment", new XElement(NamespaceUri + "A", new XAttribute("href", href))));
}
}
}

private async Task AddResultSummaryAsync(XElement testRun, string resultSummaryOutcome, string runDeploymentRoot, string testHostCrashInfo, int exitCode, SummaryCounts summaryCounts, bool isTestHostCrashed = false)
private void AddResultSummary(XElement testRun, string resultSummaryOutcome, string runDeploymentRoot, string testHostCrashInfo, int exitCode, SummaryCounts summaryCounts, bool isTestHostCrashed = false)
{
// TODO: VSTest adds Output/StdOut element to ResultSummary which we don't add.
// VSTest adds mainly two things in that element:
Expand Down Expand Up @@ -333,10 +331,10 @@ private async Task AddResultSummaryAsync(XElement testRun, string resultSummaryO
var collectorDataEntries = new XElement(NamespaceUri + "CollectorDataEntries");
resultSummary.Add(collectorDataEntries);

await AddArtifactsToCollectionAsync(_artifactsByExtension, collectorDataEntries, runDeploymentRoot).ConfigureAwait(false);
AddArtifactsToCollection(_artifactsByExtension, collectorDataEntries, runDeploymentRoot);
}

private async Task<string> CopyArtifactIntoTrxDirectoryAndReturnHrefValueAsync(FileInfo artifact, string runDeploymentRoot)
private string CopyArtifactIntoTrxDirectoryAndReturnHrefValue(FileInfo artifact, string runDeploymentRoot)
{
string artifactDirectory = CreateOrGetTrxArtifactDirectory(runDeploymentRoot);
string fileName = artifact.Name;
Expand All @@ -357,7 +355,7 @@ private async Task<string> CopyArtifactIntoTrxDirectoryAndReturnHrefValueAsync(F
break;
}

await CopyFileAsync(artifact, new FileInfo(destination)).ConfigureAwait(false);
_fileSystem.CopyFile(artifact.FullName, new FileInfo(destination).FullName);

return Path.Combine(_environment.MachineName, Path.GetFileName(destination));
}
Expand All @@ -373,18 +371,6 @@ private string CreateOrGetTrxArtifactDirectory(string runDeploymentRoot)
return directoryName;
}

private async Task CopyFileAsync(FileInfo origin, FileInfo destination)
{
if (!_isCopyingFileAllowed)
{
return;
}

using FileStream fileStream = File.OpenRead(origin.FullName);
using var destinationStream = new FileStream(destination.FullName, FileMode.Create);
await fileStream.CopyToAsync(destinationStream, _cancellationToken).ConfigureAwait(false);
}

private static void AddTestLists(XElement testRun)
{
var testLists = new XElement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,7 @@ public async Task TrxReportEngine_GenerateReportAsync_FileAlreadyExists_WillRetr
_ = _testApplicationModuleInfoMock.Setup(_ => _.GetCurrentTestApplicationFullPath()).Returns("TestAppPath");
var trxReportEngine = new TrxReportEngine(_fileSystem.Object, _testApplicationModuleInfoMock.Object, _environmentMock.Object, _commandLineOptionsMock.Object,
_configurationMock.Object, _clockMock.Object,
_artifactsByExtension, _testFrameworkMock.Object, DateTime.UtcNow, 0, CancellationToken.None,
isCopyingFileAllowed: false);
_artifactsByExtension, _testFrameworkMock.Object, DateTime.UtcNow, 0, CancellationToken.None);

// Act
_ = await trxReportEngine.GenerateReportAsync([]);
Expand Down Expand Up @@ -640,8 +639,7 @@ private TrxReportEngine GenerateTrxReportEngine(MemoryFileStream memoryStream, b

return new TrxReportEngine(_fileSystem.Object, _testApplicationModuleInfoMock.Object, _environmentMock.Object, _commandLineOptionsMock.Object,
_configurationMock.Object, _clockMock.Object,
_artifactsByExtension, _testFrameworkMock.Object, testStartTime, 0, cancellationToken,
isCopyingFileAllowed: false);
_artifactsByExtension, _testFrameworkMock.Object, testStartTime, 0, cancellationToken);
}

private sealed class MemoryFileStream : IFileStream
Expand Down