diff --git a/src/Microsoft.TestPlatform.Common/Interfaces/Engine/IArtifactProcessingManager.cs b/src/Microsoft.TestPlatform.Common/Interfaces/Engine/IArtifactProcessingManager.cs index c9bff101f8..acac95881f 100644 --- a/src/Microsoft.TestPlatform.Common/Interfaces/Engine/IArtifactProcessingManager.cs +++ b/src/Microsoft.TestPlatform.Common/Interfaces/Engine/IArtifactProcessingManager.cs @@ -14,8 +14,7 @@ internal interface IArtifactProcessingManager /// /// /// A warning to surface to the user when the artifacts directory could not be verified (so nothing was - /// collected), or when attachments were saved to the user-private fallback directory because - /// VSTEST_ARTIFACTS_DIRECTORY was missing or unusable; otherwise . + /// collected), or when there is nothing to report. /// string? CollectArtifacts(TestRunCompleteEventArgs testRunCompleteEventArgs, string runSettingsXml); Task PostProcessArtifactsAsync(); diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/PostProcessing/ArtifactProcessingManager.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/PostProcessing/ArtifactProcessingManager.cs index d40825f108..360c33ed98 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/PostProcessing/ArtifactProcessingManager.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/PostProcessing/ArtifactProcessingManager.cs @@ -234,17 +234,10 @@ private static bool IsFullyQualified(string path) return null; } - // We need to save in case of attachements, we'll show these at the end on console. - // A null collection is treated the same as an empty one, there is nothing to persist either way. - if (testRunCompleteEventArgs.AttachmentSets is not { Count: > 0 }) - { - return null; - } - // A correlation id means the .NET SDK wants artifacts produced here and consumed by itself later, so // a silent fallback away from the directory it asked for is worth a warning the user actually sees, - // not just a trace that needs --diag. It is only reported when there are artifacts to persist, - // otherwise the directory is never used and the warning would be noise. + // not just a trace that needs --diag. Computed as soon as the id is known to be present, rather than + // only once there happens to be something to persist, so it is reported on every such run. string? artifactsDirectoryFallbackWarning = null; if (!_artifactsDirectoryWasProvided) { @@ -256,6 +249,13 @@ private static bool IsFullyQualified(string path) : $"{ArtifactsDirectoryEnvironmentVariableName} environment variable was set but ignored because its value '{rawArtifactsDirectory}' is not a usable absolute path, falling back to a user-private temporary directory."; } + // We need to save in case of attachements, we'll show these at the end on console. + // A null collection is treated the same as an empty one, there is nothing to persist either way. + if (testRunCompleteEventArgs.AttachmentSets is not { Count: > 0 }) + { + return artifactsDirectoryFallbackWarning; + } + TPDebug.Assert(_testSessionProcessArtifactFolder is not null, "_testSessionProcessArtifactFolder is null"); TPDebug.Assert(_processArtifactFolder is not null, "_processArtifactFolder is null"); diff --git a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/PostProcessing/ArtifactProcessingTests.cs b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/PostProcessing/ArtifactProcessingTests.cs index de92114f09..4e8a467a1f 100644 --- a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/PostProcessing/ArtifactProcessingTests.cs +++ b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/PostProcessing/ArtifactProcessingTests.cs @@ -421,23 +421,23 @@ public void CollectArtifacts_ShouldSerializeToDisk() } [TestMethod] - [DataRow(true)] - [DataRow(false)] - public void CollectArtifacts_ShouldNotWarnWhenTheArtifactsDirectoryVariableIsMissingAndThereAreNoAttachments(bool attachmentSetsIsNull) + public void CollectArtifacts_ShouldWarnWhenTheArtifactsDirectoryVariableIsMissing() { - // With no attachments the artifacts directory is never used, so falling back from it is not worth a warning. + // A correlation id with no attachments still has nothing to persist, but the .NET SDK asked for + // artifacts handling by passing the id at all, so the warning must not depend on there being + // something to write this particular run. var testRunCompleteEventArgs = new TestRunCompleteEventArgs(_testRunStatistics.Object, false, false, null, - attachmentSetsIsNull ? null : new Collection(), + new Collection(), TimeSpan.Zero); var result = _artifactProcessingManager.CollectArtifacts(testRunCompleteEventArgs, string.Empty); - Assert.IsNull(result); - _fileHelperMock.Verify(x => x.CreateDirectory(It.IsAny()), Times.Never); - _fileHelperMock.Verify(x => x.WriteAllTextToFile(It.IsAny(), It.IsAny()), Times.Never); + Assert.IsNotNull(result); + Assert.Contains(ArtifactProcessingManager.ArtifactsDirectoryEnvironmentVariableName, result); + Assert.Contains("Microsoft.NET.Test.Sdk", result); } [TestMethod]