Skip to content

Commit

Permalink
create separate loggers to make test output easier to read (#10748)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo authored Oct 11, 2024
1 parent c5b4d03 commit c1a96c4
Show file tree
Hide file tree
Showing 55 changed files with 185 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ await RunAsync(path =>
Path.Join(path, "Some.Package.json"),
"--analysis-folder-path",
Path.Join(path, AnalyzeWorker.AnalysisDirectoryName),
"--verbose",
],
packages: [
MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0", additionalMetadata: [repositoryXml]),
Expand Down Expand Up @@ -151,7 +150,6 @@ await RunAsync(path =>
Path.Join(path, "some-global-tool.json"),
"--analysis-folder-path",
Path.Join(path, AnalyzeWorker.AnalysisDirectoryName),
"--verbose",
],
packages:
[
Expand Down Expand Up @@ -239,7 +237,6 @@ await RunAsync(path =>
Path.Join(path, "Some.MSBuild.Sdk.json"),
"--analysis-folder-path",
Path.Join(path, AnalyzeWorker.AnalysisDirectoryName),
"--verbose",
],
packages:
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ private static async Task Run(string projectTfms, string packageTfms, int expect
args.AddRange(projectTfms.Split(' ', StringSplitOptions.TrimEntries));
args.Add("--package-tfms");
args.AddRange(packageTfms.Split(' ', StringSplitOptions.TrimEntries));
args.Add("--verbose");

var actual = await Program.Main(args.ToArray());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ private static async Task RunAsync(TestFile[] files, Job job, string[] expectedU
"--output-path",
Path.Combine(tempDirectory.DirectoryPath, "output.json"),
"--base-commit-sha",
"BASE-COMMIT-SHA",
"--verbose"
"BASE-COMMIT-SHA"
};

var output = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ await Run(path =>
"--new-version",
"13.0.1",
"--previous-version",
"7.0.1",
"--verbose"
"7.0.1"
],
packages:
[
Expand Down Expand Up @@ -207,8 +206,7 @@ await Run(path =>
"--new-version",
"6.6.1",
"--previous-version",
"6.1.0",
"--verbose"
"6.1.0"
],
packages:
[
Expand Down Expand Up @@ -364,8 +362,7 @@ await File.WriteAllTextAsync(projectPath, """
"--new-version",
"13.0.1",
"--previous-version",
"7.0.1",
"--verbose"
"7.0.1"
];

// verify base run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ internal static class AnalyzeCommand
internal static readonly Option<FileInfo> DependencyFilePathOption = new("--dependency-file-path") { IsRequired = true };
internal static readonly Option<FileInfo> DiscoveryFilePathOption = new("--discovery-file-path") { IsRequired = true };
internal static readonly Option<DirectoryInfo> AnalysisFolderOption = new("--analysis-folder-path") { IsRequired = true };
internal static readonly Option<bool> VerboseOption = new("--verbose", getDefaultValue: () => false);

internal static Command GetCommand(Action<int> setExitCode)
{
Expand All @@ -20,17 +19,16 @@ internal static Command GetCommand(Action<int> setExitCode)
RepoRootOption,
DependencyFilePathOption,
DiscoveryFilePathOption,
AnalysisFolderOption,
VerboseOption
AnalysisFolderOption
};

command.TreatUnmatchedTokensAsErrors = true;

command.SetHandler(async (repoRoot, discoveryPath, dependencyPath, analysisDirectory, verbose) =>
command.SetHandler(async (repoRoot, discoveryPath, dependencyPath, analysisDirectory) =>
{
var worker = new AnalyzeWorker(new Logger(verbose));
var worker = new AnalyzeWorker(new ConsoleLogger());
await worker.RunAsync(repoRoot.FullName, discoveryPath.FullName, dependencyPath.FullName, analysisDirectory.FullName);
}, RepoRootOption, DiscoveryFilePathOption, DependencyFilePathOption, AnalysisFolderOption, VerboseOption);
}, RepoRootOption, DiscoveryFilePathOption, DependencyFilePathOption, AnalysisFolderOption);

return command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,23 @@ internal static class DiscoverCommand
internal static readonly Option<DirectoryInfo> RepoRootOption = new("--repo-root") { IsRequired = true };
internal static readonly Option<string> WorkspaceOption = new("--workspace") { IsRequired = true };
internal static readonly Option<FileInfo> OutputOption = new("--output") { IsRequired = true };
internal static readonly Option<bool> VerboseOption = new("--verbose", getDefaultValue: () => false);

internal static Command GetCommand(Action<int> setExitCode)
{
Command command = new("discover", "Generates a report of the workspace dependencies and where they are located.")
{
RepoRootOption,
WorkspaceOption,
OutputOption,
VerboseOption
OutputOption
};

command.TreatUnmatchedTokensAsErrors = true;

command.SetHandler(async (repoRoot, workspace, outputPath, verbose) =>
command.SetHandler(async (repoRoot, workspace, outputPath) =>
{
var worker = new DiscoveryWorker(new Logger(verbose));
var worker = new DiscoveryWorker(new ConsoleLogger());
await worker.RunAsync(repoRoot.FullName, workspace, outputPath.FullName);
}, RepoRootOption, WorkspaceOption, OutputOption, VerboseOption);
}, RepoRootOption, WorkspaceOption, OutputOption);

return command;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.CommandLine;

using NuGetUpdater.Core;
Expand All @@ -10,25 +9,23 @@ internal static class FrameworkCheckCommand
{
internal static readonly Option<string[]> ProjectTfmsOption = new("--project-tfms") { IsRequired = true, AllowMultipleArgumentsPerToken = true };
internal static readonly Option<string[]> PackageTfmsOption = new("--package-tfms") { IsRequired = true, AllowMultipleArgumentsPerToken = true };
internal static readonly Option<bool> VerboseOption = new("--verbose", getDefaultValue: () => false);

internal static Command GetCommand(Action<int> setExitCode)
{
Command command = new("framework-check", "Checks that a project's target frameworks are satisfied by the target frameworks supported by a package.")
{
ProjectTfmsOption,
PackageTfmsOption,
VerboseOption
PackageTfmsOption
};

command.TreatUnmatchedTokensAsErrors = true;

command.SetHandler((projectTfms, packageTfms, verbose) =>
command.SetHandler((projectTfms, packageTfms) =>
{
setExitCode(CompatibilityChecker.IsCompatible(projectTfms, packageTfms, new Logger(verbose))
setExitCode(CompatibilityChecker.IsCompatible(projectTfms, packageTfms, new ConsoleLogger())
? 0
: 1);
}, ProjectTfmsOption, PackageTfmsOption, VerboseOption);
}, ProjectTfmsOption, PackageTfmsOption);

return command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ internal static class RunCommand
internal static readonly Option<string> JobIdOption = new("--job-id") { IsRequired = true };
internal static readonly Option<FileInfo> OutputPathOption = new("--output-path") { IsRequired = true };
internal static readonly Option<string> BaseCommitShaOption = new("--base-commit-sha") { IsRequired = true };
internal static readonly Option<bool> VerboseOption = new("--verbose", getDefaultValue: () => false);

internal static Command GetCommand(Action<int> setExitCode)
{
Expand All @@ -24,18 +23,17 @@ internal static Command GetCommand(Action<int> setExitCode)
ApiUrlOption,
JobIdOption,
OutputPathOption,
BaseCommitShaOption,
VerboseOption
BaseCommitShaOption
};

command.TreatUnmatchedTokensAsErrors = true;

command.SetHandler(async (jobPath, repoContentsPath, apiUrl, jobId, outputPath, baseCommitSha, verbose) =>
command.SetHandler(async (jobPath, repoContentsPath, apiUrl, jobId, outputPath, baseCommitSha) =>
{
var apiHandler = new HttpApiHandler(apiUrl.ToString(), jobId);
var worker = new RunWorker(apiHandler, new Logger(verbose));
var worker = new RunWorker(apiHandler, new ConsoleLogger());
await worker.RunAsync(jobPath, repoContentsPath, baseCommitSha, outputPath);
}, JobPathOption, RepoContentsPathOption, ApiUrlOption, JobIdOption, OutputPathOption, BaseCommitShaOption, VerboseOption);
}, JobPathOption, RepoContentsPathOption, ApiUrlOption, JobIdOption, OutputPathOption, BaseCommitShaOption);

return command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ internal static class UpdateCommand
internal static readonly Option<string> NewVersionOption = new("--new-version") { IsRequired = true };
internal static readonly Option<string> PreviousVersionOption = new("--previous-version") { IsRequired = true };
internal static readonly Option<bool> IsTransitiveOption = new("--transitive", getDefaultValue: () => false);
internal static readonly Option<bool> VerboseOption = new("--verbose", getDefaultValue: () => false);
internal static readonly Option<string?> ResultOutputPathOption = new("--result-output-path", getDefaultValue: () => null);

internal static Command GetCommand(Action<int> setExitCode)
Expand All @@ -25,18 +24,17 @@ internal static Command GetCommand(Action<int> setExitCode)
NewVersionOption,
PreviousVersionOption,
IsTransitiveOption,
VerboseOption,
ResultOutputPathOption
};

command.TreatUnmatchedTokensAsErrors = true;

command.SetHandler(async (repoRoot, solutionOrProjectFile, dependencyName, newVersion, previousVersion, isTransitive, verbose, resultOutputPath) =>
command.SetHandler(async (repoRoot, solutionOrProjectFile, dependencyName, newVersion, previousVersion, isTransitive, resultOutputPath) =>
{
var worker = new UpdaterWorker(new Logger(verbose));
var worker = new UpdaterWorker(new ConsoleLogger());
await worker.RunAsync(repoRoot.FullName, solutionOrProjectFile.FullName, dependencyName, previousVersion, newVersion, isTransitive, resultOutputPath);
setExitCode(0);
}, RepoRootOption, SolutionOrProjectFileOption, DependencyNameOption, NewVersionOption, PreviousVersionOption, IsTransitiveOption, VerboseOption, ResultOutputPathOption);
}, RepoRootOption, SolutionOrProjectFileOption, DependencyNameOption, NewVersionOption, PreviousVersionOption, IsTransitiveOption, ResultOutputPathOption);

return command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected static async Task TestAnalyzeAsync(
var discoveryPath = Path.GetFullPath(DiscoveryWorker.DiscoveryResultFileName, directoryPath);
var dependencyPath = Path.GetFullPath(relativeDependencyPath, directoryPath);

var worker = new AnalyzeWorker(new Logger(verbose: true));
var worker = new AnalyzeWorker(new TestLogger());
var result = await worker.RunWithErrorHandlingAsync(directoryPath, discoveryPath, dependencyPath);
return result;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ public async Task ResultFileHasCorrectShapeForAuthenticationFailure()
ErrorDetails = "<some package feed>",
UpdatedVersion = "",
UpdatedDependencies = [],
}, new Logger(false));
}, new TestLogger());
var discoveryContents = await File.ReadAllTextAsync(Path.Combine(temporaryDirectory.DirectoryPath, "Some.Dependency.json"));

// raw result file should look like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void PerformCheck_CompatiblePackage_IsCompatible()
projectFrameworks,
isDevDependency,
packageFrameworks,
new Logger(verbose: false));
new TestLogger());

Assert.True(result);
}
Expand All @@ -53,7 +53,7 @@ public void PerformCheck_IncompatiblePackage_IsIncompatible()
projectFrameworks,
isDevDependency,
packageFrameworks,
new Logger(verbose: false));
new TestLogger());

Assert.False(result);
}
Expand All @@ -76,7 +76,7 @@ public void PerformCheck_DevDependencyWithPackageFrameworks_IsChecked()
projectFrameworks,
isDevDependency,
packageFrameworks,
new Logger(verbose: false));
new TestLogger());

Assert.False(result);
}
Expand All @@ -97,7 +97,7 @@ public void PerformCheck_DevDependencyWithoutPackageFrameworks_IsCompatibile()
projectFrameworks,
isDevDependency,
packageFrameworks,
new Logger(verbose: false));
new TestLogger());

Assert.True(result);
}
Expand All @@ -118,7 +118,7 @@ public void PerformCheck_WithoutPackageFrameworks_IsIncompatibile()
projectFrameworks,
isDevDependency,
packageFrameworks,
new Logger(verbose: false));
new TestLogger());

Assert.False(result);
}
Expand All @@ -138,7 +138,7 @@ public void PerformCheck_WithoutProjectFrameworks_IsIncompatible()
projectFrameworks,
isDevDependency,
packageFrameworks,
new Logger(verbose: false));
new TestLogger());

Assert.False(result);
}
Expand All @@ -161,7 +161,7 @@ public void EverythingIsCompatibleWithAnyVersion0Framework(string projectFramewo
projectFrameworks,
isDevDependency,
packageFrameworks,
new Logger(verbose: false));
new TestLogger());

Assert.True(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected static async Task TestDiscoveryAsync(
{
await UpdateWorkerTestBase.MockNuGetPackagesInDirectory(packages, directoryPath);

var worker = new DiscoveryWorker(new Logger(verbose: true));
var worker = new DiscoveryWorker(new TestLogger());
var result = await worker.RunWithErrorHandlingAsync(directoryPath, workspacePath);
return result;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class DotnetToolsJsonBuildFileTests
basePath: "/",
path: "/.config/dotnet-tools.json",
contents: DotnetToolsJson,
logger: new Logger(verbose: true));
logger: new TestLogger());

[Fact]
public void GetDependencies_ReturnsDependencies()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class GlobalJsonBuildFileTests
basePath: "/",
path: "/global.json",
contents: contents,
logger: new Logger(verbose: true));
logger: new TestLogger());

[Fact]
public void GlobalJson_Malformed_DoesNotThrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CompatibilityCheckerFacts
[InlineData("net4.8", "netstandard1.3")]
public void PackageContainsCompatibleFramework(string projectTfm, string packageTfm)
{
var result = CompatibilityChecker.IsCompatible([projectTfm], [packageTfm], new Logger(verbose: true));
var result = CompatibilityChecker.IsCompatible([projectTfm], [packageTfm], new TestLogger());

Assert.True(result);
}
Expand All @@ -37,7 +37,7 @@ public void PackageContainsCompatibleFramework(string projectTfm, string package
[InlineData("net7.0", "net48")]
public void PackageContainsIncompatibleFramework(string projectTfm, string packageTfm)
{
var result = CompatibilityChecker.IsCompatible([projectTfm], [packageTfm], new Logger(verbose: true));
var result = CompatibilityChecker.IsCompatible([projectTfm], [packageTfm], new TestLogger());

Assert.False(result);
}
Expand All @@ -48,7 +48,7 @@ public void PackageContainsIncompatibleFramework(string projectTfm, string packa
[InlineData(new[] { "net6.0", "net6.0-windows10.0.19041" }, new[] { "net6.0", ".NETStandard2.0" })]
public void PackageContainsCompatibleFrameworks(string[] projectTfms, string[] packageTfms)
{
var result = CompatibilityChecker.IsCompatible(projectTfms, packageTfms, new Logger(verbose: true));
var result = CompatibilityChecker.IsCompatible(projectTfms, packageTfms, new TestLogger());

Assert.True(result);
}
Expand All @@ -57,7 +57,7 @@ public void PackageContainsCompatibleFrameworks(string[] projectTfms, string[] p
[InlineData(new[] { "net7.0", "net472" }, new[] { "net5.0" })]
public void PackageContainsIncompatibleFrameworks(string[] projectTfms, string[] packageTfms)
{
var result = CompatibilityChecker.IsCompatible(projectTfms, packageTfms, new Logger(verbose: true));
var result = CompatibilityChecker.IsCompatible(projectTfms, packageTfms, new TestLogger());

Assert.False(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private static async Task RunAsync(Job job, TestFile[] files, RunResult expected

// act
var testApiHandler = new TestApiHandler();
var worker = new RunWorker(testApiHandler, new Logger(verbose: false));
var worker = new RunWorker(testApiHandler, new TestLogger());
var repoContentsPath = new DirectoryInfo(tempDirectory.DirectoryPath);
var actualResult = await worker.RunAsync(job, repoContentsPath, "TEST-COMMIT-SHA");
var actualApiMessages = testApiHandler.ReceivedMessages.ToArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Diagnostics;

namespace NuGetUpdater.Core.Test;

public class TestLogger : ILogger
{
public void Log(string message)
{
Debug.WriteLine(message);
}
}
Loading

0 comments on commit c1a96c4

Please sign in to comment.