Browse Source

Preparing for abstractions

Piotr Czajkowski 5 months ago
parent
commit
dbd3ecfde8

+ 6 - 0
ProcessFiles/Interfaces/IFile.cs

@@ -0,0 +1,6 @@
+namespace ProcessFiles.Interfaces
+{
+    public interface IFile
+    {
+    }
+}

+ 7 - 0
ProcessFiles/Interfaces/IFileSystem.cs

@@ -0,0 +1,7 @@
+namespace ProcessFiles.Interfaces
+{
+    public interface IFileSystem
+    {
+        public IFile File { get; set; }
+    }
+}

+ 13 - 11
ProcessFiles/ProcessFiles.cs

@@ -1,15 +1,17 @@
-using ProcessFiles.Models;
+using ProcessFiles.Interfaces;
+using ProcessFiles.Models;
 using System;
 using System.Collections.Generic;
 using System.IO;
 
 namespace ProcessFiles
 {
-    public static class ProcessFiles
+    public class ProcessFiles(IFileSystem? fileSystem = null)
     {
-        private static List<string> errors = [];
+        private readonly IFileSystem? fileSystem = fileSystem;
+        private List<string> errors = [];
 
-        private static Result WhatIsIt(string path)
+        private Result WhatIsIt(string path)
         {
             try
             {
@@ -28,7 +30,7 @@ namespace ProcessFiles
             }
         }
 
-        private static string? GetExtension(string path)
+        private string? GetExtension(string path)
         {
             var extension = Path.GetExtension(path).TrimStart('.');
             if (!string.IsNullOrWhiteSpace(extension)) return extension;
@@ -48,7 +50,7 @@ namespace ProcessFiles
             return false;
         }
 
-        private static bool IsValid(string path, string[] fileExtensions)
+        private bool IsValid(string path, string[] fileExtensions)
         {
             if (!File.Exists(path))
             {
@@ -69,7 +71,7 @@ namespace ProcessFiles
             return true;
         }
 
-        private static void PerformAction(string path, Action<string> action)
+        private void PerformAction(string path, Action<string> action)
         {
             try
             {
@@ -81,7 +83,7 @@ namespace ProcessFiles
             }
         }
 
-        private static void ProcessFile(string path, string[] fileExtensions, Action<string> action)
+        private void ProcessFile(string path, string[] fileExtensions, Action<string> action)
         {
             if (!IsValid(path, fileExtensions))
                 return;
@@ -89,7 +91,7 @@ namespace ProcessFiles
             PerformAction(path, action);
         }
 
-        private static void ProcessDir(string path, string[] fileExtensions, Action<string> action, bool recursive = false)
+        private void ProcessDir(string path, string[] fileExtensions, Action<string> action, bool recursive = false)
         {
             if (!Directory.Exists(path))
             {
@@ -119,12 +121,12 @@ namespace ProcessFiles
                 ProcessFile(file, fileExtensions, action);
         }
 
-        public static IEnumerable<string> Process(IEnumerable<string> arguments, string fileExtension, Action<string> action, bool recursive = false)
+        public IEnumerable<string> Process(IEnumerable<string> arguments, string fileExtension, Action<string> action, bool recursive = false)
         {
             return Process(arguments, [fileExtension], action, recursive);
         }
 
-        public static IEnumerable<string> Process(IEnumerable<string> arguments, string[] fileExtensions, Action<string> action, bool recursive = false)
+        public IEnumerable<string> Process(IEnumerable<string> arguments, string[] fileExtensions, Action<string> action, bool recursive = false)
         {
             errors = [];
             foreach (var argument in arguments)

+ 1 - 1
ProcessFiles/ProcessFiles.csproj

@@ -3,7 +3,7 @@
 	<PropertyGroup>
 		<Nullable>enable</Nullable>
 		<TargetFramework>net8.0</TargetFramework>
-		<Version>0.2.0</Version>
+		<Version>1.0.0</Version>
 		<Title>ProcessFiles</Title>
 		<Authors>Piotr Czajkowski</Authors>
 		<Description>Easily process files from command line arguments</Description>

+ 14 - 7
ProcessFilesTests/ProcessFilesTests.cs

@@ -37,7 +37,8 @@ namespace ProcessFilesTests
                 result = Path.GetFileName(value);
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", TestAction);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process([testFolder], "txt", TestAction);
             Assert.Empty(errors);
             Assert.Equal(expectedInFolder, result);
         }
@@ -65,7 +66,8 @@ namespace ProcessFilesTests
                 result.Add(Path.GetFileName(value));
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", TestAction, true);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process([testFolder], "txt", TestAction, true);
             Assert.Empty(errors);
             Assert.True(CheckResult(result, expectedInSubFolder));
         }
@@ -79,7 +81,8 @@ namespace ProcessFilesTests
                 result.Add(Path.GetFileName(value));
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/subFolder", testFile }, "txt", TestAction);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process(["./testFiles/subFolder", testFile], "txt", TestAction);
             Assert.Empty(errors);
             Assert.True(CheckResult(result, expectedInSubFolder));
         }
@@ -93,7 +96,8 @@ namespace ProcessFilesTests
                 result = Path.GetFileName(value);
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "txt", TestAction);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process([testFile], "txt", TestAction);
             Assert.Empty(errors);
             Assert.Equal(expectedInFolder, result);
         }
@@ -107,7 +111,8 @@ namespace ProcessFilesTests
                 result = value;
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/test.txt" }, "txt", TestAction);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process(["./testFiles/test.txt"], "txt", TestAction);
             Assert.NotEmpty(errors);
             Assert.Empty(result);
         }
@@ -121,7 +126,8 @@ namespace ProcessFilesTests
                 result = value;
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "abc", TestAction);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process([testFile], "abc", TestAction);
             Assert.NotEmpty(errors);
             Assert.Empty(result);
         }
@@ -135,7 +141,8 @@ namespace ProcessFilesTests
                 result.Add(Path.GetFileName(value));
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, new[] { "txt", "json" }, TestAction, true);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process([testFolder], ["txt", "json"], TestAction, true);
             Assert.Empty(errors);
             Assert.True(CheckResult(result, expectedInSubFolderMultipleExtensions));
         }