-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathTestReferences.cs
60 lines (48 loc) · 2.28 KB
/
TestReferences.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;
namespace Microsoft.AspNetCore.Analyzers;
public static class TestReferences
{
public static readonly ReferenceAssemblies EmptyReferenceAssemblies = new("some-tfm");
public static ImmutableArray<MetadataReference> MetadataReferences { get; } = GetMetadataReferences();
private static ImmutableArray<MetadataReference> GetMetadataReferences()
{
var seen = new HashSet<string>();
var references = ImmutableArray.CreateBuilder<MetadataReference>();
foreach (var defaultCompileLibrary in DependencyContext.Load(typeof(TestReferences).Assembly).CompileLibraries)
{
foreach (var resolveReferencePath in defaultCompileLibrary.ResolveReferencePaths(new AppBaseCompilationAssemblyResolver()))
{
TryAdd(resolveReferencePath);
}
}
// The deps file in the project is incorrect and does not contain "compile" nodes for some references.
// However these binaries are always present in the bin output. As a "temporary" workaround, we'll add
// every dll file that's present in the test's build output as a metadatareference.
foreach (var assembly in Directory.EnumerateFiles(AppContext.BaseDirectory, "*.dll"))
{
TryAdd(assembly);
}
return references.ToImmutable();
void TryAdd(string assemblyPath)
{
var name = Path.GetFileNameWithoutExtension(assemblyPath);
if (!name.StartsWith("Microsoft.Extensions", StringComparison.Ordinal) &&
!name.StartsWith("Microsoft.AspNetCore", StringComparison.Ordinal) &&
!name.StartsWith("System", StringComparison.Ordinal) &&
!name.StartsWith("netstandard", StringComparison.Ordinal))
{
return;
}
if (seen.Add(name))
{
references.Add(MetadataReference.CreateFromFile(assemblyPath));
}
}
}
}