Skip to content

Commit

Permalink
Adapt failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Mar 15, 2024
1 parent f2d80a0 commit d038671
Show file tree
Hide file tree
Showing 20 changed files with 636 additions and 89 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
<PackageVersion Include="TestableIO.System.IO.Abstractions" Version="20.0.28" />
<PackageVersion Include="TestableIO.System.IO.Abstractions" Version="20.0.34" />
<PackageVersion Include="System.IO.Compression" Version="4.3.0" />
<PackageVersion Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,26 @@ public override bool Exists

/// <inheritdoc cref="IDirectoryInfo.Parent" />
public IDirectoryInfo? Parent
=> New(Location.GetParent(), _fileSystem);
{
get
{
using IDisposable registration = RegisterProperty(nameof(Parent), PropertyAccess.Get);

return New(Location.GetParent(), _fileSystem);
}
}

/// <inheritdoc cref="IDirectoryInfo.Root" />
public IDirectoryInfo Root
=> New(_fileSystem.Storage.GetLocation(string.Empty.PrefixRoot(_fileSystem)),
_fileSystem);
{
get
{
using IDisposable registration = RegisterProperty(nameof(Root), PropertyAccess.Get);

return New(_fileSystem.Storage.GetLocation(string.Empty.PrefixRoot(_fileSystem)),
_fileSystem);
}
}

/// <inheritdoc cref="IDirectoryInfo.Create()" />
public void Create()
Expand Down
46 changes: 41 additions & 5 deletions Source/Testably.Abstractions.Testing/FileSystem/FileInfoMock.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Xml.Linq;
using Testably.Abstractions.Testing.Helpers;
using Testably.Abstractions.Testing.Statistics;
using Testably.Abstractions.Testing.Storage;
Expand All @@ -26,23 +27,51 @@ private FileInfoMock(IStorageLocation location,

/// <inheritdoc cref="IFileInfo.Directory" />
public IDirectoryInfo? Directory
=> DirectoryInfoMock.New(Location.GetParent(),
_fileSystem);
{
get
{
using IDisposable registration = RegisterProperty(nameof(Directory), PropertyAccess.Get);

return DirectoryInfoMock.New(Location.GetParent(),
_fileSystem);
}
}

/// <inheritdoc cref="IFileInfo.DirectoryName" />
public string? DirectoryName
=> Directory?.FullName;
{
get
{
using IDisposable registration = RegisterProperty(nameof(DirectoryName), PropertyAccess.Get);

return Directory?.FullName;
}
}

/// <inheritdoc cref="IFileSystemInfo.Exists" />
public override bool Exists
=> base.Exists && FileSystemType == FileSystemTypes.File;
{
get
{
using IDisposable registration = RegisterProperty(nameof(Exists), PropertyAccess.Get);

return base.Exists && FileSystemType == FileSystemTypes.File;
}
}

/// <inheritdoc cref="IFileInfo.IsReadOnly" />
public bool IsReadOnly
{
get => (Attributes & FileAttributes.ReadOnly) != 0;
get
{
using IDisposable registration = RegisterProperty(nameof(IsReadOnly), PropertyAccess.Get);

return (Attributes & FileAttributes.ReadOnly) != 0;
}
set
{
using IDisposable registration = RegisterProperty(nameof(IsReadOnly), PropertyAccess.Set);

if (value)
{
Attributes |= FileAttributes.ReadOnly;
Expand All @@ -59,6 +88,8 @@ public long Length
{
get
{
using IDisposable registration = RegisterProperty(nameof(Length), PropertyAccess.Get);

if (Container is NullContainer ||
Container.Type != FileSystemTypes.File)
{
Expand All @@ -77,6 +108,8 @@ public override string Name
{
get
{
using IDisposable registration = RegisterProperty(nameof(Name), PropertyAccess.Get);

if (Location.FullPath.EndsWith(FileSystem.Path.DirectorySeparatorChar))
{
return string.Empty;
Expand Down Expand Up @@ -370,6 +403,9 @@ public IFileInfo Replace(string destinationFileName,
return new FileInfoMock(location, fileSystem);
}

protected override IDisposable RegisterProperty(string name, PropertyAccess access)
=> _fileSystem.StatisticsRegistration.FileInfo.RegisterProperty(Location.FullPath, name, access);

protected override IDisposable RegisterMethod(string name)
=> _fileSystem.StatisticsRegistration.FileInfo.RegisterMethod(Location.FullPath, name);

Expand Down
149 changes: 136 additions & 13 deletions Source/Testably.Abstractions.Testing/FileSystem/FileStreamMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ namespace Testably.Abstractions.Testing.FileSystem;
/// </summary>
internal sealed class FileStreamMock : FileSystemStream, IFileSystemExtensibility
{
/// <inheritdoc cref="FileSystemStream.CanRead" />
public override bool CanRead
=> _access.HasFlag(FileAccess.Read);

/// <inheritdoc cref="FileSystemStream.CanWrite" />
public override bool CanWrite
=> _access.HasFlag(FileAccess.Write);

private readonly FileAccess _access;
private readonly IDisposable _accessLock;
private readonly IStorageContainer _container;
Expand Down Expand Up @@ -74,9 +66,9 @@ private FileStreamMock(MemoryStream stream,
_access = access;
_ = bufferSize;
_options = options;
_initialPosition = Position;
_initialPosition = base.Position;

_location = _fileSystem.Storage.GetLocation(Name);
_location = _fileSystem.Storage.GetLocation(base.Name);
_location.ThrowExceptionIfNotFound(_fileSystem, true);
IStorageContainer file = _fileSystem.Storage.GetContainer(_location);
if (file is NullContainer)
Expand All @@ -85,7 +77,7 @@ private FileStreamMock(MemoryStream stream,
_mode.Equals(FileMode.Truncate))
{
throw ExceptionFactory.FileNotFound(
_fileSystem.Path.GetFullPath(Name));
_fileSystem.Path.GetFullPath(base.Name));
}

file = _fileSystem.Storage.GetOrCreateContainer(_location,
Expand All @@ -97,10 +89,10 @@ private FileStreamMock(MemoryStream stream,
_fileSystem.Execute.OnWindows(
() =>
throw ExceptionFactory.AccessToPathDenied(
_fileSystem.Path.GetFullPath(Name)),
_fileSystem.Path.GetFullPath(base.Name)),
() =>
throw ExceptionFactory.FileAlreadyExists(
_fileSystem.Path.GetFullPath(Name), 17));
_fileSystem.Path.GetFullPath(base.Name), 17));
}
else if (_mode.Equals(FileMode.CreateNew))
{
Expand All @@ -122,6 +114,134 @@ private FileStreamMock(MemoryStream stream,
InitializeStream();
}

/// <inheritdoc cref="FileSystemStream.CanRead" />
public override bool CanRead
{
get
{
using IDisposable registration = RegisterProperty(nameof(CanRead), PropertyAccess.Get);

return _access.HasFlag(FileAccess.Read);
}
}

/// <inheritdoc cref="FileSystemStream.CanSeek" />
public override bool CanSeek
{
get
{
using IDisposable registration = RegisterProperty(nameof(CanSeek), PropertyAccess.Get);

return base.CanSeek;
}
}

/// <inheritdoc cref="FileSystemStream.CanTimeout" />
public override bool CanTimeout
{
get
{
using IDisposable registration = RegisterProperty(nameof(CanTimeout), PropertyAccess.Get);

return base.CanTimeout;
}
}

/// <inheritdoc cref="FileSystemStream.CanWrite" />
public override bool CanWrite
{
get
{
using IDisposable registration = RegisterProperty(nameof(CanWrite), PropertyAccess.Get);

return _access.HasFlag(FileAccess.Write);
}
}

/// <inheritdoc cref="FileSystemStream.IsAsync" />
public override bool IsAsync
{
get
{
using IDisposable registration = RegisterProperty(nameof(IsAsync), PropertyAccess.Get);

return base.IsAsync;
}
}

/// <inheritdoc cref="FileSystemStream.Length" />
public override long Length
{
get
{
using IDisposable registration = RegisterProperty(nameof(Length), PropertyAccess.Get);

return base.Length;
}
}

/// <inheritdoc cref="FileSystemStream.Name" />
public override string Name
{
get
{
using IDisposable registration = RegisterProperty(nameof(Name), PropertyAccess.Get);

return base.Name;
}
}

/// <inheritdoc cref="FileSystemStream.Position" />
public override long Position
{
get
{
using IDisposable registration = RegisterProperty(nameof(Position), PropertyAccess.Get);

return base.Position;
}
set
{
using IDisposable registration = RegisterProperty(nameof(Position), PropertyAccess.Set);

base.Position = value;
}
}

/// <inheritdoc cref="FileSystemStream.ReadTimeout" />
public override int ReadTimeout
{
get
{
using IDisposable registration = RegisterProperty(nameof(ReadTimeout), PropertyAccess.Get);

return base.ReadTimeout;
}
set
{
using IDisposable registration = RegisterProperty(nameof(ReadTimeout), PropertyAccess.Set);

base.ReadTimeout = value;
}
}

/// <inheritdoc cref="FileSystemStream.WriteTimeout" />
public override int WriteTimeout
{
get
{
using IDisposable registration = RegisterProperty(nameof(WriteTimeout), PropertyAccess.Get);

return base.WriteTimeout;
}
set
{
using IDisposable registration = RegisterProperty(nameof(WriteTimeout), PropertyAccess.Set);

base.WriteTimeout = value;
}
}

/// <inheritdoc cref="FileSystemStream.BeginRead(byte[], int, int, AsyncCallback?, object?)" />
public override IAsyncResult BeginRead(byte[] buffer,
int offset,
Expand Down Expand Up @@ -538,6 +658,9 @@ public void StoreMetadata<T>(string key, T? value)
public T? RetrieveMetadata<T>(string key)
=> _container.Extensibility.RetrieveMetadata<T>(key);

private IDisposable RegisterProperty(string name, PropertyAccess access)
=> _fileSystem.StatisticsRegistration.FileStream.RegisterProperty(_location.FullPath, name, access);

private IDisposable RegisterMethod(string name)
=> _fileSystem.StatisticsRegistration.FileStream.RegisterMethod(_location.FullPath, name);

Expand Down
Loading

0 comments on commit d038671

Please sign in to comment.