Skip to content

Commit

Permalink
created new assets file missing exception
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrrm committed Aug 31, 2022
1 parent 23dceff commit d15887c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NuGet.VisualStudio.Implementation.Exceptions
{
internal class AssetsFileMissingException : InvalidOperationException
{
public AssetsFileMissingException() : base()
{
}

public AssetsFileMissingException(string message) : base(message)
{
}

public AssetsFileMissingException(string message, Exception innerException) : base(message, innerException)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using NuGet.ProjectManagement.Projects;
using NuGet.ProjectModel;
using NuGet.VisualStudio.Etw;
using NuGet.VisualStudio.Implementation.Exceptions;
using NuGet.VisualStudio.Implementation.Resources;
using NuGet.VisualStudio.Telemetry;
using Task = System.Threading.Tasks.Task;
Expand Down Expand Up @@ -166,12 +167,16 @@ public bool TryCreateContext(string projectUniqueName, out IVsPathContext output

return outputPathContext != null;
}
catch (Exception exception)
catch (Exception e) when (e is AssetsFileMissingException)
{
_telemetryProvider.PostFault(exception, typeof(VsPathContextProvider).FullName);
outputPathContext = null;
return false;
}
catch (Exception exception)
{
_telemetryProvider.PostFault(exception, typeof(VsPathContextProvider).FullName);
throw;
}
}

public bool TryCreateSolutionContext(out IVsPathContext2 outputPathContext)
Expand Down Expand Up @@ -271,12 +276,12 @@ internal async Task<IVsPathContext> CreatePathContextAsync(NuGetProject nuGetPro
}
}
}
catch (Exception e) when (e is KeyNotFoundException || e is InvalidOperationException)
catch (Exception e) when (e is AssetsFileMissingException)
{
var projectUniqueName = NuGetProject.GetUniqueNameOrName(nuGetProject);
var errorMessage = string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_CreateContextError, projectUniqueName, e.Message);
_logger.Value.LogError(errorMessage);
throw new InvalidOperationException(errorMessage, e);
throw new AssetsFileMissingException(errorMessage, e);
}

return context;
Expand All @@ -296,7 +301,7 @@ private async Task<IVsPathContext> GetPathContextFromAssetsFileAsync(

if ((lockFile?.PackageFolders?.Count ?? 0) == 0)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_LockFileError));
throw new AssetsFileMissingException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_LockFileError));
}

// The user packages folder is always the first package folder. Subsequent package folders are always
Expand Down Expand Up @@ -327,7 +332,7 @@ private async Task<IVsPathContext> GetPathContextFromAssetsFileAsync(
var packageInstallPath = fppr.GetPackageDirectory(pid.Id, pid.Version);
if (string.IsNullOrEmpty(packageInstallPath))
{
throw new KeyNotFoundException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_PackageDirectoryNotFound, pid));
throw new AssetsFileMissingException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_PackageDirectoryNotFound, pid));
}

trie[packageInstallPath] = packageInstallPath;
Expand Down Expand Up @@ -355,7 +360,7 @@ private async Task<IVsPathContext> GetPathContextFromPackagesConfigAsync(
var packageInstallPath = msbuildNuGetProject.FolderNuGetProject.GetInstalledPath(pid);
if (string.IsNullOrEmpty(packageInstallPath))
{
throw new KeyNotFoundException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_PackageDirectoryNotFound, pid));
throw new AssetsFileMissingException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_PackageDirectoryNotFound, pid));
}

trie[packageInstallPath] = packageInstallPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using NuGet.ProjectModel;
using NuGet.Test.Utility;
using NuGet.Versioning;
using NuGet.VisualStudio.Implementation.Exceptions;
using NuGet.VisualStudio.Implementation.Extensibility;
using NuGet.VisualStudio.Implementation.Resources;
using NuGet.VisualStudio.Telemetry;
Expand Down Expand Up @@ -238,15 +239,14 @@ await SimpleTestPackageUtility.CreateFolderFeedPackagesConfigAsync(
}

[Fact]
public async Task CreatePathContextAsync_WithUnrestoredPackagesConfig_LogsError()
public async Task CreatePathContextAsync_WithUnrestoredPackagesConfig_Throws()
{
// Arrange
using (var testDirectory = TestDirectory.Create())
{
var userPackageFolder = Path.Combine(testDirectory.Path, "packagesA");

var settings = Mock.Of<ISettings>();
var logger = new Mock<ILogger>();
Mock.Get(settings)
.Setup(x => x.GetSection("config"))
.Returns(() => new VirtualSettingSection("config",
Expand All @@ -256,7 +256,7 @@ public async Task CreatePathContextAsync_WithUnrestoredPackagesConfig_LogsError(
var target = new VsPathContextProvider(
settings,
Mock.Of<IVsSolutionManager>(),
logger.Object,
Mock.Of<ILogger>(),
getLockFileOrNullAsync: null,
_telemetryProvider.Object);

Expand All @@ -280,14 +280,9 @@ public async Task CreatePathContextAsync_WithUnrestoredPackagesConfig_LogsError(
});

// Act
var result = await target.CreatePathContextAsync(project.Object, CancellationToken.None);

// Assert
var message = string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_PackageDirectoryNotFound, "Foo.1.0.1");
var errorMessage = string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_CreateContextError, projectUniqueName, message);
logger.Verify(l => l.LogError(errorMessage));

Assert.Equal(null, result);
var exception = await Assert.ThrowsAsync<AssetsFileMissingException>(() => target.CreatePathContextAsync(project.Object, CancellationToken.None));
Assert.Equal(0, _telemetryProvider.Invocations.Count);
Assert.Contains(projectUniqueName, exception.Message);
}
}

Expand Down Expand Up @@ -440,29 +435,23 @@ public void CreateSolutionContext_WithSolutionDirectory()
}

[Fact]
public async Task CreatePathContextAsync_WithUnrestoredPackageReference_LogsError()
public async Task CreatePathContextAsync_WithUnrestoredPackageReference_Throws()
{
var logger = new Mock<ILogger>();
// Arrange
var target = new VsPathContextProvider(
Mock.Of<ISettings>(),
Mock.Of<IVsSolutionManager>(),
logger.Object,
Mock.Of<ILogger>(),
getLockFileOrNullAsync: _ => Task.FromResult(null as LockFile),
_telemetryProvider.Object);

var projectUniqueName = Guid.NewGuid().ToString();

var project = new TestPackageReferenceProject(projectUniqueName);

// Act
var result = await target.CreatePathContextAsync(project, CancellationToken.None);

// Assert
var message = string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_LockFileError);
var errorMessage = string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_CreateContextError, projectUniqueName, message);
logger.Verify(l => l.LogError(errorMessage));

Assert.Equal(null, result);
var exception = await Assert.ThrowsAsync<AssetsFileMissingException>(() => target.CreatePathContextAsync(project, CancellationToken.None));
Assert.Contains(projectUniqueName, exception.Message);
}

[Fact]
Expand Down

0 comments on commit d15887c

Please sign in to comment.