Skip to content

Commit 966d3e7

Browse files
Fix environment variable expansion logic
1 parent 4d267fe commit 966d3e7

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

Flow.Launcher.Test/Plugins/ExplorerTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,5 +442,22 @@ public void GivenTwoPaths_WhenComparedHasCode_ThenShouldBeSame(string path1, str
442442
// When, Then
443443
Assert.IsTrue(hash1 == hash2);
444444
}
445+
446+
[TestCase(@"%appdata%", true)]
447+
[TestCase(@"%appdata%\123", true)]
448+
[TestCase(@"c:\foo %appdata%\", false)]
449+
[TestCase(@"c:\users\%USERNAME%\downloads", true)]
450+
[TestCase(@"c:\downloads", false)]
451+
[TestCase(@"%", false)]
452+
[TestCase(@"%%", false)]
453+
[TestCase(@"%bla%blabla%", false)]
454+
public void GivenPath_WhenHavingEnvironmentVariableOrNot_ThenShouldBeExpected(string path, bool expectedResult)
455+
{
456+
// When
457+
var result = EnvironmentVariables.HasEnvironmentVar(path);
458+
459+
// Then
460+
Assert.AreEqual(result, expectedResult);
461+
}
445462
}
446463
}

Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.IO;
5+
using System.Linq;
56
using Flow.Launcher.Plugin.SharedCommands;
67

78
namespace Flow.Launcher.Plugin.Explorer.Search
@@ -29,9 +30,14 @@ internal static bool IsEnvironmentVariableSearch(string search)
2930
&& EnvStringPaths.Count > 0;
3031
}
3132

32-
internal static bool BeginsWithEnvironmentVar(string search)
33+
public static bool HasEnvironmentVar(string search)
3334
{
34-
return search[0] == '%' && search[1..].Contains("%\\");
35+
// "c:\foo %appdata%\" returns false
36+
var splited = search.Split(Path.DirectorySeparatorChar);
37+
return splited.Any(dir => dir.StartsWith('%') &&
38+
dir.EndsWith('%') &&
39+
dir.Length > 2 &&
40+
dir.Split('%').Length == 3);
3541
}
3642

3743
internal static Dictionary<string, string> LoadEnvironmentStringPaths()

Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,12 @@ private async Task<List<Result>> PathSearchAsync(Query query, CancellationToken
173173

174174
var results = new HashSet<Result>(PathEqualityComparator.Instance);
175175

176-
var isEnvironmentVariable = EnvironmentVariables.IsEnvironmentVariableSearch(querySearch);
177-
178-
if (isEnvironmentVariable)
176+
if (EnvironmentVariables.IsEnvironmentVariableSearch(querySearch))
179177
return EnvironmentVariables.GetEnvironmentStringPathSuggestions(querySearch, query, Context);
180178

181-
// Query is a location path with a full environment variable, eg. %appdata%\somefolder\
182-
var isEnvironmentVariablePath = EnvironmentVariables.BeginsWithEnvironmentVar(querySearch);
183-
184-
var locationPath = querySearch;
185-
186-
if (isEnvironmentVariablePath)
187-
locationPath = Environment.ExpandEnvironmentVariables(locationPath);
179+
// Query is a location path with a full environment variable, eg. %appdata%\somefolder\, c:\users\%USERNAME%\downloads
180+
var needToExpand = EnvironmentVariables.HasEnvironmentVar(querySearch);
181+
var locationPath = needToExpand ? Environment.ExpandEnvironmentVariables(querySearch) : querySearch;
188182

189183
// Check that actual location exists, otherwise directory search will throw directory not found exception
190184
if (!FilesFolders.ReturnPreviousDirectoryIfIncompleteString(locationPath).LocationExists())

0 commit comments

Comments
 (0)