-
Notifications
You must be signed in to change notification settings - Fork 289
Add artifact naming service with template support for consistent naming across extensions #6587
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
d51897b
eb60a65
32a3424
3f373e1
4d2d2f3
ca27413
3ee129e
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 |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # Artifact Naming Service | ||
|
|
||
| The artifact naming service provides a standardized way to generate consistent names and paths for test artifacts across all extensions. | ||
|
|
||
| ## Features | ||
|
|
||
| ### Template-Based Naming | ||
| Use placeholders in angle brackets to create dynamic file names: | ||
|
|
||
| ``` | ||
| <process-name>_<pid>_<id>_hang.dmp | ||
| ``` | ||
| Resolves to: `MyTests_12345_a1b2c3d4_hang.dmp` | ||
|
|
||
| ### Complex Path Templates | ||
| Create structured directory layouts: | ||
|
|
||
| ``` | ||
| <root>/artifacts/<os>/<assembly>/dumps/<process-name>_<pid>_<tfm>_<time>.dmp | ||
| ``` | ||
| Resolves to: `c:/myproject/artifacts/linux/MyTests/dumps/my-child-process_10001_net9.0_2025-09-22T13:49:34.dmp` | ||
|
|
||
| ### Available Placeholders | ||
|
|
||
| | Placeholder | Description | Example | | ||
| |-------------|-------------|---------| | ||
| | `<process-name>` | Name of the process | `MyTests` | | ||
| | `<pid>` | Process ID | `12345` | | ||
| | `<id>` | Short random identifier (8 chars) | `a1b2c3d4` | | ||
| | `<os>` | Operating system | `windows`, `linux`, `macos` | | ||
| | `<assembly>` | Assembly name | `MyTests` | | ||
|
||
| | `<tfm>` | Target framework moniker | `net9.0`, `net8.0` | | ||
| | `<time>` | Timestamp (1-second precision) | `2025-09-22T13:49:34` | | ||
| | `<root>` | Project root directory | Found via solution/git/working dir | | ||
|
|
||
| ### Backward Compatibility | ||
| Legacy patterns are still supported: | ||
|
|
||
| ```csharp | ||
| // Old pattern | ||
| "myfile_%p.dmp" | ||
|
|
||
| // Works with legacy support | ||
| service.ResolveTemplateWithLegacySupport("myfile_%p.dmp", | ||
| legacyReplacements: new Dictionary<string, string> { ["%p"] = "12345" }); | ||
| ``` | ||
|
|
||
| ### Custom Replacements | ||
| Override default values for specific scenarios: | ||
|
|
||
| ```csharp | ||
| // When dumping a different process than the test host | ||
| var customReplacements = new Dictionary<string, string> | ||
| { | ||
| ["process-name"] = "Notepad", | ||
| ["pid"] = "1111" | ||
| }; | ||
|
|
||
| string result = service.ResolveTemplate("<process-name>_<pid>.dmp", customReplacements); | ||
| // Result: "Notepad_1111.dmp" | ||
| ``` | ||
|
|
||
| ## Usage in Extensions | ||
|
|
||
| Extensions can use the service through dependency injection: | ||
|
|
||
| ```csharp | ||
| public class MyExtension | ||
| { | ||
| private readonly IArtifactNamingService _artifactNamingService; | ||
|
|
||
| public MyExtension(IServiceProvider serviceProvider) | ||
| { | ||
| _artifactNamingService = serviceProvider.GetArtifactNamingService(); | ||
| } | ||
|
|
||
| public void CreateArtifact(string template) | ||
| { | ||
| string fileName = _artifactNamingService.ResolveTemplate(template); | ||
| // Use fileName for artifact creation | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Hang Dump Integration | ||
|
|
||
| The hang dump extension now uses the artifact naming service and supports both legacy and modern patterns: | ||
|
|
||
| ```bash | ||
| # Legacy pattern (still works) | ||
| --hangdump-filename "mydump_%p.dmp" | ||
|
|
||
| # New template pattern | ||
| --hangdump-filename "<process-name>_<pid>_<id>_hang.dmp" | ||
|
|
||
| # Complex path template | ||
| --hangdump-filename "<root>/dumps/<os>/<process-name>_<pid>_<time>.dmp" | ||
| ``` | ||
|
|
||
| This provides consistent artifact naming across all extensions while maintaining backward compatibility. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| // 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.Globalization; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| using Microsoft.Testing.Platform.Helpers; | ||
|
|
||
| namespace Microsoft.Testing.Platform.Services; | ||
|
|
||
| internal sealed class ArtifactNamingService : IArtifactNamingService | ||
| { | ||
| private readonly ITestApplicationModuleInfo _testApplicationModuleInfo; | ||
| private readonly IEnvironment _environment; | ||
| private readonly IClock _clock; | ||
| private readonly IProcessHandler _processHandler; | ||
|
|
||
| private static readonly Regex TemplateFieldRegex = new(@"<([^>]+)>", RegexOptions.Compiled); | ||
|
|
||
| public ArtifactNamingService( | ||
| ITestApplicationModuleInfo testApplicationModuleInfo, | ||
| IEnvironment environment, | ||
| IClock clock, | ||
| IProcessHandler processHandler) | ||
| { | ||
| _testApplicationModuleInfo = testApplicationModuleInfo; | ||
| _environment = environment; | ||
| _clock = clock; | ||
| _processHandler = processHandler; | ||
| } | ||
|
|
||
| public string ResolveTemplate(string template, IDictionary<string, string>? customReplacements = null) | ||
| { | ||
| ArgumentGuard.IsNotNullOrEmpty(template); | ||
|
Check failure on line 35 in src/Platform/Microsoft.Testing.Platform/Services/ArtifactNamingService.cs
|
||
|
|
||
| var defaultReplacements = GetDefaultReplacements(); | ||
| var allReplacements = MergeReplacements(defaultReplacements, customReplacements); | ||
|
|
||
| return TemplateFieldRegex.Replace(template, match => | ||
| { | ||
| string fieldName = match.Groups[1].Value; | ||
| return allReplacements.TryGetValue(fieldName, out string? value) ? value : match.Value; | ||
| }); | ||
| } | ||
|
|
||
| public string ResolveTemplateWithLegacySupport(string template, IDictionary<string, string>? customReplacements = null, IDictionary<string, string>? legacyReplacements = null) | ||
| { | ||
| ArgumentGuard.IsNotNullOrEmpty(template); | ||
|
Check failure on line 49 in src/Platform/Microsoft.Testing.Platform/Services/ArtifactNamingService.cs
|
||
|
|
||
| // First apply legacy replacements | ||
| string processedTemplate = template; | ||
| if (legacyReplacements is not null) | ||
| { | ||
| foreach (var (legacyPattern, replacement) in legacyReplacements) | ||
| { | ||
| processedTemplate = processedTemplate.Replace(legacyPattern, replacement); | ||
| } | ||
| } | ||
|
|
||
| // Then apply modern template resolution | ||
| return ResolveTemplate(processedTemplate, customReplacements); | ||
| } | ||
|
|
||
| private Dictionary<string, string> GetDefaultReplacements() | ||
| { | ||
| var replacements = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| // Assembly info | ||
| string? assemblyName = _testApplicationModuleInfo.TryGetAssemblyName(); | ||
| if (!RoslynString.IsNullOrEmpty(assemblyName)) | ||
| { | ||
| replacements["assembly"] = assemblyName; | ||
| } | ||
|
|
||
| // Process info | ||
| using var currentProcess = _processHandler.GetCurrentProcess(); | ||
| replacements["pid"] = currentProcess.Id.ToString(CultureInfo.InvariantCulture); | ||
| replacements["process-name"] = currentProcess.ProcessName; | ||
|
Check failure on line 79 in src/Platform/Microsoft.Testing.Platform/Services/ArtifactNamingService.cs
|
||
|
|
||
| // OS info | ||
| replacements["os"] = GetOperatingSystemName(); | ||
|
|
||
| // Target framework info | ||
| string tfm = GetTargetFrameworkMoniker(); | ||
| if (!RoslynString.IsNullOrEmpty(tfm)) | ||
| { | ||
| replacements["tfm"] = tfm; | ||
| } | ||
|
|
||
| // Time info (precision to 1 second) | ||
| replacements["time"] = _clock.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture); | ||
|
|
||
| // Random ID for uniqueness | ||
| replacements["id"] = GenerateShortId(); | ||
|
|
||
| // Root directory | ||
| replacements["root"] = GetRootDirectory(); | ||
|
|
||
| return replacements; | ||
| } | ||
|
|
||
| private static Dictionary<string, string> MergeReplacements(Dictionary<string, string> defaultReplacements, IDictionary<string, string>? customReplacements) | ||
| { | ||
| if (customReplacements is null || customReplacements.Count == 0) | ||
| { | ||
| return defaultReplacements; | ||
| } | ||
|
|
||
| var merged = new Dictionary<string, string>(defaultReplacements, StringComparer.OrdinalIgnoreCase); | ||
| foreach (var (key, value) in customReplacements) | ||
| { | ||
| merged[key] = value; | ||
| } | ||
|
|
||
| return merged; | ||
| } | ||
|
|
||
| private static string GetOperatingSystemName() | ||
| { | ||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
| { | ||
| return "windows"; | ||
| } | ||
|
|
||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
| { | ||
| return "linux"; | ||
| } | ||
|
|
||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
| { | ||
| return "macos"; | ||
| } | ||
|
|
||
| return "unknown"; | ||
| } | ||
|
|
||
| private static string GetTargetFrameworkMoniker() | ||
| { | ||
| // Extract TFM from current runtime | ||
| string frameworkDescription = RuntimeInformation.FrameworkDescription; | ||
|
|
||
| if (frameworkDescription.Contains(".NET Core")) | ||
| { | ||
| // Try to extract version from .NET Core description | ||
| var match = Regex.Match(frameworkDescription, @"\.NET Core (\d+\.\d+)"); | ||
| if (match.Success) | ||
| { | ||
| return $"netcoreapp{match.Groups[1].Value}"; | ||
| } | ||
| } | ||
| else if (frameworkDescription.Contains(".NET ")) | ||
| { | ||
| // Try to extract version from .NET 5+ description | ||
| var match = Regex.Match(frameworkDescription, @"\.NET (\d+\.\d+)"); | ||
| if (match.Success) | ||
| { | ||
| string version = match.Groups[1].Value; | ||
| return version switch | ||
| { | ||
| "5.0" => "net5.0", | ||
| "6.0" => "net6.0", | ||
| "7.0" => "net7.0", | ||
| "8.0" => "net8.0", | ||
| "9.0" => "net9.0", | ||
| "10.0" => "net10.0", | ||
| _ => $"net{version}" | ||
| }; | ||
| } | ||
| } | ||
| else if (frameworkDescription.Contains(".NET Framework")) | ||
| { | ||
| // Try to extract version from .NET Framework description | ||
| var match = Regex.Match(frameworkDescription, @"\.NET Framework (\d+\.\d+)"); | ||
| if (match.Success) | ||
| { | ||
| return $"net{match.Groups[1].Value.Replace(".", "")}"; | ||
| } | ||
| } | ||
|
|
||
| return Environment.Version.ToString(); | ||
| } | ||
|
|
||
| private static string GenerateShortId() | ||
| { | ||
| return Guid.NewGuid().ToString("N")[..8]; | ||
| } | ||
|
|
||
| private string GetRootDirectory() | ||
| { | ||
| string currentDirectory = _testApplicationModuleInfo.GetCurrentTestApplicationDirectory(); | ||
|
|
||
| // Try to find solution root, git root, or working directory | ||
| string? rootDirectory = FindSolutionRoot(currentDirectory) | ||
| ?? FindGitRoot(currentDirectory) | ||
| ?? currentDirectory; | ||
|
|
||
| return rootDirectory; | ||
| } | ||
|
|
||
| private static string? FindSolutionRoot(string startDirectory) | ||
| { | ||
| string? directory = startDirectory; | ||
| while (directory is not null) | ||
| { | ||
| if (Directory.GetFiles(directory, "*.sln").Length > 0) | ||
| { | ||
| return directory; | ||
| } | ||
|
|
||
| directory = Directory.GetParent(directory)?.FullName; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static string? FindGitRoot(string startDirectory) | ||
| { | ||
| string? directory = startDirectory; | ||
| while (directory is not null) | ||
| { | ||
| if (Directory.Exists(Path.Combine(directory, ".git"))) | ||
| { | ||
| return directory; | ||
| } | ||
|
|
||
| directory = Directory.GetParent(directory)?.FullName; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.