2016-02-10 20:13:56 -08:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
2016-06-03 17:36:40 -07:00
using System ;
2016-02-10 20:13:56 -08:00
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
2016-03-10 10:12:43 -08:00
using System.Text ;
2016-06-03 17:36:40 -07:00
using Microsoft.DotNet.Tools.Test.Utilities.Mock ;
2016-02-10 20:13:56 -08:00
using Microsoft.Extensions.EnvironmentAbstractions ;
namespace Microsoft.Extensions.DependencyModel.Tests
{
class FileSystemMockBuilder
{
private Dictionary < string , string > _files = new Dictionary < string , string > ( ) ;
2016-06-03 17:36:40 -07:00
public string TemporaryFolder { get ; set ; }
2016-02-10 20:13:56 -08:00
internal static IFileSystem Empty { get ; } = Create ( ) . Build ( ) ;
public static FileSystemMockBuilder Create ( )
{
return new FileSystemMockBuilder ( ) ;
}
public FileSystemMockBuilder AddFile ( string name , string content = "" )
{
_files . Add ( name , content ) ;
return this ;
}
public FileSystemMockBuilder AddFiles ( string basePath , params string [ ] files )
{
foreach ( var file in files )
{
AddFile ( Path . Combine ( basePath , file ) ) ;
}
return this ;
}
internal IFileSystem Build ( )
{
2016-06-03 17:36:40 -07:00
return new FileSystemMock ( _files , TemporaryFolder ) ;
2016-02-10 20:13:56 -08:00
}
private class FileSystemMock : IFileSystem
{
2016-06-03 17:36:40 -07:00
public FileSystemMock ( Dictionary < string , string > files , string temporaryFolder )
2016-02-10 20:13:56 -08:00
{
File = new FileMock ( files ) ;
2016-06-03 17:36:40 -07:00
Directory = new DirectoryMock ( files , temporaryFolder ) ;
2016-02-10 20:13:56 -08:00
}
public IFile File { get ; }
public IDirectory Directory { get ; }
}
private class FileMock : IFile
{
private Dictionary < string , string > _files ;
public FileMock ( Dictionary < string , string > files )
{
_files = files ;
}
public bool Exists ( string path )
{
return _files . ContainsKey ( path ) ;
}
public string ReadAllText ( string path )
{
string text ;
if ( ! _files . TryGetValue ( path , out text ) )
{
throw new FileNotFoundException ( path ) ;
}
return text ;
}
2016-03-10 10:12:43 -08:00
public Stream OpenRead ( string path )
{
return new MemoryStream ( Encoding . UTF8 . GetBytes ( ReadAllText ( path ) ) ) ;
}
2016-06-06 12:51:27 -07:00
2016-06-10 15:06:48 -07:00
public Stream OpenFile (
string path ,
FileMode fileMode ,
FileAccess fileAccess ,
FileShare fileShare ,
int bufferSize ,
FileOptions fileOptions )
{
throw new NotImplementedException ( ) ;
}
2016-06-06 12:51:27 -07:00
public void CreateEmptyFile ( string path )
{
_files . Add ( path , string . Empty ) ;
}
2016-02-10 20:13:56 -08:00
}
private class DirectoryMock : IDirectory
{
private Dictionary < string , string > _files ;
2016-06-03 17:36:40 -07:00
private readonly TemporaryDirectoryMock _temporaryDirectory ;
public DirectoryMock ( Dictionary < string , string > files , string temporaryDirectory )
2016-02-10 20:13:56 -08:00
{
_files = files ;
2016-06-03 17:36:40 -07:00
_temporaryDirectory = new TemporaryDirectoryMock ( temporaryDirectory ) ;
}
public ITemporaryDirectory CreateTemporaryDirectory ( )
{
return _temporaryDirectory ;
2016-02-10 20:13:56 -08:00
}
2016-08-22 13:20:17 -07:00
public IEnumerable < string > GetFiles ( string path , string searchPattern )
{
throw new NotImplementedException ( ) ;
}
2016-08-25 22:53:04 -07:00
public string GetDirectoryFullName ( string path )
{
throw new NotImplementedException ( ) ;
}
2016-02-10 20:13:56 -08:00
public bool Exists ( string path )
{
return _files . Keys . Any ( k = > k . StartsWith ( path ) ) ;
}
}
2016-06-03 17:36:40 -07:00
private class TemporaryDirectoryMock : ITemporaryDirectoryMock
{
public bool DisposedTemporaryDirectory { get ; private set ; }
public TemporaryDirectoryMock ( string temporaryDirectory )
{
DirectoryPath = temporaryDirectory ;
}
public string DirectoryPath { get ; }
public void Dispose ( )
{
DisposedTemporaryDirectory = true ;
}
}
2016-02-10 20:13:56 -08:00
}
}