Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #78 from bazmecode/master
Browse files Browse the repository at this point in the history
added case-sensitive search for bug #77 fix.
  • Loading branch information
mbos committed Oct 24, 2015
2 parents e172b42 + 205e8de commit b2d0073
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
14 changes: 13 additions & 1 deletion Pass4Win/FileSystemInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
* A copy of the license is obtainable at http://www.gnu.org/licenses/gpl-3.0.en.html#content
*/




namespace Pass4Win
{
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

/// <summary>
Expand Down Expand Up @@ -62,7 +66,7 @@ public void Search(string searchtext)
this.SearchList.Clear();
foreach (FileInfo tmpFileInfo in new DirectoryInfo(this.passWordStore).GetFiles("*.gpg", SearchOption.AllDirectories))
{
if (tmpFileInfo.Name.Contains(searchtext))
if(Regex.IsMatch(tmpFileInfo.Name, WildcardToRegex(searchtext), RegexOptions.IgnoreCase))
{
this.SearchList.Add(tmpFileInfo.FullName);
}
Expand Down Expand Up @@ -115,5 +119,13 @@ public List<string> UpdateDirectoryList(DirectoryInfo directoryInfo)
{
return (from ffile in directoryInfo.GetFiles() where !ffile.Name.StartsWith(".") where ffile.Extension.ToLower() == ".gpg" select Path.GetFileNameWithoutExtension(ffile.Name)).ToList();
}

private string WildcardToRegex(string pattern)
{
return "^" + Regex.Escape(pattern)
.Replace(@"\*", ".*")
.Replace(@"\?", ".")
+ "$";
}
}
}
77 changes: 76 additions & 1 deletion Pass4WinTests/FileSystemInterfaceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Pass4Win.Tests


namespace Pass4Win.Tests
{
using System;
using System.IO;
using System.Windows.Forms;

Expand Down Expand Up @@ -56,5 +59,77 @@ public void FileSearch()
Assert.True(tmpFileInfo.Exists);
}
}

/// <summary>
/// Tests for case-sensitive searching for files in a given directory
/// </summary>
[Test()]
public void CaseSensitiveFileSearch()
{
string fileName = "caseSensitiveFile.gpg";
string caseSensitiveFile = Application.LocalUserAppDataPath + "\\" + fileName;
try
{
FileInfo fileInfo = new FileInfo(caseSensitiveFile);
using (File.Create(Path.Combine(caseSensitiveFile)))
{
}

FileSystemInterface fsi = new FileSystemInterface(Application.LocalUserAppDataPath);
fsi.Search(fileName.ToLower());

Assert.AreEqual(1, fsi.SearchList.Count);
foreach (var row in fsi.SearchList)
{
Assert.IsNotNull(row);
FileInfo tmpFileInfo = new FileInfo(row);
Assert.True(tmpFileInfo.Exists);
}
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
finally
{
File.Delete(caseSensitiveFile);
}
}

/// <summary>
/// Tests for wildcard searching for files in a given directory
/// </summary>
[Test()]
public void WildCardFileSearch()
{
string fileName = "caseSensitiveFile.gpg";
string caseSensitiveFile = Application.LocalUserAppDataPath + "\\" + fileName;
try
{
FileInfo fileInfo = new FileInfo(caseSensitiveFile);
using (File.Create(Path.Combine(caseSensitiveFile)))
{
}

FileSystemInterface fsi = new FileSystemInterface(Application.LocalUserAppDataPath);
fsi.Search("*.*");

Assert.AreEqual(1, fsi.SearchList.Count);
foreach (var row in fsi.SearchList)
{
Assert.IsNotNull(row);
FileInfo tmpFileInfo = new FileInfo(row);
Assert.True(tmpFileInfo.Exists);
}
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
finally
{
File.Delete(caseSensitiveFile);
}
}
}
}

0 comments on commit b2d0073

Please sign in to comment.