2016-02-11 04:13:56 +00: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-04 00:36:40 +00:00
|
|
|
|
using System;
|
2016-02-11 04:13:56 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2016-03-10 18:12:43 +00:00
|
|
|
|
using System.Text;
|
2016-06-04 00:36:40 +00:00
|
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities.Mock;
|
2016-02-11 04:13:56 +00:00
|
|
|
|
using Microsoft.Extensions.EnvironmentAbstractions;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Extensions.DependencyModel.Tests
|
|
|
|
|
{
|
|
|
|
|
class FileSystemMockBuilder
|
|
|
|
|
{
|
|
|
|
|
private Dictionary<string, string> _files = new Dictionary<string, string>();
|
|
|
|
|
|
2016-06-04 00:36:40 +00:00
|
|
|
|
public string TemporaryFolder { get; set; }
|
|
|
|
|
|
2016-02-11 04:13:56 +00: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-04 00:36:40 +00:00
|
|
|
|
return new FileSystemMock(_files, TemporaryFolder);
|
2016-02-11 04:13:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class FileSystemMock : IFileSystem
|
|
|
|
|
{
|
2016-06-04 00:36:40 +00:00
|
|
|
|
public FileSystemMock(Dictionary<string, string> files, string temporaryFolder)
|
2016-02-11 04:13:56 +00:00
|
|
|
|
{
|
|
|
|
|
File = new FileMock(files);
|
2016-06-04 00:36:40 +00:00
|
|
|
|
Directory = new DirectoryMock(files, temporaryFolder);
|
2016-02-11 04:13:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IFile File { get; }
|
|
|
|
|
|
|
|
|
|
public IDirectory Directory { get; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class FileMock : IFile
|
|
|
|
|
{
|
|
|
|
|
private Dictionary<string, string> _files;
|
2017-05-20 04:35:45 +00:00
|
|
|
|
|
2016-02-11 04:13:56 +00:00
|
|
|
|
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 18:12:43 +00:00
|
|
|
|
|
|
|
|
|
public Stream OpenRead(string path)
|
|
|
|
|
{
|
|
|
|
|
return new MemoryStream(Encoding.UTF8.GetBytes(ReadAllText(path)));
|
|
|
|
|
}
|
2016-06-06 19:51:27 +00:00
|
|
|
|
|
2016-06-10 22:06:48 +00:00
|
|
|
|
public Stream OpenFile(
|
|
|
|
|
string path,
|
|
|
|
|
FileMode fileMode,
|
|
|
|
|
FileAccess fileAccess,
|
|
|
|
|
FileShare fileShare,
|
|
|
|
|
int bufferSize,
|
|
|
|
|
FileOptions fileOptions)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-06 19:51:27 +00:00
|
|
|
|
public void CreateEmptyFile(string path)
|
|
|
|
|
{
|
|
|
|
|
_files.Add(path, string.Empty);
|
|
|
|
|
}
|
2017-05-20 04:35:45 +00:00
|
|
|
|
|
|
|
|
|
public void WriteAllText(string path, string content)
|
|
|
|
|
{
|
|
|
|
|
_files[path] = content;
|
|
|
|
|
}
|
2018-02-06 21:38:06 +00:00
|
|
|
|
|
2018-01-28 21:35:04 +00:00
|
|
|
|
public void Move(string source, string destination)
|
|
|
|
|
{
|
|
|
|
|
if (!Exists(source))
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException("source does not exist.");
|
|
|
|
|
}
|
|
|
|
|
if (Exists(destination))
|
|
|
|
|
{
|
|
|
|
|
throw new IOException("destination exists.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var content = _files[source];
|
|
|
|
|
_files.Remove(source);
|
|
|
|
|
_files[destination] = content;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 21:38:06 +00:00
|
|
|
|
public void Delete(string path)
|
|
|
|
|
{
|
|
|
|
|
if (!Exists(path))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_files.Remove(path);
|
|
|
|
|
}
|
2016-02-11 04:13:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class DirectoryMock : IDirectory
|
|
|
|
|
{
|
|
|
|
|
private Dictionary<string, string> _files;
|
2016-06-04 00:36:40 +00:00
|
|
|
|
private readonly TemporaryDirectoryMock _temporaryDirectory;
|
|
|
|
|
|
|
|
|
|
public DirectoryMock(Dictionary<string, string> files, string temporaryDirectory)
|
2016-02-11 04:13:56 +00:00
|
|
|
|
{
|
|
|
|
|
_files = files;
|
2016-06-04 00:36:40 +00:00
|
|
|
|
_temporaryDirectory = new TemporaryDirectoryMock(temporaryDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ITemporaryDirectory CreateTemporaryDirectory()
|
|
|
|
|
{
|
|
|
|
|
return _temporaryDirectory;
|
2016-02-11 04:13:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-28 21:35:04 +00:00
|
|
|
|
public IEnumerable<string> EnumerateFileSystemEntries(string path)
|
2016-08-22 20:20:17 +00:00
|
|
|
|
{
|
2018-01-28 21:35:04 +00:00
|
|
|
|
foreach (var entry in _files.Keys.Where(k => Path.GetDirectoryName(k) == path))
|
|
|
|
|
{
|
|
|
|
|
yield return entry;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern)
|
|
|
|
|
{
|
|
|
|
|
if (searchPattern != "*")
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
return EnumerateFileSystemEntries(path);
|
2016-08-22 20:20:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-26 05:53:04 +00:00
|
|
|
|
public string GetDirectoryFullName(string path)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-11 04:13:56 +00:00
|
|
|
|
public bool Exists(string path)
|
|
|
|
|
{
|
|
|
|
|
return _files.Keys.Any(k => k.StartsWith(path));
|
|
|
|
|
}
|
2017-05-31 20:44:03 +00:00
|
|
|
|
|
|
|
|
|
public void CreateDirectory(string path)
|
|
|
|
|
{
|
2018-01-28 21:35:04 +00:00
|
|
|
|
var current = path;
|
|
|
|
|
while (!string.IsNullOrEmpty(current))
|
|
|
|
|
{
|
|
|
|
|
_files[current] = current;
|
|
|
|
|
current = Path.GetDirectoryName(current);
|
|
|
|
|
}
|
2017-05-31 20:44:03 +00:00
|
|
|
|
}
|
2018-02-06 21:38:06 +00:00
|
|
|
|
|
|
|
|
|
public void Delete(string path, bool recursive)
|
|
|
|
|
{
|
|
|
|
|
if (!recursive && Exists(path) == true)
|
|
|
|
|
{
|
|
|
|
|
if (_files.Keys.Where(k => k.StartsWith(path)).Count() > 1)
|
|
|
|
|
{
|
|
|
|
|
throw new IOException("The directory is not empty");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var k in _files.Keys.Where(k => k.StartsWith(path)).ToList())
|
|
|
|
|
{
|
|
|
|
|
_files.Remove(k);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-28 21:35:04 +00:00
|
|
|
|
|
|
|
|
|
public void Move(string source, string destination)
|
|
|
|
|
{
|
|
|
|
|
if (!Exists(source))
|
|
|
|
|
{
|
|
|
|
|
throw new IOException("The source directory does not exist.");
|
|
|
|
|
}
|
|
|
|
|
if (Exists(destination))
|
|
|
|
|
{
|
|
|
|
|
throw new IOException("The destination already exists.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var kvp in _files.Where(kvp => kvp.Key.StartsWith(source)).ToList())
|
|
|
|
|
{
|
|
|
|
|
var newKey = destination + kvp.Key.Substring(source.Length);
|
|
|
|
|
var newValue = kvp.Value.StartsWith(source) ?
|
|
|
|
|
destination + kvp.Value.Substring(source.Length) :
|
|
|
|
|
kvp.Value;
|
|
|
|
|
|
|
|
|
|
_files.Add(newKey, newValue);
|
|
|
|
|
_files.Remove(kvp.Key);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-11 04:13:56 +00:00
|
|
|
|
}
|
2016-06-04 00:36:40 +00: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-11 04:13:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|