Skip to content

Commit

Permalink
refactor: use non-static OS selector in tests (#464)
Browse files Browse the repository at this point in the history
As a prerequisite to #460 refactor how to handle OS-specific use cases
in tests:
- Make the `Test` class non-static and a property of the
source-generated test classes
- Replace all calls to the `Test` methods to use the property instead

---------

Co-authored-by: Valentin <valentin.breuss@baur.at>
  • Loading branch information
vbreuss and vbreuss authored Feb 27, 2024
1 parent d037fd4 commit bc8cf56
Show file tree
Hide file tree
Showing 25 changed files with 1,513 additions and 1,175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ public abstract class FileSystemTestBase<TFileSystem>
where TFileSystem : IFileSystem
{
public abstract string BasePath { get; }
public Test Test { get; }
public TFileSystem FileSystem { get; }
public ITimeSystem TimeSystem { get; }

// ReSharper disable once UnusedMember.Global
protected FileSystemTestBase(TFileSystem fileSystem,
protected FileSystemTestBase(
Test test,
TFileSystem fileSystem,
ITimeSystem timeSystem)
{
Test = test;
FileSystem = fileSystem;
TimeSystem = timeSystem;

Expand Down
128 changes: 61 additions & 67 deletions Tests/Helpers/Testably.Abstractions.TestHelpers/Test.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,66 @@
using System.IO.Abstractions;
using System.Runtime.InteropServices;
using Xunit;

namespace Testably.Abstractions.TestHelpers;

public static class Test
{
private static bool? _isNetFramework;

public static bool IsNetFramework
{
get
{
_isNetFramework ??= RuntimeInformation
.FrameworkDescription.StartsWith(".NET Framework");
return _isNetFramework.Value;
}
}

public static bool RunsOnLinux
=> RuntimeInformation.IsOSPlatform(OSPlatform.Linux);

public static bool RunsOnMac
=> RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

public static bool RunsOnWindows
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

public static bool IsNet7OrGreater
using System.IO.Abstractions;
using System.Runtime.InteropServices;
using Xunit;

namespace Testably.Abstractions.TestHelpers;

public class Test
{
public static bool IsNet7OrGreater
#if NET7_0_OR_GREATER
=> true;
#else
=> false;
#endif

public static void SkipBrittleTestsOnRealFileSystem(
IFileSystem fileSystem, bool condition = true)
{
Skip.If(fileSystem is RealFileSystem && condition,
"Brittle tests are skipped on the real file system.");
}

public static void SkipBrittleTestsOnRealTimeSystem(
ITimeSystem timeSystem, bool condition = true)
{
Skip.If(timeSystem is RealTimeSystem && condition,
"Brittle tests are skipped on the real time system.");
}

public static void SkipIfLongRunningTestsShouldBeSkipped(IFileSystem fileSystem)
{
#if DEBUG && !INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE
Skip.If(fileSystem is RealFileSystem,
"Long-Running tests are skipped in DEBUG mode unless the build constant 'INCLUDE_LONG_RUNNING_TESTS_ALSO_IN_DEBUG_MODE' is set.");
#endif
// ReSharper disable once CommentTypo
// Do nothing when in release mode or `INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE` is set
}

public static void SkipIfTestsOnRealFileSystemShouldBeSkipped(IFileSystem fileSystem)
{
=> true;
#else
=> false;
#endif
public bool IsNetFramework { get; }

public bool RunsOnLinux { get; }

public bool RunsOnMac { get; }

public bool RunsOnWindows { get; }

public Test()
{
RunsOnLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
RunsOnMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
RunsOnWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
IsNetFramework = RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework");
}

public static void SkipBrittleTestsOnRealFileSystem(
IFileSystem fileSystem, bool condition = true)
{
Skip.If(fileSystem is RealFileSystem && condition,
"Brittle tests are skipped on the real file system.");
}

public static void SkipBrittleTestsOnRealTimeSystem(
ITimeSystem timeSystem, bool condition = true)
{
Skip.If(timeSystem is RealTimeSystem && condition,
"Brittle tests are skipped on the real time system.");
}

public static void SkipIfLongRunningTestsShouldBeSkipped(IFileSystem fileSystem)
{
#if DEBUG && !INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE
Skip.If(fileSystem is RealFileSystem,
"Long-Running tests are skipped in DEBUG mode unless the build constant 'INCLUDE_LONG_RUNNING_TESTS_ALSO_IN_DEBUG_MODE' is set.");
#endif
// ReSharper disable once CommentTypo
// Do nothing when in release mode or `INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE` is set
}

public static void SkipIfTestsOnRealFileSystemShouldBeSkipped(IFileSystem fileSystem)
{
#if NCRUNCH
Skip.If(fileSystem is RealFileSystem, "NCrunch should not test the real file system.");
#endif
Skip.If(fileSystem is RealFileSystem, "NCrunch should not test the real file system.");
#endif
#if DEBUG && SKIP_TESTS_ON_REAL_FILESYSTEM
Skip.If(fileSystem is RealFileSystem,
"Tests against real FileSystem are skipped in DEBUG mode with the build constant 'SKIP_TESTS_ON_REAL_FILESYSTEM'.");
#endif
// Do nothing when in release mode or `SKIP_TESTS_ON_REAL_FILESYSTEM` is not set
}
"Tests against real FileSystem are skipped in DEBUG mode with the build constant 'SKIP_TESTS_ON_REAL_FILESYSTEM'.");
#endif
// Do nothing when in release mode or `SKIP_TESTS_ON_REAL_FILESYSTEM` is not set
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace {@class.Namespace}
{{
public abstract partial class {@class.Name}<TFileSystem>
{{
protected {@class.Name}(TFileSystem fileSystem, ITimeSystem timeSystem)
: base(fileSystem, timeSystem)
protected {@class.Name}(Test test, TFileSystem fileSystem, ITimeSystem timeSystem)
: base(test, fileSystem, timeSystem)
{{
}}
}}
Expand All @@ -43,6 +43,7 @@ public MockFileSystemTests() : this(new MockFileSystem())
}}
private MockFileSystemTests(MockFileSystem mockFileSystem) : base(
new Test(),
mockFileSystem,
mockFileSystem.TimeSystem)
{{
Expand Down Expand Up @@ -70,7 +71,7 @@ public sealed class RealFileSystemTests : {@class.Name}<RealFileSystem>, IDispos
private readonly IDirectoryCleaner _directoryCleaner;
public RealFileSystemTests(ITestOutputHelper testOutputHelper)
: base(new RealFileSystem(), new RealTimeSystem())
: base(new Test(), new RealFileSystem(), new RealTimeSystem())
{{
_directoryCleaner = FileSystem
.SetCurrentDirectoryToEmptyTemporaryDirectory($""{@class.Namespace}{{FileSystem.Path.DirectorySeparatorChar}}{@class.Name}-"", testOutputHelper.WriteLine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void CreateDirectory_FileWithSameNameAlreadyExists_ShouldThrowIOException
[SkippableFact]
public void CreateDirectory_Root_ShouldNotThrowException()
{
string path = FileTestHelper.RootDrive();
string path = FileTestHelper.RootDrive(Test);
FileSystem.Directory.CreateDirectory(path);

Exception? exception = Record.Exception(() =>
Expand Down Expand Up @@ -313,9 +313,8 @@ public void CreateDirectory_TrailingDirectorySeparator_ShouldNotBeTrimmed(
result.Name.Should().Be(expectedName.TrimEnd(
FileSystem.Path.DirectorySeparatorChar,
FileSystem.Path.AltDirectorySeparatorChar));
result.FullName.Should().Be(System.IO.Path.Combine(BasePath, expectedName
.Replace(FileSystem.Path.AltDirectorySeparatorChar,
FileSystem.Path.DirectorySeparatorChar)));
result.FullName.Should().Be($"{BasePath}{FileSystem.Path.DirectorySeparatorChar}{expectedName}"
.Replace(FileSystem.Path.AltDirectorySeparatorChar, FileSystem.Path.DirectorySeparatorChar));
FileSystem.Should().HaveDirectory(nameWithSuffix);
}
#endif
Expand Down
Loading

0 comments on commit bc8cf56

Please sign in to comment.