diff --git a/TrimTail.sln b/TrimTail.sln index cda2cf6..11ba966 100644 --- a/TrimTail.sln +++ b/TrimTail.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.10.34916.146 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrimTail", "TrimTail\TrimTail.csproj", "{39D166FE-7C39-4B23-88F5-452204C811D4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrimTailTests", "TrimTailTests\TrimTailTests.csproj", "{561B2D63-F0D4-40BA-8061-A25DA82D1307}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {39D166FE-7C39-4B23-88F5-452204C811D4}.Debug|Any CPU.Build.0 = Debug|Any CPU {39D166FE-7C39-4B23-88F5-452204C811D4}.Release|Any CPU.ActiveCfg = Release|Any CPU {39D166FE-7C39-4B23-88F5-452204C811D4}.Release|Any CPU.Build.0 = Release|Any CPU + {561B2D63-F0D4-40BA-8061-A25DA82D1307}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {561B2D63-F0D4-40BA-8061-A25DA82D1307}.Debug|Any CPU.Build.0 = Debug|Any CPU + {561B2D63-F0D4-40BA-8061-A25DA82D1307}.Release|Any CPU.ActiveCfg = Release|Any CPU + {561B2D63-F0D4-40BA-8061-A25DA82D1307}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TrimTail/Program.cs b/TrimTail/Program.cs index 3d6f6a4..ecb78a9 100644 --- a/TrimTail/Program.cs +++ b/TrimTail/Program.cs @@ -1,10 +1,10 @@ namespace TrimTail { - internal class Program + public class Program { private static readonly SemaphoreSlim sem = new(1); - private static bool HasTrailingBlanks(in string file_path) + public static bool HasTrailingBlanks(in string file_path) { using var file = File.OpenText(file_path); string? line; @@ -19,7 +19,7 @@ private static bool HasTrailingBlanks(in string file_path) return false; } - private static void RemoveTrailingBlanks(in string file_path) + public static void RemoveTrailingBlanks(in string file_path) { if (!HasTrailingBlanks(file_path)) return; @@ -47,10 +47,18 @@ private static void RemoveTrailingBlanks(in string file_path) } } } - File.Replace(temp_path, file_path, null); + try + { + File.Replace(temp_path, file_path, null); + } + catch (IOException) + { + File.Copy(temp_path, file_path, true); + File.Delete(temp_path); + } } - private static void ProcessDir(string dir_path, HashSet allowed_exts) + public static void ProcessDir(string dir_path, HashSet allowed_exts) { Console.WriteLine($"Processing directory: {dir_path}"); diff --git a/TrimTailTests/TrimTailTests.cs b/TrimTailTests/TrimTailTests.cs new file mode 100644 index 0000000..36e9e51 --- /dev/null +++ b/TrimTailTests/TrimTailTests.cs @@ -0,0 +1,58 @@ +using TrimTail; + +namespace TrimTailTests +{ + [TestClass] + public class TrimTailTests + { + [TestMethod] + public void TestHasTrailingBlanks() + { + const string filePath = "test.txt"; + + File.WriteAllText(filePath, "Test w/ trailing spaces "); + Assert.IsTrue(Program.HasTrailingBlanks(filePath)); + File.Delete(filePath); + + File.WriteAllText(filePath, "Test w/ trailing tabs\t\t"); + Assert.IsTrue(Program.HasTrailingBlanks(filePath)); + File.Delete(filePath); + + File.WriteAllText(filePath, "Test w/o trailing blanks"); + Assert.IsFalse(Program.HasTrailingBlanks(filePath)); + File.Delete(filePath); + } + + [TestMethod] + public void TestRemoveTrailingBlanks() + { + const string filePath = "test.txt"; + string content; + + File.WriteAllText(filePath, "Test w/ trailing blanks "); + Program.RemoveTrailingBlanks(filePath); + content = File.ReadAllText(filePath); + Assert.AreEqual("Test w/ trailing blanks", content); + File.Delete(filePath); + + File.WriteAllText(filePath, "Test w/ trailing blanks and newline \r\n"); + Program.RemoveTrailingBlanks(filePath); + content = File.ReadAllText(filePath); + Assert.AreEqual("Test w/ trailing blanks and newline\r\n", content); + File.Delete(filePath); + } + + [TestMethod] + public void TestProcessDir() + { + const string dirPath = "testDir"; + Directory.CreateDirectory(dirPath); + File.WriteAllText(Path.Combine(dirPath, "test1.txt"), "This is a test with trailing blanks "); + File.WriteAllText(Path.Combine(dirPath, "test2.txt"), "Another test with trailing blanks "); + Program.ProcessDir(dirPath, [".txt"]); + var files = Directory.EnumerateFiles(dirPath, "*", SearchOption.AllDirectories); + Assert.IsTrue(files.All(filePath => !File.ReadAllText(filePath).EndsWith(' '))); + Directory.Delete(dirPath, true); + } + } +} \ No newline at end of file diff --git a/TrimTailTests/TrimTailTests.csproj b/TrimTailTests/TrimTailTests.csproj new file mode 100644 index 0000000..fceaf4d --- /dev/null +++ b/TrimTailTests/TrimTailTests.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + +