Skip to content

Commit 5147e95

Browse files
committed
Refactor file system operations to use IFileSystem interface
Replaced direct file and directory operations with IFileSystem methods. This change standardizes file system interactions and simplifies dependency injection across the codebase.
1 parent 62f782f commit 5147e95

File tree

15 files changed

+102
-83
lines changed

15 files changed

+102
-83
lines changed

src/GitVersion.App.Tests/ArgumentParserTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class ArgumentParserTests : TestBase
1212
{
1313
private IEnvironment environment;
1414
private IArgumentParser argumentParser;
15+
private IFileSystem fileSystem;
1516

1617
[SetUp]
1718
public void SetUp()
@@ -23,6 +24,7 @@ public void SetUp()
2324
});
2425
this.environment = sp.GetRequiredService<IEnvironment>();
2526
this.argumentParser = sp.GetRequiredService<IArgumentParser>();
27+
this.fileSystem = sp.GetRequiredService<IFileSystem>();
2628
}
2729

2830
[Test]
@@ -337,7 +339,8 @@ public void UpdateAssemblyInfoWithMultipleFilenamesMatchingGlobbing()
337339
using var file2 = File.Create(assemblyFile2);
338340

339341
var subdir = PathHelper.Combine(repo.RepositoryPath, "subdir");
340-
Directory.CreateDirectory(subdir);
342+
343+
this.fileSystem.CreateDirectory(subdir);
341344
var assemblyFile3 = PathHelper.Combine(subdir, "LocalAssemblyInfo.cs");
342345
using var file3 = File.Create(assemblyFile3);
343346

@@ -358,7 +361,7 @@ public void UpdateAssemblyInfoWithRelativeFilename()
358361
using var file = File.Create(assemblyFile);
359362

360363
var targetPath = PathHelper.Combine(repo.RepositoryPath, "subdir1", "subdir2");
361-
Directory.CreateDirectory(targetPath);
364+
this.fileSystem.CreateDirectory(targetPath);
362365

363366
var arguments = this.argumentParser.ParseArguments($"-targetpath {targetPath} -updateAssemblyInfo ..\\..\\CommonAssemblyInfo.cs");
364367
arguments.UpdateAssemblyInfo.ShouldBe(true);

src/GitVersion.App/ArgumentParser.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
namespace GitVersion;
88

9-
internal class ArgumentParser(IEnvironment environment, ICurrentBuildAgent buildAgent, IConsole console, IGlobbingResolver globbingResolver)
9+
internal class ArgumentParser(IEnvironment environment,
10+
IFileSystem fileSystem,
11+
ICurrentBuildAgent buildAgent,
12+
IConsole console,
13+
IGlobbingResolver globbingResolver)
1014
: IArgumentParser
1115
{
1216
private readonly IEnvironment environment = environment.NotNull();
17+
private readonly IFileSystem fileSystem = fileSystem.NotNull();
1318
private readonly ICurrentBuildAgent buildAgent = buildAgent.NotNull();
1419
private readonly IConsole console = console.NotNull();
1520
private readonly IGlobbingResolver globbingResolver = globbingResolver.NotNull();
@@ -97,19 +102,19 @@ public Arguments ParseArguments(string[] commandLineArguments)
97102
return arguments;
98103
}
99104

100-
private static void ValidateConfigurationFile(Arguments arguments)
105+
private void ValidateConfigurationFile(Arguments arguments)
101106
{
102107
if (arguments.ConfigurationFile.IsNullOrWhiteSpace()) return;
103108

104109
if (Path.IsPathRooted(arguments.ConfigurationFile))
105110
{
106-
if (!File.Exists(arguments.ConfigurationFile)) throw new WarningException($"Could not find config file at '{arguments.ConfigurationFile}'");
111+
if (!this.fileSystem.Exists(arguments.ConfigurationFile)) throw new WarningException($"Could not find config file at '{arguments.ConfigurationFile}'");
107112
arguments.ConfigurationFile = Path.GetFullPath(arguments.ConfigurationFile);
108113
}
109114
else
110115
{
111116
var configFilePath = Path.GetFullPath(PathHelper.Combine(arguments.TargetPath, arguments.ConfigurationFile));
112-
if (!File.Exists(configFilePath)) throw new WarningException($"Could not find config file at '{configFilePath}'");
117+
if (!this.fileSystem.Exists(configFilePath)) throw new WarningException($"Could not find config file at '{configFilePath}'");
113118
arguments.ConfigurationFile = configFilePath;
114119
}
115120
}
@@ -161,7 +166,7 @@ private void ParseTargetPath(Arguments arguments, string? name, IReadOnlyList<st
161166
{
162167
EnsureArgumentValueCount(values);
163168
arguments.TargetPath = value;
164-
if (!Directory.Exists(value))
169+
if (!this.fileSystem.DirectoryExists(value))
165170
{
166171
this.console.WriteLine($"The working directory '{value}' does not exist.");
167172
}

src/GitVersion.App/GitVersionExecutor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace GitVersion;
88

99
internal class GitVersionExecutor(
1010
ILog log,
11+
IFileSystem fileSystem,
1112
IConsole console,
1213
IVersionWriter versionWriter,
1314
IHelpWriter helpWriter,
@@ -21,6 +22,7 @@ internal class GitVersionExecutor(
2122
: IGitVersionExecutor
2223
{
2324
private readonly ILog log = log.NotNull();
25+
private readonly IFileSystem fileSystem = fileSystem.NotNull();
2426
private readonly IConsole console = console.NotNull();
2527
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
2628
private readonly IHelpWriter helpWriter = helpWriter.NotNull();
@@ -131,7 +133,7 @@ private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int e
131133
GitExtensions.DumpGraphLog(logMessage => this.log.Info(logMessage));
132134
}
133135

134-
if (!Directory.Exists(workingDirectory))
136+
if (!this.fileSystem.DirectoryExists(workingDirectory))
135137
{
136138
this.log.Warning($"The working directory '{workingDirectory}' does not exist.");
137139
}

src/GitVersion.Configuration/ConfigurationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public IGitVersionConfiguration Provide(IReadOnlyDictionary<object, object?>? ov
2121
{
2222
var gitVersionOptions = this.options.Value;
2323
var workingDirectory = gitVersionOptions.WorkingDirectory;
24-
var projectRootDirectory = workingDirectory.FindGitDir()?.WorkingTreeDirectory;
24+
var projectRootDirectory = this.fileSystem.FindGitDir(workingDirectory)?.WorkingTreeDirectory;
2525

2626
var configurationFile = this.configFileLocator.GetConfigurationFile(workingDirectory)
2727
?? this.configFileLocator.GetConfigurationFile(projectRootDirectory);

src/GitVersion.Core.Tests/Core/DynamicRepositoryTests.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,7 @@ public class DynamicRepositoryTests : TestBase
1111
private string? workDirectory;
1212

1313
[SetUp]
14-
public void SetUp()
15-
{
16-
// // Note: we can't use guid because paths will be too long
17-
this.workDirectory = PathHelper.Combine(Path.GetTempPath(), "GV");
18-
19-
if (!Directory.Exists(this.workDirectory))
20-
{
21-
Directory.CreateDirectory(this.workDirectory);
22-
}
23-
}
14+
public void SetUp() => this.workDirectory = PathHelper.Combine(Path.GetTempPath(), "GV");
2415

2516
[TearDown]
2617
public void TearDown()
@@ -50,16 +41,17 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran
5041
};
5142
var options = Options.Create(gitVersionOptions);
5243

53-
Directory.CreateDirectory(dynamicDirectory);
54-
Directory.CreateDirectory(workingDirectory);
55-
5644
var sp = ConfigureServices(services => services.AddSingleton(options));
5745

5846
sp.DiscoverRepository();
5947

6048
var gitPreparer = sp.GetRequiredService<IGitPreparer>();
6149
gitPreparer.Prepare();
6250

51+
var fileSystem = sp.GetRequiredService<IFileSystem>();
52+
fileSystem.CreateDirectory(dynamicDirectory);
53+
fileSystem.CreateDirectory(workingDirectory);
54+
6355
var gitVersionCalculator = sp.GetRequiredService<IGitVersionCalculateTool>();
6456

6557
var versionVariables = gitVersionCalculator.CalculateVersionVariables();

src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void CacheKeyForWorktree()
6767
{
6868
using var fixture = new EmptyRepositoryFixture();
6969
fixture.Repository.MakeACommit();
70-
var worktreePath = PathHelper.Combine(Directory.GetParent(fixture.RepositoryPath)?.FullName, Guid.NewGuid().ToString());
70+
var worktreePath = GetWorktreePath(fixture);
7171
try
7272
{
7373
// create a branch and a new worktree for it
@@ -395,7 +395,7 @@ public void GetProjectRootDirectoryWorkingDirectoryWithWorktree()
395395
using var fixture = new EmptyRepositoryFixture();
396396
fixture.Repository.MakeACommit();
397397

398-
var worktreePath = PathHelper.Combine(Directory.GetParent(fixture.RepositoryPath)?.FullName, Guid.NewGuid().ToString());
398+
var worktreePath = GetWorktreePath(fixture);
399399
try
400400
{
401401
// create a branch and a new worktree for it
@@ -451,7 +451,7 @@ public void GetDotGitDirectoryWorktree()
451451
using var fixture = new EmptyRepositoryFixture();
452452
fixture.Repository.MakeACommit();
453453

454-
var worktreePath = PathHelper.Combine(Directory.GetParent(fixture.RepositoryPath)?.FullName, Guid.NewGuid().ToString());
454+
var worktreePath = GetWorktreePath(fixture);
455455
try
456456
{
457457
// create a branch and a new worktree for it
@@ -588,6 +588,12 @@ public void CalculateVersionVariables_ShallowFetch_ThrowException()
588588
exception?.Message.ShouldBe("Repository is a shallow clone. Git repositories must contain the full history. See https://gitversion.net/docs/reference/requirements#unshallow for more info.");
589589
}
590590

591+
private static string GetWorktreePath(EmptyRepositoryFixture fixture)
592+
{
593+
var worktreePath = PathHelper.Combine(Directory.GetParent(fixture.RepositoryPath)?.FullName, Guid.NewGuid().ToString());
594+
return worktreePath;
595+
}
596+
591597
private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVersionOptions, ILog? logger = null, IGitRepository? repository = null, IFileSystem? fs = null)
592598
{
593599
this.sp = GetServiceProvider(gitVersionOptions, logger, repository, fs);

src/GitVersion.Core/Core/Abstractions/IFileSystem.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ public interface IFileSystem
99
string ReadAllText(string path);
1010
void WriteAllText(string? file, string fileContents);
1111
void WriteAllText(string? file, string fileContents, Encoding encoding);
12-
IEnumerable<string> DirectoryEnumerateFiles(string? directory, string searchPattern, SearchOption searchOption);
1312
Stream OpenWrite(string path);
1413
Stream OpenRead(string path);
1514
void CreateDirectory(string path);
1615
bool DirectoryExists(string path);
16+
string[] GetFiles(string path);
17+
string[] GetDirectories(string path);
18+
IEnumerable<string> DirectoryEnumerateFiles(string? directory, string searchPattern, SearchOption searchOption);
1719
long GetLastDirectoryWrite(string path);
1820
}

src/GitVersion.Core/Core/FileSystem.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ public void WriteAllText(string? file, string fileContents, Encoding encoding)
2929
File.WriteAllText(file, fileContents, encoding);
3030
}
3131

32-
public IEnumerable<string> DirectoryEnumerateFiles(string? directory, string searchPattern, SearchOption searchOption)
33-
{
34-
ArgumentException.ThrowIfNullOrWhiteSpace(directory);
35-
36-
return Directory.EnumerateFiles(directory, searchPattern, searchOption);
37-
}
38-
3932
public Stream OpenWrite(string path) => File.OpenWrite(path);
4033

4134
public Stream OpenRead(string path) => File.OpenRead(path);
@@ -44,6 +37,17 @@ public IEnumerable<string> DirectoryEnumerateFiles(string? directory, string sea
4437

4538
public bool DirectoryExists(string path) => Directory.Exists(path);
4639

40+
public string[] GetFiles(string path) => Directory.GetFiles(path);
41+
42+
public string[] GetDirectories(string path) => Directory.GetDirectories(path);
43+
44+
public IEnumerable<string> DirectoryEnumerateFiles(string? directory, string searchPattern, SearchOption searchOption)
45+
{
46+
ArgumentException.ThrowIfNullOrWhiteSpace(directory);
47+
48+
return Directory.EnumerateFiles(directory, searchPattern, searchOption);
49+
}
50+
4751
public long GetLastDirectoryWrite(string path) => new DirectoryInfo(path)
4852
.GetDirectories("*.*", SearchOption.AllDirectories)
4953
.Select(d => d.LastWriteTimeUtc)

src/GitVersion.Core/Core/GitPreparer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace GitVersion;
1010

1111
internal class GitPreparer(
1212
ILog log,
13+
IFileSystem fileSystem,
1314
IEnvironment environment,
1415
ICurrentBuildAgent buildAgent,
1516
IOptions<GitVersionOptions> options,
@@ -19,6 +20,7 @@ internal class GitPreparer(
1920
: IGitPreparer
2021
{
2122
private readonly ILog log = log.NotNull();
23+
private readonly IFileSystem fileSystem = fileSystem.NotNull();
2224
private readonly IEnvironment environment = environment.NotNull();
2325
private readonly IMutatingGitRepository repository = repository.NotNull();
2426
private readonly IOptions<GitVersionOptions> options = options.NotNull();
@@ -112,7 +114,7 @@ private void CreateDynamicRepository(string? targetBranch)
112114
{
113115
var gitVersionOptions = this.options.Value;
114116
var authentication = gitVersionOptions.AuthenticationInfo;
115-
if (!Directory.Exists(gitDirectory))
117+
if (!this.fileSystem.DirectoryExists(gitDirectory))
116118
{
117119
CloneRepository(gitVersionOptions.RepositoryInfo.TargetUrl, gitDirectory, authentication);
118120
}

src/GitVersion.Core/Extensions/ConfigurationExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,24 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration,
123123
return label;
124124
}
125125

126-
public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this string path)
126+
public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this IFileSystem fileSystem, string path)
127127
{
128128
string? startingDir = path;
129129
while (startingDir is not null)
130130
{
131131
var dirOrFilePath = PathHelper.Combine(startingDir, ".git");
132-
if (Directory.Exists(dirOrFilePath))
132+
if (fileSystem.DirectoryExists(dirOrFilePath))
133133
{
134134
return (dirOrFilePath, Path.GetDirectoryName(dirOrFilePath)!);
135135
}
136136

137-
if (File.Exists(dirOrFilePath))
137+
if (fileSystem.Exists(dirOrFilePath))
138138
{
139139
string? relativeGitDirPath = ReadGitDirFromFile(dirOrFilePath);
140140
if (!string.IsNullOrWhiteSpace(relativeGitDirPath))
141141
{
142142
var fullGitDirPath = Path.GetFullPath(PathHelper.Combine(startingDir, relativeGitDirPath));
143-
if (Directory.Exists(fullGitDirPath))
143+
if (fileSystem.DirectoryExists(fullGitDirPath))
144144
{
145145
return (fullGitDirPath, Path.GetDirectoryName(dirOrFilePath)!);
146146
}

0 commit comments

Comments
 (0)