Skip to content

Commit 5f8bb21

Browse files
committed
Address PR review comments: improve tfm resolution and null handling
- Use TargetFrameworkAttribute.FrameworkDisplayName before falling back to RuntimeInformation.FrameworkDescription for <tfm> placeholder - Omit <asm> and <tfm> keys from replacements when null instead of using empty string (prevents path traversal with directory templates) - Strengthen subdirectory assertion to verify directory name equals 'HangDump'
1 parent 0aa8d87 commit 5f8bb21

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,22 @@ private async Task TakeDumpAsync(IProcess process, CancellationToken cancellatio
314314
["pname"] = process.Name,
315315
["pid"] = processId,
316316
["os"] = ArtifactNamingHelper.GetOperatingSystemName(),
317-
["asm"] = Assembly.GetEntryAssembly()?.GetName().Name ?? string.Empty,
318-
["tfm"] = TargetFrameworkParser.GetShortTargetFramework(RuntimeInformation.FrameworkDescription)
319-
?? string.Empty,
320317
["time"] = _clock.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss.fffffff", CultureInfo.InvariantCulture),
321318
};
322319

320+
string? asmName = Assembly.GetEntryAssembly()?.GetName().Name;
321+
if (asmName is not null)
322+
{
323+
replacements["asm"] = asmName;
324+
}
325+
326+
string? tfm = TargetFrameworkParser.GetShortTargetFramework(Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkDisplayName)
327+
?? TargetFrameworkParser.GetShortTargetFramework(RuntimeInformation.FrameworkDescription);
328+
if (tfm is not null)
329+
{
330+
replacements["tfm"] = tfm;
331+
}
332+
323333
string pattern = _dumpFileNamePattern ?? $"{process.Name}_%p_hang.dmp";
324334

325335
// First resolve <placeholder> templates, then handle legacy %p pattern for backward compatibility.

test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/HangDumpTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,12 @@ public async Task HangDump_TemplateFileNameWithSubdirectory_CreateDump(string tf
159159
dumpFiles,
160160
$"Expected single dump file in '{resultDirectory}'\n{testHostResult}'");
161161

162-
// The dump file should be in a subdirectory (not directly under resultDirectory)
162+
// The dump file should be in a subdirectory named after the assembly
163163
string? parentDir = Path.GetDirectoryName(dumpFile);
164164
Assert.IsNotNull(parentDir);
165165
Assert.AreNotEqual(resultDirectory, parentDir, "Dump file should be in a subdirectory created from the <asm> placeholder");
166+
Assert.AreEqual("HangDump", Path.GetFileName(parentDir),
167+
$"Subdirectory should be named after the assembly ('HangDump'). Actual: {Path.GetFileName(parentDir)}");
166168
}
167169

168170
[DataRow("Mini")]

0 commit comments

Comments
 (0)