Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix is text file for paths with no extension #184

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ FileExtensions.AddTextExtension(".ext1");
True(FileExtensions.IsTextExtension(".ext1"));
True(FileExtensions.IsTextFile("file.ext1"));
```
<sup><a href='/src/Tests/ExtensionsTests.cs#L50-L56' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddTextExtension' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/ExtensionsTests.cs#L53-L59' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddTextExtension' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -268,7 +268,7 @@ True(FileExtensions.IsTextExtension(".ext1"));
FileExtensions.RemoveTextExtension(".ext1");
False(FileExtensions.IsTextExtension(".ext1"));
```
<sup><a href='/src/Tests/ExtensionsTests.cs#L66-L73' title='Snippet source file'>snippet source</a> | <a href='#snippet-RemoveTextExtension' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/ExtensionsTests.cs#L69-L76' title='Snippet source file'>snippet source</a> | <a href='#snippet-RemoveTextExtension' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -286,7 +286,7 @@ public static void AddTextFileConvention() =>
// Treat files ending with .txtViaConvention as text files
FileExtensions.AddTextFileConvention(path => path.EndsWith(".txtViaConvention"));
```
<sup><a href='/src/Tests/ExtensionsTests.cs#L24-L30' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddTextFileConvention' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/ExtensionsTests.cs#L27-L33' title='Snippet source file'>snippet source</a> | <a href='#snippet-AddTextFileConvention' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Then any call to `FileExtensions.IsTextFile` will, in addition to checking the known text extensions, also check if any of the added text contentions return true.
Expand All @@ -296,7 +296,7 @@ Then any call to `FileExtensions.IsTextFile` will, in addition to checking the k
```cs
True(FileExtensions.IsTextFile("c:/path/file.txtViaConvention"));
```
<sup><a href='/src/Tests/ExtensionsTests.cs#L17-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-TextViaConvention' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/ExtensionsTests.cs#L20-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-TextViaConvention' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

An alternative approach to a text file convention would be to check if a file has a preamble that matches an known text encoding.
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;NU5119</NoWarn>
<Version>8.7.0</Version>
<Version>8.7.1</Version>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Description>A collection of minimal binary files.</Description>
Expand Down
6 changes: 6 additions & 0 deletions src/EmptyFiles/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public static bool IsTextFile(CharSpan path)
}

var extension = PathPolyfill.GetExtension(path);

if (extension.Length == 0)
{
return false;
}

return IsTextExtension(extension);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Tests/ExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public void IsText()

#endregion

False(FileExtensions.IsTextFile(".StartingWithDot"));
False(FileExtensions.IsTextFile("NoExtension"));

#region TextViaConvention

True(FileExtensions.IsTextFile("c:/path/file.txtViaConvention"));
Expand Down