Skip to content

Commit d37c201

Browse files
committed
fs: add abstractions for reading all text and enum dir
Add abstractions to the IFileSystem interface for enumerating directories and reading the entire contents of a file to a string.
1 parent 4462770 commit d37c201

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/shared/Core/FileSystem.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ public interface IFileSystem
8484
/// </returns>
8585
IEnumerable<string> EnumerateFiles(string path, string searchPattern);
8686

87+
/// <summary>
88+
/// Returns an enumerable collection of directory full names in a specified path.
89+
/// </summary>
90+
/// <param name="path">The relative or absolute path to the directory to search. This string is not case-sensitive.</param>
91+
/// <returns>
92+
/// An enumerable collection of the full names (including paths) for the directories
93+
/// in the directory specified by path.
94+
/// </returns>
95+
IEnumerable<string> EnumerateDirectories(string path);
96+
97+
/// <summary>
98+
/// Opens a text file, reads all the text in the file, and then closes the file
99+
/// </summary>
100+
/// <param name="path">The file to open for reading.</param>
101+
/// <returns>A string containing all the text in the file.</returns>
102+
string ReadAllText(string path);
103+
104+
/// <summary>
105+
/// Opens a text file, reads all lines of the file, and then closes the file.
106+
/// </summary>
107+
/// <param name="path">The file to open for reading.</param>
108+
/// <returns>A string array containing all lines of the file.</returns>
109+
string[] ReadAllLines(string path);
87110
}
88111

89112
/// <summary>
@@ -111,5 +134,11 @@ public Stream OpenFileStream(string path, FileMode fileMode, FileAccess fileAcce
111134
public void DeleteFile(string path) => File.Delete(path);
112135

113136
public IEnumerable<string> EnumerateFiles(string path, string searchPattern) => Directory.EnumerateFiles(path, searchPattern);
137+
138+
public IEnumerable<string> EnumerateDirectories(string path) => Directory.EnumerateDirectories(path);
139+
140+
public string ReadAllText(string path) => File.ReadAllText(path);
141+
142+
public string[] ReadAllLines(string path) => File.ReadAllLines(path);
114143
}
115144
}

src/shared/TestInfrastructure/Objects/TestFileSystem.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Text;
45
using System.Text.RegularExpressions;
56

67
namespace GitCredentialManager.Tests.Objects
@@ -92,6 +93,41 @@ bool IsPatternMatch(string s, string p)
9293
}
9394
}
9495

96+
IEnumerable<string> IFileSystem.EnumerateDirectories(string path)
97+
{
98+
StringComparison comparer = IsCaseSensitive
99+
? StringComparison.Ordinal
100+
: StringComparison.OrdinalIgnoreCase;
101+
102+
foreach (var dirPath in Directories)
103+
{
104+
if (dirPath.StartsWith(path, comparer))
105+
{
106+
yield return dirPath;
107+
}
108+
}
109+
}
110+
111+
string IFileSystem.ReadAllText(string path)
112+
{
113+
if (Files.TryGetValue(path, out byte[] data))
114+
{
115+
return Encoding.UTF8.GetString(data);
116+
}
117+
118+
throw new IOException("File not found");
119+
}
120+
121+
string[] IFileSystem.ReadAllLines(string path)
122+
{
123+
if (Files.TryGetValue(path, out byte[] data))
124+
{
125+
return Encoding.UTF8.GetString(data).Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
126+
}
127+
128+
throw new IOException("File not found");
129+
}
130+
95131
#endregion
96132

97133
/// <summary>

0 commit comments

Comments
 (0)