// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using System; using System.Collections.Concurrent; using System.IO; using Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage; using Microsoft.SqlTools.ServiceLayer.SqlContext; using Moq; namespace Microsoft.SqlTools.ServiceLayer.Test.Common { public class MemoryFileSystem { public static IFileStreamFactory GetFileStreamFactory(int sizeFactor=1) { return GetFileStreamFactory(new ConcurrentDictionary(), sizeFactor); } public static IFileStreamFactory GetFileStreamFactory(ConcurrentDictionary storage, int sizeFactor=1) { Mock mock = new Mock(); mock.Setup(fsf => fsf.CreateFile()) .Returns(() => { string fileName = Guid.NewGuid().ToString(); storage.TryAdd(fileName, new byte[8192 * sizeFactor]); return fileName; }); mock.Setup(fsf => fsf.GetReader(It.IsAny())) .Returns(output => new ServiceBufferFileStreamReader(new MemoryStream(storage[output]), new QueryExecutionSettings())); mock.Setup(fsf => fsf.GetWriter(It.IsAny())) .Returns(output => new ServiceBufferFileStreamWriter(new MemoryStream(storage[output]), new QueryExecutionSettings())); return mock.Object; } } }