Skip to content

Commit

Permalink
feat: use SimulationMode instead of OSPlatform (#563)
Browse files Browse the repository at this point in the history
* Use SimulationMode instead of OSPlatform
* Throw NotSupportedException when wrapping a DirectoryInfo, DriveInfo or FileInfo on a simulated MockFileSystem.
* Accept API changes
  • Loading branch information
vbreuss authored Apr 15, 2024
1 parent c360e56 commit 2466577
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public IDirectoryInfo New(string path)
using IDisposable registration = RegisterMethod(nameof(Wrap),
directoryInfo);

if (_fileSystem.SimulationMode != SimulationMode.Native)
{
throw new NotSupportedException(
"Wrapping a DirectoryInfo in a simulated file system is not supported!");
}

return DirectoryInfoMock.New(
_fileSystem.Storage.GetLocation(
directoryInfo?.FullName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public IDriveInfo New(string driveName)
using IDisposable registration = RegisterMethod(nameof(Wrap),
driveInfo);

if (_fileSystem.SimulationMode != SimulationMode.Native)
{
throw new NotSupportedException(
"Wrapping a DriveInfo in a simulated file system is not supported!");
}

if (driveInfo?.Name == null)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public IFileInfo New(string fileName)
using IDisposable registration = RegisterMethod(nameof(Wrap),
fileInfo);

if (_fileSystem.SimulationMode != SimulationMode.Native)
{
throw new NotSupportedException(
"Wrapping a FileInfo in a simulated file system is not supported!");
}

return FileInfoMock.New(
_fileSystem.Storage.GetLocation(
fileInfo?.FullName,
Expand Down
10 changes: 5 additions & 5 deletions Source/Testably.Abstractions.Testing/Helpers/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ internal partial class Execute
/// </summary>
public StringComparison StringComparisonMode { get; }

internal Execute(MockFileSystem fileSystem, OSPlatform osPlatform, bool isNetFramework = false)
internal Execute(MockFileSystem fileSystem, SimulationMode simulationMode)
{
IsLinux = osPlatform == OSPlatform.Linux;
IsMac = osPlatform == OSPlatform.OSX;
IsWindows = osPlatform == OSPlatform.Windows;
IsNetFramework = isNetFramework && IsWindows;
IsLinux = simulationMode == SimulationMode.Linux;
IsMac = simulationMode == SimulationMode.MacOS;
IsWindows = simulationMode == SimulationMode.Windows;
IsNetFramework = false;
StringComparisonMode = IsLinux
? StringComparison.Ordinal
: StringComparison.OrdinalIgnoreCase;
Expand Down
35 changes: 15 additions & 20 deletions Source/Testably.Abstractions.Testing/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using Testably.Abstractions.Testing.FileSystem;
using Testably.Abstractions.Testing.Helpers;
using Testably.Abstractions.Testing.Statistics;
Expand Down Expand Up @@ -30,6 +29,15 @@ public sealed class MockFileSystem : IFileSystem
/// </summary>
public IRandomSystem RandomSystem { get; }

/// <summary>
/// The simulation mode for the underlying operating system.
/// </summary>
/// <remarks>
/// Can be changed by setting <see cref="Initialization.SimulatingOperatingSystem(Testing.SimulationMode)" /> in
/// the constructor.
/// </remarks>
public SimulationMode SimulationMode { get; }

/// <summary>
/// Contains statistical information about the file system usage.
/// </summary>
Expand Down Expand Up @@ -93,9 +101,10 @@ internal MockFileSystem(Action<Initialization> initializationCallback)
Initialization initialization = new();
initializationCallback(initialization);

Execute = initialization.OperatingSystem == null
SimulationMode = initialization.SimulationMode;
Execute = SimulationMode == SimulationMode.Native
? new Execute(this)
: new Execute(this, initialization.OperatingSystem.Value);
: new Execute(this, SimulationMode);
StatisticsRegistration = new FileSystemStatistics(this);
using IDisposable release = StatisticsRegistration.Ignore();
RandomSystem = new MockRandomSystem();
Expand Down Expand Up @@ -225,28 +234,14 @@ internal class Initialization
/// <summary>
/// The simulated operating system.
/// </summary>
internal OSPlatform? OperatingSystem { get; private set; }
internal SimulationMode SimulationMode { get; private set; } = SimulationMode.Native;

/// <summary>
/// Specify the operating system that should be simulated.
/// </summary>
/// <remarks>
/// Supported values are<br />
/// - <see cref="OSPlatform.Linux" /><br />
/// - <see cref="OSPlatform.OSX" /><br />
/// - <see cref="OSPlatform.Windows" />
/// </remarks>
internal Initialization SimulatingOperatingSystem(OSPlatform operatingSystem)
internal Initialization SimulatingOperatingSystem(SimulationMode simulationMode)
{
if (operatingSystem != OSPlatform.Linux &&
operatingSystem != OSPlatform.OSX &&
operatingSystem != OSPlatform.Windows)
{
throw new NotSupportedException(
"Only Linux, OSX and Windows are supported operating systems.");
}

OperatingSystem = operatingSystem;
SimulationMode = simulationMode;
return this;
}

Expand Down
29 changes: 29 additions & 0 deletions Source/Testably.Abstractions.Testing/SimulationMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Runtime.InteropServices;

namespace Testably.Abstractions.Testing;

/// <summary>
/// The simulation mode for the <see cref="MockFileSystem" />.
/// </summary>
public enum SimulationMode
{
/// <summary>
/// Use the underlying operating system (default mode).
/// </summary>
Native = 0,

/// <summary>
/// Simulates <see cref="OSPlatform.Linux" />.
/// </summary>
Linux = 1,

/// <summary>
/// Simulates <see cref="OSPlatform.OSX" />.
/// </summary>
MacOS = 2,

/// <summary>
/// Simulates <see cref="OSPlatform.Windows" />.
/// </summary>
Windows = 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ namespace Testably.Abstractions.Testing
public Testably.Abstractions.Testing.FileSystem.INotificationHandler Notify { get; }
public System.IO.Abstractions.IPath Path { get; }
public Testably.Abstractions.IRandomSystem RandomSystem { get; }
public Testably.Abstractions.Testing.SimulationMode SimulationMode { get; }
public Testably.Abstractions.Testing.Statistics.IFileSystemStatistics Statistics { get; }
public Testably.Abstractions.ITimeSystem TimeSystem { get; }
public override string ToString() { }
Expand Down Expand Up @@ -181,6 +182,13 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.RandomProvider.Generator<T> op_Implicit(T[] values) { }
}
}
public enum SimulationMode
{
Native = 0,
Linux = 1,
MacOS = 2,
Windows = 3,
}
public static class TimeProvider
{
public static Testably.Abstractions.Testing.TimeSystem.ITimeProvider Now() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ namespace Testably.Abstractions.Testing
public Testably.Abstractions.Testing.FileSystem.INotificationHandler Notify { get; }
public System.IO.Abstractions.IPath Path { get; }
public Testably.Abstractions.IRandomSystem RandomSystem { get; }
public Testably.Abstractions.Testing.SimulationMode SimulationMode { get; }
public Testably.Abstractions.Testing.Statistics.IFileSystemStatistics Statistics { get; }
public Testably.Abstractions.ITimeSystem TimeSystem { get; }
public override string ToString() { }
Expand Down Expand Up @@ -181,6 +182,13 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.RandomProvider.Generator<T> op_Implicit(T[] values) { }
}
}
public enum SimulationMode
{
Native = 0,
Linux = 1,
MacOS = 2,
Windows = 3,
}
public static class TimeProvider
{
public static Testably.Abstractions.Testing.TimeSystem.ITimeProvider Now() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ namespace Testably.Abstractions.Testing
public Testably.Abstractions.Testing.FileSystem.INotificationHandler Notify { get; }
public System.IO.Abstractions.IPath Path { get; }
public Testably.Abstractions.IRandomSystem RandomSystem { get; }
public Testably.Abstractions.Testing.SimulationMode SimulationMode { get; }
public Testably.Abstractions.Testing.Statistics.IFileSystemStatistics Statistics { get; }
public Testably.Abstractions.ITimeSystem TimeSystem { get; }
public override string ToString() { }
Expand Down Expand Up @@ -181,6 +182,13 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.RandomProvider.Generator<T> op_Implicit(T[] values) { }
}
}
public enum SimulationMode
{
Native = 0,
Linux = 1,
MacOS = 2,
Windows = 3,
}
public static class TimeProvider
{
public static Testably.Abstractions.Testing.TimeSystem.ITimeProvider Now() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ namespace Testably.Abstractions.Testing
public Testably.Abstractions.Testing.FileSystem.INotificationHandler Notify { get; }
public System.IO.Abstractions.IPath Path { get; }
public Testably.Abstractions.IRandomSystem RandomSystem { get; }
public Testably.Abstractions.Testing.SimulationMode SimulationMode { get; }
public Testably.Abstractions.Testing.Statistics.IFileSystemStatistics Statistics { get; }
public Testably.Abstractions.ITimeSystem TimeSystem { get; }
public override string ToString() { }
Expand Down Expand Up @@ -179,6 +180,13 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.RandomProvider.Generator<T> op_Implicit(T[] values) { }
}
}
public enum SimulationMode
{
Native = 0,
Linux = 1,
MacOS = 2,
Windows = 3,
}
public static class TimeProvider
{
public static Testably.Abstractions.Testing.TimeSystem.ITimeProvider Now() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ namespace Testably.Abstractions.Testing
public Testably.Abstractions.Testing.FileSystem.INotificationHandler Notify { get; }
public System.IO.Abstractions.IPath Path { get; }
public Testably.Abstractions.IRandomSystem RandomSystem { get; }
public Testably.Abstractions.Testing.SimulationMode SimulationMode { get; }
public Testably.Abstractions.Testing.Statistics.IFileSystemStatistics Statistics { get; }
public Testably.Abstractions.ITimeSystem TimeSystem { get; }
public override string ToString() { }
Expand Down Expand Up @@ -179,6 +180,13 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.RandomProvider.Generator<T> op_Implicit(T[] values) { }
}
}
public enum SimulationMode
{
Native = 0,
Linux = 1,
MacOS = 2,
Windows = 3,
}
public static class TimeProvider
{
public static Testably.Abstractions.Testing.TimeSystem.ITimeProvider Now() { }
Expand Down
Loading

0 comments on commit 2466577

Please sign in to comment.