mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 12:18:50 +02:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user