Skip to content
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

Reduce console output when running tests and update more tests to use ITestOutputHelper #5901

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using NuGet.CommandLine.XPlat.Utility;
using NuGet.Configuration;
Expand All @@ -16,10 +17,19 @@ namespace NuGet.CommandLine.XPlat.ListPackage
internal class ListPackageConsoleRenderer : IReportRenderer
{
protected List<ReportProblem> _problems = new();
private readonly TextWriter _consoleOut;
private readonly TextWriter _consoleError;

public ListPackageConsoleRenderer()
: this(Console.Out, Console.Error)
{ }

internal ListPackageConsoleRenderer(TextWriter consoleOut, TextWriter consoleError)
{
_consoleOut = consoleOut;
_consoleError = consoleError;
}

public void AddProblem(ProblemType problemType, string text)
{
_problems.Add(new ReportProblem(problemType, string.Empty, text));
Expand All @@ -38,40 +48,40 @@ public void Render(ListPackageReportModel listPackageReportModel)
private void WriteToConsole(ListPackageReportModel listPackageReportModel)
{
// Print non-project related problems first.
PrintProblems(_problems, listPackageReportModel.ListPackageArgs);
PrintProblems(_consoleOut, _consoleError, _problems, listPackageReportModel.ListPackageArgs);

if (_problems?.Any(p => p.ProblemType == ProblemType.Error) == true)
{
return;
}

WriteSources(listPackageReportModel.ListPackageArgs);
WriteProjects(listPackageReportModel.Projects, listPackageReportModel.ListPackageArgs);
WriteSources(_consoleOut, listPackageReportModel.ListPackageArgs);
WriteProjects(_consoleOut, _consoleError, listPackageReportModel.Projects, listPackageReportModel.ListPackageArgs);

// Print a legend message for auto-reference markers used
if (listPackageReportModel.Projects.Any(p => p.AutoReferenceFound))
{
Console.WriteLine(Strings.ListPkg_AutoReferenceDescription);
_consoleOut.WriteLine(Strings.ListPkg_AutoReferenceDescription);
}
}

private static void WriteSources(ListPackageArgs listPackageArgs)
private static void WriteSources(TextWriter consoleOut, ListPackageArgs listPackageArgs)
{
// Print sources, but not for generic list (which is offline)
if (listPackageArgs.ReportType != ReportType.Default)
{
Console.WriteLine();
Console.WriteLine(Strings.ListPkg_SourcesUsedDescription);
PrintSources(listPackageArgs.PackageSources);
Console.WriteLine();
consoleOut.WriteLine();
consoleOut.WriteLine(Strings.ListPkg_SourcesUsedDescription);
PrintSources(consoleOut, listPackageArgs.PackageSources);
consoleOut.WriteLine();
}
}

private static void WriteProjects(List<ListPackageProjectModel> projects, ListPackageArgs listPackageArgs)
private static void WriteProjects(TextWriter consoleOut, TextWriter consoleError, List<ListPackageProjectModel> projects, ListPackageArgs listPackageArgs)
{
foreach (ListPackageProjectModel project in projects)
{
PrintProblems(project.ProjectProblems, listPackageArgs);
PrintProblems(consoleOut, consoleError, project.ProjectProblems, listPackageArgs);

if (project.ProjectProblems?.Any(p => p.ProblemType == ProblemType.Error) == true)
{
Expand All @@ -80,7 +90,7 @@ private static void WriteProjects(List<ListPackageProjectModel> projects, ListPa

if (project.TargetFrameworkPackages == null)
{
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.ListPkg_NoPackagesFoundForFrameworks, project.ProjectName));
consoleOut.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.ListPkg_NoPackagesFoundForFrameworks, project.ProjectName));
continue;
}

Expand All @@ -93,13 +103,13 @@ private static void WriteProjects(List<ListPackageProjectModel> projects, ListPa
switch (listPackageArgs.ReportType)
{
case ReportType.Outdated:
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.ListPkg_NoUpdatesForProject, project.ProjectName));
consoleOut.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.ListPkg_NoUpdatesForProject, project.ProjectName));
break;
case ReportType.Deprecated:
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.ListPkg_NoDeprecatedPackagesForProject, project.ProjectName));
consoleOut.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.ListPkg_NoDeprecatedPackagesForProject, project.ProjectName));
break;
case ReportType.Vulnerable:
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.ListPkg_NoVulnerablePackagesForProject, project.ProjectName));
consoleOut.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.ListPkg_NoVulnerablePackagesForProject, project.ProjectName));
break;
}
}
Expand All @@ -110,7 +120,7 @@ private static void WriteProjects(List<ListPackageProjectModel> projects, ListPa
continue;
}

Console.WriteLine(GetProjectHeader(project.ProjectName, listPackageArgs));
consoleOut.WriteLine(GetProjectHeader(project.ProjectName, listPackageArgs));

foreach (ListPackageReportFrameworkPackage frameworkPackages in project.TargetFrameworkPackages)
{
Expand All @@ -126,16 +136,16 @@ private static void WriteProjects(List<ListPackageProjectModel> projects, ListPa
switch (listPackageArgs.ReportType)
{
case ReportType.Outdated:
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, " [{0}]: " + Strings.ListPkg_NoUpdatesForFramework, frameworkPackages.Framework));
consoleOut.WriteLine(string.Format(CultureInfo.CurrentCulture, " [{0}]: " + Strings.ListPkg_NoUpdatesForFramework, frameworkPackages.Framework));
break;
case ReportType.Deprecated:
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, " [{0}]: " + Strings.ListPkg_NoDeprecationsForFramework, frameworkPackages.Framework));
consoleOut.WriteLine(string.Format(CultureInfo.CurrentCulture, " [{0}]: " + Strings.ListPkg_NoDeprecationsForFramework, frameworkPackages.Framework));
break;
case ReportType.Vulnerable:
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, " [{0}]: " + Strings.ListPkg_NoVulnerabilitiesForFramework, frameworkPackages.Framework));
consoleOut.WriteLine(string.Format(CultureInfo.CurrentCulture, " [{0}]: " + Strings.ListPkg_NoVulnerabilitiesForFramework, frameworkPackages.Framework));
break;
case ReportType.Default:
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, " [{0}]: " + Strings.ListPkg_NoPackagesForFramework, frameworkPackages.Framework));
consoleOut.WriteLine(string.Format(CultureInfo.CurrentCulture, " [{0}]: " + Strings.ListPkg_NoPackagesForFramework, frameworkPackages.Framework));
break;
}

Expand All @@ -145,7 +155,7 @@ private static void WriteProjects(List<ListPackageProjectModel> projects, ListPa
{
// Print name of the framework
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, " [{0}]: ", frameworkPackages.Framework));
consoleOut.WriteLine(string.Format(CultureInfo.CurrentCulture, " [{0}]: ", frameworkPackages.Framework));
Console.ResetColor();

// Print top-level packages
Expand Down Expand Up @@ -176,15 +186,15 @@ private static void WriteProjects(List<ListPackageProjectModel> projects, ListPa
}
}

private static void PrintSources(IEnumerable<PackageSource> packageSources)
private static void PrintSources(TextWriter consoleOut, IEnumerable<PackageSource> packageSources)
{
foreach (var source in packageSources)
{
Console.WriteLine(" " + source.Source);
consoleOut.WriteLine(" " + source.Source);
}
}

private static void PrintProblems(IEnumerable<ReportProblem> problems, ListPackageArgs listPackageArgs)
private static void PrintProblems(TextWriter consoleOut, TextWriter consoleError, IEnumerable<ReportProblem> problems, ListPackageArgs listPackageArgs)
{
if (problems == null)
{
Expand All @@ -199,8 +209,8 @@ private static void PrintProblems(IEnumerable<ReportProblem> problems, ListPacka
listPackageArgs.Logger.LogWarning(problem.Text);
break;
case ProblemType.Error:
Console.Error.WriteLine(problem.Text);
Console.WriteLine();
consoleError.WriteLine(problem.Text);
consoleOut.WriteLine();
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private ListPackageJsonRenderer()

public ListPackageJsonRenderer(TextWriter textWriter = null)
{
_writer = textWriter != null ? textWriter : Console.Out;
_writer = textWriter ?? Console.Out;
}

public void AddProblem(ProblemType problemType, string text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
packageA100);
var projectA = XPlatTestUtils.CreateProject("ProjectA", pathContext, packageA100, "net472");
var workingDirectory = Path.Combine(pathContext.SolutionRoot, projectA.ProjectName);
SelfSignedCertificateMockServer tcpListenerServer = new SelfSignedCertificateMockServer(packageSourceDirectory);
SelfSignedCertificateMockServer tcpListenerServer = new SelfSignedCertificateMockServer(packageSourceDirectory, _testOutputHelper);
var serverTask = tcpListenerServer.StartServerAsync();
pathContext.Settings.AddSource("https-feed", $"{tcpListenerServer.URI}v3/index.json", "disableTLSCertificateValidation", "true");

Expand All @@ -59,7 +59,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
packageB100);
var projectB = XPlatTestUtils.CreateProject("ProjectB", pathContext, packageB100, "net472");
var workingDirectory = Path.Combine(pathContext.SolutionRoot, projectB.ProjectName);
SelfSignedCertificateMockServer tcpListenerServer = new SelfSignedCertificateMockServer(packageSourceDirectory);
SelfSignedCertificateMockServer tcpListenerServer = new SelfSignedCertificateMockServer(packageSourceDirectory, _testOutputHelper);
var serverTask = tcpListenerServer.StartServerAsync();
pathContext.Settings.AddSource("https-feed", $"{tcpListenerServer.URI}v3/index.json");

Expand All @@ -81,8 +81,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
packageB100);
var projectB = XPlatTestUtils.CreateProject("ProjectB", pathContext, packageB100, "net472");
var workingDirectory = Path.Combine(pathContext.SolutionRoot, projectB.ProjectName);
SelfSignedCertificateMockServer tcpListenerServer1 = new SelfSignedCertificateMockServer(packageSourceDirectory);
SelfSignedCertificateMockServer tcpListenerServer2 = new SelfSignedCertificateMockServer(packageSourceDirectory);
SelfSignedCertificateMockServer tcpListenerServer1 = new SelfSignedCertificateMockServer(packageSourceDirectory, _testOutputHelper);
SelfSignedCertificateMockServer tcpListenerServer2 = new SelfSignedCertificateMockServer(packageSourceDirectory, _testOutputHelper);
var serverTask = tcpListenerServer1.StartServerAsync();
var serverTask2 = tcpListenerServer2.StartServerAsync();
pathContext.Settings.AddSource("https-feed1", $"{tcpListenerServer1.URI}v3/index.json");
Expand All @@ -107,8 +107,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
packageB100);
var projectB = XPlatTestUtils.CreateProject("ProjectB", pathContext, packageB100, "net472");
var workingDirectory = Path.Combine(pathContext.SolutionRoot, projectB.ProjectName);
SelfSignedCertificateMockServer tcpListenerServer1 = new SelfSignedCertificateMockServer(packageSourceDirectory);
SelfSignedCertificateMockServer tcpListenerServer2 = new SelfSignedCertificateMockServer(packageSourceDirectory);
SelfSignedCertificateMockServer tcpListenerServer1 = new SelfSignedCertificateMockServer(packageSourceDirectory, _testOutputHelper);
SelfSignedCertificateMockServer tcpListenerServer2 = new SelfSignedCertificateMockServer(packageSourceDirectory, _testOutputHelper);
var serverTask = tcpListenerServer1.StartServerAsync();
var serverTask2 = tcpListenerServer2.StartServerAsync();
pathContext.Settings.AddSource("https-feed1", $"{tcpListenerServer1.URI}v3/index.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Xunit;
using Xunit.Abstractions;

namespace NuGet.XPlat.FuncTest
{
public class BasicLoggingTests
{
private readonly ITestOutputHelper _testOutputHelper;

public BasicLoggingTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public void BasicLogging_NoParams_ExitCode()
{
// Arrange
var log = new TestCommandOutputLogger();
var log = new TestCommandOutputLogger(_testOutputHelper);

var args = new string[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.CommandLineUtils;
Expand Down Expand Up @@ -211,14 +212,18 @@ await SimpleTestPackageUtility.CreatePackagesAsync(
await RestoreProjectsAsync(pathContext, projectA, projectB, _testOutputHelper);

// Act
var output = new StringBuilder();
var error = new StringBuilder();
using TextWriter consoleOut = new StringWriter(output);
using TextWriter consoleError = new StringWriter(error);
var logger = new TestLogger(_testOutputHelper);
ListPackageCommandRunner listPackageCommandRunner = new();
var packageRefArgs = new ListPackageArgs(
path: Path.Combine(pathContext.SolutionRoot, "solution.sln"),
packageSources: [new(mockServer.ServiceIndexUri)],
frameworks: ["net6.0"],
reportType: ReportType.Vulnerable,
renderer: new ListPackageConsoleRenderer(),
renderer: new ListPackageConsoleRenderer(consoleOut, consoleError),
includeTransitive: false,
prerelease: false,
highestPatch: false,
Expand Down Expand Up @@ -294,7 +299,7 @@ private void VerifyCommand(Action<string, Mock<IListPackageCommandRunner>, Comma
File.WriteAllText(projectPath, string.Empty);

var logLevel = LogLevel.Information;
var logger = new TestCommandOutputLogger();
var logger = new TestCommandOutputLogger(_testOutputHelper);
var testApp = new CommandLineApplication();
var mockCommandRunner = new Mock<IListPackageCommandRunner>();
mockCommandRunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
using NuGet.CommandLine.XPlat;
using NuGet.Common;
using NuGet.Test.Utility;
using Xunit.Abstractions;

namespace NuGet.XPlat.FuncTest
{
internal class TestCommandOutputLogger : CommandOutputLogger
{
private readonly bool _observeLogLevel;

public TestLogger Logger { get; set; } = new TestLogger();
public TestLogger Logger { get; set; }

public TestCommandOutputLogger(bool observeLogLevel = false)
public TestCommandOutputLogger(ITestOutputHelper testOutputHelper, bool observeLogLevel = false)
: base(LogLevel.Debug)
{
_observeLogLevel = observeLogLevel;

Logger = new TestLogger(testOutputHelper);
}

protected override void LogInternal(LogLevel logLevel, string message)
Expand Down
Loading