-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add end-to-end C# update runner (#10521)
- Loading branch information
Showing
33 changed files
with
1,311 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Cli.Test/EntryPointTests.Run.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
using NuGetUpdater.Core.Run; | ||
using NuGetUpdater.Core.Run.ApiModel; | ||
using NuGetUpdater.Core.Test; | ||
using NuGetUpdater.Core.Test.Update; | ||
|
||
using Xunit; | ||
|
||
namespace NuGetUpdater.Cli.Test; | ||
|
||
using TestFile = (string Path, string Content); | ||
|
||
public partial class EntryPointTests | ||
{ | ||
public class Run | ||
{ | ||
[Fact] | ||
public async Task Run_Simple() | ||
{ | ||
// verify we can pass command line arguments and hit the appropriate URLs | ||
await RunAsync( | ||
packages: | ||
[ | ||
MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0"), | ||
MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.1", "net8.0"), | ||
], | ||
files: | ||
[ | ||
("src/project.csproj", """ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Some.Package" Version="1.0.0" /> | ||
</ItemGroup> | ||
</Project> | ||
""") | ||
], | ||
job: new Job() | ||
{ | ||
PackageManager = "nuget", | ||
AllowedUpdates = [ | ||
new() | ||
{ | ||
UpdateType = "all" | ||
} | ||
], | ||
Source = new() | ||
{ | ||
Provider = "github", | ||
Repo = "test", | ||
Directory = "src", | ||
} | ||
}, | ||
expectedUrls: | ||
[ | ||
"/update_jobs/TEST-ID/update_dependency_list", | ||
"/update_jobs/TEST-ID/increment_metric", | ||
"/update_jobs/TEST-ID/create_pull_request", | ||
"/update_jobs/TEST-ID/mark_as_processed", | ||
] | ||
); | ||
} | ||
|
||
private static async Task RunAsync(TestFile[] files, Job job, string[] expectedUrls, MockNuGetPackage[]? packages = null) | ||
{ | ||
using var tempDirectory = new TemporaryDirectory(); | ||
|
||
// write test files | ||
foreach (var testFile in files) | ||
{ | ||
var fullPath = Path.Join(tempDirectory.DirectoryPath, testFile.Path); | ||
var directory = Path.GetDirectoryName(fullPath)!; | ||
Directory.CreateDirectory(directory); | ||
await File.WriteAllTextAsync(fullPath, testFile.Content); | ||
} | ||
|
||
// write job file | ||
var jobPath = Path.Combine(tempDirectory.DirectoryPath, "job.json"); | ||
await File.WriteAllTextAsync(jobPath, JsonSerializer.Serialize(new { Job = job }, RunWorker.SerializerOptions)); | ||
|
||
// save packages | ||
await UpdateWorkerTestBase.MockNuGetPackagesInDirectory(packages, tempDirectory.DirectoryPath); | ||
|
||
var actualUrls = new List<string>(); | ||
using var http = TestHttpServer.CreateTestStringServer(url => | ||
{ | ||
actualUrls.Add(new Uri(url).PathAndQuery); | ||
return (200, "ok"); | ||
}); | ||
var args = new List<string>() | ||
{ | ||
"run", | ||
"--job-path", | ||
jobPath, | ||
"--repo-contents-path", | ||
tempDirectory.DirectoryPath, | ||
"--api-url", | ||
http.BaseUrl, | ||
"--job-id", | ||
"TEST-ID", | ||
"--output-path", | ||
Path.Combine(tempDirectory.DirectoryPath, "output.json"), | ||
"--base-commit-sha", | ||
"BASE-COMMIT-SHA", | ||
"--verbose" | ||
}; | ||
|
||
var output = new StringBuilder(); | ||
// redirect stdout | ||
var originalOut = Console.Out; | ||
Console.SetOut(new StringWriter(output)); | ||
int result = -1; | ||
try | ||
{ | ||
result = await Program.Main(args.ToArray()); | ||
} | ||
catch | ||
{ | ||
// restore stdout | ||
Console.SetOut(originalOut); | ||
throw; | ||
} | ||
|
||
Assert.True(result == 0, output.ToString()); | ||
Assert.Equal(expectedUrls, actualUrls); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Cli/Commands/RunCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.CommandLine; | ||
|
||
using NuGetUpdater.Core; | ||
using NuGetUpdater.Core.Run; | ||
|
||
namespace NuGetUpdater.Cli.Commands; | ||
|
||
internal static class RunCommand | ||
{ | ||
internal static readonly Option<FileInfo> JobPathOption = new("--job-path") { IsRequired = true }; | ||
internal static readonly Option<DirectoryInfo> RepoContentsPathOption = new("--repo-contents-path") { IsRequired = true }; | ||
internal static readonly Option<Uri> ApiUrlOption = new("--api-url") { IsRequired = true }; | ||
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) | ||
{ | ||
Command command = new("run", "Runs a full dependabot job.") | ||
{ | ||
JobPathOption, | ||
RepoContentsPathOption, | ||
ApiUrlOption, | ||
JobIdOption, | ||
OutputPathOption, | ||
BaseCommitShaOption, | ||
VerboseOption | ||
}; | ||
|
||
command.TreatUnmatchedTokensAsErrors = true; | ||
|
||
command.SetHandler(async (jobPath, repoContentsPath, apiUrl, jobId, outputPath, baseCommitSha, verbose) => | ||
{ | ||
var apiHandler = new HttpApiHandler(apiUrl.ToString(), jobId); | ||
var worker = new RunWorker(apiHandler, new Logger(verbose)); | ||
await worker.RunAsync(jobPath, repoContentsPath, baseCommitSha, outputPath); | ||
}, JobPathOption, RepoContentsPathOption, ApiUrlOption, JobIdOption, OutputPathOption, BaseCommitShaOption, VerboseOption); | ||
|
||
return command; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.