[Chore] System.IO.Abstractions update (#21061)

* System.IO.Abstractions update

* fix tests
This commit is contained in:
Davide Giacometti
2022-10-16 14:23:31 +02:00
committed by GitHub
parent 1c264e0899
commit 887da6dc1a
22 changed files with 72 additions and 84 deletions

View File

@@ -19,8 +19,8 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="System.IO.Abstractions" Version="12.2.5" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="12.2.3" />
<PackageReference Include="System.IO.Abstractions" Version="17.2.3" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="17.2.3" />
</ItemGroup>
<ItemGroup>

View File

@@ -2,13 +2,13 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using Hosts.Helpers;
using Hosts.Models;
using Hosts.Settings;
using Hosts.Tests.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Settings.UI.Library.Enumerations;
@@ -30,13 +30,8 @@ namespace Hosts.Tests
[TestMethod]
public void Hosts_Exists()
{
var fileSystem = new MockFileSystem
{
FileSystemWatcher = new TestFileSystemWatcherFactory(),
};
var fileSystem = new CustomMockFileSystem();
var userSettings = new Mock<IUserSettings>();
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(string.Empty));
var result = service.Exists();
@@ -47,15 +42,8 @@ namespace Hosts.Tests
[TestMethod]
public void Hosts_Not_Exists()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
})
{
FileSystemWatcher = new TestFileSystemWatcherFactory(),
};
var fileSystem = new CustomMockFileSystem();
var userSettings = new Mock<IUserSettings>();
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object);
var result = service.Exists();
@@ -76,13 +64,8 @@ namespace Hosts.Tests
# 10.1.1.30 host30 host30.local # new entry
";
var fileSystem = new MockFileSystem
{
FileSystemWatcher = new TestFileSystemWatcherFactory(),
};
var fileSystem = new CustomMockFileSystem();
var userSettings = new Mock<IUserSettings>();
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
@@ -106,13 +89,8 @@ namespace Hosts.Tests
@"10.1.1.2 host2 host2.local # another comment
";
var fileSystem = new MockFileSystem
{
FileSystemWatcher = new TestFileSystemWatcherFactory(),
};
var fileSystem = new CustomMockFileSystem();
var userSettings = new Mock<IUserSettings>();
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
@@ -137,13 +115,8 @@ namespace Hosts.Tests
10.1.1.2 host2 host2.local # another comment
";
var fileSystem = new MockFileSystem
{
FileSystemWatcher = new TestFileSystemWatcherFactory(),
};
var fileSystem = new CustomMockFileSystem();
var userSettings = new Mock<IUserSettings>();
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
@@ -162,11 +135,7 @@ namespace Hosts.Tests
[TestMethod]
public async Task Empty_Hosts()
{
var fileSystem = new MockFileSystem
{
FileSystemWatcher = new TestFileSystemWatcherFactory(),
};
var fileSystem = new CustomMockFileSystem();
var userSettings = new Mock<IUserSettings>();
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object);
@@ -197,14 +166,9 @@ namespace Hosts.Tests
10.1.1.2 host2 host2.local # another comment
";
var fileSystem = new MockFileSystem
{
FileSystemWatcher = new TestFileSystemWatcherFactory(),
};
var fileSystem = new CustomMockFileSystem();
var userSettings = new Mock<IUserSettings>();
userSettings.Setup(m => m.AdditionalLinesPosition).Returns(AdditionalLinesPosition.Top);
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object);
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
@@ -234,11 +198,7 @@ namespace Hosts.Tests
# footer
";
var fileSystem = new MockFileSystem
{
FileSystemWatcher = new TestFileSystemWatcherFactory(),
};
var fileSystem = new CustomMockFileSystem();
var userSettings = new Mock<IUserSettings>();
userSettings.Setup(m => m.AdditionalLinesPosition).Returns(AdditionalLinesPosition.Bottom);

View File

@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
namespace Hosts.Tests.Mocks
{
public class CustomMockFileSystem : MockFileSystem
{
public override IFileSystemWatcherFactory FileSystemWatcher { get; }
public CustomMockFileSystem()
: base()
{
FileSystemWatcher = new MockFileSystemWatcherFactory();
}
}
}

View File

@@ -2,13 +2,14 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.IO.Abstractions;
namespace Hosts.Tests
namespace Hosts.Tests.Mocks
{
public class TestFileSystemWatcher : FileSystemWatcherBase
public class MockFileSystemWatcher : FileSystemWatcherBase
{
public override bool IncludeSubdirectories { get; set; }
@@ -26,13 +27,15 @@ namespace Hosts.Tests
public override ISynchronizeInvoke SynchronizingObject { get; set; }
public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType) => default(WaitForChangedResult);
public override Collection<string> Filters => throw new System.NotImplementedException();
public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout) => default(WaitForChangedResult);
public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType) => default;
public TestFileSystemWatcher(string path) => Path = path;
public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout) => default;
public TestFileSystemWatcher(string path, string filter)
public MockFileSystemWatcher(string path) => Path = path;
public MockFileSystemWatcher(string path, string filter)
{
Path = path;
Filter = filter;

View File

@@ -4,16 +4,16 @@
using System.IO.Abstractions;
namespace Hosts.Tests
namespace Hosts.Tests.Mocks
{
public class TestFileSystemWatcherFactory : IFileSystemWatcherFactory
public class MockFileSystemWatcherFactory : IFileSystemWatcherFactory
{
public IFileSystemWatcher CreateNew() => new TestFileSystemWatcher(null);
public IFileSystemWatcher CreateNew() => new MockFileSystemWatcher(null);
public IFileSystemWatcher CreateNew(string path) => new TestFileSystemWatcher(path);
public IFileSystemWatcher CreateNew(string path) => new MockFileSystemWatcher(path);
public IFileSystemWatcher CreateNew(string path, string filter) => new TestFileSystemWatcher(path, filter);
public IFileSystemWatcher CreateNew(string path, string filter) => new MockFileSystemWatcher(path, filter);
public IFileSystemWatcher FromPath(string path) => new TestFileSystemWatcher(path);
public IFileSystemWatcher FromPath(string path) => new MockFileSystemWatcher(path);
}
}