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

fix incorrect as usage in ProjectFactory #6191

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Changes from all 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
25 changes: 8 additions & 17 deletions src/NuGet.Clients/NuGet.CommandLine/Commands/ProjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,15 @@ private void Initialize(dynamic project)
}

// This happens before we obtain warning properties, so this Logger is still IConsole.
IConsole console = Logger as IConsole;
switch (LogLevel)
if (Logger is IConsole console)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of tests were failing because Logger was NullLogger which resulted in an invalid cast exception. Instead of as we could use is and do a onetime check if the Logger is an IConsole type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing we could do is

if (LogLevel != LogLevel.Debug)
{
    var console = (IConsole)Logger;
    console.Verbosity = LogLevel switch
    {
        LogLevel.Verbose => Verbosity.Detailed,
        LogLevel.Information => Verbosity.Normal,
        LogLevel.Minimal => Verbosity.Quiet,
        _ => console.Verbosity
    };
}

And in our tests make sure LogLevel == LogLevel.Debug

{
case LogLevel.Verbose:
{
console.Verbosity = Verbosity.Detailed;
break;
}
case LogLevel.Information:
{
console.Verbosity = Verbosity.Normal;
break;
}
case LogLevel.Minimal:
{
console.Verbosity = Verbosity.Quiet;
break;
}
console.Verbosity = LogLevel switch
{
LogLevel.Verbose => Verbosity.Detailed,
LogLevel.Information => Verbosity.Normal,
LogLevel.Minimal => Verbosity.Quiet,
_ => console.Verbosity
};
}
}

Expand Down