System.IO.Abstractions icon indicating copy to clipboard operation
System.IO.Abstractions copied to clipboard

Directory.Exists() incorrectly returns false if directory in path ends in period

Open blarson opened this issue 4 years ago • 6 comments

Describe the bug After calling MockFileSystem.AddFile(string, MockFileData) using a path that includes a directory ending in a period, MockFileSystem.Directory.Exists(string) will return false when called against the directory containing that file when targeting .NET Framework 4.6.1. When targeting either .NET Framework 4.7.2 or .NET Core 3.1, MockFileSystem.Directory.Exists(string) correctly returns true.

To Reproduce Steps to reproduce the behavior:

        static void Main(string[] args)
        {
            var mockFileSystem = new MockFileSystem();

            var directory = "C:\\baddir.\\subdir";
            var filepath = mockFileSystem.Path.Combine(directory, "Test.txt");

            mockFileSystem.AddFile(filepath, new MockFileData(string.Empty));
            Debug.Assert(mockFileSystem.Directory.Exists(directory));
        }

Expected behavior MockFileSystem.Directory.Exists(string) should return true.

Additional context .NET Framework 4.6.1; System.IO.Abstractions.TestingHelpers v13.2.25

blarson avatar Mar 23 '21 16:03 blarson

Thanks for reporting! @all-contributors Please add @blarson for bug reports.

fgreinacher avatar Apr 19 '21 19:04 fgreinacher

@fgreinacher

I've put up a pull request to add @blarson! :tada:

allcontributors[bot] avatar Apr 19 '21 19:04 allcontributors[bot]

This seems to be a limitation of the file API and works also identical on the real file system. See https://stackoverflow.com/q/15775867 or Microsoft Windows Naming Conventions:

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".

Therefore I would suggest to close this issue!

vbreuss avatar Apr 18 '23 19:04 vbreuss

This seems to be a limitation of the file API and works also identical on the real file system. See https://stackoverflow.com/q/15775867 or Microsoft Windows Naming Conventions:

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".

Therefore I would suggest to close this issue!

The limitation doesn't exist on Linux - at least, it's possible to work with files/directories ending with a period in bash without any issues. I haven't checked with dotnet, or this abstraction, yet.

hangy avatar Apr 19 '23 09:04 hangy

@hangy , my comment only referred to this issue on .NET Framework 4.6.1 (and therefore only to Windows). The mocked file system works under this circumstance the same as the real file system. E.g. Directory.CreateDirectory("C:\\baddir.\\subdir"); will create a directory without trailing dot "C:\\baddir\\subdir" when using .NET Framework 4.6.1. Therefore I would argue, that the mocked file system works as intended (same as the real file system).

On newer versions and especially under .NET core, a trailing dot should pose no problem!

vbreuss avatar Apr 19 '23 14:04 vbreuss

@vbreuss Sorry, I missed that this issue is mainly about the Windows bug. If there are any inconsistencies with .NET 7/Linux, that's a different topic. 👍🏻

hangy avatar Apr 19 '23 20:04 hangy

This issue only affects .NET Framework 4.6.2 and there the mock file system behaves the same as the real file system.

It is a bug in .NET Framework itself.

vbreuss avatar Mar 02 '24 10:03 vbreuss