Skip to content

Commit

Permalink
fix: Output correct case-sensitive message (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveTryon authored Apr 17, 2024
1 parent 5c9396f commit 292fc41
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void LogIndividualFileResults(IEnumerable<FileValidationResult> validFai
return;
}

var caseSensitiveComment = validFailures.Any() && this.osUtils.IsCaseSensitiveOS() ?
var caseSensitiveComment = !validFailures.Any() || this.osUtils.IsCaseSensitiveOS() ?
string.Empty :
"\r\n Note: If the manifest file was originally created using a" +
"\r\n case-sensitive OS, you may also need to validate it" +
Expand Down
60 changes: 60 additions & 0 deletions test/Microsoft.Sbom.Api.Tests/ConsoleCapture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.Sbom.Api.Tests;

using System;
using System.IO;

/// <summary>
/// A simple class to capture console output. Always wrap it in a try/finally block to
/// ensure that the original console output is restored.
/// </summary>
internal class ConsoleCapture
{
private readonly TextWriter oldStdOut = Console.Out;
private readonly TextWriter oldStdError = Console.Error;
private TextWriter? stdOutWriter;
private TextWriter? stdErrWriter;

/// <summary>
/// The content of the captured output to StdOut. Is only valid after the Restore method has been called.
/// </summary>
public string CapturedStdOut { get; private set; } = string.Empty;

/// <summary>
/// The content of the captured output to StdError. Is only valid after the Restore method has been called.
/// </summary>
public string CapturedStdError { get; private set; } = string.Empty;

public ConsoleCapture()
{
stdOutWriter = new StringWriter();
Console.SetOut(stdOutWriter);

stdErrWriter = new StringWriter();
Console.SetError(stdErrWriter);
}

/// <summary>
/// Restores the original console output and sets the Captured* properties
/// </summary>
public void Restore()
{
if (stdOutWriter is not null)
{
CapturedStdOut = stdOutWriter?.ToString() ?? string.Empty;
Console.SetOut(oldStdOut);
stdOutWriter?.Dispose();
stdOutWriter = null;
}

if (stdErrWriter is not null)
{
CapturedStdError = stdErrWriter?.ToString() ?? string.Empty;
Console.SetError(oldStdError);
stdErrWriter?.Dispose();
stdErrWriter = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace Microsoft.Sbom.Workflows;
[TestClass]
public class SbomParserBasedValidationWorkflowTests : ValidationWorkflowTestsBase
{
private const string CaseSensitiveMessageMarker = "case-sensitive";

private readonly Mock<ILogger> mockLogger = new();
private readonly Mock<IOSUtils> mockOSUtils = new();
private readonly Mock<IFileSystemUtilsExtension> fileSystemUtilsExtensionMock = new();
Expand Down Expand Up @@ -183,8 +185,18 @@ public async Task SbomParserBasedValidationWorkflowTests_ReturnsSuccessAndValida
fileSystemMock.Object,
osUtilsMock.Object);

var result = await validator.RunAsync();
Assert.IsTrue(result);
var cc = new ConsoleCapture();

try
{
var result = await validator.RunAsync();
Assert.IsTrue(result);
}
finally
{
cc.Restore();
}

var nodeValidationResults = validationResultGenerator.NodeValidationResults;

var additionalFileErrors = nodeValidationResults.Where(a => a.ErrorType == ErrorType.AdditionalFile).ToList();
Expand All @@ -200,6 +212,8 @@ public async Task SbomParserBasedValidationWorkflowTests_ReturnsSuccessAndValida
var otherErrors = nodeValidationResults.Where(a => a.ErrorType == ErrorType.Other).ToList();
Assert.AreEqual(0, otherErrors.Count);

Assert.IsFalse(cc.CapturedStdOut.Contains(CaseSensitiveMessageMarker), "Case-sensitive marker should not have been output");

configurationMock.VerifyAll();
signValidatorMock.VerifyAll();
fileSystemMock.VerifyAll();
Expand Down Expand Up @@ -328,8 +342,18 @@ public async Task SbomParserBasedValidationWorkflowTests_ReturnsSuccessAndValida
fileSystemMock.Object,
osUtilsMock.Object);

var result = await validator.RunAsync();
Assert.IsFalse(result);
var cc = new ConsoleCapture();

try
{
var result = await validator.RunAsync();
Assert.IsFalse(result);
}
finally
{
cc.Restore();
}

var nodeValidationResults = validationResultGenerator.NodeValidationResults;

var additionalFileErrors = nodeValidationResults.Where(a => a.ErrorType == ErrorType.AdditionalFile).ToList();
Expand All @@ -348,6 +372,8 @@ public async Task SbomParserBasedValidationWorkflowTests_ReturnsSuccessAndValida
Assert.AreEqual(1, otherErrors.Count);
Assert.AreEqual("./child2/grandchild1/file10", otherErrors.First().Path);

Assert.IsTrue(cc.CapturedStdOut.Contains(CaseSensitiveMessageMarker), "Case-sensitive marker should have been output");

configurationMock.VerifyAll();
signValidatorMock.VerifyAll();
fileSystemMock.VerifyAll();
Expand Down

0 comments on commit 292fc41

Please sign in to comment.