Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolev92 committed Feb 19, 2025
1 parent 9e76bae commit 4e3bfac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -315,8 +316,7 @@ await FileUtility.ReplaceWithLock(

private async Task CommitDgSpecFileAsync(ILogger log, bool toolCommit)
{
// TODO NK - did hash change?
if (!toolCommit && DidDGHashChange && _dependencyGraphSpecFilePath != null && _dependencyGraphSpec != null)
if (!toolCommit && _dependencyGraphSpecFilePath != null && _dependencyGraphSpec != null && (DidDGHashChange || !File.Exists(_dependencyGraphSpecFilePath)))
{
log.LogVerbose($"Persisting dg to {_dependencyGraphSpecFilePath}");

Expand Down
58 changes: 58 additions & 0 deletions test/NuGet.Core.Tests/NuGet.Commands.Test/RestoreResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,64 @@ public async Task RestoreResult_Commit_WritesDependencyGraphSpec()
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task RestoreResult_Commit_WithExistingDGSpecAndDidDGHashChange_WritesDependencyGraphSpec(bool didDGHashChange)
{
// Arrange
using (var td = TestDirectory.Create())
{
var path = Path.Combine(td, "project.assets.json");
var cachePath = Path.Combine(td, "project.csproj.nuget.cache");
var dgSpecPath = Path.Combine(td, "project1.nuget.g.dgspec.json");
File.WriteAllText(dgSpecPath, "{}");

var dgSpec = new DependencyGraphSpec();
var configJson = @"
{
""dependencies"": {
},
""frameworks"": {
""net45"": { }
}
}";

var spec = JsonPackageSpecReader.GetPackageSpec(configJson, "TestProject", Path.Combine(td, "project.csproj")).WithTestRestoreMetadata();
dgSpec.AddProject(spec);
dgSpec.AddRestore(spec.Name);

var logger = new TestLogger();
var result = new RestoreResult(
success: true,
restoreGraphs: null,
compatibilityCheckResults: null,
lockFile: new LockFile(),
previousLockFile: null, // different lock file
lockFilePath: path,
msbuildFiles: Enumerable.Empty<MSBuildOutputFile>(),
cacheFile: new CacheFile("NotSoRandomString"),
cacheFilePath: cachePath,
packagesLockFilePath: null,
packagesLockFile: null,
dependencyGraphSpecFilePath: dgSpecPath,
dependencyGraphSpec: dgSpec,
projectStyle: ProjectStyle.Unknown,
elapsedTime: TimeSpan.MinValue)
{
DidDGHashChange = didDGHashChange
};

// Act
await result.CommitAsync(logger, CancellationToken.None);

// Assert
Assert.Empty(logger.MinimalMessages);
Assert.Equal(didDGHashChange, logger.VerboseMessages.Contains($"Persisting dg to {dgSpecPath}"));
Assert.True(File.Exists(dgSpecPath));
}
}

[Fact]
public void WhenRestoreResult_LogMessagesAreSourcedFromTheAssetsFile()
{
Expand Down

0 comments on commit 4e3bfac

Please # to comment.