From 966d3e752efc3f7e52a83bfb3d8e46b62478c6fc Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Fri, 20 Jan 2023 15:03:40 +0800 Subject: [PATCH] Fix environment variable expansion logic --- Flow.Launcher.Test/Plugins/ExplorerTest.cs | 17 +++++++++++++++++ .../Search/EnvironmentVariables.cs | 10 ++++++++-- .../Search/SearchManager.cs | 14 ++++---------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Flow.Launcher.Test/Plugins/ExplorerTest.cs b/Flow.Launcher.Test/Plugins/ExplorerTest.cs index 70c13f296..e9d37433f 100644 --- a/Flow.Launcher.Test/Plugins/ExplorerTest.cs +++ b/Flow.Launcher.Test/Plugins/ExplorerTest.cs @@ -442,5 +442,22 @@ namespace Flow.Launcher.Test.Plugins // When, Then Assert.IsTrue(hash1 == hash2); } + + [TestCase(@"%appdata%", true)] + [TestCase(@"%appdata%\123", true)] + [TestCase(@"c:\foo %appdata%\", false)] + [TestCase(@"c:\users\%USERNAME%\downloads", true)] + [TestCase(@"c:\downloads", false)] + [TestCase(@"%", false)] + [TestCase(@"%%", false)] + [TestCase(@"%bla%blabla%", false)] + public void GivenPath_WhenHavingEnvironmentVariableOrNot_ThenShouldBeExpected(string path, bool expectedResult) + { + // When + var result = EnvironmentVariables.HasEnvironmentVar(path); + + // Then + Assert.AreEqual(result, expectedResult); + } } } diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs index a649de58e..5b0a5a8bd 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; +using System.Linq; using Flow.Launcher.Plugin.SharedCommands; namespace Flow.Launcher.Plugin.Explorer.Search @@ -29,9 +30,14 @@ namespace Flow.Launcher.Plugin.Explorer.Search && EnvStringPaths.Count > 0; } - internal static bool BeginsWithEnvironmentVar(string search) + public static bool HasEnvironmentVar(string search) { - return search[0] == '%' && search[1..].Contains("%\\"); + // "c:\foo %appdata%\" returns false + var splited = search.Split(Path.DirectorySeparatorChar); + return splited.Any(dir => dir.StartsWith('%') && + dir.EndsWith('%') && + dir.Length > 2 && + dir.Split('%').Length == 3); } internal static Dictionary LoadEnvironmentStringPaths() diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs index 916399e5c..83057ff04 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs @@ -173,18 +173,12 @@ namespace Flow.Launcher.Plugin.Explorer.Search var results = new HashSet(PathEqualityComparator.Instance); - var isEnvironmentVariable = EnvironmentVariables.IsEnvironmentVariableSearch(querySearch); - - if (isEnvironmentVariable) + if (EnvironmentVariables.IsEnvironmentVariableSearch(querySearch)) return EnvironmentVariables.GetEnvironmentStringPathSuggestions(querySearch, query, Context); - // Query is a location path with a full environment variable, eg. %appdata%\somefolder\ - var isEnvironmentVariablePath = EnvironmentVariables.BeginsWithEnvironmentVar(querySearch); - - var locationPath = querySearch; - - if (isEnvironmentVariablePath) - locationPath = Environment.ExpandEnvironmentVariables(locationPath); + // Query is a location path with a full environment variable, eg. %appdata%\somefolder\, c:\users\%USERNAME%\downloads + var needToExpand = EnvironmentVariables.HasEnvironmentVar(querySearch); + var locationPath = needToExpand ? Environment.ExpandEnvironmentVariables(querySearch) : querySearch; // Check that actual location exists, otherwise directory search will throw directory not found exception if (!FilesFolders.ReturnPreviousDirectoryIfIncompleteString(locationPath).LocationExists())