mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 12:18:50 +02:00
Remove hidden files from indexer results (#4325)
* Added functionality to not display hidden files * Added interfaces for seperating db layer * Updated variable naming and refactored Database connection class * Added tests for WindowsSearchAPI class * Fixed nit with braces * Added function to test that all connections from database are closed
This commit is contained in:
committed by
GitHub
parent
7ed03c8b90
commit
a21a3827fd
@@ -4,6 +4,9 @@ using System.Collections.Generic;
|
||||
using System.Data.OleDb;
|
||||
using Microsoft.Search.Interop;
|
||||
using Microsoft.Plugin.Indexer.SearchHelper;
|
||||
using Microsoft.Plugin.Indexer.Interface;
|
||||
using Moq;
|
||||
using System.Linq;
|
||||
|
||||
namespace Wox.Test.Plugins
|
||||
{
|
||||
@@ -11,13 +14,20 @@ namespace Wox.Test.Plugins
|
||||
[TestFixture]
|
||||
public class WindowsIndexerTest
|
||||
{
|
||||
private WindowsSearchAPI _api = new WindowsSearchAPI();
|
||||
|
||||
public WindowsSearchAPI GetWindowsSearchAPI()
|
||||
{
|
||||
var mock = new Mock<ISearch>();
|
||||
mock.Setup(x => x.Query("dummy-connection-string", "dummy-query")).Returns(new List<OleDBResult>());
|
||||
return new WindowsSearchAPI(mock.Object);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InitQueryHelper_ShouldInitialize_WhenFunctionIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
int maxCount = 10;
|
||||
WindowsSearchAPI _api = GetWindowsSearchAPI();
|
||||
ISearchQueryHelper queryHelper = null;
|
||||
|
||||
// Act
|
||||
@@ -34,6 +44,7 @@ namespace Wox.Test.Plugins
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "*";
|
||||
WindowsSearchAPI _api = GetWindowsSearchAPI();
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
@@ -50,6 +61,7 @@ namespace Wox.Test.Plugins
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "tt*^&)";
|
||||
WindowsSearchAPI _api = GetWindowsSearchAPI();
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
@@ -66,6 +78,7 @@ namespace Wox.Test.Plugins
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "tt%^&)";
|
||||
WindowsSearchAPI _api = GetWindowsSearchAPI();
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
@@ -82,6 +95,7 @@ namespace Wox.Test.Plugins
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "tt_^&)";
|
||||
WindowsSearchAPI _api = GetWindowsSearchAPI();
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
@@ -98,6 +112,7 @@ namespace Wox.Test.Plugins
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "tt?^&)";
|
||||
WindowsSearchAPI _api = GetWindowsSearchAPI();
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
@@ -114,6 +129,7 @@ namespace Wox.Test.Plugins
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "tt^&)bc";
|
||||
WindowsSearchAPI _api = GetWindowsSearchAPI();
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
@@ -128,37 +144,75 @@ namespace Wox.Test.Plugins
|
||||
public void ExecuteQuery_ShouldDisposeAllConnections_AfterFunctionCall()
|
||||
{
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
_api.ModifyQueryHelper(ref queryHelper, "*");
|
||||
string keyword = "test";
|
||||
bool commandDisposed = false;
|
||||
bool resultDisposed = false;
|
||||
OleDBSearch oleDbSearch = new OleDBSearch();
|
||||
WindowsSearchAPI _api = new WindowsSearchAPI(oleDbSearch);
|
||||
|
||||
// Act
|
||||
_api.ExecuteQuery(queryHelper, keyword);
|
||||
try
|
||||
{
|
||||
_api.command.ExecuteReader();
|
||||
}
|
||||
catch(InvalidOperationException)
|
||||
{
|
||||
commandDisposed = true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_api.WDSResults.Read();
|
||||
}
|
||||
catch(InvalidOperationException)
|
||||
{
|
||||
resultDisposed = true;
|
||||
}
|
||||
_api.Search("FilePath");
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(_api.conn.State == System.Data.ConnectionState.Closed);
|
||||
Assert.IsTrue(commandDisposed);
|
||||
Assert.IsTrue(resultDisposed);
|
||||
Assert.IsTrue(oleDbSearch.HaveAllDisposableItemsBeenDisposed());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WindowsSearchAPI_ShouldShowHiddenFiles_WhenDisplayHiddenFilesIsTrue()
|
||||
{
|
||||
// Arrange
|
||||
OleDBResult unHiddenFile = new OleDBResult(new List<object>() { "C:/test/path/file1.txt", "file1.txt", (Int64)0x0 });
|
||||
OleDBResult hiddenFile = new OleDBResult(new List<object>() { "C:/test/path/file2.txt", "file2.txt", (Int64)0x2 });
|
||||
List<OleDBResult> results = new List<OleDBResult>() { hiddenFile, unHiddenFile };
|
||||
var mock = new Mock<ISearch>();
|
||||
mock.Setup(x => x.Query(It.IsAny<string>(), It.IsAny<string>())).Returns(results);
|
||||
WindowsSearchAPI _api = new WindowsSearchAPI(mock.Object, true);
|
||||
|
||||
// Act
|
||||
var windowsSearchAPIResults = _api.Search("FilePath");
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(windowsSearchAPIResults.Count() == 2);
|
||||
Assert.IsTrue(windowsSearchAPIResults.Any(x => x.Title == "file1.txt"));
|
||||
Assert.IsTrue(windowsSearchAPIResults.Any(x => x.Title == "file2.txt"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WindowsSearchAPI_ShouldNotShowHiddenFiles_WhenDisplayHiddenFilesIsFalse()
|
||||
{
|
||||
// Arrange
|
||||
OleDBResult unHiddenFile = new OleDBResult(new List<object>() { "C:/test/path/file1.txt", "file1.txt", (Int64)0x0 });
|
||||
OleDBResult hiddenFile = new OleDBResult(new List<object>() { "C:/test/path/file2.txt", "file2.txt", (Int64)0x2 });
|
||||
List<OleDBResult> results = new List<OleDBResult>() { hiddenFile, unHiddenFile };
|
||||
var mock = new Mock<ISearch>();
|
||||
mock.Setup(x => x.Query(It.IsAny<string>(), It.IsAny<string>())).Returns(results);
|
||||
WindowsSearchAPI _api = new WindowsSearchAPI(mock.Object, false);
|
||||
|
||||
// Act
|
||||
var windowsSearchAPIResults = _api.Search("FilePath");
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(windowsSearchAPIResults.Count() == 1);
|
||||
Assert.IsTrue(windowsSearchAPIResults.Any(x => x.Title == "file1.txt"));
|
||||
Assert.IsFalse(windowsSearchAPIResults.Any(x => x.Title == "file2.txt"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WindowsSearchAPI_ShouldNotReturnResultsWithNullValue_WhenDbResultHasANullColumn()
|
||||
{
|
||||
// Arrange
|
||||
OleDBResult file1 = new OleDBResult(new List<object>() { "C:/test/path/file1.txt", DBNull.Value, (Int64)0x0 });
|
||||
OleDBResult file2 = new OleDBResult(new List<object>() { "C:/test/path/file2.txt", "file2.txt", (Int64)0x0 });
|
||||
|
||||
List<OleDBResult> results = new List<OleDBResult>() { file1, file2 };
|
||||
var mock = new Mock<ISearch>();
|
||||
mock.Setup(x => x.Query(It.IsAny<string>(), It.IsAny<string>())).Returns(results);
|
||||
WindowsSearchAPI _api = new WindowsSearchAPI(mock.Object, false);
|
||||
|
||||
// Act
|
||||
var windowsSearchAPIResults = _api.Search("FilePath");
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(windowsSearchAPIResults.Count() == 1);
|
||||
Assert.IsFalse(windowsSearchAPIResults.Any(x => x.Title == "file1.txt"));
|
||||
Assert.IsTrue(windowsSearchAPIResults.Any(x => x.Title == "file2.txt"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user