|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.Linq;
|
7 | 7 | using System.Reflection;
|
| 8 | +using System.Text; |
8 | 9 |
|
9 | 10 | using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;
|
10 |
| - |
| 11 | +using Microsoft.VisualStudio.TestPlatform.Common.Telemetry; |
11 | 12 | using Microsoft.VisualStudio.TestPlatform.ObjectModel;
|
12 |
| - |
13 | 13 | using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
|
14 | 14 |
|
15 | 15 | #nullable disable
|
@@ -91,6 +91,80 @@ public class TestExtensions
|
91 | 91 | /// </summary>
|
92 | 92 | internal bool AreDataCollectorsCached { get; set; }
|
93 | 93 |
|
| 94 | + /// <summary> |
| 95 | + /// Merge two extension dictionaries. |
| 96 | + /// </summary> |
| 97 | + /// |
| 98 | + /// <param name="first">First extension dictionary.</param> |
| 99 | + /// <param name="second">Second extension dictionary.</param> |
| 100 | + /// |
| 101 | + /// <returns> |
| 102 | + /// A dictionary representing the merger between the two input dictionaries. |
| 103 | + /// </returns> |
| 104 | + internal static Dictionary<string, HashSet<string>> CreateMergedDictionary( |
| 105 | + Dictionary<string, HashSet<string>> first, |
| 106 | + Dictionary<string, HashSet<string>> second) |
| 107 | + { |
| 108 | + var isFirstNullOrEmpty = first == null || first.Count == 0; |
| 109 | + var isSecondNullOrEmpty = second == null || second.Count == 0; |
| 110 | + |
| 111 | + // Sanity checks. |
| 112 | + if (isFirstNullOrEmpty && isSecondNullOrEmpty) |
| 113 | + { |
| 114 | + return new Dictionary<string, HashSet<string>>(); |
| 115 | + } |
| 116 | + if (isFirstNullOrEmpty) |
| 117 | + { |
| 118 | + return new Dictionary<string, HashSet<string>>(second); |
| 119 | + } |
| 120 | + if (isSecondNullOrEmpty) |
| 121 | + { |
| 122 | + return new Dictionary<string, HashSet<string>>(first); |
| 123 | + } |
| 124 | + |
| 125 | + // Copy all the keys in the first dictionary into the resulting dictionary. |
| 126 | + var result = new Dictionary<string, HashSet<string>>(first); |
| 127 | + |
| 128 | + foreach (var kvp in second) |
| 129 | + { |
| 130 | + // If the "source" set is empty there's no reason to continue merging for this key. |
| 131 | + if (kvp.Value == null || kvp.Value.Count == 0) |
| 132 | + { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + // If there's no key-value pair entry in the "destination" dictionary for the current |
| 137 | + // key in the "source" dictionary, we copy the "source" set wholesale. |
| 138 | + if (!result.ContainsKey(kvp.Key)) |
| 139 | + { |
| 140 | + result.Add(kvp.Key, kvp.Value); |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + // Getting here means there's already an entry for the "source" key in the "destination" |
| 145 | + // dictionary which means we need to copy individual set elements from the "source" set |
| 146 | + // to the "destination" set. |
| 147 | + result[kvp.Key] = MergeSets(result[kvp.Key], kvp.Value); |
| 148 | + } |
| 149 | + |
| 150 | + return result; |
| 151 | + } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Add extension-related telemetry. |
| 155 | + /// </summary> |
| 156 | + /// |
| 157 | + /// <param name="metrics">A collection representing the telemetry data.</param> |
| 158 | + /// <param name="extensions">The input extension collection.</param> |
| 159 | + internal static void AddExtensionTelemetry( |
| 160 | + IDictionary<string, object> metrics, |
| 161 | + Dictionary<string, HashSet<string>> extensions) |
| 162 | + { |
| 163 | + metrics.Add( |
| 164 | + TelemetryDataConstants.DiscoveredExtensions, |
| 165 | + SerializeExtensionDictionary(extensions)); |
| 166 | + } |
| 167 | + |
94 | 168 | /// <summary>
|
95 | 169 | /// Adds the extensions specified to the current set of extensions.
|
96 | 170 | /// </summary>
|
@@ -307,6 +381,27 @@ internal void SetTestExtensionsCacheStatusToTrue<TPluginInfo>() where TPluginInf
|
307 | 381 | }
|
308 | 382 | }
|
309 | 383 |
|
| 384 | + /// <summary> |
| 385 | + /// Gets the cached extensions for the current process. |
| 386 | + /// </summary> |
| 387 | + /// |
| 388 | + /// <returns>A dictionary representing the cached extensions for the current process.</returns> |
| 389 | + internal Dictionary<string, HashSet<string>> GetCachedExtensions() |
| 390 | + { |
| 391 | + var extensions = new Dictionary<string, HashSet<string>>(); |
| 392 | + |
| 393 | + // Write all "known" cached extension. |
| 394 | + AddCachedExtensionToDictionary(extensions, "TestDiscoverers", TestDiscoverers?.Values); |
| 395 | + AddCachedExtensionToDictionary(extensions, "TestExecutors", TestExecutors?.Values); |
| 396 | + AddCachedExtensionToDictionary(extensions, "TestExecutors2", TestExecutors2?.Values); |
| 397 | + AddCachedExtensionToDictionary(extensions, "TestSettingsProviders", TestSettingsProviders?.Values); |
| 398 | + AddCachedExtensionToDictionary(extensions, "TestLoggers", TestLoggers?.Values); |
| 399 | + AddCachedExtensionToDictionary(extensions, "TestHosts", TestHosts?.Values); |
| 400 | + AddCachedExtensionToDictionary(extensions, "DataCollectors", DataCollectors?.Values); |
| 401 | + |
| 402 | + return extensions; |
| 403 | + } |
| 404 | + |
310 | 405 | /// <summary>
|
311 | 406 | /// The invalidate cache of plugin infos.
|
312 | 407 | /// </summary>
|
@@ -390,4 +485,45 @@ private void SetTestExtensionCache<TPluginInfo>(Dictionary<string, TPluginInfo>
|
390 | 485 | }
|
391 | 486 | }
|
392 | 487 |
|
| 488 | + private void AddCachedExtensionToDictionary<T>( |
| 489 | + Dictionary<string, HashSet<string>> extensionDict, |
| 490 | + string extensionType, |
| 491 | + IEnumerable<T> extensions) |
| 492 | + where T : TestPluginInformation |
| 493 | + { |
| 494 | + if (extensions == null) |
| 495 | + { |
| 496 | + return; |
| 497 | + } |
| 498 | + |
| 499 | + extensionDict.Add(extensionType, new HashSet<string>(extensions.Select(e => e.IdentifierData))); |
| 500 | + } |
| 501 | + |
| 502 | + private static string SerializeExtensionDictionary(IDictionary<string, HashSet<string>> extensions) |
| 503 | + { |
| 504 | + StringBuilder sb = new(); |
| 505 | + |
| 506 | + foreach (var kvp in extensions) |
| 507 | + { |
| 508 | + if (kvp.Value?.Count > 0) |
| 509 | + { |
| 510 | + sb.AppendFormat("{0}=[{1}];", kvp.Key, string.Join(",", kvp.Value)); |
| 511 | + } |
| 512 | + } |
| 513 | + |
| 514 | + return sb.ToString(); |
| 515 | + } |
| 516 | + |
| 517 | + private static HashSet<string> MergeSets(HashSet<string> firstSet, HashSet<string> secondSet) |
| 518 | + { |
| 519 | + var mergedSet = new HashSet<string>(firstSet); |
| 520 | + |
| 521 | + // No need to worry about duplicates as the set implementation handles this already. |
| 522 | + foreach (var key in secondSet) |
| 523 | + { |
| 524 | + mergedSet.Add(key); |
| 525 | + } |
| 526 | + |
| 527 | + return mergedSet; |
| 528 | + } |
393 | 529 | }
|
0 commit comments