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

fix : Restore does not generate lock file on NoOp #2799

Merged
merged 6 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -130,7 +130,7 @@ public async Task<RestoreResult> ExecuteAsync(CancellationToken token)
{
noOpTelemetry.StartIntervalMeasure();

var noOpSuccess = NoOpRestoreUtilities.VerifyAssetsAndMSBuildFilesAndPackagesArePresent(_request);
var noOpSuccess = NoOpRestoreUtilities.VerifyAssetsAndMSBuildFilesAndLockFilesAndPackagesArePresent(_request);

noOpTelemetry.EndIntervalMeasure(MsbuildAssetsVerificationDuration);
noOpTelemetry.TelemetryEvent[MsbuildAssetsVerificationResult] = noOpSuccess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal static string GetCacheFilePath(RestoreRequest request, LockFile lockFil
/// This method verifies that the props/targets files and all the packages written out in the lock file are present on disk
/// This does not account if the files were manually modified since the last restore
/// </summary>
internal static bool VerifyAssetsAndMSBuildFilesAndPackagesArePresent(RestoreRequest request)
internal static bool VerifyAssetsAndMSBuildFilesAndLockFilesAndPackagesArePresent(RestoreRequest request)
heng-liu marked this conversation as resolved.
Show resolved Hide resolved
{

if (!File.Exists(request.ExistingLockFile?.Path))
Expand All @@ -134,6 +134,16 @@ internal static bool VerifyAssetsAndMSBuildFilesAndPackagesArePresent(RestoreReq
request.Log.LogVerbose(string.Format(CultureInfo.CurrentCulture, Strings.Log_PropsFileNotOnDisk, request.Project.Name, propsFilePath));
return false;
}
if (PackagesLockFileUtilities.IsNuGetLockFileSupported(request.Project))
nkolev92 marked this conversation as resolved.
Show resolved Hide resolved
{
var packageLockFilePath = PackagesLockFileUtilities.GetNuGetLockFilePath(request.Project);
if (!File.Exists(packageLockFilePath))
{
request.Log.LogVerbose(string.Format(CultureInfo.CurrentCulture, Strings.Log_LockFileNotOnDisk, request.Project.Name, packageLockFilePath));
return false;
}
}

}

if (!VerifyPackagesOnDisk(request))
Expand Down
9 changes: 9 additions & 0 deletions src/NuGet.Core/NuGet.Commands/Strings.Designer.cs

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.Core/NuGet.Commands/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,9 @@ For more information, visit http://docs.nuget.org/docs/reference/command-line-re
<data name="Log_TargetsFileNotOnDisk" xml:space="preserve">
<value>The targets file for {0} at location {1} does not exist, no-op is not possible. Continuing restore.</value>
</data>
<data name="Log_LockFileNotOnDisk" xml:space="preserve">
<value>The Lock file for {0} at location {1} does not exist, no-op is not possible. Continuing restore.</value>
nkolev92 marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="Warning_MinVersionDoesNotExist" xml:space="preserve">
<value>{0} depends on {1} but {2} was not found. An approximate best match of {3} was resolved.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6429,6 +6429,266 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
}
}

[Fact]
public async Task RestoreNetCore_NoOp_EnableRestorePackagesWithLockFile_BySetProperty_ThenDeletePackageLockFile()
{
// Related issue: https://github.com/NuGet/Home/issues/7807
// First senario : Enable RestorePackagesWithLockFile by only setting property.
// First restore should fail the No-op, generate the package lock file. After deleting the package lock file, run the second restore.
// The second restore should fail the No-op, and generate the package lock file.

// Arrange
using (var pathContext = new SimpleTestPathContext())
{
// Set up solution, project, and packages
var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);

var packageX = new SimpleTestPackageContext()
{
Id = "x",
Version = "1.0.0"
};

var projects = new List<SimpleTestProjectContext>();

var project = SimpleTestProjectContext.CreateNETCore(
$"proj",
pathContext.SolutionRoot,
NuGetFramework.Parse("net45"));

project.Properties.Add("RestorePackagesWithLockFile", "true");

project.AddPackageToAllFrameworks(packageX);
solution.Projects.Add(project);
solution.Create(pathContext.SolutionRoot);


await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
PackageSaveMode.Defaultv3,
packageX);


var packageLockFileName = project.NuGetLockFileOutputPath;
var noOpFailedMsg = "The Lock file for " + project.ProjectName + " at location " + packageLockFileName + " does not exist, no-op is not possible. Continuing restore.";

// Act
var result1 = Util.Restore(pathContext, project.ProjectPath, additionalArgs: "-verbosity Detailed");

// Assert
Assert.Equal(0, result1.Item1);
Assert.Contains("Writing packages lock file at disk.", result1.Item2);
Assert.True(File.Exists(packageLockFileName));

// Act
File.Delete(packageLockFileName);
Assert.False(File.Exists(packageLockFileName));

var result2 = Util.Restore(pathContext, project.ProjectPath, additionalArgs: "-verbosity Detailed");

//Assert
Assert.Equal(0, result2.Item1);
Assert.Contains(noOpFailedMsg, result2.Item2);
Assert.Contains("Writing packages lock file at disk.", result2.Item2);
Assert.True(File.Exists(packageLockFileName));
}
}


[Fact]
public async Task RestoreNetCore_NoOp_EnableRestorePackagesWithLockFile_BySetProperty_ThenNotDeletePackageLockFile()
{
// Related issue: https://github.com/NuGet/Home/issues/7807
// Contrast test to the first senario : do not delete package lock file at the end of the first restore.
// First restore should fail the No-op, generate the package lock file. DO NOT delete the package lock file, run the second restore.
// The second restore should No-op, and won't generate the package lock file.

// Arrange
using (var pathContext = new SimpleTestPathContext())
{
// Set up solution, project, and packages
var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);

var packageX = new SimpleTestPackageContext()
{
Id = "x",
Version = "1.0.0"
};

var projects = new List<SimpleTestProjectContext>();

var project = SimpleTestProjectContext.CreateNETCore(
$"proj",
pathContext.SolutionRoot,
NuGetFramework.Parse("net45"));

project.Properties.Add("RestorePackagesWithLockFile", "true");

project.AddPackageToAllFrameworks(packageX);
solution.Projects.Add(project);
solution.Create(pathContext.SolutionRoot);


await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
PackageSaveMode.Defaultv3,
packageX);


var packageLockFileName = project.NuGetLockFileOutputPath;
var noOpFailedMsg = "The Lock file for " + project.ProjectName + " at location " + packageLockFileName + " does not exist, no-op is not possible. Continuing restore.";
var noOpSucceedMsg = "No-Op restore. The cache will not be updated.";

// Act
var result1 = Util.Restore(pathContext, project.ProjectPath, additionalArgs: "-verbosity Detailed");

// Assert
Assert.Equal(0, result1.Item1);
Assert.Contains("Writing packages lock file at disk.", result1.Item2);
Assert.True(File.Exists(packageLockFileName));

// Act
var result2 = Util.Restore(pathContext, project.ProjectPath, additionalArgs: "-verbosity Detailed");

//Assert
Assert.Equal(0, result2.Item1);
Assert.Contains(noOpSucceedMsg, result2.Item2);
Assert.DoesNotContain("Writing packages lock file at disk.", result2.Item2);
Assert.True(File.Exists(packageLockFileName));
}
}

[Fact]
public async Task RestoreNetCore_NoOp_EnableRestorePackagesWithLockFile_ByAddLockFile_ThenDeletePackageLockFile()
{
// Related issue: https://github.com/NuGet/Home/issues/7807
// Second senario : Enable RestorePackagesWithLockFile by only adding a lock file.
// First restore should fail the No-op, regenerate the package lock file. After deleting the package lock file, run the second restore.
// The second restore: since there is no property set and no lock file exists, no lockfile will be generated. And no-op succeed.

// Arrange
using (var pathContext = new SimpleTestPathContext())
{
// Set up solution, project, and packages
var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);

var packageX = new SimpleTestPackageContext()
{
Id = "x",
Version = "1.0.0"
};

var projects = new List<SimpleTestProjectContext>();

var project = SimpleTestProjectContext.CreateNETCore(
$"proj",
pathContext.SolutionRoot,
NuGetFramework.Parse("net45"));

project.AddPackageToAllFrameworks(packageX);
solution.Projects.Add(project);
solution.Create(pathContext.SolutionRoot);

await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
PackageSaveMode.Defaultv3,
packageX);

var packageLockFileName = project.NuGetLockFileOutputPath;
Assert.False(File.Exists(packageLockFileName));
File.Create(packageLockFileName).Close();
Assert.True(File.Exists(packageLockFileName));


var noOpFailedMsg = "The Lock file for " + project.ProjectName + " at location " + packageLockFileName + " does not exist, no-op is not possible. Continuing restore.";
var noOpSucceedMsg = "No-Op restore. The cache will not be updated.";

// Act
var result1 = Util.Restore(pathContext, project.ProjectPath, additionalArgs: "-verbosity Detailed");

// Assert
Assert.Equal(0, result1.Item1);
Assert.Contains("Writing packages lock file at disk.", result1.Item2);
Assert.True(File.Exists(packageLockFileName));

// Act
File.Delete(packageLockFileName);
Assert.False(File.Exists(packageLockFileName));

var result2 = Util.Restore(pathContext, project.ProjectPath, additionalArgs: "-verbosity Detailed");

//Assert
Assert.Equal(0, result2.Item1);
Assert.Contains(noOpSucceedMsg, result2.Item2);
Assert.DoesNotContain("Writing packages lock file at disk.", result2.Item2);
Assert.False(File.Exists(packageLockFileName));
}
}

[Fact]
public async Task RestoreNetCore_NoOp_EnableRestorePackagesWithLockFile_ByAddLockFile_ThenNotDeletePackageLockFile()
{
// Related issue: https://github.com/NuGet/Home/issues/7807
// Contrast test to the second senario : do not delete package lock file at the end of the first restore.
// First restore should fail the No-op, regenerate the package lock file. DO NOT delete the package lock file, run the second restore.
// The second restore: No-op should succeed, lock file will not be regenerated.

// Arrange
using (var pathContext = new SimpleTestPathContext())
{
// Set up solution, project, and packages
var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);

var packageX = new SimpleTestPackageContext()
{
Id = "x",
Version = "1.0.0"
};

var projects = new List<SimpleTestProjectContext>();

var project = SimpleTestProjectContext.CreateNETCore(
$"proj",
pathContext.SolutionRoot,
NuGetFramework.Parse("net45"));

project.AddPackageToAllFrameworks(packageX);
solution.Projects.Add(project);
solution.Create(pathContext.SolutionRoot);

await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
PackageSaveMode.Defaultv3,
packageX);

var packageLockFileName = project.NuGetLockFileOutputPath;
Assert.False(File.Exists(packageLockFileName));
File.Create(packageLockFileName).Close();
Assert.True(File.Exists(packageLockFileName));


var noOpFailedMsg = "The Lock file for " + project.ProjectName + " at location " + packageLockFileName + " does not exist, no-op is not possible. Continuing restore.";
var noOpSucceedMsg = "No-Op restore. The cache will not be updated.";

// Act
var result1 = Util.Restore(pathContext, project.ProjectPath, additionalArgs: "-verbosity Detailed");

// Assert
Assert.Equal(0, result1.Item1);
Assert.Contains("Writing packages lock file at disk.", result1.Item2);
Assert.True(File.Exists(packageLockFileName));

// Act
var result2 = Util.Restore(pathContext, project.ProjectPath, additionalArgs: "-verbosity Detailed");

//Assert
Assert.Equal(0, result2.Item1);
Assert.Contains(noOpSucceedMsg, result2.Item2);
Assert.DoesNotContain("Writing packages lock file at disk.", result2.Item2);
Assert.True(File.Exists(packageLockFileName));
}
}

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