Skip to content

Commit 4d47720

Browse files
ericstjdougbu
andauthored
Ensure AppLocalResolver handles package with multiple libs (#27751)
* Ensure AppLocalResolver handles package with multiple libs This was failing to pass references to a package which contains more than one lib. * Use `AppBaseCompilationAssemblyResolver` in `DiagnosticProject` * Fix, then reduce use of `TestPathUtilities.GetSolutionRootDirectory(...)` (#27404) - do not traverse up past the `$(RepoRoot)` when running tests locally - if `$(RepoRoot)` is reached, search down for named `*.slnf` file - find `*.slnf` file when `$(OutputPath)` is under artifacts/bin/ - analyzer test projects publish needed files anyhow - remove `GetProjectDirectory()` helpers Co-authored-by: Doug Bunting <[email protected]>
1 parent c0edc35 commit 4d47720

File tree

5 files changed

+26
-86
lines changed

5 files changed

+26
-86
lines changed

src/Analyzers/Microsoft.AspNetCore.Analyzer.Testing/src/DiagnosticProject.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class DiagnosticProject
2525
/// </summary>
2626
public static string TestProjectName = "TestProject";
2727

28+
private static readonly ICompilationAssemblyResolver _assemblyResolver = new AppBaseCompilationAssemblyResolver();
2829
private static readonly Dictionary<Assembly, Solution> _solutionCache = new Dictionary<Assembly, Solution>();
2930

3031
public static Project Create(Assembly testAssembly, string[] sources)
@@ -41,7 +42,7 @@ public static Project Create(Assembly testAssembly, string[] sources)
4142

4243
foreach (var defaultCompileLibrary in DependencyContext.Load(testAssembly).CompileLibraries)
4344
{
44-
foreach (var resolveReferencePath in defaultCompileLibrary.ResolveReferencePaths(new AppLocalResolver()))
45+
foreach (var resolveReferencePath in defaultCompileLibrary.ResolveReferencePaths(_assemblyResolver))
4546
{
4647
solution = solution.AddMetadataReference(projectId, MetadataReference.CreateFromFile(resolveReferencePath));
4748
}
@@ -69,31 +70,5 @@ public static Project Create(Assembly testAssembly, string[] sources)
6970

7071
return solution.GetProject(testProject);
7172
}
72-
73-
// Required to resolve compilation assemblies inside unit tests
74-
private class AppLocalResolver : ICompilationAssemblyResolver
75-
{
76-
public bool TryResolveAssemblyPaths(CompilationLibrary library, List<string> assemblies)
77-
{
78-
foreach (var assembly in library.Assemblies)
79-
{
80-
var dll = Path.Combine(Directory.GetCurrentDirectory(), "refs", Path.GetFileName(assembly));
81-
if (File.Exists(dll))
82-
{
83-
assemblies.Add(dll);
84-
return true;
85-
}
86-
87-
dll = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(assembly));
88-
if (File.Exists(dll))
89-
{
90-
assemblies.Add(dll);
91-
return true;
92-
}
93-
}
94-
95-
return false;
96-
}
97-
}
9873
}
9974
}

src/Components/Analyzers/test/AnalyzerTestBase.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
using System.IO;
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Analyzer.Testing;
8-
using Microsoft.AspNetCore.Testing;
98
using Microsoft.CodeAnalysis;
109

1110
namespace Microsoft.AspNetCore.Components.Analyzers
1211
{
1312
public abstract class AnalyzerTestBase
1413
{
15-
private static readonly string ProjectDirectory = GetProjectDirectory();
14+
// Test files are copied to both the bin/ and publish/ folders. Use BaseDirectory on or off Helix.
15+
private static readonly string ProjectDirectory = AppContext.BaseDirectory;
1616

1717
public TestSource Read(string source)
1818
{
1919
if (!source.EndsWith(".cs"))
2020
{
21-
source = source + ".cs";
21+
source += ".cs";
2222
}
2323

2424
var filePath = Path.Combine(ProjectDirectory, "TestFiles", GetType().Name, source);
@@ -35,7 +35,7 @@ public Project CreateProject(string source)
3535
{
3636
if (!source.EndsWith(".cs"))
3737
{
38-
source = source + ".cs";
38+
source += ".cs";
3939
}
4040

4141
var read = Read(source);
@@ -46,22 +46,5 @@ public Task<Compilation> CreateCompilationAsync(string source)
4646
{
4747
return CreateProject(source).GetCompilationAsync();
4848
}
49-
50-
private static string GetProjectDirectory()
51-
{
52-
// On helix we use the published test files
53-
if (SkipOnHelixAttribute.OnHelix())
54-
{
55-
return AppContext.BaseDirectory;
56-
}
57-
58-
// This test code needs to be updated to support distributed testing.
59-
// See https://github.com/dotnet/aspnetcore/issues/10422
60-
#pragma warning disable 0618
61-
var solutionDirectory = TestPathUtilities.GetSolutionRootDirectory("Components");
62-
#pragma warning restore 0618
63-
var projectDirectory = Path.Combine(solutionDirectory, "Analyzers", "test");
64-
return projectDirectory;
65-
}
6649
}
6750
}
Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
55
using System.IO;
66
using Microsoft.AspNetCore.Analyzer.Testing;
7-
using Microsoft.AspNetCore.Testing;
87

98
namespace Microsoft.AspNetCore.Mvc
109
{
1110
public static class MvcTestSource
1211
{
13-
private static readonly string ProjectDirectory = GetProjectDirectory();
12+
// Test files are copied to both the bin/ and publish/ folders. Use BaseDirectory on or off Helix.
13+
private static readonly string ProjectDirectory = AppContext.BaseDirectory;
1414

1515
public static TestSource Read(string testClassName, string testMethod)
1616
{
@@ -23,21 +23,5 @@ public static TestSource Read(string testClassName, string testMethod)
2323
var fileContent = File.ReadAllText(filePath);
2424
return TestSource.Read(fileContent);
2525
}
26-
27-
private static string GetProjectDirectory()
28-
{
29-
// On helix we use the published test files
30-
if (SkipOnHelixAttribute.OnHelix())
31-
{
32-
return AppContext.BaseDirectory;
33-
}
34-
35-
// https://github.com/dotnet/aspnetcore/issues/9431
36-
#pragma warning disable 0618
37-
var solutionDirectory = TestPathUtilities.GetSolutionRootDirectory("Mvc");
38-
#pragma warning restore 0618
39-
var projectDirectory = Path.Combine(solutionDirectory, "Mvc.Analyzers", "test");
40-
return projectDirectory;
41-
}
4226
}
4327
}
Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
55
using System.IO;
66
using Microsoft.AspNetCore.Analyzer.Testing;
7-
using Microsoft.AspNetCore.Testing;
87

98
namespace Microsoft.AspNetCore.Mvc
109
{
1110
public static class MvcTestSource
1211
{
13-
private static readonly string ProjectDirectory = GetProjectDirectory();
12+
// Test files are copied to both the bin/ and publish/ folders. Use BaseDirectory on or off Helix.
13+
private static readonly string ProjectDirectory = AppContext.BaseDirectory;
1414

1515
public static TestSource Read(string testClassName, string testMethod)
1616
{
@@ -23,21 +23,5 @@ public static TestSource Read(string testClassName, string testMethod)
2323
var fileContent = File.ReadAllText(filePath);
2424
return TestSource.Read(fileContent);
2525
}
26-
27-
private static string GetProjectDirectory()
28-
{
29-
// On helix we use the published test files
30-
if (SkipOnHelixAttribute.OnHelix())
31-
{
32-
return AppContext.BaseDirectory;
33-
}
34-
35-
// https://github.com/dotnet/aspnetcore/issues/9431
36-
#pragma warning disable 0618
37-
var solutionDirectory = TestPathUtilities.GetSolutionRootDirectory("Mvc");
38-
#pragma warning restore 0618
39-
var projectDirectory = Path.Combine(solutionDirectory, "Mvc.Api.Analyzers", "test");
40-
return projectDirectory;
41-
}
4226
}
4327
}

src/Testing/src/TestPathUtilities.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ public static string GetSolutionRootDirectory(string solution)
2222
return projectFileInfo.DirectoryName;
2323
}
2424

25+
projectFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, "AspNetCore.sln"));
26+
if (projectFileInfo.Exists)
27+
{
28+
// Have reached the solution root. Work down through the src/ folder to find the solution filter.
29+
directoryInfo = new DirectoryInfo(Path.Combine(directoryInfo.FullName, "src"));
30+
foreach (var solutionFileInfo in directoryInfo.EnumerateFiles($"{solution}.slnf", SearchOption.AllDirectories))
31+
{
32+
return solutionFileInfo.DirectoryName;
33+
}
34+
35+
// No luck. Exit loop and error out.
36+
break;
37+
}
38+
2539
directoryInfo = directoryInfo.Parent;
2640
}
2741
while (directoryInfo.Parent != null);

0 commit comments

Comments
 (0)