public LocalPackageInfo FindPackage(string packageId, NuGetVersion version)
        {
            LocalPackageInfo package = null;

            var packages = FindPackagesByIdImpl(packageId);
            var count    = packages.Count;

            for (var i = 0; i < count; i++)
            {
                var candidatePackage = packages[i];
                if (candidatePackage.Version == version)
                {
                    package = candidatePackage;
                    break;
                }
            }

            if (package == null)
            {
                return(null);
            }

            // Check for an exact match on casing
            if (StringComparer.Ordinal.Equals(packageId, package.Id) &&
                EqualityUtility.SequenceEqualWithNullCheck(version.ReleaseLabels, package.Version.ReleaseLabels, StringComparer.Ordinal))
            {
                return(package);
            }

            // nuspec
            var nuspec = _packageFileCache.GetOrAddNuspec(package.ManifestPath, package.ExpandedPath);

            // files
            var files = _packageFileCache.GetOrAddFiles(package.ExpandedPath);

            // sha512
            var sha512 = _packageFileCache.GetOrAddSha512(package.Sha512Path);

            // Create a new info to match the given id/version
            return(new LocalPackageInfo(
                       packageId,
                       version,
                       package.ExpandedPath,
                       package.ManifestPath,
                       package.ZipPath,
                       package.Sha512Path,
                       nuspec,
                       files,
                       sha512));
        }
        public LocalPackageInfo FindPackage(string packageId, NuGetVersion version)
        {
            var package = FindPackageImpl(packageId, version);

            if (package == null)
            {
                return(null);
            }

            // Check for an exact match on casing
            if (StringComparer.Ordinal.Equals(packageId, package.Id) &&
                EqualityUtility.SequenceEqualWithNullCheck(version.ReleaseLabels, package.Version.ReleaseLabels, StringComparer.Ordinal))
            {
                return(package);
            }

            // nuspec
            var nuspec = _packageFileCache.GetOrAddNuspec(package.ManifestPath, package.ExpandedPath);

            // files
            var files = _packageFileCache.GetOrAddFiles(package.ExpandedPath);

            // sha512
            var sha512 = _packageFileCache.GetOrAddSha512(package.Sha512Path);

            // runtime.json
            var runtimeGraph = _packageFileCache.GetOrAddRuntimeGraph(package.ExpandedPath);

            // Create a new info to match the given id/version
            return(new LocalPackageInfo(
                       packageId,
                       version,
                       package.ExpandedPath,
                       package.ManifestPath,
                       package.ZipPath,
                       package.Sha512Path,
                       nuspec,
                       files,
                       sha512,
                       runtimeGraph));
        }
        public async Task LocalPackageFileCache_GetNuspecTwiceVerifySameInstance()
        {
            using (var pathContext = new SimpleTestPathContext())
            {
                var cache        = new LocalPackageFileCache();
                var pathResolver = new VersionFolderPathResolver(pathContext.PackageSource);

                var identity = new PackageIdentity("X", NuGetVersion.Parse("1.0.0"));

                await SimpleTestPackageUtility.CreateFolderFeedV3Async(
                    pathContext.PackageSource,
                    identity);

                var nuspec   = pathResolver.GetManifestFilePath(identity.Id, identity.Version);
                var expanded = pathResolver.GetInstallPath(identity.Id, identity.Version);

                var result1 = cache.GetOrAddNuspec(nuspec, expanded);
                var result2 = cache.GetOrAddNuspec(nuspec, expanded);

                Assert.Same(result1.Value, result2.Value);
                result1.Value.GetIdentity().Should().Be(identity);
            }
        }
        public void LocalPackageFileCache_DirNotFoundVerifyFailure()
        {
            using (var pathContext = new SimpleTestPathContext())
            {
                var cache        = new LocalPackageFileCache();
                var pathResolver = new VersionFolderPathResolver(pathContext.PackageSource);

                var identity = new PackageIdentity("X", NuGetVersion.Parse("1.0.0"));

                var nuspec   = pathResolver.GetManifestFilePath(identity.Id, identity.Version);
                var expanded = pathResolver.GetInstallPath(identity.Id, identity.Version);

                // Verify does not throw
                var result = cache.GetOrAddNuspec(nuspec, expanded);

                // This should throw
                Assert.Throws <DirectoryNotFoundException>(() => result.Value);
            }
        }
        public async Task LocalPackageFileCache_FallbackToFolderReaderVerifyResult()
        {
            using (var pathContext = new SimpleTestPathContext())
            {
                var cache        = new LocalPackageFileCache();
                var pathResolver = new VersionFolderPathResolver(pathContext.PackageSource);

                var identity = new PackageIdentity("X", NuGetVersion.Parse("1.0.0"));

                await SimpleTestPackageUtility.CreateFolderFeedV3Async(
                    pathContext.PackageSource,
                    identity);

                var nuspec   = pathResolver.GetManifestFilePath(identity.Id, identity.Version) + "invalid.nuspec";
                var expanded = pathResolver.GetInstallPath(identity.Id, identity.Version);

                var result = cache.GetOrAddNuspec(nuspec, expanded);

                result.Value.GetIdentity().Should().Be(identity);
            }
        }