Made Plugin Folder Unit tests & Expanding enviroment search (#6600)

* Made Plugin Folder Unit tests. Fixes '>' not recursive searching (with max). Added that paths with an UnauthorizedAccessException are ignored. Added expanding enviroment search.

* Fixed some merging errors

* Added feedback from review

* Made the change that  ryanbodrug-microsoft suggested

* Stupid merge request... fixed

Co-authored-by: p-storm <paul.de.man@gmail.com>
This commit is contained in:
P-Storm
2020-10-01 05:37:46 +02:00
committed by GitHub
parent b2c00b1e1a
commit 5c84de5400
31 changed files with 1264 additions and 296 deletions

View File

@@ -0,0 +1,70 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
namespace Microsoft.Plugin.Folder.Sources
{
public class ExplorerAction : IExplorerAction
{
private const string FileExplorerProgramName = "explorer";
public bool Execute(string path, IPublicAPI contextApi)
{
if (contextApi == null)
{
throw new ArgumentNullException(nameof(contextApi));
}
return OpenFileOrFolder(FileExplorerProgramName, path, contextApi);
}
public bool ExecuteSanitized(string search, IPublicAPI contextApi)
{
if (contextApi == null)
{
throw new ArgumentNullException(nameof(contextApi));
}
return Execute(SanitizedPath(search), contextApi);
}
private static string SanitizedPath(string search)
{
var sanitizedPath = Regex.Replace(search, @"[\/\\]+", "\\");
// A network path must start with \\
if (!sanitizedPath.StartsWith("\\", StringComparison.InvariantCulture))
{
return sanitizedPath;
}
return sanitizedPath.Insert(0, "\\");
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive and instead inform the user of the error")]
private static bool OpenFileOrFolder(string program, string path, IPublicAPI contextApi)
{
try
{
Process.Start(program, path);
}
catch (Exception e)
{
string messageBoxTitle = string.Format(CultureInfo.InvariantCulture, "{0} {1}", Properties.Resources.wox_plugin_folder_select_folder_OpenFileOrFolder_error_message, path);
Log.Exception($"Failed to open {path} in {FileExplorerProgramName}, {e.Message}", e, MethodBase.GetCurrentMethod().DeclaringType);
contextApi.ShowMsg(messageBoxTitle, e.Message);
}
return true;
}
}
}

View File

@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using Wox.Infrastructure.Storage;
namespace Microsoft.Plugin.Folder.Sources
{
internal class FolderLinksSettings : IFolderLinks
{
private readonly FolderSettings _settings;
public FolderLinksSettings(FolderSettings settings)
{
_settings = settings;
}
public IEnumerable<FolderLink> FolderLinks()
{
return _settings.FolderLinks;
}
}
}

View File

@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Wox.Plugin;
namespace Microsoft.Plugin.Folder.Sources
{
public interface IExplorerAction
{
bool Execute(string sanitizedPath, IPublicAPI contextApi);
bool ExecuteSanitized(string search, IPublicAPI contextApi);
}
}

View File

@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace Microsoft.Plugin.Folder.Sources
{
public interface IFolderLinks
{
IEnumerable<FolderLink> FolderLinks();
}
}

View File

@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.IO;
using Wox.Infrastructure.FileSystemHelper;
namespace Microsoft.Plugin.Folder.Sources
{
public interface IQueryFileSystemInfo : IDirectoryWrapper
{
IEnumerable<DisplayFileInfo> MatchFileSystemInfo(string search, string incompleteName, SearchOption searchOption);
}
}

View File

@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using Microsoft.Plugin.Folder.Sources.Result;
namespace Microsoft.Plugin.Folder.Sources
{
public interface IQueryInternalDirectory
{
IEnumerable<IItemResult> Query(string search);
}
}

View File

@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
namespace Microsoft.Plugin.Folder.Sources
{
public struct DisplayFileInfo : IEquatable<DisplayFileInfo>
{
public string Name { get; set; }
public string FullName { get; set; }
public DisplayType Type { get; set; }
public override bool Equals(object obj)
{
return obj is DisplayFileInfo other && Equals(other);
}
public override int GetHashCode()
{
return HashCode.Combine(Name, FullName, (int)Type);
}
public bool Equals(DisplayFileInfo other)
{
return Name == other.Name && FullName == other.FullName && Type == other.Type;
}
public static bool operator ==(DisplayFileInfo a, DisplayFileInfo b)
{
return a.Equals(b);
}
public static bool operator !=(DisplayFileInfo a, DisplayFileInfo b)
{
return !(a == b);
}
}
}

View File

@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Plugin.Folder.Sources
{
public enum DisplayType
{
Directory,
File,
}
}

View File

@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace Microsoft.Plugin.Folder.Sources
{
internal class DriveInformation : IDriveInformation
{
private static readonly List<string> DriverNames = InitialDriverList().ToList();
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Do not want to change the behavior of the application, but want to enforce static analysis")]
private static IEnumerable<string> InitialDriverList()
{
var directorySeparatorChar = System.IO.Path.DirectorySeparatorChar;
return DriveInfo.GetDrives()
.Select(driver => driver.Name.ToLower(CultureInfo.InvariantCulture).TrimEnd(directorySeparatorChar));
}
public IEnumerable<string> GetDriveNames() => DriverNames;
}
}

View File

@@ -0,0 +1,81 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
namespace Microsoft.Plugin.Folder.Sources
{
public class FolderHelper : IFolderHelper
{
private readonly IDriveInformation _driveInformation;
private readonly IFolderLinks _folderLinks;
public FolderHelper(IDriveInformation driveInformation, IFolderLinks folderLinks)
{
_driveInformation = driveInformation;
_folderLinks = folderLinks;
}
public IEnumerable<FolderLink> GetUserFolderResults(string query)
{
if (query == null)
{
throw new ArgumentNullException(paramName: nameof(query));
}
return _folderLinks.FolderLinks()
.Where(x => x.Nickname.StartsWith(query, StringComparison.OrdinalIgnoreCase));
}
public bool IsDriveOrSharedFolder(string search)
{
if (search == null)
{
throw new ArgumentNullException(nameof(search));
}
if (search.StartsWith(@"\\", StringComparison.InvariantCulture))
{ // share folder
return true;
}
var driverNames = _driveInformation.GetDriveNames()
.ToImmutableArray();
if (driverNames.Any())
{
if (driverNames.Any(dn => search.StartsWith(dn, StringComparison.InvariantCultureIgnoreCase)))
{
// normal drive letter
return true;
}
}
else
{
if (search.Length > 2 && ValidDriveLetter(search[0]) && search[1] == ':')
{ // when we don't have the drive letters we can try...
return true; // we don't know so let's give it the possibility
}
}
return false;
}
/// <summary>
/// This check is needed because char.IsLetter accepts more than [A-z]
/// </summary>
public static bool ValidDriveLetter(char c)
{
return c <= 122 && char.IsLetter(c);
}
public static string Expand(string search)
{
return Environment.ExpandEnvironmentVariables(search);
}
}
}

View File

@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace Microsoft.Plugin.Folder.Sources
{
public interface IDriveInformation
{
IEnumerable<string> GetDriveNames();
}
}

View File

@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Plugin.Folder.Sources
{
public interface IFolderHelper
{
bool IsDriveOrSharedFolder(string search);
}
}

View File

@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Wox.Infrastructure.FileSystemHelper;
namespace Microsoft.Plugin.Folder.Sources
{
public class QueryFileSystemInfo : DirectoryWrapper, IQueryFileSystemInfo
{
public IEnumerable<DisplayFileInfo> MatchFileSystemInfo(string search, string incompleteName, SearchOption searchOption)
{
// search folder and add results
var directoryInfo = new DirectoryInfo(search);
var fileSystemInfos = directoryInfo.EnumerateFileSystemInfos(incompleteName, searchOption);
return SafeEnumerateFileSystemInfos(fileSystemInfos)
.Where(fileSystemInfo => (fileSystemInfo.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
.Select(CreateDisplayFileInfo);
}
private static IEnumerable<FileSystemInfo> SafeEnumerateFileSystemInfos(IEnumerable<FileSystemInfo> fileSystemInfos)
{
using (var enumerator = fileSystemInfos.GetEnumerator())
{
while (true)
{
FileSystemInfo currentFileSystemInfo;
try
{
if (!enumerator.MoveNext())
{
break;
}
currentFileSystemInfo = enumerator.Current;
}
catch (UnauthorizedAccessException)
{
continue;
}
yield return currentFileSystemInfo;
}
}
}
private static DisplayFileInfo CreateDisplayFileInfo(FileSystemInfo fileSystemInfo)
{
return new DisplayFileInfo()
{
Name = fileSystemInfo.Name,
FullName = fileSystemInfo.FullName,
Type = GetDisplayType(fileSystemInfo),
};
}
private static DisplayType GetDisplayType(FileSystemInfo fileSystemInfo)
{
if (fileSystemInfo is DirectoryInfo)
{
return DisplayType.Directory;
}
else
{
return DisplayType.File;
}
}
}
}

View File

@@ -0,0 +1,188 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.Plugin.Folder.Sources.Result;
using Wox.Plugin;
namespace Microsoft.Plugin.Folder.Sources
{
public class QueryInternalDirectory : IQueryInternalDirectory
{
private readonly FolderSettings _settings;
private readonly IQueryFileSystemInfo _queryFileSystemInfo;
private static readonly HashSet<char> SpecialSearchChars = new HashSet<char>
{
'?', '*', '>',
};
private static string _warningIconPath;
public QueryInternalDirectory(FolderSettings folderSettings, IQueryFileSystemInfo queryFileSystemInfo)
{
_settings = folderSettings;
_queryFileSystemInfo = queryFileSystemInfo;
}
private static bool HasSpecialChars(string search)
{
return search.Any(c => SpecialSearchChars.Contains(c));
}
public static SearchOption GetSearchOptions(string query)
{
// give the ability to search all folder when it contains a >
if (query.Any(c => c.Equals('>')))
{
return SearchOption.AllDirectories;
}
return SearchOption.TopDirectoryOnly;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Do not want to change the behavior of the application, but want to enforce static analysis")]
private (string search, string incompleteName) Process(string search)
{
string incompleteName = string.Empty;
if (HasSpecialChars(search) || !_queryFileSystemInfo.Exists($@"{search}\"))
{
// if folder doesn't exist, we want to take the last part and use it afterwards to help the user
// find the right folder.
int index = search.LastIndexOf('\\');
// No slashes found, so probably not a folder
if (index <= 0 || index >= search.Length - 1)
{
return default;
}
// Remove everything after the last \ and add *
incompleteName = search.Substring(index + 1)
.ToLower(CultureInfo.InvariantCulture) + "*";
search = search.Substring(0, index + 1);
if (!_queryFileSystemInfo.Exists(search))
{
return default;
}
}
else
{
// folder exist, add \ at the end of doesn't exist
if (!search.EndsWith(@"\", StringComparison.InvariantCulture))
{
search += @"\";
}
}
return (search, incompleteName);
}
public IEnumerable<IItemResult> Query(string querySearch)
{
if (querySearch == null)
{
throw new ArgumentNullException(nameof(querySearch));
}
var processed = Process(querySearch);
if (processed == default)
{
yield break;
}
var (search, incompleteName) = processed;
var searchOption = GetSearchOptions(incompleteName);
if (searchOption == SearchOption.AllDirectories)
{
// match everything before and after search term using supported wildcard '*', ie. *searchterm*
if (string.IsNullOrEmpty(incompleteName))
{
incompleteName = "*";
}
else
{
incompleteName = "*" + incompleteName.Substring(1);
}
}
yield return new CreateOpenCurrentFolderResult(search);
// Note: Take 1000 is so that you don't search the whole system before you discard
var lookup = _queryFileSystemInfo.MatchFileSystemInfo(search, incompleteName, searchOption)
.Take(1000)
.ToLookup(r => r.Type);
var folderList = lookup[DisplayType.Directory].ToImmutableArray();
var fileList = lookup[DisplayType.File].ToImmutableArray();
var fileSystemResult = GenerateFolderResults(search, folderList)
.Concat<IItemResult>(GenerateFileResults(search, fileList))
.ToImmutableArray();
foreach (var result in fileSystemResult)
{
yield return result;
}
// Show warning message if result has been truncated
if (folderList.Length > _settings.MaxFolderResults || fileList.Length > _settings.MaxFileResults)
{
yield return GenerateTruncatedItemResult(folderList.Length + fileList.Length, fileSystemResult.Length);
}
}
private IEnumerable<FileItemResult> GenerateFileResults(string search, IEnumerable<DisplayFileInfo> fileList)
{
return fileList
.Select(fileSystemInfo => new FileItemResult()
{
FilePath = fileSystemInfo.FullName,
Search = search,
})
.OrderBy(x => x.Title)
.Take(_settings.MaxFileResults);
}
private IEnumerable<FolderItemResult> GenerateFolderResults(string search, IEnumerable<DisplayFileInfo> folderList)
{
return folderList
.Select(fileSystemInfo => new FolderItemResult(fileSystemInfo)
{
Search = search,
})
.OrderBy(x => x.Title)
.Take(_settings.MaxFolderResults);
}
private static TruncatedItemResult GenerateTruncatedItemResult(int preTruncationCount, int postTruncationCount)
{
return new TruncatedItemResult()
{
PreTruncationCount = preTruncationCount,
PostTruncationCount = postTruncationCount,
WarningIconPath = _warningIconPath,
};
}
public static void SetWarningIcon(Theme theme)
{
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
{
_warningIconPath = "Images/Warning.light.png";
}
else
{
_warningIconPath = "Images/Warning.dark.png";
}
}
}
}

View File

@@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Wox.Plugin;
namespace Microsoft.Plugin.Folder.Sources.Result
{
public class CreateOpenCurrentFolderResult : IItemResult
{
private readonly IExplorerAction _explorerAction;
public string Search { get; set; }
public CreateOpenCurrentFolderResult(string search)
: this(search, new ExplorerAction())
{
}
public CreateOpenCurrentFolderResult(string search, IExplorerAction explorerAction)
{
Search = search;
_explorerAction = explorerAction;
}
public Wox.Plugin.Result Create(IPublicAPI contextApi)
{
return new Wox.Plugin.Result
{
Title = $"Open {Search}",
QueryTextDisplay = Search,
SubTitle = $"Folder: Use > to search within the directory. Use * to search for file extensions. Or use both >*.",
IcoPath = Search,
Score = 500,
Action = c => _explorerAction.ExecuteSanitized(Search, contextApi),
};
}
}
}

View File

@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using Wox.Infrastructure;
using Wox.Plugin;
namespace Microsoft.Plugin.Folder.Sources.Result
{
public class FileItemResult : IItemResult
{
private static readonly IExplorerAction ExplorerAction = new ExplorerAction();
public string FilePath { get; set; }
public string Title => Path.GetFileName(FilePath);
public string Search { get; set; }
public Wox.Plugin.Result Create(IPublicAPI contextApi)
{
var result = new Wox.Plugin.Result
{
Title = Title,
SubTitle = "Folder: " + FilePath,
IcoPath = FilePath,
TitleHighlightData = StringMatcher.FuzzySearch(Search, Path.GetFileName(FilePath)).MatchData,
Action = c => ExplorerAction.Execute(FilePath, contextApi),
ContextData = new SearchResult { Type = ResultType.File, FullPath = FilePath },
};
return result;
}
}
}

View File

@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
namespace Microsoft.Plugin.Folder.Sources.Result
{
public class FileSystemResult : List<DisplayFileInfo>
{
public FileSystemResult()
{
}
public FileSystemResult([NotNull] IEnumerable<DisplayFileInfo> collection)
: base(collection)
{
}
public FileSystemResult(int capacity)
: base(capacity)
{
}
public static FileSystemResult Error(Exception exception)
{
return new FileSystemResult { Exception = exception };
}
public Exception Exception { get; private set; }
public bool HasException() => Exception != null;
}
}

View File

@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Wox.Infrastructure;
using Wox.Plugin;
namespace Microsoft.Plugin.Folder.Sources.Result
{
public class FolderItemResult : IItemResult
{
private static readonly IExplorerAction ExplorerAction = new ExplorerAction();
public FolderItemResult()
{
}
public FolderItemResult(DisplayFileInfo fileSystemInfo)
{
Title = fileSystemInfo.Name;
Subtitle = fileSystemInfo.FullName;
Path = fileSystemInfo.FullName;
}
public string Title { get; set; }
public string Subtitle { get; set; }
public string Path { get; set; }
public string Search { get; set; }
public Wox.Plugin.Result Create(IPublicAPI contextApi)
{
return new Wox.Plugin.Result
{
Title = Title,
IcoPath = Path,
SubTitle = "Folder: " + Subtitle,
QueryTextDisplay = Path,
TitleHighlightData = StringMatcher.FuzzySearch(Search, Title).MatchData,
ContextData = new SearchResult { Type = ResultType.Folder, FullPath = Path },
Action = c => ExplorerAction.Execute(Path, contextApi),
};
}
}
}

View File

@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Wox.Plugin;
namespace Microsoft.Plugin.Folder.Sources.Result
{
public interface IItemResult
{
string Search { get; set; }
Wox.Plugin.Result Create(IPublicAPI contextApi);
}
}

View File

@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Globalization;
using Wox.Plugin;
namespace Microsoft.Plugin.Folder.Sources.Result
{
public class TruncatedItemResult : IItemResult
{
public int PreTruncationCount { get; set; }
public int PostTruncationCount { get; set; }
public string WarningIconPath { get; set; }
public string Search { get; set; }
public Wox.Plugin.Result Create(IPublicAPI contextApi)
{
return new Wox.Plugin.Result
{
Title = Properties.Resources.Microsoft_plugin_folder_truncation_warning_title,
QueryTextDisplay = Search,
SubTitle = string.Format(CultureInfo.InvariantCulture, Properties.Resources.Microsoft_plugin_folder_truncation_warning_subtitle, PostTruncationCount, PreTruncationCount),
IcoPath = WarningIconPath,
};
}
}
}