Skip to content

Commit

Permalink
Correct check for whether a package exists in a package folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmalinowski committed Jun 14, 2016
1 parent d37c917 commit 536f117
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
18 changes: 16 additions & 2 deletions src/Microsoft.NuGet.Build.Tasks.Tests/NuGetTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,23 @@ public static ResolvePackagesResult ResolvePackagesWithJsonFileContents(
filesInPackages.Add(fileInPackage);
}
}
else
{
// We will assume there is a location in the lock file we're using
var lockFile = JObject.Parse(projectLockJsonFileContents);
var firstLocation = ((JObject)lockFile["packageFolders"]).Properties().First().Name;

foreach (var fileInPackage in GetFakeFileNamesFromPackages(projectLockJsonFileContents, firstLocation))
{
filesInPackages.Add(fileInPackage);
}
}

// Don't require the packages be restored on the machine
ResolveNuGetPackageAssets task = null;
DirectoryExists directoryExists = path => task.GetPackageFolders().Any(l => path.StartsWith(l)) || Directory.Exists(path);
FileExists fileExists = path => filesInPackages.Contains(path) || File.Exists(path);

task = new ResolveNuGetPackageAssets(directoryExists, fileExists, tryGetRuntimeVersion);
task = new ResolveNuGetPackageAssets(fileExists, tryGetRuntimeVersion);
var sw = new StringWriter();
task.BuildEngine = new MockBuildEngine(sw);

Expand Down Expand Up @@ -105,6 +115,10 @@ private static IEnumerable<string> GetFakeFileNamesFromPackages(string projectJs
yield return Path.Combine(packagesDirectory, library.Name, file).Replace('/', '\\');
}
}

// Some earlier versions of NuGet didn't include the hash file in the file list, so fake that
// in here.
yield return Path.Combine(packagesDirectory, library.Name.Replace('/', '\\'), library.Name.Replace('/', '.') + ".nupkg.sha512");
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.NuGet.Build.Tasks/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Microsoft.NuGet.Build.Tasks
{
internal delegate bool DirectoryExists(string path);
internal delegate bool FileExists(string path);
internal delegate string TryGetRuntimeVersion(string path);
}
12 changes: 4 additions & 8 deletions src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,13 @@ public sealed class ResolveNuGetPackageAssets : Task
private readonly Dictionary<string, string> _projectReferencesToOutputBasePaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

#region UnitTestSupport
private readonly DirectoryExists _directoryExists = new DirectoryExists(Directory.Exists);
private readonly FileExists _fileExists = new FileExists(File.Exists);
private readonly TryGetRuntimeVersion _tryGetRuntimeVersion = new TryGetRuntimeVersion(TryGetRuntimeVersion);
private readonly bool _reportExceptionsToMSBuildLogger = true;

internal ResolveNuGetPackageAssets(DirectoryExists directoryExists, FileExists fileExists, TryGetRuntimeVersion tryGetRuntimeVersion)
internal ResolveNuGetPackageAssets(FileExists fileExists, TryGetRuntimeVersion tryGetRuntimeVersion)
: this()
{
if (directoryExists != null)
{
_directoryExists = directoryExists;
}

if (fileExists != null)
{
_fileExists = fileExists;
Expand Down Expand Up @@ -894,7 +888,9 @@ private string GetNuGetPackagePath(string packageId, string packageVersion)
{
string packagePath = Path.Combine(packagesFolder, packageId, packageVersion);

if (_directoryExists(packagePath))
// The proper way to check if a package is available is to look for the hash file, since that's the last
// file written as a part of the restore process. If it's not there, it means something failed part way through.
if (_fileExists(Path.Combine(packagePath, $"{packageId}.{packageVersion}.nupkg.sha512")))
{
return packagePath;
}
Expand Down

0 comments on commit 536f117

Please sign in to comment.