Directory.Exists() incorrectly returns false if directory in path ends in period
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
Thanks for reporting! @all-contributors Please add @blarson for bug reports.
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!
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 ,
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 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. 👍🏻
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.