Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/System.IO.Abstractions.Extensions/IFileInfoExtensions.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,6 +165,12 @@ public static void WriteAllText(this IFileInfo file, string contents)
file.FileSystem.File.WriteAllText(file.FullName, contents);
}

/// <inheritdoc cref="IPath.GetFileNameWithoutExtension(string?)"/>
public static string GetFileNameWithoutExtension(this IFileInfo file)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
publicstaticstringGetFileNameWithoutExtension(thisIFileInfofile)
publicstaticstringNameWithoutExtension(thisIFileInfofile)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with GetFileNameWithoutExtension, as it starts with a verb. However, if we used C# 14, we could add a nice extension property with the name NameWithoutExtension.

{
return file.FileSystem.Path.GetFileNameWithoutExtension(file.Name);
}

private static FileMode GetWriteFileMode(IFileInfo info, bool overwrite)
{
if (!overwrite && info.Exists)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -290,5 +290,23 @@ public void AppendText_FileExistsAndHasText_LinesAreAppended(params string[] app
Assert.AreEqual(expected[i], actual[i]);
}
}

[TestCase("test.extension", "test")]
[TestCase("noextension", "noextension")]
[TestCase(".extensiononly", "")]
Comment thread
Blaisor marked this conversation as resolved.
[TestCase("double.dot.ext", "double.dot")]
public void GetFileNameWithoutExtension_WithValidFileName_ReturnsNameWithoutExtension(string fileName, string expectedName)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
publicvoidGetFileNameWithoutExtension_WithValidFileName_ReturnsNameWithoutExtension(stringfileName,stringexpectedName)
publicvoidNameWithoutExtension_WithValidFileName_ReturnsNameWithoutExtension(stringfileName,stringexpectedName)

{
//arrange
var fs = new FileSystem();
var current = fs.DirectoryInfo.New(fs.Directory.GetCurrentDirectory());
var file = current.File(fileName);

//act
var name = file.GetFileNameWithoutExtension();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
varname=file.GetFileNameWithoutExtension();
varname=file.NameWithoutExtension();


//assert
Assert.AreEqual(expectedName, name);
}
}
}