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 folder cache to reduce flakiness #769

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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 @@ -191,7 +191,7 @@ private Task<Project> CreateProject()
break;

case TargetFramework.Net9_0:
AddNuGetReference("Microsoft.NETCore.App.Ref", "9.0.0-rc.1.24431.7", "ref/net9.0/");
AddNuGetReference("Microsoft.NETCore.App.Ref", "9.0.0", "ref/net9.0/");
break;

case TargetFramework.AspNetCore5_0:
Expand Down
42 changes: 33 additions & 9 deletions tests/Meziantou.Analyzer.Test/Helpers/ProjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Meziantou.Analyzer.Rules;
using Meziantou.Analyzer.Test.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
Expand Down Expand Up @@ -45,19 +47,28 @@ public sealed partial class ProjectBuilder
public string DefaultAnalyzerId { get; set; }
public string DefaultAnalyzerMessage { get; set; }

private static Task<string[]> GetNuGetReferences(string packageName, string version, params string[] paths)
private static async Task<string[]> GetNuGetReferences(string packageName, string version, params string[] paths)
{
var task = NuGetPackagesCache.GetOrAdd(
packageName + '@' + version + ':' + string.Join(",", paths),
_ => new Lazy<Task<string[]>>(Download));

return task.Value;
var bytes = Encoding.UTF8.GetBytes(packageName + '@' + version + ':' + string.Join(",", paths));
#if NET8_0_OR_GREATER
var hash = SHA256.HashData(bytes);
#else
using var sha256 = SHA256.Create();
var hash = sha256.ComputeHash(bytes);
#endif
var key = Convert.ToBase64String(hash).Replace('/', '_');
var task = NuGetPackagesCache.GetOrAdd(key, _ => new Lazy<Task<string[]>>(Download));
return await task.Value.ConfigureAwait(false);

async Task<string[]> Download()
{
var tempFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Meziantou.AnalyzerTests", "ref", packageName + '@' + version);
if (!Directory.Exists(tempFolder) || !Directory.EnumerateFileSystemEntries(tempFolder).Any())
var cacheFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Meziantou.AnalyzerTests", "ref", key);
bool IsCacheValid() => Directory.Exists(cacheFolder) && Directory.EnumerateFileSystemEntries(cacheFolder).Any();

if (!IsCacheValid())
{
var tempFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));

Directory.CreateDirectory(tempFolder);
using var stream = await SharedHttpClient.Instance.GetStreamAsync(new Uri($"https://www.nuget.org/api/v2/package/{packageName}/{version}")).ConfigureAwait(false);
using var zip = new ZipArchive(stream, ZipArchiveMode.Read);
Expand All @@ -66,9 +77,22 @@ async Task<string[]> Download()
{
entry.ExtractToFile(Path.Combine(tempFolder, entry.Name), overwrite: true);
}

try
{
Directory.CreateDirectory(Path.GetDirectoryName(cacheFolder));
Directory.Move(tempFolder, cacheFolder);
}
catch (Exception ex)
{
if (!IsCacheValid())
{
throw new InvalidOperationException("Cannot download NuGet package " + packageName + "@" + version + "\n" + ex);
}
}
}

var dlls = Directory.GetFiles(tempFolder, "*.dll");
var dlls = Directory.GetFiles(cacheFolder, "*.dll");

// Filter invalid .NET assembly
var result = new List<string>();
Expand Down