-
Notifications
You must be signed in to change notification settings - Fork 308
Align CTRF report writer overwrite behavior and add public IArtifactNamingService #9780
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/Platform/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,6 @@ | ||
| #nullable enable | ||
| Microsoft.Testing.Platform.Services.ArtifactFileNameSanitizer | ||
| Microsoft.Testing.Platform.Services.ArtifactNamingService | ||
| Microsoft.Testing.Platform.Services.ArtifactNamingService.ArtifactNamingService(Microsoft.Testing.Platform.Services.ITestApplicationModuleInfo! testApplicationModuleInfo, Microsoft.Testing.Platform.Helpers.IEnvironment! environment, Microsoft.Testing.Platform.Helpers.IClock! clock) -> void | ||
| Microsoft.Testing.Platform.Services.ArtifactNamingService.ResolveFileName(string! template) -> string! | ||
| static Microsoft.Testing.Platform.Services.ArtifactFileNameSanitizer.ReplaceInvalidFileNameChars(string! fileName) -> string! | ||
3 changes: 3 additions & 0 deletions
3
src/Platform/Microsoft.Testing.Platform/PublicAPI/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| #nullable enable | ||
| Microsoft.Testing.Platform.Services.IArtifactNamingService | ||
| Microsoft.Testing.Platform.Services.IArtifactNamingService.ResolveFileName(string! template) -> string! | ||
| static Microsoft.Testing.Platform.Services.ServiceProviderExtensions.GetArtifactNamingService(this System.IServiceProvider! serviceProvider) -> Microsoft.Testing.Platform.Services.IArtifactNamingService! |
55 changes: 55 additions & 0 deletions
55
src/Platform/Microsoft.Testing.Platform/Services/ArtifactFileNameSanitizer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.Testing.Platform.Helpers; | ||
|
|
||
| namespace Microsoft.Testing.Platform.Services; | ||
|
|
||
| // Mirrors the extension-side Microsoft.Testing.Extensions.ReportFileNameSanitizer (linked source in | ||
| // src/Platform/SharedExtensionHelpers/ReportFileNameSanitizer.cs). The extension copy has SHIPPED | ||
| // InternalAPI entries in five extensions, so it cannot be moved without churn/risk. This core-internal | ||
| // copy exists pending future consolidation of the two. | ||
| internal static partial class ArtifactFileNameSanitizer | ||
| { | ||
| private const char ReplacementChar = '_'; | ||
| private static readonly Regex ReservedFileNamesRegex = BuildReservedFileNameRegex(); | ||
|
|
||
| internal static string ReplaceInvalidFileNameChars(string fileName) | ||
| { | ||
| char[] result = new char[fileName.Length]; | ||
|
|
||
| for (int i = 0; i < fileName.Length; ++i) | ||
| { | ||
| result[i] = IsInvalidFileNameChar(fileName[i]) ? ReplacementChar : fileName[i]; | ||
| } | ||
|
|
||
| string replaced = new string(result).TrimEnd(); | ||
| ArgumentGuard.Ensure(replaced.Length > 0, nameof(fileName), $"File name {fileName} is empty after sanitizing invalid characters."); | ||
|
|
||
| if (IsReservedFileName(replaced)) | ||
| { | ||
| replaced = ReplacementChar + replaced; // Cannot add to the end because it can have extensions. | ||
| } | ||
|
|
||
| return replaced; | ||
| } | ||
|
|
||
| private static bool IsInvalidFileNameChar(char c) | ||
| => c is < ' ' or '"' or '<' or '>' or '|' or ':' or '*' or '?' or '\\' or '/' or '@' or '(' or ')' or '^' or ' '; | ||
|
|
||
| private static bool IsReservedFileName(string fileName) => | ||
| // CreateFile: | ||
| // The following reserved device names cannot be used as the name of a file: | ||
| // CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, | ||
| // LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. | ||
| // Also avoid these names followed by an extension, for example, NUL.tx7. | ||
| // Windows NT: CLOCK$ is also a reserved device name. | ||
| ReservedFileNamesRegex.IsMatch(fileName); | ||
|
|
||
| #if NET7_0_OR_GREATER | ||
| [GeneratedRegex(@"(?i:^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9]|CLOCK\$)(\..*)?)$", RegexOptions.None, "en-150")] | ||
| private static partial Regex BuildReservedFileNameRegex(); | ||
| #else | ||
| private static Regex BuildReservedFileNameRegex() => new(@"(?i:^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9]|CLOCK\$)(\..*)?)$"); | ||
| #endif | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/Platform/Microsoft.Testing.Platform/Services/ArtifactNamingService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| 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; | ||
|
|
||
| public ArtifactNamingService(ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, IClock clock) | ||
| { | ||
| _testApplicationModuleInfo = testApplicationModuleInfo; | ||
| _environment = environment; | ||
| _clock = clock; | ||
| } | ||
|
|
||
| public string ResolveFileName(string template) | ||
| { | ||
| string processName = Path.GetFileNameWithoutExtension(_testApplicationModuleInfo.GetCurrentTestApplicationFullPath()); | ||
| string processId = _environment.ProcessId.ToString(CultureInfo.InvariantCulture); | ||
| Dictionary<string, string> replacements = ArtifactNamingHelper.GetStandardReplacements(processName, processId, _clock.UtcNow); | ||
| string resolved = ArtifactNamingHelper.ResolveTemplate(template, replacements); | ||
|
|
||
| string directoryPart = Path.GetDirectoryName(resolved) ?? string.Empty; | ||
| string sanitized = ArtifactFileNameSanitizer.ReplaceInvalidFileNameChars(Path.GetFileName(resolved)); | ||
|
|
||
| return directoryPart.Length == 0 ? sanitized : Path.Combine(directoryPart, sanitized); | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/Platform/Microsoft.Testing.Platform/Services/IArtifactNamingService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| namespace Microsoft.Testing.Platform.Services; | ||
|
|
||
| /// <summary> | ||
| /// Provides file-name templating for artifacts produced by the test platform and its extensions. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The service expands the standard placeholders and sanitizes the resulting leaf file name so it is | ||
| /// valid on the current operating system. It is registered as a common service and can be resolved | ||
| /// through <see cref="ServiceProviderExtensions.GetArtifactNamingService(System.IServiceProvider)"/>. | ||
| /// </remarks> | ||
| public interface IArtifactNamingService | ||
| { | ||
| /// <summary> | ||
| /// Resolves a file-name template into a concrete file name. | ||
| /// </summary> | ||
| /// <param name="template"> | ||
| /// The template to resolve. The following placeholders are expanded: | ||
| /// <list type="bullet"> | ||
| /// <item><description><c>{pname}</c> — the current test application process name.</description></item> | ||
| /// <item><description><c>{pid}</c> — the current process id.</description></item> | ||
| /// <item><description><c>{asm}</c> — the entry assembly name.</description></item> | ||
| /// <item><description><c>{tfm}</c> — the short target framework moniker (including platform).</description></item> | ||
| /// <item><description><c>{arch}</c> — the process architecture.</description></item> | ||
| /// <item><description><c>{time}</c> — the current UTC timestamp.</description></item> | ||
| /// </list> | ||
| /// Unknown placeholders are preserved as-is. Any directory portion of the template is preserved | ||
| /// while only the leaf file name is sanitized. | ||
| /// </param> | ||
| /// <returns>The resolved file name, with the leaf sanitized to be valid for the current file system.</returns> | ||
| string ResolveFileName(string template); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The base branch (a946ef7) introduced
BaseSerializer.ReadFieldsandBaseSerializer.WriteListPayload<T>asprotected staticmethods but they were never declared here, causing RS0051 errors across all 3 TFMs.