Skip to content

Commit

Permalink
create interfaces for workers to make testing more direct (#10910)
Browse files Browse the repository at this point in the history
create interfaces for workers to make testing more direct (#10910)
  • Loading branch information
brettfo authored Nov 7, 2024
1 parent e719655 commit 9ada831
Show file tree
Hide file tree
Showing 15 changed files with 643 additions and 197 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.CommandLine;

using NuGetUpdater.Core;
using NuGetUpdater.Core.Analyze;
using NuGetUpdater.Core.Discover;
using NuGetUpdater.Core.Run;

namespace NuGetUpdater.Cli.Commands;
Expand Down Expand Up @@ -31,7 +33,12 @@ internal static Command GetCommand(Action<int> setExitCode)
command.SetHandler(async (jobPath, repoContentsPath, apiUrl, jobId, outputPath, baseCommitSha) =>
{
var apiHandler = new HttpApiHandler(apiUrl.ToString(), jobId);
var worker = new RunWorker(apiHandler, new ConsoleLogger());
var logger = new ConsoleLogger();
var experimentsManager = await ExperimentsManager.FromJobFileAsync(jobPath.FullName, logger);
var discoverWorker = new DiscoveryWorker(logger);
var analyzeWorker = new AnalyzeWorker(logger);
var updateWorker = new UpdaterWorker(experimentsManager, logger);
var worker = new RunWorker(apiHandler, discoverWorker, analyzeWorker, updateWorker, logger);
await worker.RunAsync(jobPath, repoContentsPath, baseCommitSha, outputPath);
}, JobPathOption, RepoContentsPathOption, ApiUrlOption, JobIdOption, OutputPathOption, BaseCommitShaOption);

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using NuGetUpdater.Core.Analyze;
using NuGetUpdater.Core.Discover;

namespace NuGetUpdater.Core.Test;

internal class TestAnalyzeWorker : IAnalyzeWorker
{
private readonly Func<(string, WorkspaceDiscoveryResult, DependencyInfo), Task<AnalysisResult>> _getResult;

public TestAnalyzeWorker(Func<(string, WorkspaceDiscoveryResult, DependencyInfo), Task<AnalysisResult>> getResult)
{
_getResult = getResult;
}

public Task<AnalysisResult> RunAsync(string repoRoot, WorkspaceDiscoveryResult discovery, DependencyInfo dependencyInfo)
{
return _getResult((repoRoot, discovery, dependencyInfo));
}

public static TestAnalyzeWorker FromResults(params (string RepoRoot, WorkspaceDiscoveryResult Discovery, DependencyInfo DependencyInfo, AnalysisResult Result)[] results)
{
return new TestAnalyzeWorker(((string RepoRoot, WorkspaceDiscoveryResult Discovery, DependencyInfo DependencyInfo) input) =>
{
foreach (var set in results)
{
if (set.RepoRoot == input.RepoRoot &&
set.Discovery == input.Discovery &&
set.DependencyInfo == input.DependencyInfo)
{
return Task.FromResult(set.Result);
}
}
throw new NotImplementedException($"No saved response for {input}");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using NuGetUpdater.Core.Discover;

namespace NuGetUpdater.Core.Test;

internal class TestDiscoveryWorker : IDiscoveryWorker
{
private readonly Func<(string, string), Task<WorkspaceDiscoveryResult>> _getResult;

public TestDiscoveryWorker(Func<(string, string), Task<WorkspaceDiscoveryResult>> getResult)
{
_getResult = getResult;
}

public Task<WorkspaceDiscoveryResult> RunAsync(string repoRootPath, string workspacePath)
{
return _getResult((repoRootPath, workspacePath));
}

public static TestDiscoveryWorker FromResults(params (string RepoRootPath, string WorkspacePath, WorkspaceDiscoveryResult Result)[] results)
{
return new TestDiscoveryWorker(((string RepoRootPath, string WorkspacePath) input) =>
{
foreach (var set in results)
{
if (set.RepoRootPath == input.RepoRootPath &&
set.WorkspacePath == input.WorkspacePath)
{
return Task.FromResult(set.Result);
}
}
throw new NotImplementedException($"No saved response for {input}");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using NuGetUpdater.Core.Updater;

namespace NuGetUpdater.Core.Test;

internal class TestUpdaterWorker : IUpdaterWorker
{
private readonly Func<(string, string, string, string, string, bool), Task<UpdateOperationResult>> _getResult;

public TestUpdaterWorker(Func<(string, string, string, string, string, bool), Task<UpdateOperationResult>> getResult)
{
_getResult = getResult;
}

public Task<UpdateOperationResult> RunAsync(string repoRootPath, string workspacePath, string dependencyName, string previousDependencyVersion, string newDependencyVersion, bool isTransitive)
{
return _getResult((repoRootPath, workspacePath, dependencyName, previousDependencyVersion, newDependencyVersion, isTransitive));
}

public static TestUpdaterWorker FromResults(params (string RepoRootPath, string WorkspacePath, string DependencyName, string PreviousDependencyVersion, string NewDependencyVersion, bool IsTransitive, UpdateOperationResult Result)[] results)
{
return new TestUpdaterWorker(((string RepoRootPath, string WorkspacePath, string DependencyName, string PreviousDependencyVersion, string NewDependencyVersion, bool IsTransitive) input) =>
{
foreach (var set in results)
{
if (set.RepoRootPath == input.RepoRootPath &&
set.WorkspacePath == input.WorkspacePath &&
set.DependencyName == input.DependencyName &&
set.PreviousDependencyVersion == input.PreviousDependencyVersion &&
set.NewDependencyVersion == input.NewDependencyVersion &&
set.IsTransitive == input.IsTransitive)
{
return Task.FromResult(set.Result);
}
}
throw new NotImplementedException($"No saved response for {input}");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace NuGetUpdater.Core.Analyze;

using MultiDependency = (string PropertyName, ImmutableArray<string> TargetFrameworks, ImmutableHashSet<string> DependencyNames);

public partial class AnalyzeWorker
public partial class AnalyzeWorker : IAnalyzeWorker
{
public const string AnalysisDirectoryName = "./.dependabot/analysis";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace NuGetUpdater.Core.Discover;

public partial class DiscoveryWorker
public partial class DiscoveryWorker : IDiscoveryWorker
{
public const string DiscoveryResultFileName = "./.dependabot/discovery.json";

Expand Down Expand Up @@ -58,7 +58,7 @@ internal async Task<WorkspaceDiscoveryResult> RunWithErrorHandlingAsync(string r
return result;
}

internal async Task<WorkspaceDiscoveryResult> RunAsync(string repoRootPath, string workspacePath)
public async Task<WorkspaceDiscoveryResult> RunAsync(string repoRootPath, string workspacePath)
{
MSBuildHelper.RegisterMSBuild(Environment.CurrentDirectory, repoRootPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static bool IsEnabled(Dictionary<string, object>? experiments, string ex

if (experiments.TryGetValue(experimentName, out var value))
{
if ((value?.ToString()?? "").Equals("true", StringComparison.OrdinalIgnoreCase))
if ((value?.ToString() ?? "").Equals("true", StringComparison.OrdinalIgnoreCase))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using NuGetUpdater.Core.Analyze;
using NuGetUpdater.Core.Discover;

namespace NuGetUpdater.Core;

public interface IAnalyzeWorker
{
Task<AnalysisResult> RunAsync(string repoRoot, WorkspaceDiscoveryResult discovery, DependencyInfo dependencyInfo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using NuGetUpdater.Core.Discover;

namespace NuGetUpdater.Core;

public interface IDiscoveryWorker
{
Task<WorkspaceDiscoveryResult> RunAsync(string repoRootPath, string workspacePath);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

using NuGetUpdater.Core.Updater;

namespace NuGetUpdater.Core;

public interface IUpdaterWorker
{
Task<UpdateOperationResult> RunAsync(string repoRootPath, string workspacePath, string dependencyName, string previousDependencyVersion, string newDependencyVersion, bool isTransitive);
}
Loading

0 comments on commit 9ada831

Please sign in to comment.