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

Highest version filtering for extensions. #1051

Merged
merged 12 commits into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -5,6 +5,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Utilities.Helpers
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -69,6 +70,12 @@ public FileAttributes GetFileAttributes(string path)
return new FileInfo(path).Attributes;
}

/// <inheritdoc/>
public FileVersionInfo GetVersionInfo(string path)
{
return FileVersionInfo.GetVersionInfo(path);
}

/// <inheritdoc/>
public void CopyFile(string sourcePath, string destinationPath)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces
{
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

/// <summary>
Expand Down Expand Up @@ -63,6 +64,13 @@ public interface IFileHelper
/// <returns>Attributes of the file.</returns>
FileAttributes GetFileAttributes(string path);

/// <summary>
/// Gets the version information of the file.
/// </summary>
/// <param name="path">Full path to the file.</param>
/// <returns>File Version information of the file.</returns>
FileVersionInfo GetVersionInfo(string path);

/// <summary>
/// Copy a file in the file system.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<TargetFrameworks>netstandard1.4;net451</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\Resources.resx"/>
<EmbeddedResource Include="Resources\Resources.resx" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
<PackageReference Include="Microsoft.Internal.Dia.Interop">
Expand All @@ -19,6 +19,11 @@
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.4' ">
<PackageReference Include="System.Diagnostics.FileVersionInfo">
<Version>4.3.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Update="Resources\Resources.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ public IEnumerable<string> GetTestPlatformExtensions(IEnumerable<string> sources
extensions = extensions.Concat(sources.SelectMany(s => this.fileHelper.EnumerateFiles(Path.GetDirectoryName(s), SearchOption.TopDirectoryOnly, TestAdapterEndsWithPattern)));
}

extensions = this.FilterExtensionsBasedOnVersion(extensions);

return extensions;
}

Expand Down Expand Up @@ -244,6 +246,38 @@ public Task CleanTestHostAsync(CancellationToken cancellationToken)
return Task.FromResult(true);
}

private IEnumerable<string> FilterExtensionsBasedOnVersion(IEnumerable<string> extensions)
{
Dictionary<string, Tuple<Version, string>> extensionMap = new Dictionary<string, Tuple<Version, string>>();

foreach (var extensionFullPath in extensions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach (var extensionFullPath in extensions) [](start = 12, length = 45)

Please try group by, and then get the highest file version within the group if the group size > 1

{
// assemblyName is the key
var extensionAssemblyName = Path.GetFileNameWithoutExtension(extensionFullPath);

var currentFileVersion = this.fileHelper.GetVersionInfo(extensionFullPath)?.FileVersion;
if (Version.TryParse(currentFileVersion, out var currentVersion))
{
if (extensionMap.TryGetValue(extensionAssemblyName, out var oldExtensionInfo))
{
// If the version of current file is higher than the one in the map
// replace the older with the current file
if (currentVersion > oldExtensionInfo.Item1)
{
extensionMap[extensionAssemblyName] = new Tuple<Version, string>(currentVersion, extensionFullPath);
}
}
else
{
extensionMap.Add(extensionAssemblyName, new Tuple<Version, string>(currentVersion, extensionFullPath));
}
}
}

// List of highest versioned files.
return extensionMap.Select(p => p.Value.Item2);
}

/// <summary>
/// Raises HostLaunched event
/// </summary>
Expand Down