/// <summary> /// Get all versions for a specific package Id. /// </summary> /// <param name="sourceRepository"></param> /// <param name="packageId"></param> /// <param name="project"></param> /// <param name="includePrerelease"></param> /// <returns></returns> public static IEnumerable <NuGetVersion> GetAllVersionsForPackageId(SourceRepository sourceRepository, string packageId, NuGetProject project, bool includePrerelease) { IEnumerable <string> targetFrameworks = GetProjectTargetFrameworks(project); SearchFilter searchfilter = new SearchFilter(); searchfilter.IncludePrerelease = includePrerelease; searchfilter.SupportedFrameworks = targetFrameworks; searchfilter.IncludeDelisted = false; PSSearchResource resource = sourceRepository.GetResource <PSSearchResource>(); PSSearchMetadata result = null; IEnumerable <NuGetVersion> allVersions = Enumerable.Empty <NuGetVersion>(); try { Task <IEnumerable <PSSearchMetadata> > task = resource.Search(packageId, searchfilter, 0, 30, CancellationToken.None); result = task.Result .Where(p => string.Equals(p.Identity.Id, packageId, StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); allVersions = result.Versions; } catch (Exception) { if (result == null || !allVersions.Any()) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, Resources.UnknownPackage, packageId)); } } return(result.Versions); }
public async Task TestAllSearchResources(string SourceUrl) { SourceRepository repo = GetSourceRepository(SourceUrl); UISearchResource resource = repo.GetResource <UISearchResource>(); SearchLatestResource latestResource = repo.GetResource <SearchLatestResource>(); //Check if we are able to obtain a resource Assert.True(resource != null); //check if the resource is of type IVsSearch. SearchFilter filter = new SearchFilter(); //create a dummy filter. List <FrameworkName> fxNames = new List <FrameworkName>(); fxNames.Add(new FrameworkName(".NETFramework, Version=4.5")); filter.SupportedFrameworks = fxNames.Select(e => e.ToString()); string SearchTerm = "dotnetrdf"; IEnumerable <UISearchMetadata> uiSearchResults = await resource.Search(SearchTerm, filter, 0, 100, new CancellationToken()); var latestSearchResults = await latestResource.Search(SearchTerm, filter, 0, 100, CancellationToken.None); // Check if non empty search result is returned. Assert.True(uiSearchResults.Count() > 0); //check if there is atleast one result which has Id exactly as the search terms. Assert.True(uiSearchResults.Any(p => p.Identity.Id.Equals(SearchTerm, StringComparison.OrdinalIgnoreCase))); foreach (var result in uiSearchResults) { Assert.Equal(result.Identity.Id, result.LatestPackageMetadata.Identity.Id); Assert.Equal(result.Identity.Version.ToNormalizedString(), result.LatestPackageMetadata.Identity.Version.ToNormalizedString()); } // Verify search and latest search return the same results var searchEnumerator = uiSearchResults.GetEnumerator(); var latestEnumerator = latestSearchResults.GetEnumerator(); for (int i = 0; i < 10; i++) { searchEnumerator.MoveNext(); latestEnumerator.MoveNext(); Assert.Equal(searchEnumerator.Current.LatestPackageMetadata.Identity.Id, latestEnumerator.Current.Id); Assert.Equal(searchEnumerator.Current.LatestPackageMetadata.LicenseUrl, latestEnumerator.Current.LicenseUrl); //Assert.Equal(searchEnumerator.Current.LatestPackageMetadata.ReportAbuseUrl, latestEnumerator.Current.ReportAbuseUrl); Assert.Equal(searchEnumerator.Current.LatestPackageMetadata.RequireLicenseAcceptance, latestEnumerator.Current.RequireLicenseAcceptance); Assert.Equal(searchEnumerator.Current.LatestPackageMetadata.Summary, latestEnumerator.Current.Summary); Assert.Equal(searchEnumerator.Current.LatestPackageMetadata.Authors, String.Join(" ", latestEnumerator.Current.Authors)); Assert.Equal(searchEnumerator.Current.LatestPackageMetadata.Title, latestEnumerator.Current.Title); } PSSearchResource psResource = repo.GetResource <PSSearchResource>(); IEnumerable <PSSearchMetadata> psSearchResults = await psResource.Search(SearchTerm, filter, 0, 100, new System.Threading.CancellationToken()); SimpleSearchResource simpleSearch = repo.GetResource <SimpleSearchResource>(); IEnumerable <SimpleSearchMetadata> simpleSearchResults = await simpleSearch.Search(SearchTerm, filter, 0, 100, new System.Threading.CancellationToken()); //Check that exact search results are returned irrespective of search resource ( UI, powershell and commandline). Assert.Equal(uiSearchResults.Count(), psSearchResults.Count()); Assert.Equal(psSearchResults.Count(), simpleSearchResults.Count()); }
public async Task TestAllSearchResources(string SourceUrl) { SourceRepository repo = GetSourceRepository(SourceUrl); UISearchResource resource = await repo.GetResource <UISearchResource>(); //Check if we are able to obtain a resource Assert.True(resource != null); //check if the resource is of type IVsSearch. SearchFilter filter = new SearchFilter(); //create a dummy filter. List <FrameworkName> fxNames = new List <FrameworkName>(); fxNames.Add(new FrameworkName(".NET Framework, Version=4.0")); filter.SupportedFrameworks = fxNames.Select(e => e.ToString()); string SearchTerm = "Elmah"; IEnumerable <UISearchMetadata> uiSearchResults = await resource.Search(SearchTerm, filter, 0, 100, new System.Threading.CancellationToken()); // Check if non empty search result is returned. Assert.True(uiSearchResults.Count() > 0); //check if there is atleast one result which has Id exactly as the search terms. Assert.True(uiSearchResults.Any(p => p.Identity.Id.Equals(SearchTerm, StringComparison.OrdinalIgnoreCase))); PSSearchResource psResource = await repo.GetResource <PSSearchResource>(); IEnumerable <PSSearchMetadata> psSearchResults = await psResource.Search(SearchTerm, filter, 0, 100, new System.Threading.CancellationToken()); SimpleSearchResource simpleSearch = await repo.GetResource <SimpleSearchResource>(); IEnumerable <SimpleSearchMetadata> simpleSearchResults = await simpleSearch.Search(SearchTerm, filter, 0, 100, new System.Threading.CancellationToken()); //Check that exact search results are returned irrespective of search resource ( UI, powershell and commandline). Assert.True(uiSearchResults.Count() == psSearchResults.Count()); Assert.True(psSearchResults.Count() == simpleSearchResults.Count()); }
/// <summary> /// Get list of packages from the remote package source. Used for Get-Package -ListAvailable. /// </summary> /// <param name="packageId"></param> /// <param name="targetFrameworks"></param> /// <param name="includePrerelease"></param> /// <param name="skip"></param> /// <param name="take"></param> /// <returns></returns> protected IEnumerable <PSSearchMetadata> GetPackagesFromRemoteSource(string packageId, IEnumerable <string> targetFrameworks, bool includePrerelease, int skip, int take) { SearchFilter searchfilter = new SearchFilter(); searchfilter.IncludePrerelease = includePrerelease; searchfilter.SupportedFrameworks = targetFrameworks; searchfilter.IncludeDelisted = false; IEnumerable <PSSearchMetadata> packages = Enumerable.Empty <PSSearchMetadata>(); PSSearchResource resource = ActiveSourceRepository.GetResource <PSSearchResource>(); if (resource != null) { Task <IEnumerable <PSSearchMetadata> > task = resource.Search(packageId, searchfilter, skip, take, CancellationToken.None); packages = task.Result; } return(packages); }