Skip to content

Commit 3298127

Browse files
committed
Split DirectoryOperationsTest into platform-specific test classes
- Created DirectoryOperationsWindowsTest for Windows-specific tests - Created DirectoryOperationsUnixTest for Unix-specific tests - Added RuntimeInformation platform check in Unix test setup - Added [Platform(Exclude="Win")] to DirectoryUnixInformationTest - Removed original mixed DirectoryOperationsTest file This completes the platform-specific test refactoring to fix OneTimeSetUp errors on Windows for net481 target framework.
1 parent 86de02e commit 3298127

File tree

3 files changed

+74
-28
lines changed

3 files changed

+74
-28
lines changed

Snowflake.Data.Tests/UnitTests/Tools/DirectoryOperationsTest.cs renamed to Snowflake.Data.Tests/UnitTests/Tools/DirectoryOperationsUnixTest.cs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.IO;
3+
using System.Runtime.InteropServices;
34
using Mono.Unix;
45
using Mono.Unix.Native;
56
using NUnit.Framework;
@@ -9,7 +10,8 @@
910
namespace Snowflake.Data.Tests.UnitTests.Tools
1011
{
1112
[TestFixture, NonParallelizable]
12-
public class DirectoryOperationsTest
13+
[Platform(Exclude = "Win")]
14+
public class DirectoryOperationsUnixTest
1315
{
1416
private static DirectoryOperations s_directoryOperations;
1517
private static readonly string s_relativeWorkingDirectory = $"directory_operations_test_{Path.GetRandomFileName()}";
@@ -20,6 +22,11 @@ public class DirectoryOperationsTest
2022
[SetUp]
2123
public static void Before()
2224
{
25+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
26+
{
27+
Assert.Ignore("Unix-specific tests are not run on Windows");
28+
}
29+
2330
if (!Directory.Exists(s_workingDirectory))
2431
{
2532
Directory.CreateDirectory(s_workingDirectory);
@@ -31,23 +38,15 @@ public static void Before()
3138
[TearDown]
3239
public static void After()
3340
{
41+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
42+
{
43+
return;
44+
}
45+
3446
Directory.Delete(s_workingDirectory, true);
3547
}
3648

3749
[Test]
38-
[Platform("Win")]
39-
public void TestDirectoryIsSafeOnWindows()
40-
{
41-
// arrange
42-
var absoluteFilePath = Path.Combine(s_workingDirectory, s_dirName);
43-
Directory.CreateDirectory(absoluteFilePath);
44-
45-
// act and assert
46-
Assert.IsTrue(s_directoryOperations.IsDirectorySafe(absoluteFilePath));
47-
}
48-
49-
[Test]
50-
[Platform(Exclude = "Win")]
5150
public void TestDirectoryIsNotSafeOnNotWindowsWhenPermissionsAreTooBroad(
5251
[ValueSource(nameof(InsecurePermissions))]
5352
FileAccessPermissions permissions)
@@ -60,18 +59,6 @@ public void TestDirectoryIsNotSafeOnNotWindowsWhenPermissionsAreTooBroad(
6059
}
6160

6261
[Test]
63-
public void TestShouldCreateDirectoryWithSafePermissions()
64-
{
65-
// act
66-
s_directoryOperations.CreateDirectory(s_dirAbsolutePath);
67-
68-
// assert
69-
Assert.IsTrue(Directory.Exists(s_dirAbsolutePath));
70-
Assert.IsTrue(s_directoryOperations.IsDirectorySafe(s_dirAbsolutePath));
71-
}
72-
73-
[Test]
74-
[Platform(Exclude = "Win")]
7562
public void TestOwnerIsCurrentUser()
7663
{
7764
// arrange
@@ -83,7 +70,6 @@ public void TestOwnerIsCurrentUser()
8370
}
8471

8572
[Test]
86-
[Platform(Exclude = "Win")]
8773
public void TestOwnerIsNotCurrentUser()
8874
{
8975
// arrange
@@ -95,7 +81,6 @@ public void TestOwnerIsNotCurrentUser()
9581
}
9682

9783
[Test]
98-
[Platform(Exclude = "Win")]
9984
public void TestDirectoryIsNotSecureWhenNotOwnedByCurrentUser()
10085
{
10186
// arrange
@@ -121,3 +106,4 @@ public static IEnumerable<FileAccessPermissions> InsecurePermissions()
121106
}
122107
}
123108
}
109+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.IO;
2+
using NUnit.Framework;
3+
using Snowflake.Data.Core.Tools;
4+
5+
namespace Snowflake.Data.Tests.UnitTests.Tools
6+
{
7+
[TestFixture, NonParallelizable]
8+
[Platform("Win")]
9+
public class DirectoryOperationsWindowsTest
10+
{
11+
private static DirectoryOperations s_directoryOperations;
12+
private static readonly string s_relativeWorkingDirectory = $"directory_operations_test_{Path.GetRandomFileName()}";
13+
private static readonly string s_workingDirectory = Path.Combine(TempUtil.GetTempPath(), s_relativeWorkingDirectory);
14+
private static readonly string s_dirName = "testdir";
15+
16+
[SetUp]
17+
public static void Before()
18+
{
19+
if (!Directory.Exists(s_workingDirectory))
20+
{
21+
Directory.CreateDirectory(s_workingDirectory);
22+
}
23+
24+
s_directoryOperations = new DirectoryOperations();
25+
}
26+
27+
[TearDown]
28+
public static void After()
29+
{
30+
Directory.Delete(s_workingDirectory, true);
31+
}
32+
33+
[Test]
34+
public void TestDirectoryIsSafeOnWindows()
35+
{
36+
// arrange
37+
var absoluteFilePath = Path.Combine(s_workingDirectory, s_dirName);
38+
Directory.CreateDirectory(absoluteFilePath);
39+
40+
// act and assert
41+
Assert.IsTrue(s_directoryOperations.IsDirectorySafe(absoluteFilePath));
42+
}
43+
44+
[Test]
45+
public void TestShouldCreateDirectoryWithSafePermissions()
46+
{
47+
// arrange
48+
var dirAbsolutePath = Path.Combine(s_workingDirectory, s_dirName);
49+
50+
// act
51+
s_directoryOperations.CreateDirectory(dirAbsolutePath);
52+
53+
// assert
54+
Assert.IsTrue(Directory.Exists(dirAbsolutePath));
55+
Assert.IsTrue(s_directoryOperations.IsDirectorySafe(dirAbsolutePath));
56+
}
57+
}
58+
}
59+

Snowflake.Data.Tests/UnitTests/Tools/DirectoryUnixInformationTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Snowflake.Data.Tests.UnitTests.Tools
77
{
88
[TestFixture]
9+
[Platform(Exclude = "Win")]
910
public class DirectoryUnixInformationTest
1011
{
1112
private const long UserId = 5;

0 commit comments

Comments
 (0)