Skip to content

Commit

Permalink
scan all ProgramFiles dirs
Browse files Browse the repository at this point in the history
fixes #22
  • Loading branch information
SimonCropp committed May 11, 2020
1 parent e90e1bd commit d79c888
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/DiffEngine.Tests/OrderReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public void BadEnvironmentVariable()
Assert.Equal("Unable to parse tool from `DiffEngine.ToolOrder` environment variable: Foo", exception.Message);
}

public OrderReaderTest(ITestOutputHelper output) : base(output)
public OrderReaderTest(ITestOutputHelper output) :
base(output)
{
}
}
28 changes: 28 additions & 0 deletions src/DiffEngine.Tests/OsSettingsResolverTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Linq;
using Xunit;
using Xunit.Abstractions;

public class OsSettingsResolverTest :
XunitContextBase
{
[Fact]
public void Simple()
{
var paths = OsSettingsResolver.ExpandProgramFiles(new[] {"Path"}).ToList();
Assert.Equal("Path", paths.Single());
}

[Fact]
public void Expand()
{
var paths = OsSettingsResolver.ExpandProgramFiles(new[] {@"%ProgramFiles%\Path"}).ToList();
Assert.Equal(@"%ProgramFiles%\Path", paths[0]);
Assert.Equal(@"%ProgramW6432%\Path", paths[1]);
Assert.Equal(@"%ProgramFiles(x86)%\Path", paths[2]);
}

public OsSettingsResolverTest(ITestOutputHelper output) :
base(output)
{
}
}
2 changes: 2 additions & 0 deletions src/DiffEngine/DiffEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net461</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Caseless.Fody" Version="1.9.0" PrivateAssets="All" />
<PackageReference Include="EmptyFiles" Version="2.0.3" PrivateAssets="None" />
<PackageReference Include="Fody" Version="6.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" Condition="$(Configuration) == 'Release'" />
<PackageReference Include="ProjectDefaults" Version="1.0.42" PrivateAssets="All" />
<PackageReference Include="Nullable" Version="1.2.1" PrivateAssets="All" />
Expand Down
3 changes: 3 additions & 0 deletions src/DiffEngine/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" GenerateXsd="false">
<Caseless />
</Weavers>
25 changes: 23 additions & 2 deletions src/DiffEngine/OsSettingsResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using DiffEngine;

Expand All @@ -14,7 +15,9 @@ public static bool Resolve(
if (windows != null &&
RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (WildcardFileFinder.TryFindExe(windows.ExePaths, out path))
var paths = ExpandProgramFiles(windows.ExePaths);

if (WildcardFileFinder.TryFindExe(paths, out path))
{
arguments = windows.Arguments;
return true;
Expand Down Expand Up @@ -45,4 +48,22 @@ public static bool Resolve(
arguments = null;
return false;
}

public static IEnumerable<string> ExpandProgramFiles(IEnumerable<string> paths)
{
// Note: Windows can have multiple paths, and will resolve %ProgramFiles% as 'C:\Program Files (x86)'
// when running inside a 32-bit process. To
// overcome this issue, we need to manually add any option so the correct paths will be resolved

foreach (var windowsPath in paths)
{
yield return windowsPath;

if (windowsPath.Contains("%ProgramFiles%"))
{
yield return windowsPath.Replace("%ProgramFiles%", "%ProgramW6432%");
yield return windowsPath.Replace("%ProgramFiles%", "%ProgramFiles(x86)%");
}
}
}
}
2 changes: 1 addition & 1 deletion src/DiffEngine/WildcardFileFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static bool TryFindExe(
IEnumerable<string> paths,
[NotNullWhen(true)] out string? exePath)
{
foreach (var path in paths)
foreach (var path in paths.Distinct(StringComparer.OrdinalIgnoreCase))
{
if (TryFind(path, out exePath))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649</NoWarn>
<Version>3.1.0</Version>
<Version>3.1.1</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<PackageTags>Json, Testing, Verify, Snapshot, Approvals</PackageTags>
<Description>Enables simple verification of complex models and documents.</Description>
Expand Down

0 comments on commit d79c888

Please sign in to comment.