Skip to content

Commit

Permalink
Fix PackageSaveMode nuspec always reinstalls (#4148)
Browse files Browse the repository at this point in the history
* Fix PackageSaveMode nuspec always reinstalls

* Enable PackageSaveMode unit tests

* Default to v2 if PackageSaveMode is not specified
  • Loading branch information
huangqinjin committed Aug 5, 2021
1 parent 87f0137 commit 343ec0f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
15 changes: 8 additions & 7 deletions src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,14 @@ private CommandLineSourceRepositoryProvider GetSourceRepositoryProvider()

var packageIdentity = new PackageIdentity(packageId, version);

var PackageSaveMode = Packaging.PackageSaveMode.Defaultv2;
if (EffectivePackageSaveMode != Packaging.PackageSaveMode.None)
{
PackageSaveMode = EffectivePackageSaveMode;
}

// Check if the package already exists or a higher version exists already.
var skipInstall = project.PackageExists(packageIdentity);
var skipInstall = project.PackageExists(packageIdentity, PackageSaveMode);

// For SxS allow other versions to install. For non-SxS skip if a higher version exists.
skipInstall |= (ExcludeVersion && alreadyInstalledVersions.Any(e => e >= version));
Expand All @@ -352,17 +358,12 @@ private CommandLineSourceRepositoryProvider GetSourceRepositoryProvider()
var projectContext = new ConsoleProjectContext(Console)
{
PackageExtractionContext = new PackageExtractionContext(
Packaging.PackageSaveMode.Defaultv2,
PackageSaveMode,
PackageExtractionBehavior.XmlDocFileSaveMode,
clientPolicyContext,
Console)
};

if (EffectivePackageSaveMode != Packaging.PackageSaveMode.None)
{
projectContext.PackageExtractionContext.PackageSaveMode = EffectivePackageSaveMode;
}

resolutionContext.SourceCacheContext.NoCache = NoCache;
resolutionContext.SourceCacheContext.DirectDownload = DirectDownload;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public bool PackageExists(PackageIdentity packageIdentity, PackageSaveMode packa
if (manifestExists)
{
var reader = new NuspecReader(nuspecPath);
packageExists = packageIdentity.Equals(reader.GetIdentity());
manifestExists = packageIdentity.Equals(reader.GetIdentity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ public void InstallCommand_FromPackagesConfigFile_SpecifyingRelativeSolutionDir(
}
}

[Fact]
public void InstallCommand_PackageSaveModeNuspec()
{
using (var pathContext = new SimpleTestPathContext())
Expand All @@ -713,14 +714,15 @@ public void InstallCommand_PackageSaveModeNuspec()
// Assert
var nuspecFile = Path.Combine(
outputDirectory,
"testPackage1.1.1.0", "testPackage1.1.1.0.nuspec");
"testPackage1.1.1.0", "testPackage1.nuspec");

Assert.True(File.Exists(nuspecFile));
var nupkgFiles = Directory.GetFiles(outputDirectory, "*.nupkg", SearchOption.AllDirectories);
Assert.Equal(0, nupkgFiles.Length);
}
}

[Fact]
public void InstallCommand_PackageSaveModeNupkg()
{
using (var pathContext = new SimpleTestPathContext())
Expand All @@ -743,14 +745,15 @@ public void InstallCommand_PackageSaveModeNupkg()
// Assert
var nupkgFile = Path.Combine(
outputDirectory,
"testPackage1.1.1.0", "testPackage1.1.1.0.nuspec");
"testPackage1.1.1.0", "testPackage1.1.1.0.nupkg");

Assert.True(File.Exists(nupkgFile));
var nuspecFiles = Directory.GetFiles(outputDirectory, "*.nuspec", SearchOption.AllDirectories);
Assert.Equal(0, nuspecFiles.Length);
}
}

[Fact]
public void InstallCommand_PackageSaveModeNuspecNupkg()
{
using (var pathContext = new SimpleTestPathContext())
Expand All @@ -773,8 +776,10 @@ public void InstallCommand_PackageSaveModeNuspecNupkg()
// Assert
var nupkgFile = Path.Combine(
outputDirectory,
"testPackage1.1.1.0", "testPackage1.1.1.0.nuspec");
var nuspecFile = Path.ChangeExtension(nupkgFile, "nuspec");
"testPackage1.1.1.0", "testPackage1.1.1.0.nupkg");
var nuspecFile = Path.Combine(
outputDirectory,
"testPackage1.1.1.0", "testPackage1.nuspec");

Assert.True(File.Exists(nupkgFile));
Assert.True(File.Exists(nuspecFile));
Expand All @@ -784,6 +789,7 @@ public void InstallCommand_PackageSaveModeNuspecNupkg()
// Test that after a package is installed with -PackageSaveMode nuspec, nuget.exe
// can detect that the package is already installed when trying to install the same
// package.
[Fact]
public void InstallCommand_PackageSaveModeNuspecReinstall()
{
using (var pathContext = new SimpleTestPathContext())
Expand All @@ -796,25 +802,31 @@ public void InstallCommand_PackageSaveModeNuspecReinstall()
"testPackage1", "1.1.0", source);

var args = new string[] {
"-ForceEnglishOutput",
"-OutputDirectory", outputDirectory,
"-Source", source,
"-PackageSaveMode", "nuspec" };
var r = Program.Main(args);
Assert.Equal(0, r);

// Act
var r = RunInstall(pathContext, "testPackage1", 0, args);

// Assert
Assert.Equal(0, r.Item1);

// Act (Install a second time)
var result = RunInstall(pathContext, "testPackage1", 0, args);

var output = result.Item2;

// Assert
var expectedOutput = "'testPackage1 1.1.0' already installed." +
Environment.NewLine;
Assert.Equal(expectedOutput, output);
var alreadyInstalledMessage = "Package \"testPackage1.1.1.0\" is already installed.";
Assert.Contains(alreadyInstalledMessage, output, StringComparison.OrdinalIgnoreCase);
r.ExitCode.Should().Be(0);
}
}

// Test that PackageSaveMode specified in nuget.config file is used.
[Fact]
public void InstallCommand_PackageSaveModeInConfigFile()
{
using (var pathContext = new SimpleTestPathContext())
Expand Down Expand Up @@ -847,7 +859,7 @@ public void InstallCommand_PackageSaveModeInConfigFile()

var nuspecFile = Path.Combine(
outputDirectory,
"testPackage1.1.1.0", "testPackage1.1.1.0.nuspec");
"testPackage1.1.1.0", "testPackage1.nuspec");

Assert.True(File.Exists(nuspecFile));
var nupkgFiles = Directory.GetFiles(outputDirectory, "*.nupkg", SearchOption.AllDirectories);
Expand Down

0 comments on commit 343ec0f

Please sign in to comment.