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 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
10 changes: 10 additions & 0 deletions src/Microsoft.TestPlatform.CoreUtilities/Helpers/FileHelper.cs
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 All @@ -15,6 +16,8 @@ namespace Microsoft.VisualStudio.TestPlatform.Utilities.Helpers
/// </summary>
public class FileHelper : IFileHelper
{
private static readonly Version DefaultFileVersion = new Version(0, 0);

/// <inheritdoc/>
public DirectoryInfo CreateDirectory(string path)
{
Expand Down Expand Up @@ -69,6 +72,13 @@ public FileAttributes GetFileAttributes(string path)
return new FileInfo(path).Attributes;
}

/// <inheritdoc/>
public Version GetFileVersion(string path)
{
var currentFileVersion = FileVersionInfo.GetVersionInfo(path)?.FileVersion;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: null check not required

return Version.TryParse(currentFileVersion, out var currentVersion) ? currentVersion : DefaultFileVersion;
Copy link
Contributor

Choose a reason for hiding this comment

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

May not work in UAP. We should move to PlatformAbstraction otherwise.

}

/// <inheritdoc/>
public void CopyFile(string sourcePath, string destinationPath)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

/// <summary>
Expand Down Expand Up @@ -63,6 +65,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>
Version GetFileVersion(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
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.TestPlatform.TestHostProvider.Hosting;
using Microsoft.TestPlatform.TestHostProvider.Resources;
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Extensions;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.Interfaces;
Expand Down Expand Up @@ -194,6 +195,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 @@ -250,6 +253,82 @@ public Task CleanTestHostAsync(CancellationToken cancellationToken)
return Task.FromResult(true);
}

/// <summary>
/// Filter duplicate extensions, include only the highest versioned extension
/// </summary>
/// <param name="extensions">Entire list of extensions</param>
/// <returns>Filtered list of extensions</returns>
private IEnumerable<string> FilterExtensionsBasedOnVersion(IEnumerable<string> extensions)
{
Dictionary<string, string> selectedExtensions = new Dictionary<string, string>();
Dictionary<string, Version> highestFileVersions = new Dictionary<string, Version>();
Dictionary<string, Version> conflictingExtensions = new Dictionary<string, Version>();

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

if (selectedExtensions.TryGetValue(extensionAssemblyName, out var oldExtensionPath))
{
// This extension is duplicate
var currentVersion = this.GetAndLogFileVersion(extensionFullPath);

var oldVersionFound = highestFileVersions.TryGetValue(extensionAssemblyName, out var oldVersion);
if (!oldVersionFound)
{
oldVersion = this.GetAndLogFileVersion(oldExtensionPath);
}

// If the version of current file is higher than the one in the map
// replace the older with the current file
if (currentVersion > oldVersion)
{
highestFileVersions[extensionAssemblyName] = currentVersion;
conflictingExtensions[extensionAssemblyName] = currentVersion;
selectedExtensions[extensionAssemblyName] = extensionFullPath;
}
else
{
if (currentVersion < oldVersion)
{
conflictingExtensions[extensionAssemblyName] = oldVersion;
}

if (!oldVersionFound)
{
highestFileVersions.Add(extensionAssemblyName, oldVersion);
}
}
}
else
{
selectedExtensions.Add(extensionAssemblyName, extensionFullPath);
}
}

// Log warning if conflicting version extensions are found
if (conflictingExtensions.Any())
{
var extensionsString = string.Join("\n", conflictingExtensions.Select(kv => string.Format(" {0} : {1}", kv.Key, kv.Value)));
string message = string.Format(CultureInfo.CurrentCulture, Resources.MultipleFileVersions, extensionsString);
this.messageLogger.SendMessage(TestMessageLevel.Warning, message);
}

return selectedExtensions.Values;
}

private Version GetAndLogFileVersion(string path)
{
var fileVersion = this.fileHelper.GetFileVersion(path);
if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("FileVersion for {0} : {1}", path, fileVersion);
}

return fileVersion;
}

/// <summary>
/// Raises HostLaunched event
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Extensions.DependencyModel;
using Microsoft.TestPlatform.TestHostProvider.Hosting;
using Microsoft.TestPlatform.TestHostProvider.Resources;
Expand All @@ -28,7 +28,6 @@ namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand Down Expand Up @@ -227,7 +226,7 @@ public virtual TestProcessStartInfo GetTestHostProcessStartInfo(
}
else
{
string message = string.Format(Resources.NoTestHostFileExist, sourcePath);
string message = string.Format(CultureInfo.CurrentCulture, Resources.NoTestHostFileExist, sourcePath);
EqtTrace.Verbose("DotnetTestHostmanager: " + message);
throw new FileNotFoundException(message);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="MultipleFileVersions" xml:space="preserve">
<value>Multiple versions of same extension found. Selecting the highest version.
{0}</value>
</data>
<data name="NoTestHostFileExist" xml:space="preserve">
<value>No test is available in {0}. Make sure test project has a nuget reference of package "Microsoft.NET.Test.Sdk" and framework version settings are appropriate and try again.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<target state="translated">V {0} není k dispozici žádný test. Ověřte, jestli má testovací projekt odkaz na balíček nuget Microsoft.NET.Test.Sdk a jestli je nastavení verze rozhraní správné. Pak to zkuste znovu.</target>
<note />
</trans-unit>
<trans-unit id="MultipleFileVersions">
<source>Multiple versions of same extension found. Selecting the highest version.
{0}</source>
<target state="new">Multiple extensions with different file versions found. Selected extensions with version:
{0}</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<target state="translated">In {0} ist kein Test verfügbar. Stellen Sie sicher, dass das Testprojekt über einen NuGet-Verweis des Pakets "Microsoft.NET.Test.Sdk" verfügt und die Einstellungen für die Frameworkversion korrekt sind, und versuchen Sie es anschließend noch mal.</target>
<note />
</trans-unit>
<trans-unit id="MultipleFileVersions">
<source>Multiple versions of same extension found. Selecting the highest version.
{0}</source>
<target state="new">Multiple extensions with different file versions found. Selected extensions with version:
{0}</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<target state="translated">No hay pruebas disponibles en {0}. Asegúrese de que el proyecto tiene una referencia NuGet del paquete "Microsoft.NET.Test.Sdk" y de que la configuración de la versión del marco es correcta e inténtelo de nuevo.</target>
<note />
</trans-unit>
<trans-unit id="MultipleFileVersions">
<source>Multiple versions of same extension found. Selecting the highest version.
{0}</source>
<target state="new">Multiple extensions with different file versions found. Selected extensions with version:
{0}</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<target state="translated">Aucun test n'est disponible dans {0}. Vérifiez que le projet de test a une référence NuGet de package "Microsoft.NET.Test.Sdk" et que les paramètres de version du framework sont corrects, puis réessayez.</target>
<note />
</trans-unit>
<trans-unit id="MultipleFileVersions">
<source>Multiple versions of same extension found. Selecting the highest version.
{0}</source>
<target state="new">Multiple extensions with different file versions found. Selected extensions with version:
{0}</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<target state="translated">Non ci sono test disponibili in {0}. Assicurarsi che il progetto di test contenga un riferimento NuGet del pacchetto "Microsoft.NET.Test.Sdk" e che le impostazioni relative alla versione del framework siano appropriate, quindi riprovare.</target>
<note />
</trans-unit>
<trans-unit id="MultipleFileVersions">
<source>Multiple versions of same extension found. Selecting the highest version.
{0}</source>
<target state="new">Multiple extensions with different file versions found. Selected extensions with version:
{0}</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<target state="translated">{0} で使用できるテストはありません。テスト プロジェクトにパッケージ "Microsoft.NET.Test.Sdk" の NuGet 参照があることを確認し、フレームワークのバージョン設定が適切であることを確認して、もう一度お試しください。</target>
<note />
</trans-unit>
<trans-unit id="MultipleFileVersions">
<source>Multiple versions of same extension found. Selecting the highest version.
{0}</source>
<target state="new">Multiple extensions with different file versions found. Selected extensions with version:
{0}</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<target state="translated">{0}에서 테스트를 사용할 수 없습니다. 테스트 프로젝트에 “Microsoft.NET.Test.Sdk” 패키지의 nuget 참조가 있고 프레임워크 버전 설정이 적절한지 확인한 후 다시 시도하세요.</target>
<note />
</trans-unit>
<trans-unit id="MultipleFileVersions">
<source>Multiple versions of same extension found. Selecting the highest version.
{0}</source>
<target state="new">Multiple extensions with different file versions found. Selected extensions with version:
{0}</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<target state="translated">W elemencie {0} nie są dostępne żadne testy. Upewnij się, że projekt testowy ma odwołanie nuget do pakietu „Microsoft.NET.Test.Sdk” oraz że ustawienia wersji platformy i struktury są odpowiednie, a następnie spróbuj ponownie.</target>
<note />
</trans-unit>
<trans-unit id="MultipleFileVersions">
<source>Multiple versions of same extension found. Selecting the highest version.
{0}</source>
<target state="new">Multiple extensions with different file versions found. Selected extensions with version:
{0}</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<target state="translated">Nenhum teste disponível em {0}. Verifique se o projeto de teste tem uma referência NuGet do pacote "Microsoft.NET.Test.Sdk" e se as configurações de versão da estrutura são apropriadas e tente novamente.</target>
<note />
</trans-unit>
<trans-unit id="MultipleFileVersions">
<source>Multiple versions of same extension found. Selecting the highest version.
{0}</source>
<target state="new">Multiple extensions with different file versions found. Selected extensions with version:
{0}</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Loading