private static GitHubSearcher GetMockClient(Mock <ITelemetryService> mockTelemetry, Func <SearchRepositoriesRequest, Task <IReadOnlyList <WritableRepositoryInformation> > > searchResultFunc = null, GitHubIndexerConfiguration configuration = null) { var mockSearchApiRequester = new Mock <IGitHubSearchWrapper>(); mockSearchApiRequester .Setup(r => r.GetResponse(It.IsAny <SearchRepositoriesRequest>())) .Returns(async(SearchRepositoriesRequest request) => { return(new GitHubSearchApiResponse(searchResultFunc == null ? new List <WritableRepositoryInformation>() : await searchResultFunc(request), DateTimeOffset.Now, DateTimeOffset.Now)); }); var optionsSnapshot = new Mock <IOptionsSnapshot <GitHubIndexerConfiguration> >(); optionsSnapshot .Setup(x => x.Value) .Returns(() => configuration ?? new GitHubIndexerConfiguration()); return(new GitHubSearcher( mockSearchApiRequester.Object, mockTelemetry.Object, Mock.Of <ILogger <GitHubSearcher> >(), optionsSnapshot.Object)); }
private static ReposIndexer CreateIndexer( WritableRepositoryInformation searchResult, IReadOnlyList <GitFileInfo> repoFiles, Mock <ITelemetryService> mockTelemetry, Action <string> onDisposeHandler, Func <ICheckedOutFile, IReadOnlyList <string> > configFileParser = null, bool shouldTimeOut = false) { var mockConfig = new Mock <IOptionsSnapshot <GitHubIndexerConfiguration> >(); var config = new GitHubIndexerConfiguration(); if (shouldTimeOut) { config.RepoIndexingTimeout = TimeSpan.Zero; } mockConfig .SetupGet(x => x.Value) .Returns(config); var mockSearcher = new Mock <IGitRepoSearcher>(); mockSearcher .Setup(x => x.GetPopularRepositories()) .Returns(Task.FromResult(new List <WritableRepositoryInformation>() { searchResult } as IReadOnlyList <WritableRepositoryInformation> ?? new List <WritableRepositoryInformation>())); var mockRepoCache = new Mock <IRepositoriesCache>(); RepositoryInformation mockVal; mockRepoCache .Setup(x => x.TryGetCachedVersion(It.IsAny <WritableRepositoryInformation>(), out mockVal)) .Callback(() => { if (shouldTimeOut) { // A minute should be long enough to cancel the task. // If the task isn't canceled, the test runtime will only be minorly affected. Thread.Sleep(60 * 1000); } }) .Returns(false); // Simulate no cache mockRepoCache .Setup(x => x.Persist(It.IsAny <RepositoryInformation>())); var mockConfigFileParser = new Mock <IConfigFileParser>(); mockConfigFileParser .Setup(x => x.Parse(It.IsAny <ICheckedOutFile>())) .Returns(configFileParser ?? ((ICheckedOutFile file) => new List <string>())); var mockFetchedRepo = new Mock <IFetchedRepo>(); mockFetchedRepo .Setup(x => x.GetFileInfos()) .Returns(repoFiles); mockFetchedRepo .Setup(x => x.CheckoutFiles(It.IsAny <IReadOnlyCollection <string> >())) .Returns((IReadOnlyCollection <string> paths) => paths.Select(x => new CheckedOutFile(filePath: x, repoId: searchResult.Id) as ICheckedOutFile).ToList()); var mockRepoFetcher = new Mock <IRepoFetcher>(); mockRepoFetcher .Setup(x => x.FetchRepo(It.IsAny <WritableRepositoryInformation>())) .Returns(mockFetchedRepo.Object); var mockBlobClient = new Mock <ICloudBlobClient>(); var mockBlobContainer = new Mock <ICloudBlobContainer>(); var mockBlob = new Mock <ISimpleCloudBlob>(); mockBlobClient .Setup(x => x.GetContainerReference(It.IsAny <string>())) .Returns(() => mockBlobContainer.Object); mockBlobContainer .Setup(x => x.GetBlobReference(It.IsAny <string>())) .Returns(() => mockBlob.Object); mockBlob .Setup(x => x.ETag) .Returns("\"some-etag\""); mockBlob .Setup(x => x.Properties) .Returns(new CloudBlockBlob(new Uri("https://example/blob")).Properties); mockBlob .Setup(x => x.OpenWriteAsync(It.IsAny <AccessCondition>())) .ReturnsAsync(() => new RecordingStream(bytes => { onDisposeHandler?.Invoke(Encoding.UTF8.GetString(bytes)); })); return(new ReposIndexer( mockSearcher.Object, Mock.Of <ILogger <ReposIndexer> >(), mockRepoCache.Object, mockConfigFileParser.Object, mockRepoFetcher.Object, mockBlobClient.Object, mockConfig.Object, mockTelemetry.Object)); }