Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added update command line argument -Version to fix Issue 1760 #570

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -12,6 +12,7 @@
using NuGet.ProjectManagement;
using NuGet.Protocol.Core.Types;
using NuGet.Packaging.Core;
using NuGet.Versioning;

namespace NuGet.CommandLine
{
Expand All @@ -25,6 +26,9 @@ public class UpdateCommand : Command
[Option(typeof(NuGetCommand), "UpdateCommandIdDescription")]
public ICollection<string> Id { get; } = new List<string>();

[Option(typeof(NuGetCommand), "UpdateCommandVersionDescription")]
public string Version { get; set; }

[Option(typeof(NuGetCommand), "UpdateCommandRepositoryPathDescription")]
public string RepositoryPath { get; set; }

Expand Down Expand Up @@ -265,10 +269,13 @@ private async Task UpdatePackagesAsync(MSBuildProjectSystem project, string pack

var installed = await nugetProject.GetInstalledPackagesAsync(CancellationToken.None);

// If -Id has been specified and has exactly one package, use the explicit version requested
var targetVersion = Version != null && Id != null && Id.Count == 1 ? new NuGetVersion(Version) : null;

var targetIdentities = installed
.Select(pr => pr.PackageIdentity.Id)
.Where(id => targetIds.Contains(id))
.Select(id => new PackageIdentity(id, null))
.Select(id => new PackageIdentity(id, targetVersion))
.ToList();

if (targetIdentities.Any())
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/NuGet.Clients/NuGet.CommandLine/NuGetCommand.resx
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ nuget spec -a MyAssembly.dll</value>
<data name="UpdateCommandSourceDescription" xml:space="preserve">
<value>A list of package sources to search for updates.</value>
</data>
<data name="UpdateCommandVersionDescription" xml:space="preserve">
<value>Updates the package in -Id to the version indicated. Requires -Id to contain exactly one package id.</value>
</data>
<data name="UpdateCommandUsageExamples" xml:space="preserve">
<value>nuget update

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,188 @@ await msBuildProject.InstallPackageAsync(
}
}

[Fact]
public async Task UpdateCommand_Success_Version_Upgrade()
{
using (var packagesSourceDirectory = TestFileSystemUtility.CreateRandomTestFolder())
using (var solutionDirectory = TestFileSystemUtility.CreateRandomTestFolder())
using (var workingPath = TestFileSystemUtility.CreateRandomTestFolder())
{
var projectDirectory = Path.Combine(solutionDirectory, "proj1");
var packagesDirectory = Path.Combine(solutionDirectory, "packages");

var a1 = new PackageIdentity("A", new NuGetVersion("1.0.0"));
var a2 = new PackageIdentity("A", new NuGetVersion("2.0.0"));
var a3 = new PackageIdentity("A", new NuGetVersion("3.0.0"));

var a1Package = Util.CreateTestPackage(
a1.Id,
a1.Version.ToString(),
packagesSourceDirectory,
new List<NuGetFramework>() { NuGetFramework.Parse("net45") },
new List<PackageDependencyGroup>() { });

var a2Package = Util.CreateTestPackage(
a2.Id,
a2.Version.ToString(),
packagesSourceDirectory,
new List<NuGetFramework>() { NuGetFramework.Parse("net45") },
new List<PackageDependencyGroup>() { });

var a3Package = Util.CreateTestPackage(
a3.Id,
a3.Version.ToString(),
packagesSourceDirectory,
new List<NuGetFramework>() { NuGetFramework.Parse("net45") },
new List<PackageDependencyGroup>() { });

var packagesFolder = PathUtility.GetRelativePath(projectDirectory, packagesDirectory);

Directory.CreateDirectory(projectDirectory);
// create project 1
Util.CreateFile(
projectDirectory,
"proj1.csproj",
Util.CreateProjFileContent());

Util.CreateFile(solutionDirectory, "a.sln",
Util.CreateSolutionFileContent());

var projectFile = Path.Combine(projectDirectory, "proj1.csproj");
var solutionFile = Path.Combine(solutionDirectory, "a.sln");

var testNuGetProjectContext = new TestNuGetProjectContext();
var msbuildDirectory = MsBuildUtility.GetMsbuildDirectory("14.0", null);
var projectSystem = new MSBuildProjectSystem(msbuildDirectory, projectFile, testNuGetProjectContext);
var msBuildProject = new MSBuildNuGetProject(projectSystem, packagesDirectory, projectDirectory);
using (var stream = File.OpenRead(a1Package))
{
var downloadResult = new DownloadResourceResult(stream);
await msBuildProject.InstallPackageAsync(
a1,
downloadResult,
testNuGetProjectContext,
CancellationToken.None);
}

var args = new[]
{
"update",
solutionFile,
"-Source",
packagesSourceDirectory,
"-Version",
"2.0.0",
"-Id",
"A"
};

var r = CommandRunner.Run(
Util.GetNuGetExePath(),
workingPath,
string.Join(" ", args),
waitForExit: true);

Assert.True(r.Item1 == 0, "Output is " + r.Item2 + ". Error is " + r.Item3);

var content = File.ReadAllText(projectFile);
Assert.False(content.Contains(@"<HintPath>..\packages\A.1.0.0\lib\net45\file.dll</HintPath>"));
Assert.True(content.Contains(@"<HintPath>..\packages\A.2.0.0\lib\net45\file.dll</HintPath>"));
Assert.False(content.Contains(@"<HintPath>..\packages\A.3.0.0\lib\net45\file.dll</HintPath>"));
}
}

[Fact]
public async Task UpdateCommand_Success_Version_Downgrade()
{
using (var packagesSourceDirectory = TestFileSystemUtility.CreateRandomTestFolder())
using (var solutionDirectory = TestFileSystemUtility.CreateRandomTestFolder())
using (var workingPath = TestFileSystemUtility.CreateRandomTestFolder())
{
var projectDirectory = Path.Combine(solutionDirectory, "proj1");
var packagesDirectory = Path.Combine(solutionDirectory, "packages");

var a1 = new PackageIdentity("A", new NuGetVersion("1.0.0"));
var a2 = new PackageIdentity("A", new NuGetVersion("2.0.0"));
var a3 = new PackageIdentity("A", new NuGetVersion("3.0.0"));

var a1Package = Util.CreateTestPackage(
a1.Id,
a1.Version.ToString(),
packagesSourceDirectory,
new List<NuGetFramework>() { NuGetFramework.Parse("net45") },
new List<PackageDependencyGroup>() { });

var a2Package = Util.CreateTestPackage(
a2.Id,
a2.Version.ToString(),
packagesSourceDirectory,
new List<NuGetFramework>() { NuGetFramework.Parse("net45") },
new List<PackageDependencyGroup>() { });

var a3Package = Util.CreateTestPackage(
a3.Id,
a3.Version.ToString(),
packagesSourceDirectory,
new List<NuGetFramework>() { NuGetFramework.Parse("net45") },
new List<PackageDependencyGroup>() { });

var packagesFolder = PathUtility.GetRelativePath(projectDirectory, packagesDirectory);

Directory.CreateDirectory(projectDirectory);
// create project 1
Util.CreateFile(
projectDirectory,
"proj1.csproj",
Util.CreateProjFileContent());

Util.CreateFile(solutionDirectory, "a.sln",
Util.CreateSolutionFileContent());

var projectFile = Path.Combine(projectDirectory, "proj1.csproj");
var solutionFile = Path.Combine(solutionDirectory, "a.sln");

var testNuGetProjectContext = new TestNuGetProjectContext();
var msbuildDirectory = MsBuildUtility.GetMsbuildDirectory("14.0", null);
var projectSystem = new MSBuildProjectSystem(msbuildDirectory, projectFile, testNuGetProjectContext);
var msBuildProject = new MSBuildNuGetProject(projectSystem, packagesDirectory, projectDirectory);
using (var stream = File.OpenRead(a2Package))
{
var downloadResult = new DownloadResourceResult(stream);
await msBuildProject.InstallPackageAsync(
a2,
downloadResult,
testNuGetProjectContext,
CancellationToken.None);
}

var args = new[]
{
"update",
solutionFile,
"-Source",
packagesSourceDirectory,
"-Version",
"1.0.0",
"-Id",
"A"
};

var r = CommandRunner.Run(
Util.GetNuGetExePath(),
workingPath,
string.Join(" ", args),
waitForExit: true);

Assert.True(r.Item1 == 0, "Output is " + r.Item2 + ". Error is " + r.Item3);

var content = File.ReadAllText(projectFile);
Assert.True(content.Contains(@"<HintPath>..\packages\A.1.0.0\lib\net45\file.dll</HintPath>"));
Assert.False(content.Contains(@"<HintPath>..\packages\A.2.0.0\lib\net45\file.dll</HintPath>"));
Assert.False(content.Contains(@"<HintPath>..\packages\A.3.0.0\lib\net45\file.dll</HintPath>"));
}
}

[Fact]
public async Task UpdateCommand_Success_ProjectFile_References()
{
Expand Down