Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
Fixes #441: InvalidOperationException on Directory.EnumerateFileSyste…
Browse files Browse the repository at this point in the history
…mEntries()
  • Loading branch information
Yomodo committed May 24, 2018
1 parent 36173be commit 8cc8a03
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public void AlphaFS_Directory_EnumerateFileSystemEntryInfos_TypeFileSystemEntryI






private void Directory_EnumerateFileSystemEntryInfos_TypeFileSystemEntryInfo(bool isNetwork)
{
UnitTestConstants.PrintUnitTestHeader(isNetwork);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;

namespace AlphaFS.UnitTest
{
Expand All @@ -38,21 +37,6 @@ public void AlphaFS_Directory_GetFileSystemEntryInfo_LocalAndNetwork_Success()
}


[TestMethod]
public void AlphaFS_Directory_GetFileSystemEntryInfo_LogicalDrives_Local_Success()
{
Directory_GetFileSystemEntryInfo_LogicalDrives();
}


[TestMethod]
public void AlphaFS_Directory_GetFileSystemEntryInfo_CatchDirectoryNotFoundException_FileExistsWithSameNameAsDirectory_LocalAndNetwork_Success()
{
Directory_GetFileSystemEntryInfo_CatchDirectoryNotFoundException_FileExistsWithSameNameAsDirectory(false);
Directory_GetFileSystemEntryInfo_CatchDirectoryNotFoundException_FileExistsWithSameNameAsDirectory(true);
}




private void Directory_GetFileSystemEntryInfo(bool isNetwork)
Expand Down Expand Up @@ -80,85 +64,6 @@ private void Directory_GetFileSystemEntryInfo(bool isNetwork)
Assert.IsTrue((fsei.Attributes & System.IO.FileAttributes.Directory) != 0, "The Directory attribute is not found, but is expected.");
Assert.AreEqual(tempPath, fsei.FullPath, "The paths are not equal, but are expected to be.");

Console.WriteLine();
}


private void Directory_GetFileSystemEntryInfo_LogicalDrives()
{
UnitTestConstants.PrintUnitTestHeader(false);


var drives = Alphaleonis.Win32.Filesystem.Directory.EnumerateLogicalDrives().ToList();

foreach (var drive in drives)
{
Console.WriteLine("\nInput Logical Drive: [{0}]", drive.Name);

try
{
var fsei = Alphaleonis.Win32.Filesystem.Directory.GetFileSystemEntryInfo(drive.Name);
UnitTestConstants.Dump(fsei, -19);


Assert.IsTrue(fsei.GetType().IsEquivalentTo(typeof(Alphaleonis.Win32.Filesystem.FileSystemEntryInfo)));
Assert.IsTrue((fsei.Attributes & System.IO.FileAttributes.Directory) != 0, "The Directory attribute is not found, but is expected.");


// Fixed local drives should have these attributes.
if (new Alphaleonis.Win32.Filesystem.DriveInfo(drive.Name).DriveType == System.IO.DriveType.Fixed)
{
Assert.IsTrue((fsei.Attributes & System.IO.FileAttributes.Hidden) != 0, "The Hidden attribute is not found, but is expected.");
Assert.IsTrue((fsei.Attributes & System.IO.FileAttributes.System) != 0, "The System attribute is not found, but is expected.");
}


Assert.AreEqual(".", fsei.FileName, "The file names are not equal, but are expected to be.");
Assert.AreEqual(drive.Name, fsei.FullPath, "The full paths are not equal, but are expected to be.");
}
catch (Exception ex)
{
Console.WriteLine("\n\tCaught UNEXPECTED Exception: [{0}] {1}", ex.GetType().Name, ex.Message);
}


Console.WriteLine();
}
}


private void Directory_GetFileSystemEntryInfo_CatchDirectoryNotFoundException_FileExistsWithSameNameAsDirectory(bool isNetwork)
{
var path = UnitTestConstants.NotepadExe;

if (!System.IO.File.Exists(path))
Assert.Inconclusive("Test ignored because {0} was not found.", path);


UnitTestConstants.PrintUnitTestHeader(isNetwork);

var tempPath = path;
if (isNetwork)
tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);


Console.WriteLine("\nInput Directory Path: [{0}]", tempPath);


var gotException = false;
try
{
Alphaleonis.Win32.Filesystem.Directory.GetFileSystemEntryInfo(tempPath);
}
catch (Exception ex)
{
var exName = ex.GetType().Name;
gotException = exName.Equals("DirectoryNotFoundException", StringComparison.OrdinalIgnoreCase);
Console.WriteLine("\n\tCaught {0} Exception: [{1}] {2}", gotException ? "EXPECTED" : "UNEXPECTED", exName, ex.Message);
}
Assert.IsTrue(gotException, "The exception is not caught, but is expected to.");


Console.WriteLine();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* Copyright (C) 2008-2018 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace AlphaFS.UnitTest
{
public partial class AlphaFS_FileSystemEntryInfoTest
{
// Pattern: <class>_<function>_<scenario>_<expected result>


[TestMethod]
public void AlphaFS_Directory_GetFileSystemEntryInfo_CatchDirectoryNotFoundException_FileExistsWithSameNameAsDirectory_LocalAndNetwork_Success()
{
Directory_GetFileSystemEntryInfo_CatchDirectoryNotFoundException_FileExistsWithSameNameAsDirectory(false);
Directory_GetFileSystemEntryInfo_CatchDirectoryNotFoundException_FileExistsWithSameNameAsDirectory(true);
}




private void Directory_GetFileSystemEntryInfo_CatchDirectoryNotFoundException_FileExistsWithSameNameAsDirectory(bool isNetwork)
{
var path = UnitTestConstants.NotepadExe;

if (!System.IO.File.Exists(path))
Assert.Inconclusive("Test ignored because {0} was not found.", path);


UnitTestConstants.PrintUnitTestHeader(isNetwork);

var tempPath = path;
if (isNetwork)
tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);


Console.WriteLine("\nInput Directory Path: [{0}]", tempPath);


var gotException = false;
try
{
Alphaleonis.Win32.Filesystem.Directory.GetFileSystemEntryInfo(tempPath);
}
catch (Exception ex)
{
var exType = ex.GetType();

gotException = exType == typeof(System.IO.DirectoryNotFoundException);

Console.WriteLine("\n\tCaught {0} Exception: [{1}] {2}", gotException ? "EXPECTED" : "UNEXPECTED", exType.Name, ex.Message);
}


Assert.IsTrue(gotException, "The exception is not caught, but is expected to.");


Console.WriteLine();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* Copyright (C) 2008-2018 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AlphaFS.UnitTest
{
public partial class AlphaFS_FileSystemEntryInfoTest
{
// Pattern: <class>_<function>_<scenario>_<expected result>



[TestMethod]
public void AlphaFS_Directory_GetFileSystemEntryInfo_LogicalDrives_Local_Success()
{
Directory_GetFileSystemEntryInfo_LogicalDrives();
}




private void Directory_GetFileSystemEntryInfo_LogicalDrives()
{
UnitTestConstants.PrintUnitTestHeader(false);


var drives = Alphaleonis.Win32.Filesystem.Directory.EnumerateLogicalDrives().ToList();

foreach (var drive in drives)
{
Console.WriteLine("\nInput Logical Drive: [{0}]", drive.Name);

try
{
var fsei = Alphaleonis.Win32.Filesystem.Directory.GetFileSystemEntryInfo(drive.Name);
UnitTestConstants.Dump(fsei, -19);


Assert.IsTrue(fsei.GetType().IsEquivalentTo(typeof(Alphaleonis.Win32.Filesystem.FileSystemEntryInfo)));
Assert.IsTrue((fsei.Attributes & System.IO.FileAttributes.Directory) != 0, "The Directory attribute is not found, but is expected.");


// Fixed local drives should have these attributes.
if (new Alphaleonis.Win32.Filesystem.DriveInfo(drive.Name).DriveType == System.IO.DriveType.Fixed)
{
Assert.IsTrue((fsei.Attributes & System.IO.FileAttributes.Hidden) != 0, "The Hidden attribute is not found, but is expected.");
Assert.IsTrue((fsei.Attributes & System.IO.FileAttributes.System) != 0, "The System attribute is not found, but is expected.");
}


Assert.AreEqual(".", fsei.FileName, "The file names are not equal, but are expected to be.");
Assert.AreEqual(drive.Name, fsei.FullPath, "The full paths are not equal, but are expected to be.");
}
catch (Exception ex)
{
Console.WriteLine("\n\tCaught UNEXPECTED Exception: [{0}] {1}", ex.GetType().Name, ex.Message);
}


Console.WriteLine();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,7 @@ public void AlphaFS_File_GetFileSystemEntryInfo_LocalAndNetwork_Success()
File_GetFileSystemEntryInfo(true);
}



[TestMethod]
public void AlphaFS_File_GetFileSystemEntryInfo_CatchFileNotFoundException_DirectoryExistsWithSameNameAsFile_LocalAndNetwork_Success()
{
File_GetFileSystemEntryInfo_CatchFileNotFoundException_DirectoryExistsWithSameNameAsFile(false);
File_GetFileSystemEntryInfo_CatchFileNotFoundException_DirectoryExistsWithSameNameAsFile(true);
}





private void File_GetFileSystemEntryInfo(bool isNetwork)
Expand All @@ -70,43 +61,6 @@ private void File_GetFileSystemEntryInfo(bool isNetwork)
Assert.IsTrue(fsei.Attributes != System.IO.FileAttributes.Directory, "The directory attribute is found, but is not expected.");
Assert.AreEqual(tempPath, fsei.FullPath, "The paths are not equal, but are expected to be.");

Console.WriteLine();
}


private void
File_GetFileSystemEntryInfo_CatchFileNotFoundException_DirectoryExistsWithSameNameAsFile(bool isNetwork)
{
var path = UnitTestConstants.SysRoot;

if (!System.IO.Directory.Exists(path))
Assert.Inconclusive("Test ignored because {0} was not found.", path);


UnitTestConstants.PrintUnitTestHeader(isNetwork);

var tempPath = path;
if (isNetwork)
tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);


Console.WriteLine("\nInput File Path: [{0}]", tempPath);


var gotException = false;
try
{
Alphaleonis.Win32.Filesystem.File.GetFileSystemEntryInfo(tempPath);
}
catch (Exception ex)
{
var exName = ex.GetType().Name;
gotException = exName.Equals("FileNotFoundException", StringComparison.OrdinalIgnoreCase);
Console.WriteLine("\n\tCaught {0} Exception: [{1}] {2}", gotException ? "EXPECTED" : "UNEXPECTED", exName, ex.Message);
}
Assert.IsTrue(gotException, "The exception is not caught, but is expected to.");


Console.WriteLine();
}
}
Expand Down
Loading

0 comments on commit 8cc8a03

Please sign in to comment.