Browse Source

Merge pull request #1 from pczajkowski-ptw/master

Handle more than one extension
Piotr Czajkowski 1 year ago
parent
commit
f949a9acd2

+ 1 - 1
.github/workflows/dotnetcore.yml

@@ -12,7 +12,7 @@ jobs:
     - name: Setup .NET
       uses: actions/setup-dotnet@v1
       with:
-        dotnet-version: 5.0.x
+        dotnet-version: 6.0.x
     - name: Restore dependencies
       run: dotnet restore
     - name: Build

+ 64 - 27
ProcessFiles/ProcessFiles.cs

@@ -14,13 +14,13 @@ namespace ProcessFiles
 
     public static class ProcessFiles
     {
-        private static List<string> _errors;
+        private static List<string> _errors = new();
 
-        private static Result WhatIsIt(string argument)
+        private static Result WhatIsIt(string path)
         {
             try
             {
-                var attr = File.GetAttributes(argument);
+                var attr = File.GetAttributes(path);
                 if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                     return Result.Directory;
 
@@ -33,38 +33,55 @@ namespace ProcessFiles
             }
         }
 
-        private static bool IsValid(string argument, string fileExtension)
+        private static string? GetExtension(string path)
         {
-            if (string.IsNullOrWhiteSpace(argument) || !File.Exists(argument))
+            var extension = Path.GetExtension(path)?.TrimStart('.');
+            if (string.IsNullOrWhiteSpace(extension))
             {
-                _errors.Add($"{argument} doesn't exist!");
-                return false;
+                _errors.Add($"Can't establish extension of {path}!");
+                return null;
             }
 
-            var extension = Path.GetExtension(argument);
-            if (string.IsNullOrWhiteSpace(extension))
+            return extension;
+        }
+
+        private static bool CheckExtension(string extension, string[] validExtensions)
+        {
+            foreach (var validExtension in validExtensions)
             {
-                _errors.Add($"Can't establish extension of {argument}!");
+                if (extension.Equals(validExtension, StringComparison.InvariantCultureIgnoreCase))
+                    return true;
+            }
+
+            return false;
+        }
+
+        private static bool IsValid(string path, string[] fileExtensions)
+        {
+            if (!File.Exists(path))
+            {
+                _errors.Add($"{path} doesn't exist!");
                 return false;
             }
 
-            if (!extension.TrimStart('.').Equals(fileExtension, StringComparison.InvariantCultureIgnoreCase))
+            var extension = GetExtension(path);
+            if (extension == null)
+                return false;
+
+            if (!CheckExtension(extension, fileExtensions))
             {
-                _errors.Add($"Extension of {argument} doesn't match {fileExtension}!");
+                _errors.Add($"Extension of {path} doesn't match any extension ({string.Join(", ", fileExtensions)})!");
                 return false;
             }
 
             return true;
         }
 
-        private static void ProcessFile(string path, string fileExtension, Action<string> callback)
+        private static void PerformAction(string path, Action<string> action)
         {
-            if (!IsValid(path, fileExtension))
-                return;
-
             try
             {
-                callback(path);
+                action(path);
             }
             catch (Exception e)
             {
@@ -72,7 +89,15 @@ namespace ProcessFiles
             }
         }
 
-        private static void ProcessDir(string path, string fileExtension, Action<string> callback, bool recursive = false)
+        private static void ProcessFile(string path, string[] fileExtensions, Action<string> action)
+        {
+            if (!IsValid(path, fileExtensions))
+                return;
+
+            PerformAction(path, action);
+        }
+
+        private static void ProcessDir(string path, string[] fileExtensions, Action<string> action, bool recursive = false)
         {
             if (!Directory.Exists(path))
             {
@@ -80,22 +105,34 @@ namespace ProcessFiles
                 return;
             }
 
-            var searchOption = SearchOption.TopDirectoryOnly;
-            if (recursive)
-                searchOption = SearchOption.AllDirectories;
+            var searchOption = recursive switch
+            {
+                false => SearchOption.TopDirectoryOnly,
+                true => SearchOption.AllDirectories,
+            };
+
+            List<string> files = new();
+            foreach (var extension in fileExtensions)
+            {
+                files.AddRange(Directory.GetFiles(path, $"*.{extension}", searchOption));
+            }
 
-            var files = Directory.GetFiles(path, $"*.{fileExtension}", searchOption);
             if (!files.Any())
             {
-                _errors.Add($"There are no {fileExtension} files in {path}!");
+                _errors.Add($"There are no files in {path} with given extensions ({string.Join(", ", fileExtensions)})!");
                 return;
             }
 
             foreach (var file in files)
-                ProcessFile(file, fileExtension, callback);
+                ProcessFile(file, fileExtensions, action);
+        }
+
+        public static IEnumerable<string> Process(IEnumerable<string> arguments, string fileExtension, Action<string> action, bool recursive = false)
+        {
+            return Process(arguments, new string[] {fileExtension}, action, recursive);
         }
 
-        public static IEnumerable<string> Process(IEnumerable<string> arguments, string fileExtension, Action<string> callback, bool recursive = false)
+        public static IEnumerable<string> Process(IEnumerable<string> arguments, string[] fileExtensions, Action<string> action, bool recursive = false)
         {
             _errors = new List<string>();
 
@@ -104,10 +141,10 @@ namespace ProcessFiles
                 switch (WhatIsIt(argument))
                 {
                     case Result.File:
-                        ProcessFile(argument, fileExtension, callback);
+                        ProcessFile(argument, fileExtensions, action);
                         break;
                     case Result.Directory:
-                        ProcessDir(argument, fileExtension, callback, recursive);
+                        ProcessDir(argument, fileExtensions, action, recursive);
                         break;
                     case Result.Failure:
                         continue;

+ 2 - 1
ProcessFiles/ProcessFiles.csproj

@@ -1,7 +1,8 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
-    <TargetFrameworks>net5</TargetFrameworks>
+    <TargetFrameworks>net6</TargetFrameworks>
+    <Nullable>enable</Nullable>
   </PropertyGroup>
 
 </Project>

+ 45 - 32
ProcessFilesTests/ProcessFilesTests.cs

@@ -15,23 +15,34 @@ namespace ProcessFilesTests
             "test3.txt"
         };
 
-        private readonly string exptectedInFolder = "test1.txt";
+        private readonly List<string> expectedInSubFolderMultipleExtensions = new()
+        {
+            "test.json"
+        };
+
+        private readonly string expectedInFolder = "test1.txt";
+
+        public ProcessFilesTests()
+        {
+            expectedInSubFolder.Add(expectedInFolder);
+            expectedInSubFolderMultipleExtensions.AddRange(expectedInSubFolder);
+        }
 
         [Fact]
         public void ProcessFolderTest()
         {
             var result = string.Empty;
-            void Callback(string value)
+            void TestAction(string value)
             {
                 result = Path.GetFileName(value);
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", Callback);
+            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", TestAction);
             Assert.Empty(errors);
-            Assert.Equal(exptectedInFolder, result);
+            Assert.Equal(expectedInFolder, result);
         }
 
-        private bool CheckResult(List<string> result, List<string> expected)
+        private static bool CheckResult(List<string> result, List<string> expected)
         {
             if (!result.Count.Equals(expected.Count))
                 return false;
@@ -48,83 +59,85 @@ namespace ProcessFilesTests
         [Fact]
         public void ProcessFolderRecursiveTest()
         {
-            var expected = new List<string>
-            {
-                exptectedInFolder
-            };
-            expected.AddRange(expectedInSubFolder);
-
             var result = new List<string>();
-            void Callback(string value)
+            void TestAction(string value)
             {
                 result.Add(Path.GetFileName(value));
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", Callback, true);
+            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", TestAction, true);
             Assert.Empty(errors);
-            Assert.True(CheckResult(result, expected));
+            Assert.True(CheckResult(result, expectedInSubFolder));
         }
 
         [Fact]
         public void ProcessFolderAndFileTest()
-        {
-            var expected = new List<string>
-            {
-                exptectedInFolder
-            };
-            expected.AddRange(expectedInSubFolder);
-
+        {   
             var result = new List<string>();
-            void Callback(string value)
+            void TestAction(string value)
             {
                 result.Add(Path.GetFileName(value));
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/subFolder", testFile }, "txt", Callback);
+            var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/subFolder", testFile }, "txt", TestAction);
             Assert.Empty(errors);
-            Assert.True(CheckResult(result, expected));
+            Assert.True(CheckResult(result, expectedInSubFolder));
         }
 
         [Fact]
         public void ProcessFileTest()
         {
             var result = string.Empty;
-            void Callback(string value)
+            void TestAction(string value)
             {
                 result = Path.GetFileName(value);
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "txt", Callback);
+            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "txt", TestAction);
             Assert.Empty(errors);
-            Assert.Equal(exptectedInFolder, result);
+            Assert.Equal(expectedInFolder, result);
         }
 
         [Fact]
         public void ProcessFileNotExistTest()
         {
             var result = string.Empty;
-            void Callback(string value)
+            void TestAction(string value)
             {
                 result = value;
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/test.txt" }, "txt", Callback);
+            var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/test.txt" }, "txt", TestAction);
             Assert.NotEmpty(errors);
-            Assert.NotEqual(exptectedInFolder, result);
+            Assert.Empty(result);
         }
 
         [Fact]
         public void ProcessFileNotMatchExtensionTest()
         {
             var result = string.Empty;
-            void Callback(string value)
+            void TestAction(string value)
             {
                 result = value;
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "abc", Callback);
+            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "abc", TestAction);
             Assert.NotEmpty(errors);
             Assert.Empty(result);
         }
+
+        [Fact]
+        public void ProcessFolderRecursiveMultipleExtensionsTest()
+        {
+            var result = new List<string>();
+            void TestAction(string value)
+            {
+                result.Add(Path.GetFileName(value));
+            }
+
+            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, new string[] { "txt", "json" }, TestAction, true);
+            Assert.Empty(errors);
+            Assert.True(CheckResult(result, expectedInSubFolderMultipleExtensions));
+        }
     }
 }

+ 4 - 1
ProcessFilesTests/ProcessFilesTests.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
-    <TargetFramework>net5</TargetFramework>
+    <TargetFramework>net6.0</TargetFramework>
 
     <IsPackable>false</IsPackable>
   </PropertyGroup>
@@ -32,6 +32,9 @@
   </ItemGroup>
 
   <ItemGroup>
+    <None Update="testFiles\subFolder\test.json">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </None>
     <None Update="testFiles\subFolder\test3.txt">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </None>

+ 0 - 0
ProcessFilesTests/testFiles/subFolder/test.json