Skip to content
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
11 changes: 3 additions & 8 deletions Semantics.Paths/Implementations/AbsoluteFilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public FileName FileNameWithoutExtension
get
{
return _cachedFileNameWithoutExtension ??= FileName.Create<FileName>(
Path.GetFileNameWithoutExtension(WeakString) ?? "");
Path.GetFileName(WeakStringWithoutExtension));
}
}

Expand All @@ -65,19 +65,14 @@ public AbsoluteFilePath ChangeExtension(FileExtension newExtension)
{
Ensure.NotNull(newExtension);

string newPath = Path.ChangeExtension(WeakString, newExtension.WeakString);
return Create<AbsoluteFilePath>(newPath);
return Create<AbsoluteFilePath>(WeakStringWithExtension(newExtension));
}

/// <summary>
/// Removes the file extension from this path.
/// </summary>
/// <returns>A new <see cref="AbsoluteFilePath"/> without an extension.</returns>
public AbsoluteFilePath RemoveExtension()
{
string pathWithoutExtension = Path.ChangeExtension(WeakString, null) ?? "";
return Create<AbsoluteFilePath>(pathWithoutExtension);
}
public AbsoluteFilePath RemoveExtension() => Create<AbsoluteFilePath>(WeakStringWithoutExtension);

/// <summary>
/// Determines whether this file is inside the specified parent directory.
Expand Down
11 changes: 3 additions & 8 deletions Semantics.Paths/Implementations/RelativeFilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public FileName FileNameWithoutExtension
get
{
return _cachedFileNameWithoutExtension ??= FileName.Create<FileName>(
Path.GetFileNameWithoutExtension(WeakString) ?? "");
Path.GetFileName(WeakStringWithoutExtension));
}
}

Expand All @@ -65,19 +65,14 @@ public RelativeFilePath ChangeExtension(FileExtension newExtension)
{
Ensure.NotNull(newExtension);

string newPath = Path.ChangeExtension(WeakString, newExtension.WeakString);
return Create<RelativeFilePath>(newPath);
return Create<RelativeFilePath>(WeakStringWithExtension(newExtension));
}

/// <summary>
/// Removes the file extension from this path.
/// </summary>
/// <returns>A new <see cref="RelativeFilePath"/> without an extension.</returns>
public RelativeFilePath RemoveExtension()
{
string pathWithoutExtension = Path.ChangeExtension(WeakString, null) ?? "";
return Create<RelativeFilePath>(pathWithoutExtension);
}
public RelativeFilePath RemoveExtension() => Create<RelativeFilePath>(WeakStringWithoutExtension);

/// <summary>
/// Converts this relative file path to an absolute file path representation.
Expand Down
36 changes: 36 additions & 0 deletions Semantics.Paths/SemanticFilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,42 @@ public FileExtension FullFileExtension
/// <returns>The index just past the last directory separator, or 0 if there is none.</returns>
private static int FileNameStart(string path) => path.LastIndexOfAny(['/', '\\']) + 1;

/// <summary>
/// Gets the path with its <see cref="FileExtension"/> removed, so a dotfile such as <c>.bashrc</c>,
/// which has no extension, keeps its whole name. A trailing dot after the name (<c>file.</c>) is
/// removed too, as <see cref="Path.ChangeExtension(string, string)"/> removes it.
/// </summary>
private protected string WeakStringWithoutExtension
{
get
{
string path = WeakString;
int length = path.Length - FileExtension.WeakString.Length;
if (length == path.Length && length - 1 > FileNameStart(path) && path[length - 1] == '.')
{
length--;
}

return path.Substring(0, length);
}
}

/// <summary>
/// Gets the path with its <see cref="FileExtension"/> replaced by <paramref name="newExtension"/>, or
/// with <paramref name="newExtension"/> appended when the path has none, as a dotfile does.
/// </summary>
/// <param name="newExtension">The new extension, with or without its leading period.</param>
/// <returns>The path with the new extension.</returns>
private protected string WeakStringWithExtension(FileExtension newExtension)
{
Ensure.NotNull(newExtension);

string extension = newExtension.WeakString;
return extension.Length > 0 && extension[0] == '.'
? WeakStringWithoutExtension + extension
: $"{WeakStringWithoutExtension}.{extension}";
}

/// <summary>
/// Gets the filename portion of the path
/// </summary>
Expand Down
92 changes: 92 additions & 0 deletions Semantics.Test/Paths/DotfileExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace ktsu.Semantics.Test.Paths;

using ktsu.Semantics.Paths;
using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Issue #325: a dotfile (<c>.bashrc</c>) has no extension, so <c>RemoveExtension</c> leaves it alone
/// and <c>ChangeExtension</c> appends to it, instead of treating the whole name as an extension.
/// </summary>
[TestClass]
public sealed class DotfileExtensionTests
{
private static FileExtension Bak => FileExtension.Create<FileExtension>(".bak");

// Absolute and relative dotfiles from the issue, iterated inside each test rather than passed
// through [DataRow]: a public test method taking a string trips CA1062.
private static readonly string[] AbsoluteDotfiles =
[
TestPaths.Absolute("home", "user", ".bashrc"),
TestPaths.Absolute(".profile"),
];

private static readonly string[] RelativeDotfiles =
[
TestPaths.Relative(".gitignore"),
TestPaths.Relative("src", ".editorconfig"),
];

[TestMethod]
public void AbsoluteFilePath_Dotfile_RemoveExtension_KeepsName()
{
foreach (string path in AbsoluteDotfiles)
{
Assert.AreEqual(path, AbsoluteFilePath.Create<AbsoluteFilePath>(path).RemoveExtension().WeakString);
}
}

[TestMethod]
public void AbsoluteFilePath_Dotfile_ChangeExtension_Appends()
{
foreach (string path in AbsoluteDotfiles)
{
Assert.AreEqual(path + ".bak", AbsoluteFilePath.Create<AbsoluteFilePath>(path).ChangeExtension(Bak).WeakString);
}
}

[TestMethod]
public void RelativeFilePath_Dotfile_RemoveExtension_KeepsName()
{
foreach (string path in RelativeDotfiles)
{
Assert.AreEqual(path, RelativeFilePath.Create<RelativeFilePath>(path).RemoveExtension().WeakString);
}
}

[TestMethod]
public void RelativeFilePath_Dotfile_ChangeExtension_Appends()
{
foreach (string path in RelativeDotfiles)
{
Assert.AreEqual(path + ".bak", RelativeFilePath.Create<RelativeFilePath>(path).ChangeExtension(Bak).WeakString);
}
}

[TestMethod]
public void Dotfile_FileNameWithoutExtension_IsWholeName()
{
Assert.AreEqual(".bashrc", AbsoluteFilePath.Create<AbsoluteFilePath>(TestPaths.Absolute("home", "user", ".bashrc")).FileNameWithoutExtension.WeakString);
Assert.AreEqual(".gitignore", RelativeFilePath.Create<RelativeFilePath>(".gitignore").FileNameWithoutExtension.WeakString);
}

[TestMethod]
public void DotfileWithExtension_RemoveAndChangeExtension_TouchOnlyTheExtension()
{
RelativeFilePath path = RelativeFilePath.Create<RelativeFilePath>(TestPaths.Relative("src", ".config.json"));

Assert.AreEqual(TestPaths.Relative("src", ".config"), path.RemoveExtension().WeakString);
Assert.AreEqual(TestPaths.Relative("src", ".config.bak"), path.ChangeExtension(Bak).WeakString);
Assert.AreEqual(".config", path.FileNameWithoutExtension.WeakString);
}

[TestMethod]
public void DotInDirectoryName_IsNotAnExtension()
{
RelativeFilePath path = RelativeFilePath.Create<RelativeFilePath>(TestPaths.Relative("app.v2", "README"));

Assert.AreEqual(path.WeakString, path.RemoveExtension().WeakString);
Assert.AreEqual(TestPaths.Relative("app.v2", "README.bak"), path.ChangeExtension(Bak).WeakString);
}
}
Loading