-
Notifications
You must be signed in to change notification settings - Fork 291
Copy per-test result files to results directory #7452
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 all commits
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,80 @@ | ||||||||||||||
| // 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.Configurations; | ||||||||||||||
| using Microsoft.Testing.Platform.Extensions; | ||||||||||||||
| using Microsoft.Testing.Platform.Extensions.Messages; | ||||||||||||||
| using Microsoft.Testing.Platform.Helpers; | ||||||||||||||
| using Microsoft.Testing.Platform.Resources; | ||||||||||||||
|
|
||||||||||||||
| namespace Microsoft.Testing.Platform.TestHost; | ||||||||||||||
|
|
||||||||||||||
| /// <summary> | ||||||||||||||
| /// Copies per-test file artifacts to the test results directory so that the | ||||||||||||||
| /// results directory is self-contained. | ||||||||||||||
| /// </summary> | ||||||||||||||
| internal sealed class FileArtifactCopyDataConsumer : IDataConsumer | ||||||||||||||
|
Member
Author
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. Maybe this should be enabled/disabled via CLI or option? |
||||||||||||||
| { | ||||||||||||||
| private readonly IConfiguration _configuration; | ||||||||||||||
|
|
||||||||||||||
| public FileArtifactCopyDataConsumer(IConfiguration configuration) | ||||||||||||||
| => _configuration = configuration; | ||||||||||||||
|
|
||||||||||||||
| public Type[] DataTypesConsumed => [typeof(TestNodeUpdateMessage)]; | ||||||||||||||
|
|
||||||||||||||
| public string Uid => nameof(FileArtifactCopyDataConsumer); | ||||||||||||||
|
|
||||||||||||||
| public string Version => AppVersion.DefaultSemVer; | ||||||||||||||
|
|
||||||||||||||
| public string DisplayName => PlatformResources.FileArtifactCopyDataConsumerDisplayName; | ||||||||||||||
|
|
||||||||||||||
| public string Description => PlatformResources.FileArtifactCopyDataConsumerDescription; | ||||||||||||||
|
|
||||||||||||||
| public Task<bool> IsEnabledAsync() => Task.FromResult(true); | ||||||||||||||
|
|
||||||||||||||
| public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken) | ||||||||||||||
| { | ||||||||||||||
| if (value is not TestNodeUpdateMessage message) | ||||||||||||||
| { | ||||||||||||||
| return Task.CompletedTask; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| FileArtifactProperty[] artifacts = message.TestNode.Properties.OfType<FileArtifactProperty>(); | ||||||||||||||
| if (artifacts.Length == 0) | ||||||||||||||
| { | ||||||||||||||
| return Task.CompletedTask; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| string resultsDirectory = _configuration.GetTestResultDirectory(); | ||||||||||||||
|
|
||||||||||||||
| foreach (FileArtifactProperty artifact in artifacts) | ||||||||||||||
| { | ||||||||||||||
| CopyFileToResultsDirectory(artifact.FileInfo, resultsDirectory); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return Task.CompletedTask; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| private static void CopyFileToResultsDirectory(FileInfo file, string resultsDirectory) | ||||||||||||||
| { | ||||||||||||||
| // If the file is already under the results directory, skip. | ||||||||||||||
| string fullResultsDirectory = Path.GetFullPath(resultsDirectory); | ||||||||||||||
| if (file.FullName.StartsWith(fullResultsDirectory, StringComparison.OrdinalIgnoreCase)) | ||||||||||||||
|
||||||||||||||
| { | ||||||||||||||
| return; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (!Directory.Exists(resultsDirectory)) | ||||||||||||||
|
Member
Author
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. If we go with this solution, I'll use file system wrapper instead to ease testing |
||||||||||||||
| { | ||||||||||||||
| Directory.CreateDirectory(resultsDirectory); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| string destination = Path.Combine(resultsDirectory, file.Name); | ||||||||||||||
| for (int nameCounter = 1; File.Exists(destination) && nameCounter <= 10; nameCounter++) | ||||||||||||||
| { | ||||||||||||||
| destination = Path.Combine(resultsDirectory, $"{Path.GetFileNameWithoutExtension(file.Name)}_{nameCounter}{Path.GetExtension(file.Name)}"); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
||||||||||||||
| if (File.Exists(destination)) | |
| { | |
| throw new IOException($"Failed to copy file '{file.FullName}' to results directory '{resultsDirectory}' because a unique destination filename could not be determined after 10 attempts."); | |
| } |
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.
After the bounded loop completes (when nameCounter exceeds 10), the code calls CopyFileAsync even if a file still exists at the destination path. While CopyFileAsync creates a new file with FileMode.Create (which will overwrite), it would be better to verify that a unique filename was found or handle the case explicitly for clarity and consistency with error handling.