提交 15525f29 编写于 作者: C Christopher Schütz

Fix issue with "Add Import" code fix

上级 92f024ab
......@@ -277,6 +277,39 @@ public async Task TestFailedInstallRollsBackFile()
installerServiceMock.Verify();
}
[WorkItem(40857, "https://github.com/dotnet/roslyn/issues/40857")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddImport)]
public async Task TestNuGetFailureDoesNotImpactCodeFix()
{
var installerServiceMock = new Mock<IPackageInstallerService>(MockBehavior.Strict);
installerServiceMock.Setup(i => i.IsEnabled(It.IsAny<ProjectId>())).Returns(true);
installerServiceMock.Setup(i => i.GetPackageSources()).Throws(new Exception("Test Exception"));
var packageServiceMock = new Mock<ISymbolSearchService>(MockBehavior.Strict);
packageServiceMock.Setup(s => s.FindReferenceAssembliesWithTypeAsync("NuGetType", 0, It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<IList<ReferenceAssemblyWithTypeResult>>(new List<ReferenceAssemblyWithTypeResult>()));
packageServiceMock.Setup(s => s.FindPackagesWithTypeAsync(NugetOrgSource, "NuGetType", 0, It.IsAny<CancellationToken>()))
.Returns(CreateSearchResult(null));
await TestInRegularAndScriptAsync(
@"class Class
{
[|IDictionary|] Method()
{
Goo();
}
}",
@"using System.Collections;
class Class
{
IDictionary Method()
{
Goo();
}
}", fixProviderData: new FixProviderData(installerServiceMock.Object, packageServiceMock.Object));
}
private Task<IList<PackageWithTypeResult>> CreateSearchResult(
string packageName, string typeName, ImmutableArray<string> containingNamespaceNames)
{
......
......@@ -56,9 +56,19 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
: null;
var installerService = GetPackageInstallerService(document);
var packageSources = searchNuGetPackages && symbolSearchService != null && installerService?.IsEnabled(document.Project.Id) == true
? installerService.GetPackageSources()
: ImmutableArray<PackageSource>.Empty;
ImmutableArray<PackageSource> packageSources;
try
{
// Ensure any issues with getting access to package sources will not break the Code Fix for local imports
// https://github.com/dotnet/roslyn/issues/40857
packageSources = searchNuGetPackages && symbolSearchService != null && installerService?.IsEnabled(document.Project.Id) == true
? installerService.GetPackageSources()
: ImmutableArray<PackageSource>.Empty;
}
catch
{
packageSources = ImmutableArray<PackageSource>.Empty;
}
var fixesForDiagnostic = await addImportService.GetFixesForDiagnosticsAsync(
document, span, diagnostics, MaxResults, symbolSearchService, searchReferenceAssemblies, packageSources, cancellationToken).ConfigureAwait(false);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册