From 6ade494bf76ac8c45ac924ebe095feda12298837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Tue, 18 Apr 2023 13:14:43 +0200 Subject: [PATCH] fix: always keep `FileAttributes.Directory` on directories (#970) Fix #957: Always keep `FileAttributes.Directory` on directories (and never on files). --- .../MockDirectoryInfo.cs | 2 +- .../MockFileInfo.cs | 2 +- .../MockDirectoryInfoTests.cs | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs index 206092c98..45cfc7135 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs @@ -82,7 +82,7 @@ public override IFileSystemInfo ResolveLinkTarget(bool returnFinalTarget) public override FileAttributes Attributes { get { return GetMockFileDataForRead().Attributes; } - set { GetMockFileDataForWrite().Attributes = value; } + set { GetMockFileDataForWrite().Attributes = value | FileAttributes.Directory; } } /// diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileInfo.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileInfo.cs index ffd723536..de5ddc004 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileInfo.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileInfo.cs @@ -66,7 +66,7 @@ public override FileAttributes Attributes set { var mockFileData = GetMockFileDataForWrite(); - mockFileData.Attributes = value; + mockFileData.Attributes = value & ~FileAttributes.Directory; } } diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs index 4ff34462d..150d5e383 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs @@ -66,6 +66,19 @@ public void MockDirectoryInfo_Attributes_ShouldReturnMinusOneForNonExistingFile( Assert.That(directoryInfo.Attributes, Is.EqualTo(expected)); } + [Test] + public void MockDirectoryInfo_Attributes_Clear_ShouldRemainDirectory() + { + var fileSystem = new MockFileSystem(); + var path = XFS.Path(@"c:\existing\directory"); + fileSystem.Directory.CreateDirectory(path); + var directoryInfo = fileSystem.DirectoryInfo.New(path); + directoryInfo.Attributes = 0; + + Assert.That(fileSystem.File.Exists(path), Is.False); + Assert.That(directoryInfo.Attributes, Is.EqualTo(FileAttributes.Directory)); + } + [Test] public void MockDirectoryInfo_Attributes_SetterShouldThrowDirectoryNotFoundExceptionOnNonExistingFileOrDirectory() {