Skip to content

Commit bc8cf56

Browse files
authored
refactor: use non-static OS selector in tests (#464)
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 <[email protected]>
1 parent d037fd4 commit bc8cf56

25 files changed

+1513
-1175
lines changed

Tests/Helpers/Testably.Abstractions.TestHelpers/FileSystemTestBase.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ public abstract class FileSystemTestBase<TFileSystem>
1717
where TFileSystem : IFileSystem
1818
{
1919
public abstract string BasePath { get; }
20+
public Test Test { get; }
2021
public TFileSystem FileSystem { get; }
2122
public ITimeSystem TimeSystem { get; }
2223

2324
// ReSharper disable once UnusedMember.Global
24-
protected FileSystemTestBase(TFileSystem fileSystem,
25+
protected FileSystemTestBase(
26+
Test test,
27+
TFileSystem fileSystem,
2528
ITimeSystem timeSystem)
2629
{
30+
Test = test;
2731
FileSystem = fileSystem;
2832
TimeSystem = timeSystem;
2933

Original file line numberDiff line numberDiff line change
@@ -1,72 +1,66 @@
1-
using System.IO.Abstractions;
2-
using System.Runtime.InteropServices;
3-
using Xunit;
4-
5-
namespace Testably.Abstractions.TestHelpers;
6-
7-
public static class Test
8-
{
9-
private static bool? _isNetFramework;
10-
11-
public static bool IsNetFramework
12-
{
13-
get
14-
{
15-
_isNetFramework ??= RuntimeInformation
16-
.FrameworkDescription.StartsWith(".NET Framework");
17-
return _isNetFramework.Value;
18-
}
19-
}
20-
21-
public static bool RunsOnLinux
22-
=> RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
23-
24-
public static bool RunsOnMac
25-
=> RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
26-
27-
public static bool RunsOnWindows
28-
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
29-
30-
public static bool IsNet7OrGreater
1+
using System.IO.Abstractions;
2+
using System.Runtime.InteropServices;
3+
using Xunit;
4+
5+
namespace Testably.Abstractions.TestHelpers;
6+
7+
public class Test
8+
{
9+
public static bool IsNet7OrGreater
3110
#if NET7_0_OR_GREATER
32-
=> true;
33-
#else
34-
=> false;
35-
#endif
36-
37-
public static void SkipBrittleTestsOnRealFileSystem(
38-
IFileSystem fileSystem, bool condition = true)
39-
{
40-
Skip.If(fileSystem is RealFileSystem && condition,
41-
"Brittle tests are skipped on the real file system.");
42-
}
43-
44-
public static void SkipBrittleTestsOnRealTimeSystem(
45-
ITimeSystem timeSystem, bool condition = true)
46-
{
47-
Skip.If(timeSystem is RealTimeSystem && condition,
48-
"Brittle tests are skipped on the real time system.");
49-
}
50-
51-
public static void SkipIfLongRunningTestsShouldBeSkipped(IFileSystem fileSystem)
52-
{
53-
#if DEBUG && !INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE
54-
Skip.If(fileSystem is RealFileSystem,
55-
"Long-Running tests are skipped in DEBUG mode unless the build constant 'INCLUDE_LONG_RUNNING_TESTS_ALSO_IN_DEBUG_MODE' is set.");
56-
#endif
57-
// ReSharper disable once CommentTypo
58-
// Do nothing when in release mode or `INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE` is set
59-
}
60-
61-
public static void SkipIfTestsOnRealFileSystemShouldBeSkipped(IFileSystem fileSystem)
62-
{
11+
=> true;
12+
#else
13+
=> false;
14+
#endif
15+
public bool IsNetFramework { get; }
16+
17+
public bool RunsOnLinux { get; }
18+
19+
public bool RunsOnMac { get; }
20+
21+
public bool RunsOnWindows { get; }
22+
23+
public Test()
24+
{
25+
RunsOnLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
26+
RunsOnMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
27+
RunsOnWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
28+
IsNetFramework = RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework");
29+
}
30+
31+
public static void SkipBrittleTestsOnRealFileSystem(
32+
IFileSystem fileSystem, bool condition = true)
33+
{
34+
Skip.If(fileSystem is RealFileSystem && condition,
35+
"Brittle tests are skipped on the real file system.");
36+
}
37+
38+
public static void SkipBrittleTestsOnRealTimeSystem(
39+
ITimeSystem timeSystem, bool condition = true)
40+
{
41+
Skip.If(timeSystem is RealTimeSystem && condition,
42+
"Brittle tests are skipped on the real time system.");
43+
}
44+
45+
public static void SkipIfLongRunningTestsShouldBeSkipped(IFileSystem fileSystem)
46+
{
47+
#if DEBUG && !INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE
48+
Skip.If(fileSystem is RealFileSystem,
49+
"Long-Running tests are skipped in DEBUG mode unless the build constant 'INCLUDE_LONG_RUNNING_TESTS_ALSO_IN_DEBUG_MODE' is set.");
50+
#endif
51+
// ReSharper disable once CommentTypo
52+
// Do nothing when in release mode or `INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE` is set
53+
}
54+
55+
public static void SkipIfTestsOnRealFileSystemShouldBeSkipped(IFileSystem fileSystem)
56+
{
6357
#if NCRUNCH
64-
Skip.If(fileSystem is RealFileSystem, "NCrunch should not test the real file system.");
65-
#endif
58+
Skip.If(fileSystem is RealFileSystem, "NCrunch should not test the real file system.");
59+
#endif
6660
#if DEBUG && SKIP_TESTS_ON_REAL_FILESYSTEM
6761
Skip.If(fileSystem is RealFileSystem,
68-
"Tests against real FileSystem are skipped in DEBUG mode with the build constant 'SKIP_TESTS_ON_REAL_FILESYSTEM'.");
69-
#endif
70-
// Do nothing when in release mode or `SKIP_TESTS_ON_REAL_FILESYSTEM` is not set
71-
}
62+
"Tests against real FileSystem are skipped in DEBUG mode with the build constant 'SKIP_TESTS_ON_REAL_FILESYSTEM'.");
63+
#endif
64+
// Do nothing when in release mode or `SKIP_TESTS_ON_REAL_FILESYSTEM` is not set
65+
}
7266
}

Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace {@class.Namespace}
2121
{{
2222
public abstract partial class {@class.Name}<TFileSystem>
2323
{{
24-
protected {@class.Name}(TFileSystem fileSystem, ITimeSystem timeSystem)
25-
: base(fileSystem, timeSystem)
24+
protected {@class.Name}(Test test, TFileSystem fileSystem, ITimeSystem timeSystem)
25+
: base(test, fileSystem, timeSystem)
2626
{{
2727
}}
2828
}}
@@ -43,6 +43,7 @@ public MockFileSystemTests() : this(new MockFileSystem())
4343
}}
4444
4545
private MockFileSystemTests(MockFileSystem mockFileSystem) : base(
46+
new Test(),
4647
mockFileSystem,
4748
mockFileSystem.TimeSystem)
4849
{{
@@ -70,7 +71,7 @@ public sealed class RealFileSystemTests : {@class.Name}<RealFileSystem>, IDispos
7071
private readonly IDirectoryCleaner _directoryCleaner;
7172
7273
public RealFileSystemTests(ITestOutputHelper testOutputHelper)
73-
: base(new RealFileSystem(), new RealTimeSystem())
74+
: base(new Test(), new RealFileSystem(), new RealTimeSystem())
7475
{{
7576
_directoryCleaner = FileSystem
7677
.SetCurrentDirectoryToEmptyTemporaryDirectory($""{@class.Namespace}{{FileSystem.Path.DirectorySeparatorChar}}{@class.Name}-"", testOutputHelper.WriteLine);

Tests/Testably.Abstractions.Tests/FileSystem/Directory/CreateDirectoryTests.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void CreateDirectory_FileWithSameNameAlreadyExists_ShouldThrowIOException
6969
[SkippableFact]
7070
public void CreateDirectory_Root_ShouldNotThrowException()
7171
{
72-
string path = FileTestHelper.RootDrive();
72+
string path = FileTestHelper.RootDrive(Test);
7373
FileSystem.Directory.CreateDirectory(path);
7474

7575
Exception? exception = Record.Exception(() =>
@@ -313,9 +313,8 @@ public void CreateDirectory_TrailingDirectorySeparator_ShouldNotBeTrimmed(
313313
result.Name.Should().Be(expectedName.TrimEnd(
314314
FileSystem.Path.DirectorySeparatorChar,
315315
FileSystem.Path.AltDirectorySeparatorChar));
316-
result.FullName.Should().Be(System.IO.Path.Combine(BasePath, expectedName
317-
.Replace(FileSystem.Path.AltDirectorySeparatorChar,
318-
FileSystem.Path.DirectorySeparatorChar)));
316+
result.FullName.Should().Be($"{BasePath}{FileSystem.Path.DirectorySeparatorChar}{expectedName}"
317+
.Replace(FileSystem.Path.AltDirectorySeparatorChar, FileSystem.Path.DirectorySeparatorChar));
319318
FileSystem.Should().HaveDirectory(nameWithSuffix);
320319
}
321320
#endif

0 commit comments

Comments
 (0)