Piotr Czajkowski 1 місяць тому
батько
коміт
01f3a1c0ee

+ 6 - 7
ProcessFiles/Interfaces/IDirectory.cs

@@ -1,10 +1,9 @@
 using System.IO;
 
-namespace ProcessFiles.Interfaces
+namespace ProcessFiles.Interfaces;
+
+public interface IDirectory
 {
-    public interface IDirectory
-    {
-        public bool Exists(string? path);
-        public string[] GetFiles(string path, string searchPattern, SearchOption searchOption);
-    }
-}
+    public bool Exists(string? path);
+    public string[] GetFiles(string path, string searchPattern, SearchOption searchOption);
+}

+ 6 - 7
ProcessFiles/Interfaces/IFile.cs

@@ -1,10 +1,9 @@
 using System.IO;
 
-namespace ProcessFiles.Interfaces
+namespace ProcessFiles.Interfaces;
+
+public interface IFile
 {
-    public interface IFile
-    {
-        public FileAttributes GetAttributes(string path);
-        public bool Exists(string? path);
-    }
-}
+    public FileAttributes GetAttributes(string path);
+    public bool Exists(string? path);
+}

+ 7 - 8
ProcessFiles/Interfaces/IFileSystem.cs

@@ -1,9 +1,8 @@
-namespace ProcessFiles.Interfaces
+namespace ProcessFiles.Interfaces;
+
+public interface IFileSystem
 {
-    public interface IFileSystem
-    {
-        public IFile File { get; }
-        public IDirectory Directory { get; }
-        public IPath Path { get; }
-    }
-}
+    public IFile File { get; }
+    public IDirectory Directory { get; }
+    public IPath Path { get; }
+}

+ 5 - 6
ProcessFiles/Interfaces/IPath.cs

@@ -1,7 +1,6 @@
-namespace ProcessFiles.Interfaces
+namespace ProcessFiles.Interfaces;
+
+public interface IPath
 {
-    public interface IPath
-    {
-        public string? GetExtension(string? path);
-    }
-}
+    public string? GetExtension(string? path);
+}

+ 98 - 97
ProcessFiles/ProcessFiles.cs

@@ -5,132 +5,133 @@ using System.Linq;
 using ProcessFiles.Interfaces;
 using ProcessFiles.Models;
 
-namespace ProcessFiles
+namespace ProcessFiles;
+
+public class FileProcessing(IFileSystem? fileSystem = null)
 {
-    public class FileProcessing(IFileSystem? fileSystem = null)
-    {
-        private readonly IFileSystem fileSystem = fileSystem ?? new DefaultFileSystem();
-        private List<string> errors = [];
+    private readonly IFileSystem fileSystem = fileSystem ?? new DefaultFileSystem();
+    private List<string> errors = [];
 
-        private Result WhatIsIt(string path)
+    private Result WhatIsIt(string path)
+    {
+        try
         {
-            try
-            {
-                var attr = fileSystem.File.GetAttributes(path);
-                return attr.HasFlag(FileAttributes.Directory) ? Result.Directory : Result.File;
-            }
-            catch (Exception e)
-            {
-                errors.Add($"Problem getting {path} attributes:\n" +
-                    $"{e.Message} - {e.Source} - {e.TargetSite}");
+            var attr = fileSystem.File.GetAttributes(path);
+            return attr.HasFlag(FileAttributes.Directory) ? Result.Directory : Result.File;
+        }
+        catch (Exception e)
+        {
+            errors.Add($"Problem getting {path} attributes:\n" +
+                        $"{e.Message} - {e.Source} - {e.TargetSite}");
                 
-                return Result.Failure;
-            }
+            return Result.Failure;
         }
+    }
 
-        private string? GetExtension(string path)
-        {
-            var extension = fileSystem.Path.GetExtension(path)?.TrimStart('.');
-            if (!string.IsNullOrWhiteSpace(extension)) return extension;
+    private string? GetExtension(string path)
+    {
+        var extension = fileSystem.Path.GetExtension(path)?.TrimStart('.');
+        if (!string.IsNullOrWhiteSpace(extension)) return extension;
             
-            errors.Add($"Can't establish extension of {path}!");
-            return null;
-        }
+        errors.Add($"Can't establish extension of {path}!");
+        return null;
+    }
 
-        private static bool CheckExtension(string extension, string[] validExtensions)
-            => validExtensions.Any(x => x.Equals(extension, StringComparison.InvariantCultureIgnoreCase));
-        private bool IsValid(string path, string[] fileExtensions)
+    private static bool CheckExtension(string extension, string[] validExtensions)
+        => validExtensions.Any(x => x.Equals(extension, StringComparison.InvariantCultureIgnoreCase));
+    private bool IsValid(string path, string[] fileExtensions)
+    {
+        if (!fileSystem.File.Exists(path))
         {
-            if (!fileSystem.File.Exists(path))
-            {
-                errors.Add($"{path} doesn't exist!");
-                return false;
-            }
+            errors.Add($"{path} doesn't exist!");
+            return false;
+        }
 
-            var extension = GetExtension(path);
-            if (extension == null)
-                return false;
+        var extension = GetExtension(path);
+        if (extension == null)
+            return false;
 
-            if (CheckExtension(extension, fileExtensions)) return true;
+        if (CheckExtension(extension, fileExtensions)) return true;
             
-            errors.Add($"Extension of {path} doesn't match any extension ({string.Join(", ", fileExtensions)})!");
-            return false;
-        }
+        errors.Add($"Extension of {path} doesn't match any extension ({string.Join(", ", fileExtensions)})!");
+        return false;
+    }
 
-        private void PerformAction(string path, Action<string> action)
+    private void PerformAction(string path, Action<string> action)
+    {
+        try
         {
-            try
-            {
-                action(path);
-            }
-            catch (Exception e)
-            {
-                errors.Add($"{path}:\n{e.Message} - {e.Source} - {e.TargetSite}");
-            }
+            action(path);
         }
-
-        private void ProcessFile(string path, string[] fileExtensions, Action<string> action)
+        catch (Exception e)
         {
-            if (!IsValid(path, fileExtensions))
-                return;
-
-            PerformAction(path, action);
+            errors.Add($"{path}:\n{e.Message} - {e.Source} - {e.TargetSite}");
         }
+    }
 
-        private void ProcessDir(string path, string[] fileExtensions, Action<string> action, bool recursive = false)
-        {
-            if (!fileSystem.Directory.Exists(path))
-            {
-                errors.Add($"{path} doesn't exist!");
-                return;
-            }
+    private void ProcessFile(string path, string[] fileExtensions, Action<string> action)
+    {
+        if (!IsValid(path, fileExtensions))
+            return;
 
-            var searchOption = recursive switch
-            {
-                false => SearchOption.TopDirectoryOnly,
-                true => SearchOption.AllDirectories,
-            };
+        PerformAction(path, action);
+    }
 
-            List<string> files = [];
-            foreach (var extension in fileExtensions)
-            {
-                files.AddRange(fileSystem.Directory.GetFiles(path, $"*.{extension}", searchOption));
-            }
+    private void ProcessDir(string path, string[] fileExtensions, Action<string> action, bool recursive = false)
+    {
+        if (!fileSystem.Directory.Exists(path))
+        {
+            errors.Add($"{path} doesn't exist!");
+            return;
+        }
 
-            if (files.Count == 0)
-            {
-                errors.Add($"There are no files in {path} with given extensions ({string.Join(", ", fileExtensions)})!");
-                return;
-            }
+        var searchOption = recursive switch
+        {
+            false => SearchOption.TopDirectoryOnly,
+            true => SearchOption.AllDirectories
+        };
 
-            foreach (var file in files)
-                ProcessFile(file, fileExtensions, action);
+        List<string> files = [];
+        foreach (var extension in fileExtensions)
+        {
+            files.AddRange(fileSystem.Directory.GetFiles(path, $"*.{extension}", searchOption));
         }
 
-        public IEnumerable<string> Process(IEnumerable<string> arguments, string fileExtension, Action<string> action, bool recursive = false)
+        if (files.Count == 0)
         {
-            return Process(arguments, [fileExtension], action, recursive);
+            errors.Add($"There are no files in {path} with given extensions ({string.Join(", ", fileExtensions)})!");
+            return;
         }
 
-        public IEnumerable<string> Process(IEnumerable<string> arguments, string[] fileExtensions, Action<string> action, bool recursive = false)
+        foreach (var file in files)
+            ProcessFile(file, fileExtensions, action);
+    }
+
+    public IEnumerable<string> Process(IEnumerable<string> arguments, string fileExtension, Action<string> action, bool recursive = false)
+    {
+        return Process(arguments, [fileExtension], action, recursive);
+    }
+
+    public IEnumerable<string> Process(IEnumerable<string> arguments, string[] fileExtensions, Action<string> action, bool recursive = false)
+    {
+        errors = [];
+        foreach (var argument in arguments)
         {
-            errors = [];
-            foreach (var argument in arguments)
+            switch (WhatIsIt(argument))
             {
-                switch (WhatIsIt(argument))
-                {
-                    case Result.File:
-                        ProcessFile(argument, fileExtensions, action);
-                        break;
-                    case Result.Directory:
-                        ProcessDir(argument, fileExtensions, action, recursive);
-                        break;
-                    case Result.Failure:
-                        continue;
-                }
+                case Result.File:
+                    ProcessFile(argument, fileExtensions, action);
+                    break;
+                case Result.Directory:
+                    ProcessDir(argument, fileExtensions, action, recursive);
+                    break;
+                case Result.Failure:
+                    continue;
+                default:
+                    throw new ArgumentOutOfRangeException(nameof(arguments));
             }
-
-            return errors;
         }
+
+        return errors;
     }
-}
+}

+ 121 - 109
ProcessFilesTests/ProcessFilesTests.cs

@@ -4,141 +4,153 @@ using System.Linq;
 using Xunit;
 using ProcessFiles;
 
-namespace ProcessFilesTests
+namespace ProcessFilesTests;
+
+public class ProcessFilesTests
 {
-    public class ProcessFilesTests
-    {
-        private const string TestFolder = "./testFiles";
-        private const string TestFile = "./testFiles/test1.txt";
+    private const string TestFolder = "./testFiles";
+    private const string TestFile = "./testFiles/test1.txt";
 
-        private const string ExpectedInFolder = "test1.txt";
+    private const string ExpectedInFolder = "test1.txt";
 
-        private readonly List<string> expectedInSubFolder =
-        [
-            ExpectedInFolder,
-            "test2.txt",
-            "test3.txt"
-        ];
+    private readonly List<string> expectedInSubFolder =
+    [
+        ExpectedInFolder,
+        "test2.txt",
+        "test3.txt"
+    ];
 
-        private readonly List<string> expectedInSubFolderMultipleExtensions =
-        [
-            "test.json"
-        ];
+    private readonly List<string> expectedInSubFolderMultipleExtensions =
+    [
+        "test.json"
+    ];
 
+    public ProcessFilesTests()
+    {
+        expectedInSubFolderMultipleExtensions.AddRange(expectedInSubFolder);
+    }
 
-        public ProcessFilesTests()
-        {
-            expectedInSubFolderMultipleExtensions.AddRange(expectedInSubFolder);
-        }
+    [Fact]
+    public void ProcessFolderTest()
+    {
+        var result = string.Empty;
 
-        [Fact]
-        public void ProcessFolderTest()
+        var test = new FileProcessing();
+        var errors = test.Process([TestFolder], "txt", TestAction);
+        Assert.Empty(errors);
+        Assert.Equal(ExpectedInFolder, result);
+        return;
+
+        void TestAction(string value)
         {
-            var result = string.Empty;
-            void TestAction(string value)
-            {
-                result = Path.GetFileName(value);
-            }
-
-            var test = new FileProcessing();
-            var errors = test.Process([TestFolder], "txt", TestAction);
-            Assert.Empty(errors);
-            Assert.Equal(ExpectedInFolder, result);
+            result = Path.GetFileName(value);
         }
+    }
 
-        private static bool CheckResult(List<string> result, List<string> expected)
+    private static bool CheckResult(List<string> result, List<string> expected)
+    {
+        return result.Count.Equals(expected.Count) && result.All(expected.Contains);
+    }
+
+    [Fact]
+    public void ProcessFolderRecursiveTest()
+    {
+        var result = new List<string>();
+
+        var test = new FileProcessing();
+        var errors = test.Process([TestFolder], "txt", TestAction, true);
+        Assert.Empty(errors);
+        Assert.True(CheckResult(result, expectedInSubFolder));
+        return;
+
+        void TestAction(string value)
         {
-            return result.Count.Equals(expected.Count) && result.All(expected.Contains);
+            result.Add(Path.GetFileName(value));
         }
+    }
+
+    [Fact]
+    public void ProcessFolderAndFileTest()
+    {   
+        var result = new List<string>();
 
-        [Fact]
-        public void ProcessFolderRecursiveTest()
+        var test = new FileProcessing();
+        var errors = test.Process(["./testFiles/subFolder", TestFile], "txt", TestAction);
+        Assert.Empty(errors);
+        Assert.True(CheckResult(result, expectedInSubFolder));
+        return;
+
+        void TestAction(string value)
         {
-            var result = new List<string>();
-            void TestAction(string value)
-            {
-                result.Add(Path.GetFileName(value));
-            }
-
-            var test = new FileProcessing();
-            var errors = test.Process([TestFolder], "txt", TestAction, true);
-            Assert.Empty(errors);
-            Assert.True(CheckResult(result, expectedInSubFolder));
+            result.Add(Path.GetFileName(value));
         }
+    }
 
-        [Fact]
-        public void ProcessFolderAndFileTest()
-        {   
-            var result = new List<string>();
-            void TestAction(string value)
-            {
-                result.Add(Path.GetFileName(value));
-            }
-
-            var test = new FileProcessing();
-            var errors = test.Process(["./testFiles/subFolder", TestFile], "txt", TestAction);
-            Assert.Empty(errors);
-            Assert.True(CheckResult(result, expectedInSubFolder));
-        }
+    [Fact]
+    public void ProcessFileTest()
+    {
+        var result = string.Empty;
+
+        var test = new FileProcessing();
+        var errors = test.Process([TestFile], "txt", TestAction);
+        Assert.Empty(errors);
+        Assert.Equal(ExpectedInFolder, result);
+        return;
 
-        [Fact]
-        public void ProcessFileTest()
+        void TestAction(string value)
         {
-            var result = string.Empty;
-            void TestAction(string value)
-            {
-                result = Path.GetFileName(value);
-            }
-
-            var test = new FileProcessing();
-            var errors = test.Process([TestFile], "txt", TestAction);
-            Assert.Empty(errors);
-            Assert.Equal(ExpectedInFolder, result);
+            result = Path.GetFileName(value);
         }
+    }
+
+    [Fact]
+    public void ProcessFileNotExistTest()
+    {
+        var result = string.Empty;
+
+        var test = new FileProcessing();
+        var errors = test.Process(["./testFiles/test.txt"], "txt", TestAction);
+        Assert.NotEmpty(errors);
+        Assert.Empty(result);
+        return;
 
-        [Fact]
-        public void ProcessFileNotExistTest()
+        void TestAction(string value)
         {
-            var result = string.Empty;
-            void TestAction(string value)
-            {
-                result = value;
-            }
-
-            var test = new FileProcessing();
-            var errors = test.Process(["./testFiles/test.txt"], "txt", TestAction);
-            Assert.NotEmpty(errors);
-            Assert.Empty(result);
+            result = value;
         }
+    }
+
+    [Fact]
+    public void ProcessFileNotMatchExtensionTest()
+    {
+        var result = string.Empty;
+
+        var test = new FileProcessing();
+        var errors = test.Process([TestFile], "abc", TestAction);
+        Assert.NotEmpty(errors);
+        Assert.Empty(result);
+        return;
 
-        [Fact]
-        public void ProcessFileNotMatchExtensionTest()
+        void TestAction(string value)
         {
-            var result = string.Empty;
-            void TestAction(string value)
-            {
-                result = value;
-            }
-
-            var test = new FileProcessing();
-            var errors = test.Process([TestFile], "abc", TestAction);
-            Assert.NotEmpty(errors);
-            Assert.Empty(result);
+            result = value;
         }
+    }
+
+    [Fact]
+    public void ProcessFolderRecursiveMultipleExtensionsTest()
+    {
+        var result = new List<string>();
+
+        var test = new FileProcessing();
+        var errors = test.Process([TestFolder], ["txt", "json"], TestAction, true);
+        Assert.Empty(errors);
+        Assert.True(CheckResult(result, expectedInSubFolderMultipleExtensions));
+        return;
 
-        [Fact]
-        public void ProcessFolderRecursiveMultipleExtensionsTest()
+        void TestAction(string value)
         {
-            var result = new List<string>();
-            void TestAction(string value)
-            {
-                result.Add(Path.GetFileName(value));
-            }
-
-            var test = new FileProcessing();
-            var errors = test.Process([TestFolder], ["txt", "json"], TestAction, true);
-            Assert.Empty(errors);
-            Assert.True(CheckResult(result, expectedInSubFolderMultipleExtensions));
+            result.Add(Path.GetFileName(value));
         }
     }
-}
+}

+ 83 - 76
ProcessFilesTests/ProcessFiles_UnitTests.cs

@@ -3,95 +3,102 @@ using ProcessFiles;
 using ProcessFiles.Interfaces;
 using Xunit;
 
-namespace ProcessFilesTests
+namespace ProcessFilesTests;
+
+public class ProcessFilesUnitTests
 {
-    public class ProcessFilesUnitTests
+    [Fact]
+    public void ProcessFileNotExistTest()
     {
-        [Fact]
-        public void ProcessFileNotExistTest()
-        {
-            var result = string.Empty;
-            void TestAction(string value)
-            {
-                result = value;
-            }
+        var result = string.Empty;
+
+        var fakeFileSystem = Substitute.For<IFileSystem>();
+        fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(false);
 
-            var fakeFileSystem = Substitute.For<IFileSystem>();
-            fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(false);
+        var test = new FileProcessing(fakeFileSystem);
+        var errors = test.Process(["./imaginaryFolder/imaginaryTest.txt"], "txt", TestAction);
+        Assert.NotEmpty(errors);
+        Assert.Empty(result);
 
-            var test = new FileProcessing(fakeFileSystem);
-            var errors = test.Process(["./imaginaryFolder/imaginaryTest.txt"], "txt", TestAction);
-            Assert.NotEmpty(errors);
-            Assert.Empty(result);
+        fakeFileSystem.File.Received().Exists(Arg.Any<string>());
+        return;
 
-            fakeFileSystem.File.Received().Exists(Arg.Any<string>());
+        void TestAction(string value)
+        {
+            result = value;
         }
+    }
 
-        [Fact]
-        public void ProcessFileNoExtensionTest()
+    [Fact]
+    public void ProcessFileNoExtensionTest()
+    {
+        var result = string.Empty;
+
+        var fakeFileSystem = Substitute.For<IFileSystem>();
+        fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(true);
+        fakeFileSystem.Path.GetExtension(Arg.Any<string>()).Returns(string.Empty);
+
+        var test = new FileProcessing(fakeFileSystem);
+        var errors = test.Process(["imaginaryNoExtension"], "abc", TestAction);
+        Assert.NotEmpty(errors);
+        Assert.Empty(result);
+
+        fakeFileSystem.File.Received().Exists(Arg.Any<string>());
+        fakeFileSystem.Path.Received().GetExtension(Arg.Any<string>());
+        return;
+
+        void TestAction(string value)
         {
-            var result = string.Empty;
-            void TestAction(string value)
-            {
-                result = value;
-            }
-
-            var fakeFileSystem = Substitute.For<IFileSystem>();
-            fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(true);
-            fakeFileSystem.Path.GetExtension(Arg.Any<string>()).Returns(string.Empty);
-
-            var test = new FileProcessing(fakeFileSystem);
-            var errors = test.Process(["imaginaryNoExtension"], "abc", TestAction);
-            Assert.NotEmpty(errors);
-            Assert.Empty(result);
-
-            fakeFileSystem.File.Received().Exists(Arg.Any<string>());
-            fakeFileSystem.Path.Received().GetExtension(Arg.Any<string>());
+            result = value;
         }
+    }
 
-        [Fact]
-        public void ProcessFileNotMatchExtensionTest()
+    [Fact]
+    public void ProcessFileNotMatchExtensionTest()
+    {
+        var result = string.Empty;
+
+        var fakeFileSystem = Substitute.For<IFileSystem>();
+        fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(true);
+        fakeFileSystem.Path.GetExtension(Arg.Any<string>()).Returns("def");
+
+        var test = new FileProcessing(fakeFileSystem);
+        var errors = test.Process(["imaginaryFile"], "abc", TestAction);
+        Assert.NotEmpty(errors);
+        Assert.Empty(result);
+
+        fakeFileSystem.File.Received().Exists(Arg.Any<string>());
+        fakeFileSystem.Path.Received().GetExtension(Arg.Any<string>());
+        return;
+
+        void TestAction(string value)
         {
-            var result = string.Empty;
-            void TestAction(string value)
-            {
-                result = value;
-            }
-
-            var fakeFileSystem = Substitute.For<IFileSystem>();
-            fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(true);
-            fakeFileSystem.Path.GetExtension(Arg.Any<string>()).Returns("def");
-
-            var test = new FileProcessing(fakeFileSystem);
-            var errors = test.Process(["imaginaryFile"], "abc", TestAction);
-            Assert.NotEmpty(errors);
-            Assert.Empty(result);
-
-            fakeFileSystem.File.Received().Exists(Arg.Any<string>());
-            fakeFileSystem.Path.Received().GetExtension(Arg.Any<string>());
+            result = value;
         }
+    }
+
+    [Fact]
+    public void ProcessFileTest()
+    {
+        var result = string.Empty;
+
+        var fakeFileSystem = Substitute.For<IFileSystem>();
+        fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(true);
+        fakeFileSystem.Path.GetExtension(Arg.Any<string>()).Returns("txt");
+
+        const string expectedValue = "imaginary.txt";
+        var test = new FileProcessing(fakeFileSystem);
+        var errors = test.Process([expectedValue], "txt", TestAction);
+        Assert.Empty(errors);
+        Assert.Equal(expectedValue, result);
+
+        fakeFileSystem.File.Received().Exists(Arg.Any<string>());
+        fakeFileSystem.Path.Received().GetExtension(Arg.Any<string>());
+        return;
 
-        [Fact]
-        public void ProcessFileTest()
+        void TestAction(string value)
         {
-            var result = string.Empty;
-            void TestAction(string value)
-            {
-                result = value;
-            }
-
-            var fakeFileSystem = Substitute.For<IFileSystem>();
-            fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(true);
-            fakeFileSystem.Path.GetExtension(Arg.Any<string>()).Returns("txt");
-
-            const string expectedValue = "imaginary.txt";
-            var test = new FileProcessing(fakeFileSystem);
-            var errors = test.Process([expectedValue], "txt", TestAction);
-            Assert.Empty(errors);
-            Assert.Equal(expectedValue, result);
-
-            fakeFileSystem.File.Received().Exists(Arg.Any<string>());
-            fakeFileSystem.Path.Received().GetExtension(Arg.Any<string>());
+            result = value;
         }
     }
-}
+}