Skip to content

Commit

Permalink
Remove OsSpecificHelper stuff and have an overload on the method in I…
Browse files Browse the repository at this point in the history
…PathIO to specify for which platform you want it
  • Loading branch information
MichielOda committed Feb 13, 2025
1 parent cb24f25 commit 5263e5c
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 117 deletions.
3 changes: 0 additions & 3 deletions FileSystem/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,5 @@ public FileSystem()

/// <summary>Performs operations on <see cref="System.String" /> instances that contain file or directory path information. These operations are performed in a cross-platform manner.</summary>
public IPathIO Path { get; }

/// <inheritdoc />
public IOsSpecificHelper OsSpecificHelper { get; } = new OsSpecificHelper();
}
}
5 changes: 0 additions & 5 deletions FileSystem/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@ public interface IFileSystem

/// <summary>Performs operations on <see cref="System.String" /> instances that contain file or directory path information. These operations are performed in a cross-platform manner.</summary>
IPathIO Path { get; }

/// <summary>
/// Represents a way to have platform-specific functionality.
/// </summary>
IOsSpecificHelper OsSpecificHelper { get; }
}
}
20 changes: 0 additions & 20 deletions FileSystem/IOsSpecificHelper.cs

This file was deleted.

13 changes: 13 additions & 0 deletions FileSystem/IPathIO.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Skyline.DataMiner.CICD.FileSystem
{
using System;
using System.Runtime.InteropServices;

/// <summary>Performs operations on <see cref="System.String" /> instances that contain file or directory path information. These operations are performed in a cross-platform manner.</summary>
public interface IPathIO
Expand Down Expand Up @@ -174,5 +175,17 @@ public interface IPathIO
/// <exception cref="ArgumentNullException"><paramref name="filename" /> or <paramref name="replacement" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">Value cannot be null or whitespace.</exception>
string ReplaceInvalidCharsForFileName(string filename, string replacement = "_");

/// <summary>
/// Replaces invalid characters in a filename with a replacement character for a specific platform.
/// <para>This way, when running e.g. on Linux, you can still replace invalid Windows file name characters.</para>
/// </summary>
/// <param name="filename">The file name with extension included</param>
/// <param name="platform">The specific platform for which you want to replace invalid characters for.</param>
/// <param name="replacement">Replacement character.</param>
/// <returns>Cleaned string without invalid characters</returns>
/// <exception cref="ArgumentNullException"><paramref name="filename" /> or <paramref name="replacement" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">Value cannot be null or whitespace.</exception>
string ReplaceInvalidCharsForFileName(string filename, OSPlatform platform, string replacement = "_");
}
}
37 changes: 0 additions & 37 deletions FileSystem/OsSpecificHelper.cs

This file was deleted.

29 changes: 24 additions & 5 deletions FileSystem/PathIOLinux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ namespace Skyline.DataMiner.CICD.FileSystem
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

/// <inheritdoc />
internal sealed class PathIOLinux : IPathIO
Expand Down Expand Up @@ -78,7 +80,7 @@ internal sealed class PathIOLinux : IPathIO
public bool IsPathRooted(string path) => SeparatorWrapper(Path.IsPathRooted, path);

/// <inheritdoc />
public string ReplaceInvalidCharsForFileName(string filename, string replacement = "_")
public string ReplaceInvalidCharsForFileName(string filename, OSPlatform platform, string replacement = "_")
{
if (filename is null)
{
Expand All @@ -95,12 +97,29 @@ public string ReplaceInvalidCharsForFileName(string filename, string replacement
throw new ArgumentException("Value cannot be null or whitespace.", nameof(filename));
}

string cleaned = String.Join(replacement, filename.Split(Path.GetInvalidFileNameChars()));
if (platform == OSPlatform.Windows)
{
// !! Can't use Path.GetInvalidFileNameChars() which is different if run on linux.
// Does mean that not all characters are covered though (unicode, line endings, ...)

// Regex to remove invalid characters for Windows filenames
string cleaned = Regex.Replace(filename, @"[<>:""/\\|?*\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F]", replacement);

// Trim trailing dots and spaces, as they are also invalid in filenames
cleaned = cleaned.TrimEnd('.', ' ');
// Trim trailing dots and spaces, as they are also invalid in filenames
cleaned = cleaned.TrimEnd('.', ' ');

return cleaned;
}
else
{
return String.Join(replacement, filename.Split(Path.GetInvalidFileNameChars()));
}
}

return cleaned;
/// <inheritdoc />
public string ReplaceInvalidCharsForFileName(string filename, string replacement = "_")
{
return ReplaceInvalidCharsForFileName(filename, OSPlatform.Linux, replacement);
}

private static string ChangeExtensionWrapper(Func<string, string, string> changeExtensionCall, string path, string extension)
Expand Down
28 changes: 23 additions & 5 deletions FileSystem/PathIOWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Skyline.DataMiner.CICD.FileSystem
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

using Alphaleonis.Win32.Filesystem;

Expand Down Expand Up @@ -77,7 +79,7 @@ internal sealed class PathIOWin : IPathIO
public bool IsPathRooted(string path) => Path.IsPathRooted(path);

/// <inheritdoc />
public string ReplaceInvalidCharsForFileName(string filename, string replacement = "_")
public string ReplaceInvalidCharsForFileName(string filename, OSPlatform platform, string replacement = "_")
{
if (filename is null)
{
Expand All @@ -94,12 +96,28 @@ public string ReplaceInvalidCharsForFileName(string filename, string replacement
throw new ArgumentException("Value cannot be null or whitespace.", nameof(filename));
}

string cleaned = String.Join(replacement, filename.Split(Path.GetInvalidFileNameChars()));
if (platform == OSPlatform.Windows)
{
string cleaned = String.Join(replacement, filename.Split(Path.GetInvalidFileNameChars()));

// Trim trailing dots and spaces, as they are also invalid in filenames
cleaned = cleaned.TrimEnd('.', ' ');

// Trim trailing dots and spaces, as they are also invalid in filenames
cleaned = cleaned.TrimEnd('.', ' ');
return cleaned;
}
else
{
// !! Can't use Path.GetInvalidFileNameChars() which is different if run on windows.

return cleaned;
// Regex to remove invalid characters for Linux filenames
return Regex.Replace(filename, "[\x00\x2F]", replacement);
}
}

/// <inheritdoc />
public string ReplaceInvalidCharsForFileName(string filename, string replacement = "_")
{
return ReplaceInvalidCharsForFileName(filename, OSPlatform.Windows, replacement);
}

private static string PathSplitCombined(params string[] paths)
Expand Down
38 changes: 0 additions & 38 deletions FileSystemTests/OsSpecificHelperTests.cs

This file was deleted.

43 changes: 39 additions & 4 deletions FileSystemTests/PathIOLinuxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,16 @@ public void IsPathRootedTest_True_WithFullPath_Windows()
Assert.IsTrue(result);
}

// Valid
[DataRow("NormalFile.ext", "NormalFile.ext")]
[DataRow("File with spaces.ext", "File with spaces.ext")]
[DataRow("File with spaces and a dot. ext", "File with spaces and a dot. ext")]
[DataRow("File with ending dot.ext.", "File with ending dot.ext")]
[DataRow("File with ending space.ext ", "File with ending space.ext")]
[DataRow("File with ending space and dot.ext. ", "File with ending space and dot.ext")]
[DataRow("File with other invalid / chars.ext", "File with other invalid _ chars.ext")] // Linux doesn't have a lot of invalid chars
[DataRow("File with ending dot.ext.", "File with ending dot.ext.")]
[DataRow("File with ending space.ext ", "File with ending space.ext ")]
[DataRow("File with ending space and dot.ext. ", "File with ending space and dot.ext. ")]
// Invalid
[DataRow("File with other invalid / chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid \x00 chars.ext", "File with other invalid _ chars.ext")]
[TestMethod]
public void ReplaceInvalidCharsForFileNameTest(string input, string expectedOutput)
{
Expand All @@ -245,5 +248,37 @@ public void ReplaceInvalidCharsForFileNameTest(string input, string expectedOutp
// Assert
result.Should().BeEquivalentTo(expectedOutput);
}

// Valid
[DataRow("NormalFile.ext", "NormalFile.ext")]
[DataRow("File with spaces.ext", "File with spaces.ext")]
[DataRow("File with spaces and a dot. ext", "File with spaces and a dot. ext")]
// Invalid
[DataRow("File with ending dot.ext.", "File with ending dot.ext")]
[DataRow("File with ending space.ext ", "File with ending space.ext")]
[DataRow("File with ending space and dot.ext. ", "File with ending space and dot.ext")]
[DataRow("File with other invalid / chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid \\ chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid : chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid | chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid ? chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid * chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid \" chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid < chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid > chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid \x00 chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid \x1F chars.ext", "File with other invalid _ chars.ext")]
[TestMethod]
public void ReplaceInvalidCharsForFileNameTest_Windows(string input, string expectedOutput)
{
// Arrange
PathIOWin path = new PathIOWin();

// Act
var result = path.ReplaceInvalidCharsForFileName(input, OSPlatform.Windows);

// Assert
result.Should().BeEquivalentTo(expectedOutput);
}
}
}
27 changes: 27 additions & 0 deletions FileSystemTests/PathIOWinTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
[TestClass]
public class PathIOWinTests
{
// Valid
[DataRow("NormalFile.ext", "NormalFile.ext")]
[DataRow("File with spaces.ext", "File with spaces.ext")]
[DataRow("File with spaces and a dot. ext", "File with spaces and a dot. ext")]
// Invalid
[DataRow("File with ending dot.ext.", "File with ending dot.ext")]
[DataRow("File with ending space.ext ", "File with ending space.ext")]
[DataRow("File with ending space and dot.ext. ", "File with ending space and dot.ext")]
Expand All @@ -24,6 +26,8 @@ public class PathIOWinTests
[DataRow("File with other invalid \" chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid < chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid > chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid \x00 chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid \x1F chars.ext", "File with other invalid _ chars.ext")]
[TestMethod]
public void ReplaceInvalidCharsForFileNameTest(string input, string expectedOutput)
{
Expand All @@ -42,5 +46,28 @@ public void ReplaceInvalidCharsForFileNameTest(string input, string expectedOutp
// Assert
result.Should().BeEquivalentTo(expectedOutput);
}

// Valid
[DataRow("NormalFile.ext", "NormalFile.ext")]
[DataRow("File with spaces.ext", "File with spaces.ext")]
[DataRow("File with spaces and a dot. ext", "File with spaces and a dot. ext")]
[DataRow("File with ending dot.ext.", "File with ending dot.ext.")]
[DataRow("File with ending space.ext ", "File with ending space.ext ")]
[DataRow("File with ending space and dot.ext. ", "File with ending space and dot.ext. ")]
// Invalid
[DataRow("File with other invalid / chars.ext", "File with other invalid _ chars.ext")]
[DataRow("File with other invalid \x00 chars.ext", "File with other invalid _ chars.ext")]
[TestMethod]
public void ReplaceInvalidCharsForFileNameTest_Linux(string input, string expectedOutput)
{
// Arrange
PathIOLinux path = new PathIOLinux();

// Act
var result = path.ReplaceInvalidCharsForFileName(input, OSPlatform.Linux);

// Assert
result.Should().BeEquivalentTo(expectedOutput);
}
}
}

0 comments on commit 5263e5c

Please sign in to comment.