Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jan 12, 2025
2 parents 25dc2db + 098d129 commit 6bbbec3
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 34 deletions.
4 changes: 2 additions & 2 deletions docs/diff-tool.custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var resolvedTool = DiffTools.AddToolBasedOn(
Left: (temp, target) => $"\"custom args \"{target}\" \"{temp}\"",
Right: (temp, target) => $"\"custom args \"{temp}\" \"{target}\""))!;
```
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L62-L71' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddToolBasedOn' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L82-L91' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddToolBasedOn' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -69,7 +69,7 @@ var resolvedTool = DiffTools.AddToolBasedOn(

await DiffRunner.LaunchAsync(resolvedTool!, "PathToTempFile", "PathToTargetFile");
```
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L82-L93' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddToolAndLaunch' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L102-L113' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddToolAndLaunch' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion docs/diff-tool.order.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ For example `VisualStudio,Meld` will result in VisualStudio then Meld then all o
```cs
DiffTools.UseOrder(DiffTool.VisualStudio, DiffTool.AraxisMerge);
```
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L139-L143' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseOrder' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/DiffEngine.Tests/DiffToolsTest.cs#L167-L171' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseOrder' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
2 changes: 1 addition & 1 deletion src/DiffEngine.Tests/DiffEngine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
<PackageReference Include="XunitContext" />
<ProjectReference Include="..\DiffEngine\DiffEngine.csproj" />
<Compile Remove="DefinitionsTest.cs" Condition="'$(TargetFramework)' != 'net8.0'" />
<Compile Remove="DefinitionsTest.cs" Condition="'$(TargetFramework)' == 'net9.0'" />
</ItemGroup>
</Project>
77 changes: 52 additions & 25 deletions src/DiffEngine.Tests/DiffToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ public void OrderShouldNotMessWithAddTool()
Assert.Equal("MyCustomDiffTool", forExtension.Name);
}

[Fact]
public void TextConvention()
{
var diffToolPath = FakeDiffTool.Exe;
DiffTools.AddTool(
name: "MyCustomDiffTool",
autoRefresh: true,
isMdi: false,
supportsText: true,
requiresTarget: true,
launchArguments: new(
Left: (tempFile, targetFile) => $"\"{targetFile}\" \"{tempFile}\"",
Right: (tempFile, targetFile) => $"\"{tempFile}\" \"{targetFile}\""),
exePath: diffToolPath,
binaryExtensions: []);
var combine = Path.Combine(SourceDirectory, "input.temp.txtConvention");
Assert.True(DiffTools.TryFindForInputFilePath(combine, out var tool));
Assert.Equal("MyCustomDiffTool", tool.Name);
}

#if DEBUG
[Fact]
public void AddToolBasedOn()
Expand Down Expand Up @@ -99,34 +119,42 @@ public Task LaunchSpecificImageDiff() =>
DiffRunner.LaunchAsync(DiffTool.P4Merge,
Path.Combine(SourceDirectory, "input.temp.png"),
Path.Combine(SourceDirectory, "input.target.png"));
**/
//[Fact]
//public async Task LaunchImageDiff()
//{
// foreach (var tool in DiffTools.Resolved)
// {
// await DiffRunner.LaunchAsync(tool,
// Path.Combine(SourceDirectory, "input.temp.png"),
// Path.Combine(SourceDirectory, "input.target.png"));
// }
//}

//[Fact]
//public async Task LaunchTextDiff()
//{
// foreach (var tool in DiffTools.Resolved)
// {
// await DiffRunner.LaunchAsync(tool,
// Path.Combine(SourceDirectory, "input.temp.txt"),
// Path.Combine(SourceDirectory, "input.target.txt"));
// }
//}
/**
[Fact]
public async Task LaunchImageDiff()
{
foreach (var tool in DiffTools.Resolved)
{
await DiffRunner.LaunchAsync(tool,
Path.Combine(SourceDirectory, "input.temp.png"),
Path.Combine(SourceDirectory, "input.target.png"));
}
}
[Fact]
public async Task LaunchTextDiff()
{
foreach (var tool in DiffTools.Resolved)
{
await DiffRunner.LaunchAsync(tool,
Path.Combine(SourceDirectory, "input.temp.txt"),
Path.Combine(SourceDirectory, "input.target.txt"));
}
}
[Fact]
public Task LaunchSpecificTextDiff() =>
DiffRunner.LaunchAsync(DiffTool.WinMerge,
Path.Combine(SourceDirectory, "input.temp.txt"),
Path.Combine(SourceDirectory, "input.target.txt"));
[Fact]
public Task TextFileConvention()
{
var tempFile = Path.Combine(SourceDirectory, "input.temp.txtConvention");
var targetFile = Path.Combine(SourceDirectory, "input.target.txtConvention");
return DiffRunner.LaunchAsync(tempFile, targetFile);
}
**/

//todo: re enable tests with fake diff tool.
Expand Down Expand Up @@ -167,8 +195,7 @@ public void TryFindByName()
}
#endif
**/
public DiffToolsTest(ITestOutputHelper output)
:
public DiffToolsTest(ITestOutputHelper output) :
base(output) =>
DiffTools.Reset();
}
3 changes: 2 additions & 1 deletion src/DiffEngine.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
global using System.Collections.Immutable;
global using System.Management;
global using System.Management;
global using EmptyFiles;
1 change: 1 addition & 0 deletions src/DiffEngine.Tests/ModuleInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public static class ModuleInitializer
[ModuleInitializer]
public static void Initialize()
{
FileExtensions.AddTextFileConvention(_ => _.EndsWith(".txtConvention".AsSpan()));
Logging.Enable();
DiffRunner.Disabled = false;
}
Expand Down
1 change: 1 addition & 0 deletions src/DiffEngine.Tests/input.target.txtConvention
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
1 change: 1 addition & 0 deletions src/DiffEngine.Tests/input.temp.txtConvention
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
temp
5 changes: 1 addition & 4 deletions src/DiffEngine/DiffRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public static Task<LaunchResult> LaunchAsync(string tempFile, string targetFile,

return InnerLaunchAsync(
([NotNullWhen(true)] out ResolvedTool? tool) =>
{
var extension = Path.GetExtension(tempFile);
return DiffTools.TryFindByExtension(extension, out tool);
},
DiffTools.TryFindForInputFilePath(tempFile, out tool),
tempFile,
targetFile,
encoding);
Expand Down
27 changes: 27 additions & 0 deletions src/DiffEngine/DiffTools_TryFind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@ public static bool TryFindByExtension(
return ExtensionLookup.TryGetValue(extension, out tool);
}

public static bool TryFindForInputFilePath(
string path,
[NotNullWhen(true)] out ResolvedTool? tool)
{
if (FileExtensions.IsTextFile(path))
{
tool = resolved.FirstOrDefault(_ => _.SupportsText);
return tool != null;
}

return ExtensionLookup.TryGetValue(Path.GetExtension(path), out tool);
}

public static bool TryFindForInputFilePath(
CharSpan path,
[NotNullWhen(true)] out ResolvedTool? tool)
{
if (FileExtensions.IsTextFile(path))
{
tool = resolved.FirstOrDefault(_ => _.SupportsText);
return tool != null;
}

var extension = PathPolyfill.GetExtension(path).ToString();
return ExtensionLookup.TryGetValue(extension, out tool);
}

public static bool TryFindByName(
DiffTool tool,
[NotNullWhen(true)] out ResolvedTool? resolvedTool)
Expand Down

0 comments on commit 6bbbec3

Please sign in to comment.