Skip to content

Commit 098d129

Browse files
authored
Support file path for text check (#589)
1 parent 3ca2b8d commit 098d129

File tree

10 files changed

+89
-34
lines changed

10 files changed

+89
-34
lines changed

docs/diff-tool.custom.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var resolvedTool = DiffTools.AddToolBasedOn(
3939
Left: (temp, target) => $"\"custom args \"{target}\" \"{temp}\"",
4040
Right: (temp, target) => $"\"custom args \"{temp}\" \"{target}\""))!;
4141
```
42-
<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>
42+
<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>
4343
<!-- endSnippet -->
4444

4545

@@ -69,7 +69,7 @@ var resolvedTool = DiffTools.AddToolBasedOn(
6969

7070
await DiffRunner.LaunchAsync(resolvedTool!, "PathToTempFile", "PathToTargetFile");
7171
```
72-
<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>
72+
<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>
7373
<!-- endSnippet -->
7474

7575

docs/diff-tool.order.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ For example `VisualStudio,Meld` will result in VisualStudio then Meld then all o
5151
```cs
5252
DiffTools.UseOrder(DiffTool.VisualStudio, DiffTool.AraxisMerge);
5353
```
54-
<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>
54+
<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>
5555
<!-- endSnippet -->

src/DiffEngine.Tests/DiffEngine.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
1717
<PackageReference Include="XunitContext" />
1818
<ProjectReference Include="..\DiffEngine\DiffEngine.csproj" />
19-
<Compile Remove="DefinitionsTest.cs" Condition="'$(TargetFramework)' != 'net8.0'" />
19+
<Compile Remove="DefinitionsTest.cs" Condition="'$(TargetFramework)' == 'net9.0'" />
2020
</ItemGroup>
2121
</Project>

src/DiffEngine.Tests/DiffToolsTest.cs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ public void OrderShouldNotMessWithAddTool()
5555
Assert.Equal("MyCustomDiffTool", forExtension.Name);
5656
}
5757

58+
[Fact]
59+
public void TextConvention()
60+
{
61+
var diffToolPath = FakeDiffTool.Exe;
62+
DiffTools.AddTool(
63+
name: "MyCustomDiffTool",
64+
autoRefresh: true,
65+
isMdi: false,
66+
supportsText: true,
67+
requiresTarget: true,
68+
launchArguments: new(
69+
Left: (tempFile, targetFile) => $"\"{targetFile}\" \"{tempFile}\"",
70+
Right: (tempFile, targetFile) => $"\"{tempFile}\" \"{targetFile}\""),
71+
exePath: diffToolPath,
72+
binaryExtensions: []);
73+
var combine = Path.Combine(SourceDirectory, "input.temp.txtConvention");
74+
Assert.True(DiffTools.TryFindForInputFilePath(combine, out var tool));
75+
Assert.Equal("MyCustomDiffTool", tool.Name);
76+
}
77+
5878
#if DEBUG
5979
[Fact]
6080
public void AddToolBasedOn()
@@ -99,34 +119,42 @@ public Task LaunchSpecificImageDiff() =>
99119
DiffRunner.LaunchAsync(DiffTool.P4Merge,
100120
Path.Combine(SourceDirectory, "input.temp.png"),
101121
Path.Combine(SourceDirectory, "input.target.png"));
102-
**/
103-
//[Fact]
104-
//public async Task LaunchImageDiff()
105-
//{
106-
// foreach (var tool in DiffTools.Resolved)
107-
// {
108-
// await DiffRunner.LaunchAsync(tool,
109-
// Path.Combine(SourceDirectory, "input.temp.png"),
110-
// Path.Combine(SourceDirectory, "input.target.png"));
111-
// }
112-
//}
113-
114-
//[Fact]
115-
//public async Task LaunchTextDiff()
116-
//{
117-
// foreach (var tool in DiffTools.Resolved)
118-
// {
119-
// await DiffRunner.LaunchAsync(tool,
120-
// Path.Combine(SourceDirectory, "input.temp.txt"),
121-
// Path.Combine(SourceDirectory, "input.target.txt"));
122-
// }
123-
//}
124-
/**
122+
123+
[Fact]
124+
public async Task LaunchImageDiff()
125+
{
126+
foreach (var tool in DiffTools.Resolved)
127+
{
128+
await DiffRunner.LaunchAsync(tool,
129+
Path.Combine(SourceDirectory, "input.temp.png"),
130+
Path.Combine(SourceDirectory, "input.target.png"));
131+
}
132+
}
133+
134+
[Fact]
135+
public async Task LaunchTextDiff()
136+
{
137+
foreach (var tool in DiffTools.Resolved)
138+
{
139+
await DiffRunner.LaunchAsync(tool,
140+
Path.Combine(SourceDirectory, "input.temp.txt"),
141+
Path.Combine(SourceDirectory, "input.target.txt"));
142+
}
143+
}
144+
125145
[Fact]
126146
public Task LaunchSpecificTextDiff() =>
127147
DiffRunner.LaunchAsync(DiffTool.WinMerge,
128148
Path.Combine(SourceDirectory, "input.temp.txt"),
129149
Path.Combine(SourceDirectory, "input.target.txt"));
150+
151+
[Fact]
152+
public Task TextFileConvention()
153+
{
154+
var tempFile = Path.Combine(SourceDirectory, "input.temp.txtConvention");
155+
var targetFile = Path.Combine(SourceDirectory, "input.target.txtConvention");
156+
return DiffRunner.LaunchAsync(tempFile, targetFile);
157+
}
130158
**/
131159

132160
//todo: re enable tests with fake diff tool.
@@ -167,8 +195,7 @@ public void TryFindByName()
167195
}
168196
#endif
169197
**/
170-
public DiffToolsTest(ITestOutputHelper output)
171-
:
198+
public DiffToolsTest(ITestOutputHelper output) :
172199
base(output) =>
173200
DiffTools.Reset();
174201
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
global using System.Collections.Immutable;
2-
global using System.Management;
2+
global using System.Management;
3+
global using EmptyFiles;

src/DiffEngine.Tests/ModuleInitializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public static class ModuleInitializer
55
[ModuleInitializer]
66
public static void Initialize()
77
{
8+
FileExtensions.AddTextFileConvention(_ => _.EndsWith(".txtConvention".AsSpan()));
89
Logging.Enable();
910
DiffRunner.Disabled = false;
1011
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
temp

src/DiffEngine/DiffRunner.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ public static Task<LaunchResult> LaunchAsync(string tempFile, string targetFile,
5959

6060
return InnerLaunchAsync(
6161
([NotNullWhen(true)] out ResolvedTool? tool) =>
62-
{
63-
var extension = Path.GetExtension(tempFile);
64-
return DiffTools.TryFindByExtension(extension, out tool);
65-
},
62+
DiffTools.TryFindForInputFilePath(tempFile, out tool),
6663
tempFile,
6764
targetFile,
6865
encoding);

src/DiffEngine/DiffTools_TryFind.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ public static bool TryFindByExtension(
2020
return ExtensionLookup.TryGetValue(extension, out tool);
2121
}
2222

23+
public static bool TryFindForInputFilePath(
24+
string path,
25+
[NotNullWhen(true)] out ResolvedTool? tool)
26+
{
27+
if (FileExtensions.IsTextFile(path))
28+
{
29+
tool = resolved.FirstOrDefault(_ => _.SupportsText);
30+
return tool != null;
31+
}
32+
33+
return ExtensionLookup.TryGetValue(Path.GetExtension(path), out tool);
34+
}
35+
36+
public static bool TryFindForInputFilePath(
37+
CharSpan path,
38+
[NotNullWhen(true)] out ResolvedTool? tool)
39+
{
40+
if (FileExtensions.IsTextFile(path))
41+
{
42+
tool = resolved.FirstOrDefault(_ => _.SupportsText);
43+
return tool != null;
44+
}
45+
46+
var extension = PathPolyfill.GetExtension(path).ToString();
47+
return ExtensionLookup.TryGetValue(extension, out tool);
48+
}
49+
2350
public static bool TryFindByName(
2451
DiffTool tool,
2552
[NotNullWhen(true)] out ResolvedTool? resolvedTool)

0 commit comments

Comments
 (0)