Skip to content

Commit

Permalink
Add LaunchForTextAsync (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jan 14, 2025
1 parent 43ced8a commit 889caba
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
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#L167-L171' 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#L174-L178' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseOrder' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
9 changes: 8 additions & 1 deletion src/DiffEngine.Tests/DiffToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,15 @@ public Task TextFileConvention()
var targetFile = Path.Combine(SourceDirectory, "input.target.txtConvention");
return DiffRunner.LaunchAsync(tempFile, targetFile);
}
**/
[Fact]
public Task LaunchForTextAsync()
{
var tempFile = Path.Combine(SourceDirectory, "input.temp.txtConvention");
var targetFile = Path.Combine(SourceDirectory, "input.target.txtConvention");
return DiffRunner.LaunchForTextAsync(tempFile, targetFile);
}
**/
//todo: re enable tests with fake diff tool.

/**
Expand Down
29 changes: 29 additions & 0 deletions src/DiffEngine/DiffRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ public static Task<LaunchResult> LaunchAsync(string tempFile, string targetFile,
targetFile,
encoding);
}
/// <summary>
/// Launch a diff tool for the given paths.
/// </summary>
public static Task<LaunchResult> LaunchForTextAsync(string tempFile, string targetFile, Encoding? encoding = null)
{
GuardFiles(tempFile, targetFile);

return InnerLaunchAsync(
([NotNullWhen(true)] out ResolvedTool? tool) =>
DiffTools.TryFindForText(out tool),
tempFile,
targetFile,
encoding);
}

/// <summary>
/// Launch a diff tool for the given paths.
/// </summary>
public static LaunchResult LaunchForText(string tempFile, string targetFile, Encoding? encoding = null)
{
GuardFiles(tempFile, targetFile);

return InnerLaunch(
([NotNullWhen(true)] out ResolvedTool? tool) =>
DiffTools.TryFindForText(out tool),
tempFile,
targetFile,
encoding);
}

public static LaunchResult Launch(ResolvedTool tool, string tempFile, string targetFile, Encoding? encoding = null)
{
Expand Down
6 changes: 6 additions & 0 deletions src/DiffEngine/DiffTools_TryFind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public static bool TryFindByExtension(
return ExtensionLookup.TryGetValue(extension, out tool);
}

public static bool TryFindForText([NotNullWhen(true)] out ResolvedTool? tool)
{
tool = resolved.FirstOrDefault(_ => _.SupportsText);
return tool != null;
}

public static bool TryFindForInputFilePath(
string path,
[NotNullWhen(true)] out ResolvedTool? tool)
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>15.7.1</Version>
<Version>15.8.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Testing, Snapshot, Diff, Compare</PackageTags>
<Description>Launches diff tools based on file extensions. Designed to be consumed by snapshot testing libraries.</Description>
Expand Down

0 comments on commit 889caba

Please sign in to comment.