Skip to content
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,83 @@ await RunAsync(
);
}

private static async Task RunAsync(TestFile[] files, Job job, string[] expectedUrls, MockNuGetPackage[]? packages = null, string? repoContentsPath = null)
[Fact]
public async Task Run_ExitCodeIsSet()
{
using var http = TestHttpServer.CreateTestStringServer(url =>
{
var uri = new Uri(url, UriKind.Absolute);
var baseUrl = $"{uri.Scheme}://{uri.Host}:{uri.Port}";
return uri.PathAndQuery switch
{
// initial and search query are good, update should be possible...
"/index.json" => (200, $$"""
{
"version": "3.0.0",
"resources": [
{
"@id": "{{baseUrl}}/download",
"@type": "PackageBaseAddress/3.0.0"
},
{
"@id": "{{baseUrl}}/query",
"@type": "SearchQueryService"
},
{
"@id": "{{baseUrl}}/registrations",
"@type": "RegistrationsBaseUrl"
}
]
}
"""),
_ => (401, "") // everything else is unauthorized
};
});
var feedUrl = $"{http.BaseUrl.TrimEnd('/')}/index.json";
await RunAsync(
files: [
("NuGet.Config", $"""
<configuration>
<packageSources>
<clear />
<add key="private_feed" value="{feedUrl}" allowInsecureConnections="true" />
</packageSources>
</configuration>
"""),
("src/Directory.Build.props", "<Project />"),
("src/Directory.Build.targets", "<Project />"),
("src/Directory.Packages.props", "<Project />"),
("src/project.csproj", """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package" Version="1.2.3" />
</ItemGroup>
</Project>
""")
],
job: new Job()
{
Source = new()
{
Provider = "github",
Repo = "test",
Directory = "src",
}
},
expectedUrls:
[
"POST /update_jobs/TEST-ID/increment_metric",
"POST /update_jobs/TEST-ID/record_update_job_error",
"PATCH /update_jobs/TEST-ID/mark_as_processed",
],
expectedExitCode: 1
);
}

private static async Task RunAsync(TestFile[] files, Job job, string[] expectedUrls, MockNuGetPackage[]? packages = null, string? repoContentsPath = null, int expectedExitCode = 0)
{
using var tempDirectory = new TemporaryDirectory();

Expand Down Expand Up @@ -124,7 +200,7 @@ private static async Task RunAsync(TestFile[] files, Job job, string[] expectedU
throw;
}

Assert.True(result == 0, output.ToString());
Assert.True(result == expectedExitCode, $"Expected exit code {expectedExitCode} but got {result}.\nSTDOUT:\n" + output.ToString());
Assert.Equal(expectedUrls, actualUrls);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ internal static Command GetCommand(Action<int> setExitCode)
var analyzeWorker = new AnalyzeWorker(jobId!, experimentsManager, logger);
var updateWorker = new UpdaterWorker(jobId!, experimentsManager, logger);
var worker = new RunWorker(jobId!, apiHandler, discoverWorker, analyzeWorker, updateWorker, logger);
await worker.RunAsync(jobPath!, repoContentsPath!, caseInsensitiveRepoContentsPath, baseCommitSha!);
var result = await worker.RunAsync(jobPath!, repoContentsPath!, caseInsensitiveRepoContentsPath, baseCommitSha!);
setExitCode(result);
return 0;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ public RunWorker(string jobId, IApiHandler apiHandler, IDiscoveryWorker discover
_updaterWorker = updateWorker;
}

public async Task RunAsync(FileInfo jobFilePath, DirectoryInfo repoContentsPath, DirectoryInfo? caseInsensitiveRepoContentsPath, string baseCommitSha)
public async Task<int> RunAsync(FileInfo jobFilePath, DirectoryInfo repoContentsPath, DirectoryInfo? caseInsensitiveRepoContentsPath, string baseCommitSha)
{
var jobFileContent = await File.ReadAllTextAsync(jobFilePath.FullName);
var jobWrapper = Deserialize(jobFileContent);
var experimentsManager = ExperimentsManager.GetExperimentsManager(jobWrapper.Job.Experiments);
await RunAsync(jobWrapper.Job, repoContentsPath, caseInsensitiveRepoContentsPath, baseCommitSha, experimentsManager);
var result = await RunAsync(jobWrapper.Job, repoContentsPath, caseInsensitiveRepoContentsPath, baseCommitSha, experimentsManager);
return result;
}

public async Task RunAsync(Job job, DirectoryInfo repoContentsPath, DirectoryInfo? caseInsensitiveRepoContentsPath, string baseCommitSha, ExperimentsManager experimentsManager)
public async Task<int> RunAsync(Job job, DirectoryInfo repoContentsPath, DirectoryInfo? caseInsensitiveRepoContentsPath, string baseCommitSha, ExperimentsManager experimentsManager)
{
await RunScenarioHandlersWithErrorHandlingAsync(job, repoContentsPath, caseInsensitiveRepoContentsPath, baseCommitSha, experimentsManager);
var result = await RunScenarioHandlersWithErrorHandlingAsync(job, repoContentsPath, caseInsensitiveRepoContentsPath, baseCommitSha, experimentsManager);
return result;
}

private static readonly ImmutableArray<IUpdateHandler> UpdateHandlers =
Expand All @@ -65,8 +67,9 @@ public async Task RunAsync(Job job, DirectoryInfo repoContentsPath, DirectoryInf
public static IUpdateHandler GetUpdateHandler(Job job) =>
UpdateHandlers.FirstOrDefault(h => h.CanHandle(job)) ?? throw new InvalidOperationException("Unable to find appropriate update handler.");

private async Task RunScenarioHandlersWithErrorHandlingAsync(Job job, DirectoryInfo repoContentsPath, DirectoryInfo? caseInsensitiveRepoContentsPath, string baseCommitSha, ExperimentsManager experimentsManager)
private async Task<int> RunScenarioHandlersWithErrorHandlingAsync(Job job, DirectoryInfo repoContentsPath, DirectoryInfo? caseInsensitiveRepoContentsPath, string baseCommitSha, ExperimentsManager experimentsManager)
{
int result = 0;
JobErrorBase? error = null;

try
Expand All @@ -83,9 +86,11 @@ private async Task RunScenarioHandlersWithErrorHandlingAsync(Job job, DirectoryI
if (error is not null)
{
await _apiHandler.RecordUpdateJobError(error, _logger);
result = 1;
}

await _apiHandler.MarkAsProcessed(new(baseCommitSha));
return result;
}

internal static ImmutableArray<UpdateOperationBase> PatchInOldVersions(ImmutableArray<UpdateOperationBase> updateOperations, ProjectDiscoveryResult? projectDiscovery)
Expand Down
11 changes: 10 additions & 1 deletion nuget/updater/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,19 @@ function Update-Files {
$script:operationExitCode = $process.ExitCode
}

function Update-Dependencies {
Get-Files
if ($script:operationExitCode -ne 0) {
return
}

Update-Files
}

try {
Switch ($args[0]) {
"fetch_files" { }
"update_files" { Get-Files; Update-Files }
"update_files" { Update-Dependencies }
default { throw "unknown command: $args[0]" }
}
exit $operationExitCode
Expand Down
Loading