-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Adding IFileIO - Release
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
| ||
|
||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace ExtensionTests; | ||
|
||
public class FileIOTests | ||
{ | ||
IFileIO _files = new FileIO(); | ||
|
||
[Fact] | ||
public void DirectoryExists_ShouldBeTrue() | ||
{ | ||
var exists = _files.DirectoryExists("./"); | ||
exists.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void DirectoryDoesNotExists_ShouldBeFalse() | ||
{ | ||
var exists = _files.DirectoryExists("./blah/"); | ||
exists.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void GetFilesCount_NoFilter() | ||
{ | ||
var results = _files.GetFiles("./", ""); | ||
results.Count().Should().BeGreaterThan(10); | ||
} | ||
|
||
[Fact] | ||
public void GetFilesCount_FilterToOneFile() | ||
{ | ||
var results = _files.GetFiles("./", "Calebs.Extensions.dll"); | ||
results.Count().Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public void GetDirectoryName() | ||
{ | ||
var results = _files.GetDirectoryName("./Calebs.Extensions.dll"); | ||
results.Should().Be("."); | ||
} | ||
|
||
[Fact] | ||
public void ReadAllText_Reads() | ||
{ | ||
var exists = _files.FileExists("./resources/example.txt"); | ||
exists.Should().BeTrue(); | ||
|
||
var result = _files.ReadAllText("./resources/example.txt"); | ||
result.Should().StartWith("test"); | ||
} | ||
|
||
[Fact] | ||
public void FilesExists_DoesExist() | ||
{ | ||
var result = _files.FileExists("./Calebs.Extensions.dll"); | ||
result.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void FilesExists_DoesNotExist() | ||
{ | ||
var result = _files.FileExists("./blah"); | ||
result.Should().BeFalse(); | ||
} | ||
|
||
|
||
//Need WriteAllLines | ||
//Need DeleteFile | ||
|
||
[Fact] | ||
public void WriteAllLines_withDeleteFile() | ||
{ | ||
// string settingsPath = Path.Combine(_hostingEnvironment.ContentRootPath, "AppData"); | ||
var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create); | ||
folderPath.Length.Should().BeGreaterThan(1); | ||
|
||
// Source File | ||
var confirmSource = _files.FileExists("./resources/example.txt"); | ||
confirmSource.Should().BeTrue(); | ||
|
||
var source = _files.ReadAllText("./resources/example.txt"); | ||
var sourceList = new List<string>() { source }; | ||
|
||
// Target File - Confirm not already there | ||
var target = Path.Combine(folderPath, "example.txt"); | ||
var targetExist = _files.FileExists(target); | ||
|
||
if (targetExist) | ||
{ | ||
_files.DeleteFile(target); | ||
targetExist = _files.FileExists(target); | ||
} | ||
|
||
targetExist.Should().BeFalse(); | ||
|
||
// Do the thing - Write File | ||
_files.WriteAllLines(target, sourceList); | ||
|
||
// Confirm created and content matches | ||
var resultFile = _files.FileExists(target); | ||
resultFile.Should().BeTrue(); | ||
|
||
var result = _files.ReadAllText(target).Trim(); | ||
|
||
result.Should().Be(source); | ||
|
||
// Clean Up | ||
_files.DeleteFile(target); | ||
var confirmDelete = _files.FileExists(target); | ||
confirmDelete.Should().BeFalse(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,5 @@ public void ShouldCombineLists() | |
|
||
l2.Count.Should().Be(2); | ||
} | ||
|
||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
| ||
namespace ExtensionTests; | ||
|
||
using Calebs.Extensions; | ||
|
||
public class StringExtensionTests | ||
{ | ||
[Fact] | ||
public void ShouldReturnEmptyStringOnNull() | ||
{ | ||
string? value = null; | ||
Check warning on line 11 in src/ExtensionTests/StringExtensionTests.cs GitHub Actions / build
Check warning on line 11 in src/ExtensionTests/StringExtensionTests.cs GitHub Actions / build
Check warning on line 11 in src/ExtensionTests/StringExtensionTests.cs GitHub Actions / build
|
||
string v2 = value.ValueOrEmpty(); | ||
|
||
v2.Should().Be(String.Empty); | ||
} | ||
|
||
[Fact] | ||
public void ShouldReturnValueIfNotNull() | ||
{ | ||
string? value = "hello"; | ||
Check warning on line 20 in src/ExtensionTests/StringExtensionTests.cs GitHub Actions / build
Check warning on line 20 in src/ExtensionTests/StringExtensionTests.cs GitHub Actions / build
Check warning on line 20 in src/ExtensionTests/StringExtensionTests.cs GitHub Actions / build
|
||
string v2 = value.ValueOrEmpty(); | ||
|
||
v2.Should().Be(value); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
test | ||
second line | ||
third line multi words |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Calebs.Extensions.SystemIO; | ||
|
||
public class FileIO : IFileIO | ||
{ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
|
||
namespace Calebs.Extensions.SystemIO; | ||
|
||
/// <summary> | ||
/// A File IO interface and implementation of common IO uses - designed for edge testability | ||
/// </summary> | ||
public interface IFileIO | ||
{ | ||
string[] GetFiles(string path, string filter = "") => Directory.GetFiles(path, filter); | ||
bool DirectoryExists(string path) => Directory.Exists(path); | ||
string GetDirectoryName(string path) => Path.GetDirectoryName(path); | ||
string ReadAllText (string path) => File.ReadAllText(path); | ||
bool FileExists(string path) => File.Exists(path); | ||
void WriteAllLines(string path, IEnumerable<string> lines) => File.WriteAllLines(path, lines); | ||
void DeleteFile(string path) => File.Delete(path); | ||
} |