Skip to content

Commit 5b59c8f

Browse files
committed
Fix tests
1 parent afe8cfe commit 5b59c8f

9 files changed

+45
-14
lines changed

src/libraries/System.IO.FileSystem/tests/Base/AllGetSetAttributes.cs

+2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ public void InvalidParameters()
2525
[Theory, MemberData(nameof(TrailingCharacters))]
2626
public void SetAttributes_MissingFile(char trailingChar)
2727
{
28+
if (!CanBeReadOnly) return;
2829
Assert.Throws<FileNotFoundException>(() => SetAttributes(GetTestFilePath() + trailingChar, FileAttributes.ReadOnly));
2930
}
3031

3132
[Theory, MemberData(nameof(TrailingCharacters))]
3233
public void SetAttributes_MissingDirectory(char trailingChar)
3334
{
35+
if (!CanBeReadOnly) return;
3436
Assert.Throws<DirectoryNotFoundException>(() => SetAttributes(Path.Combine(GetTestFilePath(), "file" + trailingChar), FileAttributes.ReadOnly));
3537
}
3638

src/libraries/System.IO.FileSystem/tests/Base/BaseGetSetAttributes.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace System.IO.Tests
77
{
88
public abstract class BaseGetSetAttributes : FileSystemTest
99
{
10+
protected abstract bool CanBeReadOnly { get; }
1011
protected abstract FileAttributes GetAttributes(string path);
1112
protected abstract void SetAttributes(string path, FileAttributes attributes);
1213

src/libraries/System.IO.FileSystem/tests/Base/FileGetSetAttributes.cs

+21-7
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ namespace System.IO.Tests
88
// Tests that are valid for File and FileInfo
99
public abstract class FileGetSetAttributes : BaseGetSetAttributes
1010
{
11-
[Theory]
12-
[InlineData(FileAttributes.ReadOnly)]
13-
[InlineData(FileAttributes.Normal)]
11+
[Fact]
1412
[PlatformSpecific(TestPlatforms.AnyUnix)]
15-
public void SettingAttributes_Unix(FileAttributes attributes)
13+
public void SettingAttributes_Unix_Normal()
1614
{
1715
string path = CreateItem();
18-
AssertSettingAttributes(path, attributes);
16+
AssertSettingAttributes(path, FileAttributes.Normal);
17+
}
18+
19+
[ConditionalFact(nameof(CanBeReadOnly))]
20+
[PlatformSpecific(TestPlatforms.AnyUnix)]
21+
public void SettingAttributes_Unix_ReadOnly()
22+
{
23+
string path = CreateItem();
24+
AssertSettingAttributes(path, FileAttributes.ReadOnly);
1925
}
2026

2127
[Theory]
@@ -28,16 +34,24 @@ public void SettingAttributes_OSXAndFreeBSD(FileAttributes attributes)
2834
}
2935

3036
[Theory]
31-
[InlineData(FileAttributes.ReadOnly)]
3237
[InlineData(FileAttributes.Hidden)]
3338
[InlineData(FileAttributes.System)]
3439
[InlineData(FileAttributes.Archive)]
3540
[InlineData(FileAttributes.Normal)]
3641
[InlineData(FileAttributes.Temporary)]
42+
public void SettingAttributes_Windows(FileAttributes attributes)
43+
{
44+
string path = CreateItem();
45+
AssertSettingAttributes(path, attributes);
46+
}
47+
48+
[Theory]
49+
[InlineData(FileAttributes.ReadOnly)]
3750
[InlineData(FileAttributes.ReadOnly | FileAttributes.Hidden)]
3851
[PlatformSpecific(TestPlatforms.Windows)]
39-
public void SettingAttributes_Windows(FileAttributes attributes)
52+
public void SettingAttributes_Windows_ReadOnly(FileAttributes attributes)
4053
{
54+
if (!CanBeReadOnly) return;
4155
string path = CreateItem();
4256
AssertSettingAttributes(path, attributes);
4357
}

src/libraries/System.IO.FileSystem/tests/DirectoryInfo/GetSetAttributes.cs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ protected override string CreateItem(string path = null, [CallerMemberName] stri
1515
protected override DirectoryInfo CreateInfo(string path) => new DirectoryInfo(path);
1616
protected override void DeleteItem(string path) => Directory.Delete(path);
1717
protected override bool IsDirectory => true;
18+
protected override bool CanBeReadOnly => true;
1819

1920
[Theory]
2021
[InlineData(FileAttributes.ReadOnly)]

src/libraries/System.IO.FileSystem/tests/File/GetSetAttributes.cs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class File_GetSetAttributes : BaseGetSetAttributes
99
{
1010
protected override FileAttributes GetAttributes(string path) => File.GetAttributes(path);
1111
protected override void SetAttributes(string path, FileAttributes attributes) => File.SetAttributes(path, attributes);
12+
protected override bool CanBeReadOnly => false;
1213

1314
// Getting only throws for File, not FileInfo
1415
[Theory, MemberData(nameof(TrailingCharacters))]

src/libraries/System.IO.FileSystem/tests/File/GetSetAttributes_SafeFileHandle.cs

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.Win32.SafeHandles;
5+
46
namespace System.IO.Tests
57
{
6-
public sealed class GetSetAttributes_SafeFileHandle : FileGetSetAttributes
8+
public class GetSetAttributes_SafeFileHandle : FileGetSetAttributes
79
{
10+
protected virtual SafeFileHandle OpenFileHandle(string path, FileAccess fileAccess) =>
11+
File.OpenHandle(
12+
path,
13+
FileMode.OpenOrCreate,
14+
fileAccess,
15+
FileShare.None);
16+
17+
protected override bool CanBeReadOnly => false;
18+
819
protected override FileAttributes GetAttributes(string path)
920
{
10-
using var fileHandle =
11-
File.OpenHandle(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
21+
using SafeFileHandle fileHandle = OpenFileHandle(path, FileAccess.Read);
1222
return File.GetAttributes(fileHandle);
1323
}
14-
24+
1525
protected override void SetAttributes(string path, FileAttributes attributes)
1626
{
17-
using var fileHandle =
18-
File.OpenHandle(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
27+
using SafeFileHandle fileHandle = OpenFileHandle(path, FileAccess.ReadWrite);
1928
File.SetAttributes(fileHandle, attributes);
2029
}
2130
}

src/libraries/System.IO.FileSystem/tests/File/GetSetAttributes_String.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
namespace System.IO.Tests
55
{
66
// Concrete class to run common file attributes tests on the File class
7-
public class File_GetSetAttributes_String : FileGetSetAttributes
7+
public sealed class File_GetSetAttributes_String : FileGetSetAttributes
88
{
99
protected override FileAttributes GetAttributes(string path) => File.GetAttributes(path);
1010
protected override void SetAttributes(string path, FileAttributes attributes) => File.SetAttributes(path, attributes);
11+
protected override bool CanBeReadOnly => true;
1112
}
1213
}

src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetAttributes.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace System.IO.Tests
77
{
88
public class FileInfo_GetSetAttributes : InfoGetSetAttributes<FileInfo>
99
{
10+
protected override bool CanBeReadOnly => true;
1011
protected override FileAttributes GetAttributes(string path) => new FileInfo(path).Attributes;
1112
protected override void SetAttributes(string path, FileAttributes attributes) => new FileInfo(path).Attributes = attributes;
1213
protected override FileInfo CreateInfo(string path) => new FileInfo(path);

src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetAttributesCommon.cs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace System.IO.Tests
66
// Concrete class to run common file attributes tests on the FileInfo class
77
public class FileInfo_GetSetAttributesCommon : FileGetSetAttributes
88
{
9+
protected override bool CanBeReadOnly => true;
910
protected override FileAttributes GetAttributes(string path) => new FileInfo(path).Attributes;
1011
protected override void SetAttributes(string path, FileAttributes attributes) => new FileInfo(path).Attributes = attributes;
1112
}

0 commit comments

Comments
 (0)