Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -443,30 +443,7 @@ public async ValueTask<IReadOnlyList<ProjectAction>> GetInstallActionsAsync(

foreach (ResolvedAction resolvedAction in resolvedActions)
{
List<ImplicitProjectAction>? implicitActions = null;

if (resolvedAction.Action is BuildIntegratedProjectAction buildIntegratedAction)
{
implicitActions = new List<ImplicitProjectAction>();

foreach (NuGetProjectAction? buildAction in buildIntegratedAction.GetProjectActions())
{
var implicitAction = new ImplicitProjectAction(
CreateProjectActionId(),
buildAction.PackageIdentity,
buildAction.NuGetProjectActionType);

implicitActions.Add(implicitAction);
}
}

string projectId = resolvedAction.Project.GetMetadata<string>(NuGetProjectMetadataKeys.ProjectId);
var projectAction = new ProjectAction(
CreateProjectActionId(),
projectId,
resolvedAction.Action.PackageIdentity,
resolvedAction.Action.NuGetProjectActionType,
implicitActions);
ProjectAction projectAction = CreateProjectAction(resolvedAction);

_state.ResolvedActions[projectAction.Id] = resolvedAction;

Expand Down Expand Up @@ -578,26 +555,20 @@ public async ValueTask<IReadOnlyList<ProjectAction>> GetUpdateActionsAsync(

NuGetPackageManager packageManager = await _sharedState.PackageManager.GetValueAsync(cancellationToken);
IEnumerable<NuGetProjectAction> actions = await packageManager.PreviewUpdatePackagesAsync(
packageIdentities.ToList(),
projects,
resolutionContext,
projectContext,
primarySources,
secondarySources,
cancellationToken);
packageIdentities.ToList(),
projects,
resolutionContext,
projectContext,
primarySources,
secondarySources,
cancellationToken);

var projectActions = new List<ProjectAction>();

foreach (NuGetProjectAction action in actions)
{
string projectId = action.Project.GetMetadata<string>(NuGetProjectMetadataKeys.ProjectId);
var resolvedAction = new ResolvedAction(action.Project, action);
var projectAction = new ProjectAction(
CreateProjectActionId(),
projectId,
action.PackageIdentity,
action.NuGetProjectActionType,
implicitActions: null);
ProjectAction projectAction = CreateProjectAction(resolvedAction);

_state.ResolvedActions[projectAction.Id] = resolvedAction;

Expand All @@ -622,6 +593,36 @@ public async ValueTask<IReadOnlyCollection<IProjectContextInfo>> GetProjectsWith
return await Task.WhenAll(tasks);
}

private static ProjectAction CreateProjectAction(ResolvedAction resolvedAction)
{
List<ImplicitProjectAction>? implicitActions = null;

if (resolvedAction.Action is BuildIntegratedProjectAction buildIntegratedAction)
{
implicitActions = new List<ImplicitProjectAction>();

foreach (NuGetProjectAction? buildAction in buildIntegratedAction.GetProjectActions())
Comment thread
dtivel marked this conversation as resolved.
Outdated
{
var implicitAction = new ImplicitProjectAction(
CreateProjectActionId(),
buildAction.PackageIdentity,
buildAction.NuGetProjectActionType);

implicitActions.Add(implicitAction);
}
}

string projectId = resolvedAction.Project.GetMetadata<string>(NuGetProjectMetadataKeys.ProjectId);
var projectAction = new ProjectAction(
CreateProjectActionId(),
projectId,
resolvedAction.Action.PackageIdentity,
resolvedAction.Action.NuGetProjectActionType,
implicitActions);

return projectAction;
}

private static string CreateProjectActionId()
{
return Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Frameworks;
using NuGet.LibraryModel;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.ProjectManagement;
Expand Down Expand Up @@ -108,7 +109,7 @@ await PerformOperationAsync(async (projectManager) =>
}

[Fact]
public async Task GetInstallActionsAsync_WithProjectReferenceProject_WhenUpdatingPackage_ReturnsCorrectActions()
public async Task GetInstallActionsAsync_WithPackageReferenceProject_WhenUpdatingPackage_ReturnsCorrectActions()
{
const string projectName = "a";
string projectId = Guid.NewGuid().ToString();
Expand Down Expand Up @@ -210,6 +211,8 @@ await PerformOperationAsync(async (projectManager) =>
Assert.Equal(NuGetProjectActionType.Install, implicitAction.ProjectActionType);

await projectManager.ExecuteActionsAsync(actions, CancellationToken.None);

AddPackageDependency(projectSystemCache, projectNames, packageSpec, packageV1);
});

await PerformOperationAsync(async (projectManager) =>
Expand Down Expand Up @@ -387,6 +390,163 @@ public async Task GetInstalledPackagesAsync_WhenProjectReturnsNullPackageReferen
}
}

[Fact]
public async Task GetUpdateActionsAsync_WithPackageReferenceProject_WhenUpdatingPackage_ReturnsCorrectActions()
{
const string projectName = "a";
string projectId = Guid.NewGuid().ToString();
var projectSystemCache = new ProjectSystemCache();

using (TestDirectory testDirectory = TestDirectory.Create())
{
var packageV1 = new SimpleTestPackageContext(packageId: "b", version: "1.0.0");
var packageV2 = new SimpleTestPackageContext(packageV1.Id, version: "2.0.0");
string packageSourceDirectoryPath = Path.Combine(testDirectory, "packageSource");

await SimpleTestPackageUtility.CreateFolderFeedV3Async(
packageSourceDirectoryPath,
PackageSaveMode.Defaultv3,
packageV1,
packageV2);

var packageSource = new PackageSource(packageSourceDirectoryPath);
var packageSources = new List<PackageSource>() { packageSource };

Initialize(packageSources);

string projectFullPath = Path.Combine(testDirectory.Path, $"{projectName}.csproj");
var unconfiguredProject = new Mock<UnconfiguredProject>();
var configuredProject = new Mock<ConfiguredProject>();
var projectServices = new Mock<ConfiguredProjectServices>();
var packageReferencesService = new Mock<IPackageReferencesService>();
var result = new Mock<IUnresolvedPackageReference>();

unconfiguredProject.Setup(x => x.GetSuggestedConfiguredProjectAsync())
.ReturnsAsync(configuredProject.Object);

configuredProject.SetupGet(x => x.Services)
.Returns(projectServices.Object);

projectServices.SetupGet(x => x.PackageReferences)
.Returns(packageReferencesService.Object);

packageReferencesService.Setup(x => x.AddAsync(It.IsNotNull<string>(), It.IsNotNull<string>()))
.ReturnsAsync(new AddReferenceResult<IUnresolvedPackageReference>(result.Object, added: true));

var nuGetProjectServices = new Mock<INuGetProjectServices>();

nuGetProjectServices.SetupGet(x => x.ScriptService)
.Returns(Mock.Of<IProjectScriptHostService>());

var project = new CpsPackageReferenceProject(
projectName: projectName,
projectUniqueName: projectFullPath,
projectFullPath: projectFullPath,
projectSystemCache,
unconfiguredProject.Object,
nuGetProjectServices.Object,
projectId);

PackageSpec packageSpec = CreatePackageSpec(
project.ProjectName,
Path.Combine(testDirectory, "package.spec"));
DependencyGraphSpec projectRestoreInfo = ProjectJsonTestHelpers.GetDGSpecFromPackageSpecs(packageSpec);
projectRestoreInfo.AddProject(packageSpec);
var projectNames = new ProjectNames(
fullName: projectFullPath,
uniqueName: projectFullPath,
shortName: projectName,
customUniqueName: projectName,
projectId: projectId);
projectSystemCache.AddProjectRestoreInfo(projectNames, projectRestoreInfo, Array.Empty<IAssetsLogMessage>());

_solutionManager.NuGetProjects.Add(project);

string[] projectIds = new[] { projectId };
string[] packageSourceNames = new[] { packageSource.Name };

await PerformOperationAsync(async (projectManager) =>
{
IReadOnlyList<ProjectAction> actions = await projectManager.GetInstallActionsAsync(
projectIds,
packageV1.Identity,
VersionConstraints.None,
includePrelease: true,
DependencyBehavior.Lowest,
packageSourceNames,
CancellationToken.None);

Assert.NotEmpty(actions);
Assert.Equal(1, actions.Count);

ProjectAction action = actions[0];

Assert.Equal(packageV1.Identity, action.PackageIdentity);
Assert.Equal(NuGetProjectActionType.Install, action.ProjectActionType);
Assert.Equal(projectId, action.ProjectId);

Assert.Equal(1, action.ImplicitActions.Count);

ImplicitProjectAction implicitAction = action.ImplicitActions[0];

Assert.Equal(packageV1.Identity, implicitAction.PackageIdentity);
Assert.Equal(NuGetProjectActionType.Install, implicitAction.ProjectActionType);

await projectManager.ExecuteActionsAsync(actions, CancellationToken.None);

AddPackageDependency(projectSystemCache, projectNames, packageSpec, packageV1);
});

await PerformOperationAsync(async (projectManager) =>
{
IReadOnlyList<ProjectAction> actions = await projectManager.GetUpdateActionsAsync(
projectIds,
new[] { packageV2.Identity },
VersionConstraints.None,
includePrelease: true,
DependencyBehavior.Lowest,
packageSourceNames,
CancellationToken.None);

Assert.NotEmpty(actions);
Assert.Equal(1, actions.Count);

ProjectAction action = actions[0];

Assert.Equal(packageV2.Identity, action.PackageIdentity);
Assert.Equal(NuGetProjectActionType.Install, action.ProjectActionType);
Assert.Equal(projectId, action.ProjectId);

Assert.Equal(2, action.ImplicitActions.Count);

ImplicitProjectAction implicitAction = action.ImplicitActions[0];

Assert.Equal(packageV1.Identity, implicitAction.PackageIdentity);
Assert.Equal(NuGetProjectActionType.Uninstall, implicitAction.ProjectActionType);

implicitAction = action.ImplicitActions[1];

Assert.Equal(packageV2.Identity, implicitAction.PackageIdentity);
Assert.Equal(NuGetProjectActionType.Install, implicitAction.ProjectActionType);
});
}
}

private static void AddPackageDependency(ProjectSystemCache projectSystemCache, ProjectNames projectNames, PackageSpec packageSpec, SimpleTestPackageContext package)
{
var dependency = new LibraryDependency()
{
LibraryRange = new LibraryRange(
name: package.Id,
versionRange: new VersionRange(package.Identity.Version),
typeConstraint: LibraryDependencyTarget.Package)
};

packageSpec.TargetFrameworks.First().Dependencies.Add(dependency);
DependencyGraphSpec projectRestoreInfo = ProjectJsonTestHelpers.GetDGSpecFromPackageSpecs(packageSpec);
projectSystemCache.AddProjectRestoreInfo(projectNames, projectRestoreInfo, Array.Empty<IAssetsLogMessage>());
}

private void Initialize(IReadOnlyList<PackageSource> packageSources = null)
{
SourceRepositoryProvider sourceRepositoryProvider;
Expand Down