From 36fa19032dbb132c25cf594c2cf06276d94ef573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20B=C3=A4ckstr=C3=B6m?= Date: Thu, 22 Jun 2017 10:50:47 +0300 Subject: [PATCH] Issue #1 - Tasks using Pattern matching for finding files now throw an exception if the directory does not exist or the user does not have read access to the directory. --- Frends.File.Tests/UnitTest.cs | 14 ++++++++++++++ Frends.File/File.cs | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/Frends.File.Tests/UnitTest.cs b/Frends.File.Tests/UnitTest.cs index 3b2116d..4e32b15 100644 --- a/Frends.File.Tests/UnitTest.cs +++ b/Frends.File.Tests/UnitTest.cs @@ -239,6 +239,20 @@ public void FindFiles() Assert.That(results.All(x => x.Extension.Equals(".xml"))); } + [Test] + public void FindFilesShouldThrowIfFolderNotExist() + { + var ex = Assert.Throws(() => File.Find( + new FindInput() + { + Directory = "DoesNotExist", + Pattern = "**.*" + }, + new FindOption())); + + Assert.That(ex.Message, Does.Contain("Directory does not exist or you do not have read access")); + } + [Test] public async Task WriteFileAppend() { diff --git a/Frends.File/File.cs b/Frends.File/File.cs index 4f27723..116f27b 100644 --- a/Frends.File/File.cs +++ b/Frends.File/File.cs @@ -132,6 +132,13 @@ private static TResult ExecuteAction(Func action, bool useGive internal static PatternMatchingResult FindMatchingFiles(string directoryPath, string pattern) { + // Check the user can access the folder + // This will return false if the path does not exist or you do not have read permissions. + if (!Directory.Exists(directoryPath)) + { + throw new Exception($"Directory does not exist or you do not have read access. Tried to access directory '{directoryPath}'"); + } + var matcher = new Matcher(); matcher.AddInclude(pattern); var results = matcher.Execute(new DirectoryInfoWrapper(new DirectoryInfo(directoryPath)));