-
Notifications
You must be signed in to change notification settings - Fork 356
Highest version filtering for extensions. #1051
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
Changes from 4 commits
c6fb057
1165621
56b3df5
a6c93fc
00fcecf
9aac00b
b6140e6
5aebe15
f0f5ded
2cddd94
e759a5c
a2faed2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
||
|
|
@@ -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) | ||
| { | ||
|
|
@@ -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; | ||
| return Version.TryParse(currentFileVersion, out var currentVersion) ? currentVersion : DefaultFileVersion; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -188,6 +189,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; | ||
| } | ||
|
|
||
|
|
@@ -244,6 +247,78 @@ 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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); | ||
|
|
||
| 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); | ||
| EqtTrace.Verbose("FileVersion for {0} : {1}", path, fileVersion); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if logging enabled. |
||
| return fileVersion; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Raises HostLaunched event | ||
| /// </summary> | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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