Skip to content

Commit

Permalink
complete unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoRossignoli committed Feb 14, 2022
1 parent c175cda commit be224d1
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public void DeleteDirectory(string directoryPath, bool recursive)

public string GetTempPath()
=> Path.GetTempPath();

public long GetFileLength(string path)
=> new FileInfo(path).Length;
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,10 @@ public interface IFileHelper
/// </summary>
/// <param name="path"></param>
public string GetTempPath();

/// <summary>
/// Get file length
/// </summary>
/// <param name="path"></param>
public long GetFileLength(string path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces.IFileHelper.Del
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces.IFileHelper.DeleteEmptyDirectroy(string directoryPath) -> void
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces.IFileHelper.DeleteDirectory(string directoryPath, bool recursive) -> void
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces.IFileHelper.GetTempPath() -> string
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces.IFileHelper.GetFileLength(string path) -> long
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces.IFileHelper.DirectoryExists(string path) -> bool
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces.IFileHelper.Exists(string path) -> bool
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces.IFileHelper.GetCurrentDirectory() -> string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper.Delete(string p
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper.DeleteEmptyDirectroy(string dirPath) -> void
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper.DeleteDirectory(string directoryPath, bool recursive) -> void
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper.GetTempPath() -> string
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper.GetFileLength(string path) -> long
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper.DirectoryExists(string path) -> bool
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper.EnumerateFiles(string directory, System.IO.SearchOption searchOption, params string[] endsWithSearchPatterns) -> System.Collections.Generic.IEnumerable<string>
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper.Exists(string path) -> bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#nullable disable

namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.ArtifactProcessing;

using System;
Expand Down Expand Up @@ -31,11 +29,11 @@ internal class ArtifactProcessingManager : IArtifactProcessingManager
private const string RunsettingsFileName = "runsettings.xml";
private const string ExecutionCompleteFileName = "executionComplete.json";

private readonly string _testSessionCorrelationId;
private readonly string? _testSessionCorrelationId;
private readonly IFileHelper _fileHelper;
private readonly ITestRunAttachmentsProcessingManager _testRunAttachmentsProcessingManager;
private readonly string _testSessionProcessArtifactFolder;
private readonly string _processArtifactFolder;
private readonly string? _testSessionProcessArtifactFolder;
private readonly string? _processArtifactFolder;
private readonly IDataSerializer _dataSerialized;
private readonly ITestRunAttachmentsProcessingEventsHandler _testRunAttachmentsProcessingEventsHandler;
private readonly IFeatureFlag _featureFlag;
Expand All @@ -49,18 +47,18 @@ public ArtifactProcessingManager(string testSessionCorrelationId) :
FeatureFlag.Instance)
{ }

public ArtifactProcessingManager(string testSessionCorrelationId,
IFileHelper fileHelper,
ITestRunAttachmentsProcessingManager testRunAttachmentsProcessingManager,
IDataSerializer dataSerialized,
ITestRunAttachmentsProcessingEventsHandler testRunAttachmentsProcessingEventsHandler,
IFeatureFlag featureFlag)
public ArtifactProcessingManager(string? testSessionCorrelationId,
IFileHelper fileHelper!!,
ITestRunAttachmentsProcessingManager testRunAttachmentsProcessingManager!!,
IDataSerializer dataSerialized!!,
ITestRunAttachmentsProcessingEventsHandler testRunAttachmentsProcessingEventsHandler!!,
IFeatureFlag featureFlag!!)
{
_fileHelper = fileHelper ?? throw new ArgumentNullException(nameof(fileHelper));
_testRunAttachmentsProcessingManager = testRunAttachmentsProcessingManager ?? throw new ArgumentNullException(nameof(testRunAttachmentsProcessingManager));
_dataSerialized = dataSerialized ?? throw new ArgumentNullException(nameof(dataSerialized));
_testRunAttachmentsProcessingEventsHandler = testRunAttachmentsProcessingEventsHandler ?? throw new ArgumentNullException(nameof(testRunAttachmentsProcessingEventsHandler));
_featureFlag = featureFlag ?? throw new ArgumentNullException(nameof(featureFlag));
_fileHelper = fileHelper;
_testRunAttachmentsProcessingManager = testRunAttachmentsProcessingManager;
_dataSerialized = dataSerialized;
_testRunAttachmentsProcessingEventsHandler = testRunAttachmentsProcessingEventsHandler;
_featureFlag = featureFlag;

// We don't validate for null, it's expected, we'll have testSessionCorrelationId only in case of .NET SDK run.
if (testSessionCorrelationId is not null)
Expand All @@ -71,7 +69,7 @@ public ArtifactProcessingManager(string testSessionCorrelationId,
}
}

public void CollectArtifacts(TestRunCompleteEventArgs testRunCompleteEventArgs, string runSettingsXml)
public void CollectArtifacts(TestRunCompleteEventArgs testRunCompleteEventArgs!!, string runSettingsXml!!)
{
if (!_featureFlag.IsEnabled(FeatureFlag.ARTIFACTS_POSTPROCESSING))
{
Expand All @@ -85,9 +83,6 @@ public void CollectArtifacts(TestRunCompleteEventArgs testRunCompleteEventArgs,
return;
}

_ = testRunCompleteEventArgs ?? throw new ArgumentNullException(nameof(testRunCompleteEventArgs));
_ = runSettingsXml ?? throw new ArgumentNullException(nameof(runSettingsXml));

try
{
// We need to save in case of attachements, we'll show these at the end on console.
Expand Down Expand Up @@ -163,14 +158,12 @@ private async Task DataCollectorsAttachmentsPostProcessing(TestArtifacts[] testA
{
// We take the biggest runsettings in size, it should be the one with more configuration.
// In future we can think to merge...but it's not easy for custom config, we could break something.
string runsettingsFile = testArtifacts
string? runsettingsFile = testArtifacts
.SelectMany(x => x.Artifacts.Where(x => x.Type == ArtifactType.Runsettings))
.Select(x => new FileInfo(x.FileName))
.OrderByDescending(x => x.Length)
.FirstOrDefault()?
.FullName;
.OrderByDescending(x => _fileHelper.GetFileLength(x.FileName))
.FirstOrDefault()?.FileName;

string runsettingsXml = null;
string? runsettingsXml = null;
if (runsettingsFile is not null)
{
using var artifactStream = _fileHelper.GetStream(runsettingsFile, FileMode.Open, FileAccess.Read);
Expand All @@ -194,11 +187,11 @@ private async Task DataCollectorsAttachmentsPostProcessing(TestArtifacts[] testA
string executionCompleteMessage = await streamReader.ReadToEndAsync();
EqtTrace.Verbose($"ArtifactProcessingManager.MergeDataCollectorAttachments: ExecutionComplete message \n{executionCompleteMessage}");
TestRunCompleteEventArgs eventArgs = _dataSerialized.DeserializePayload<TestRunCompleteEventArgs>(_dataSerialized.DeserializeMessage(executionCompleteMessage));
foreach (var invokedDataCollector in eventArgs?.InvokedDataCollectors)
foreach (var invokedDataCollector in eventArgs?.InvokedDataCollectors ?? Enumerable.Empty<InvokedDataCollector>())
{
invokedDataCollectors.Add(invokedDataCollector);
}
foreach (var attachmentSet in eventArgs?.AttachmentSets)
foreach (var attachmentSet in eventArgs?.AttachmentSets ?? Enumerable.Empty<AttachmentSet>())
{
attachments.Add(attachmentSet);
}
Expand All @@ -208,7 +201,7 @@ await _testRunAttachmentsProcessingManager.ProcessTestRunAttachmentsAsync(runset
new RequestData()
{
IsTelemetryOptedIn = IsTelemetryOptedIn(),
ProtocolConfig = ObjectModel.Constants.DefaultProtocolConfig
ProtocolConfig = Constants.DefaultProtocolConfig
},
attachments,
invokedDataCollectors,
Expand All @@ -223,11 +216,11 @@ private TestArtifacts[] LoadTestArtifacts() => _fileHelper.GetFiles(_processArti
.Select(testSessionArtifact => new TestArtifacts(testSessionArtifact.Key, testSessionArtifact.Select(x => ParseArtifact(x.Artifact)).Where(x => x is not null).ToArray()))
.ToArray();

private static Artifact ParseArtifact(string fileName) =>
private static Artifact? ParseArtifact(string fileName!!) =>
Path.GetFileName(fileName) switch
{
RunsettingsFileName => new Artifact(fileName, ArtifactType.ExecutionComplete),
ExecutionCompleteFileName => new Artifact(fileName, ArtifactType.Runsettings),
RunsettingsFileName => new Artifact(fileName, ArtifactType.Runsettings),
ExecutionCompleteFileName => new Artifact(fileName, ArtifactType.ExecutionComplete),
_ => null
};

Expand Down
Loading

0 comments on commit be224d1

Please sign in to comment.