Skip to content

Commit

Permalink
save work (started ArtifactProcessingTests)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoRossignoli committed Feb 11, 2022
1 parent 75e71d0 commit f5188bd
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 66 deletions.
67 changes: 20 additions & 47 deletions src/Microsoft.TestPlatform.CoreUtilities/Helpers/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,27 @@ public class FileHelper : IFileHelper

/// <inheritdoc/>
public DirectoryInfo CreateDirectory(string path)
{
return Directory.CreateDirectory(path);
}
=> Directory.CreateDirectory(path);

/// <inheritdoc/>
public string GetCurrentDirectory()
{
return Directory.GetCurrentDirectory();
}
=> Directory.GetCurrentDirectory();

/// <inheritdoc/>
public bool Exists(string path)
{
return File.Exists(path);
}
=> File.Exists(path);

/// <inheritdoc/>
public bool DirectoryExists(string path)
{
return Directory.Exists(path);
}
=> Directory.Exists(path);

/// <inheritdoc/>
public Stream GetStream(string filePath, FileMode mode, FileAccess access = FileAccess.ReadWrite)
{
return new FileStream(filePath, mode, access);
}
=> new FileStream(filePath, mode, access);

/// <inheritdoc/>
public Stream GetStream(string filePath, FileMode mode, FileAccess access, FileShare share)
{
return new FileStream(filePath, mode, access, share);
}

=> new FileStream(filePath, mode, access, share);

/// <inheritdoc/>
public IEnumerable<string> EnumerateFiles(
Expand All @@ -77,40 +64,29 @@ public IEnumerable<string> EnumerateFiles(

/// <inheritdoc/>
public FileAttributes GetFileAttributes(string path)
{
return new FileInfo(path).Attributes;
}
=> new FileInfo(path).Attributes;

/// <inheritdoc/>
public Version GetFileVersion(string path)
{
var currentFileVersion = FileVersionInfo.GetVersionInfo(path)?.FileVersion;
return Version.TryParse(currentFileVersion, out var currentVersion) ? currentVersion : DefaultFileVersion;
}
=> Version.TryParse(FileVersionInfo.GetVersionInfo(path)?.FileVersion, out var currentVersion) ?
currentVersion :
DefaultFileVersion;

/// <inheritdoc/>
public void CopyFile(string sourcePath, string destinationPath)
{
File.Copy(sourcePath, destinationPath);
}
=> File.Copy(sourcePath, destinationPath);

/// <inheritdoc/>
public void MoveFile(string sourcePath, string destinationPath)
{
File.Move(sourcePath, destinationPath);
}
=> File.Move(sourcePath, destinationPath);

/// <inheritdoc/>
public void WriteAllTextToFile(string filePath, string content)
{
File.WriteAllText(filePath, content);
}
=> File.WriteAllText(filePath, content);

/// <inheritdoc/>
public string GetFullPath(string path)
{
return Path.GetFullPath(path);
}
=> Path.GetFullPath(path);

/// <inheritdoc/>
public void DeleteEmptyDirectroy(string dirPath)
Expand All @@ -131,20 +107,17 @@ public void DeleteEmptyDirectroy(string dirPath)

/// <inheritdoc/>
public string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
{
return Directory.GetFiles(path, searchPattern, searchOption);
}
=> Directory.GetFiles(path, searchPattern, searchOption);

/// <inheritdoc/>
public void Delete(string path)
{
File.Delete(path);
}
=> File.Delete(path);

public void DeleteDirectory(string directoryPath, bool recursive)
{
Directory.Delete(directoryPath, recursive);
}
=> Directory.Delete(directoryPath, recursive);

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

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

/// <summary>
/// Get temporary file path
/// </summary>
/// <param name="path"></param>
public string GetTempPath();
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces.IFileHelper.Cop
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces.IFileHelper.Delete(string path) -> void
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.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 @@ -57,6 +57,7 @@ Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper.CreateDirectory
Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper.Delete(string path) -> void
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.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
Expand Up @@ -55,18 +55,19 @@ public ArtifactProcessingManager(string testSessionCorrelationId,
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));

// 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)
{
_testSessionCorrelationId = testSessionCorrelationId;
_processArtifactFolder = Path.Combine(Path.GetTempPath(), _testSessionCorrelationId);
_processArtifactFolder = Path.Combine(_fileHelper.GetTempPath(), _testSessionCorrelationId);
_testSessionProcessArtifactFolder = Path.Combine(_processArtifactFolder, $"{Process.GetCurrentProcess().Id}_{Guid.NewGuid()}");
}
_fileHelper = fileHelper;
_testRunAttachmentsProcessingManager = testRunAttachmentsProcessingManager;
_dataSerialized = dataSerialized;
_testRunAttachmentsProcessingEventsHandler = testRunAttachmentsProcessingEventsHandler;
_featureFlag = featureFlag;
}

public void CollectArtifacts(TestRunCompleteEventArgs testRunCompleteEventArgs, string runSettingsXml)
Expand All @@ -83,6 +84,9 @@ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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;
using System.Collections.ObjectModel;

using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.ArtifactProcessing;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Moq;

namespace TestPlatform.CrossPlatEngine.UnitTests;

[TestClass]
public class ArtifactProcessingTests
{
private readonly Mock<IFileHelper> _fileHelperMock = new();
private readonly Mock<ITestRunAttachmentsProcessingManager> _testRunAttachmentsProcessingManagerMock = new();
private readonly Mock<ITestRunAttachmentsProcessingEventsHandler> _testRunAttachmentsProcessingEventsHandlerMock = new();
private readonly Mock<IFeatureFlag> _featureFlagMock = new();
private readonly Mock<IDataSerializer> _dataSerializer = new();
private readonly Mock<ITestRunStatistics> _testRunStatistics = new();
private ArtifactProcessingManager _artifactProcessingManager;

public ArtifactProcessingTests()
{
_featureFlagMock.Setup(x => x.IsEnabled(It.IsAny<string>())).Returns(true);
_fileHelperMock.Setup(x => x.GetTempPath()).Returns("/tmp");

_artifactProcessingManager =
new ArtifactProcessingManager(Guid.NewGuid().ToString(),
_fileHelperMock.Object,
_testRunAttachmentsProcessingManagerMock.Object,
_dataSerializer.Object,
_testRunAttachmentsProcessingEventsHandlerMock.Object,
_featureFlagMock.Object);
}

[TestMethod]
public void CollectArtifacts_NullSessionIdShouldReturn()
{
_artifactProcessingManager =
new ArtifactProcessingManager(null,
_fileHelperMock.Object,
_testRunAttachmentsProcessingManagerMock.Object,
_dataSerializer.Object,
_testRunAttachmentsProcessingEventsHandlerMock.Object,
_featureFlagMock.Object);

var testRunCompleteEventArgs = new TestRunCompleteEventArgs(_testRunStatistics.Object,
false,
false,
null,
new Collection<AttachmentSet>()
{
new AttachmentSet(new Uri("//sample"),"")
},
TimeSpan.Zero);

_artifactProcessingManager.CollectArtifacts(testRunCompleteEventArgs, string.Empty);
_fileHelperMock.Verify(x => x.WriteAllTextToFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors;

using System;

using TestPlatform.CommandLine.Processors;

using TestTools.UnitTesting;

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors;
[TestClass]
public class ArtifactProcessingPostProcessModeProcessorTest
{
private readonly Mock<IArtifactProcessingManager> artifactProcessingManagerMock = new();
private readonly Mock<IFeatureFlag> featureFlagMock = new();
private readonly Mock<IArtifactProcessingManager> _artifactProcessingManagerMock = new();
private readonly Mock<IFeatureFlag> _featureFlagMock = new();

[TestMethod]
public void ProcessorExecutorInitialize_ShouldFailIfNullCtor()
{
Assert.ThrowsException<ArgumentNullException>(() => new ArtifactProcessingPostProcessModeProcessorExecutor(null, artifactProcessingManagerMock.Object));
Assert.ThrowsException<ArgumentNullException>(() => new ArtifactProcessingPostProcessModeProcessorExecutor(null, _artifactProcessingManagerMock.Object));
Assert.ThrowsException<ArgumentNullException>(() => new ArtifactProcessingPostProcessModeProcessorExecutor(new CommandLineOptions(), null));
}

[TestMethod]
public void ProcessorExecutorInitialize_ShouldNotFailIfNullArg()
{
ArtifactProcessingPostProcessModeProcessorExecutor artifactProcessingPostProcessModeProcessorExecutor = new(new CommandLineOptions(), artifactProcessingManagerMock.Object);
ArtifactProcessingPostProcessModeProcessorExecutor artifactProcessingPostProcessModeProcessorExecutor = new(new CommandLineOptions(), _artifactProcessingManagerMock.Object);
artifactProcessingPostProcessModeProcessorExecutor.Initialize(null);
}

[TestMethod]
public void ProcessorExecutorInitialize_ShouldSetCommandOption()
{
var commandOptions = new CommandLineOptions();
ArtifactProcessingPostProcessModeProcessorExecutor artifactProcessingPostProcessModeProcessorExecutor = new(commandOptions, artifactProcessingManagerMock.Object);
ArtifactProcessingPostProcessModeProcessorExecutor artifactProcessingPostProcessModeProcessorExecutor = new(commandOptions, _artifactProcessingManagerMock.Object);
artifactProcessingPostProcessModeProcessorExecutor.Initialize(null);
Assert.AreEqual(ArtifactProcessingMode.PostProcess, commandOptions.ArtifactProcessingMode);
}
Expand All @@ -56,20 +56,20 @@ public void ProcessorCapabilities()
[TestMethod]
public void ProcessorExecutorInitialize_ExceptionShouldNotBubbleUp()
{
artifactProcessingManagerMock.Setup(x => x.PostProcessArtifactsAsync()).Callback(() => throw new Exception());
ArtifactProcessingPostProcessModeProcessorExecutor artifactProcessingPostProcessModeProcessorExecutor = new(new CommandLineOptions(), artifactProcessingManagerMock.Object);
_artifactProcessingManagerMock.Setup(x => x.PostProcessArtifactsAsync()).Callback(() => throw new Exception());
ArtifactProcessingPostProcessModeProcessorExecutor artifactProcessingPostProcessModeProcessorExecutor = new(new CommandLineOptions(), _artifactProcessingManagerMock.Object);
artifactProcessingPostProcessModeProcessorExecutor.Initialize(null);
Assert.AreEqual(ArgumentProcessorResult.Fail, artifactProcessingPostProcessModeProcessorExecutor.Execute());
}

[TestMethod]
public void ArtifactProcessingPostProcessMode_ContainsPostProcessCommand()
{
featureFlagMock.Setup(x => x.IsEnabled(It.IsAny<string>())).Returns(true);
Assert.IsTrue(ArtifactProcessingPostProcessModeProcessor.ContainsPostProcessCommand(new string[] { "--artifactsProcessingMode-postprocess" }, featureFlagMock.Object));
Assert.IsTrue(ArtifactProcessingPostProcessModeProcessor.ContainsPostProcessCommand(new string[] { "--ARTIfactsProcessingMode-postprocess" }, featureFlagMock.Object));
Assert.IsFalse(ArtifactProcessingPostProcessModeProcessor.ContainsPostProcessCommand(new string[] { "-ARTIfactsProcessingMode-postprocess" }, featureFlagMock.Object));
Assert.IsFalse(ArtifactProcessingPostProcessModeProcessor.ContainsPostProcessCommand(new string[] { "--ARTIfactsProcessingMode-postproces" }, featureFlagMock.Object));
Assert.IsFalse(ArtifactProcessingPostProcessModeProcessor.ContainsPostProcessCommand(null, featureFlagMock.Object));
_featureFlagMock.Setup(x => x.IsEnabled(It.IsAny<string>())).Returns(true);
Assert.IsTrue(ArtifactProcessingPostProcessModeProcessor.ContainsPostProcessCommand(new string[] { "--artifactsProcessingMode-postprocess" }, _featureFlagMock.Object));
Assert.IsTrue(ArtifactProcessingPostProcessModeProcessor.ContainsPostProcessCommand(new string[] { "--ARTIfactsProcessingMode-postprocess" }, _featureFlagMock.Object));
Assert.IsFalse(ArtifactProcessingPostProcessModeProcessor.ContainsPostProcessCommand(new string[] { "-ARTIfactsProcessingMode-postprocess" }, _featureFlagMock.Object));
Assert.IsFalse(ArtifactProcessingPostProcessModeProcessor.ContainsPostProcessCommand(new string[] { "--ARTIfactsProcessingMode-postproces" }, _featureFlagMock.Object));
Assert.IsFalse(ArtifactProcessingPostProcessModeProcessor.ContainsPostProcessCommand(null, _featureFlagMock.Object));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors;

using System;

using TestPlatform.CommandLine.Processors;

using TestTools.UnitTesting;

[TestClass]
Expand Down

0 comments on commit f5188bd

Please sign in to comment.