Skip to content

Commit 3239914

Browse files
arturcicBi0T1N
authored andcommitted
added System.IO.Abstractions package
replaced own IFileSystem with the one from the package replaced System.IO.(File|FileInfo|Directory|DirectoryInfo)
1 parent 0f833ed commit 3239914

File tree

78 files changed

+413
-561
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+413
-561
lines changed

new-cli/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
2222
<PackageVersion Include="Serilog.Sinks.Map" Version="2.0.0" />
2323
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.25153.1" />
24+
<PackageVersion Include="System.IO.Abstractions" Version="22.0.11" />
2425
<PackageVersion Include="System.Text.Json" Version="9.0.2" />
2526
</ItemGroup>
2627
</Project>

new-cli/GitVersion.Common/GitVersion.Common.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
</ItemGroup>
55
<ItemGroup>
66
<Compile Include="..\..\src\GitVersion.Core\Core\Abstractions\IEnvironment.cs" Link="Infrastructure\%(Filename)%(Extension)" />
7-
<Compile Include="..\..\src\GitVersion.Core\Core\Abstractions\IFileSystem.cs" Link="Infrastructure\%(Filename)%(Extension)" />
87
<Compile Include="..\..\src\GitVersion.Core\Core\Exceptions\WarningException.cs" Link="Exceptions\%(Filename)%(Extension)"/>
98
<Compile Include="..\..\src\GitVersion.Core\Core\RegexPatterns.cs" Link="%(Filename)%(Extension)" />
109
<Compile Include="..\..\src\GitVersion.Core\Extensions\DictionaryExtensions.cs" Link="%(Filename)%(Extension)" />

new-cli/GitVersion.Core/CoreModule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO.Abstractions;
12
using GitVersion.Infrastructure;
23

34
namespace GitVersion;

new-cli/GitVersion.Core/GitVersion.Core.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
<PackageReference Include="Serilog.Sinks.Console" />
99
<PackageReference Include="Serilog.Sinks.File" />
1010
<PackageReference Include="Serilog.Sinks.Map" />
11+
<PackageReference Include="System.IO.Abstractions" />
1112
</ItemGroup>
1213
<ItemGroup>
1314
<Compile Include="..\..\src\GitVersion.Core\Core\Environment.cs">
1415
<Link>Infrastructure\Environment.cs</Link>
1516
</Compile>
16-
<Compile Include="..\..\src\GitVersion.Core\Core\FileSystem.cs">
17-
<Link>Infrastructure\FileSystem.cs</Link>
18-
</Compile>
1917
</ItemGroup>
2018
</Project>

src/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<PackageVersion Include="NUnit3TestAdapter" Version="5.0.0" />
3838
<PackageVersion Include="Shouldly" Version="4.3.0" />
3939
<PackageVersion Include="System.Drawing.Common" Version="9.0.2" />
40+
<PackageVersion Include="System.IO.Abstractions" Version="22.0.11" />
4041
<PackageVersion Include="System.Security.Cryptography.Xml" Version="9.0.2" />
4142
<PackageVersion Include="System.Text.Json" Version="9.0.2" />
4243
<PackageVersion Include="YamlDotNet" Version="16.3.0" />

src/GitVersion.App.Tests/ArgumentParserTests.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO.Abstractions;
12
using GitVersion.Configuration;
23
using GitVersion.Core.Tests.Helpers;
34
using GitVersion.Helpers;
@@ -283,7 +284,7 @@ public void UpdateAssemblyInfoWithFilename()
283284
using var repo = new EmptyRepositoryFixture();
284285

285286
var assemblyFile = PathHelper.Combine(repo.RepositoryPath, "CommonAssemblyInfo.cs");
286-
using var file = File.Create(assemblyFile);
287+
using var file = this.fileSystem.File.Create(assemblyFile);
287288

288289
var arguments = this.argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateAssemblyInfo CommonAssemblyInfo.cs");
289290
arguments.UpdateAssemblyInfo.ShouldBe(true);
@@ -297,10 +298,10 @@ public void UpdateAssemblyInfoWithMultipleFilenames()
297298
using var repo = new EmptyRepositoryFixture();
298299

299300
var assemblyFile1 = PathHelper.Combine(repo.RepositoryPath, "CommonAssemblyInfo.cs");
300-
using var file = File.Create(assemblyFile1);
301+
using var file = this.fileSystem.File.Create(assemblyFile1);
301302

302303
var assemblyFile2 = PathHelper.Combine(repo.RepositoryPath, "VersionAssemblyInfo.cs");
303-
using var file2 = File.Create(assemblyFile2);
304+
using var file2 = this.fileSystem.File.Create(assemblyFile2);
304305

305306
var arguments = this.argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateAssemblyInfo CommonAssemblyInfo.cs VersionAssemblyInfo.cs");
306307
arguments.UpdateAssemblyInfo.ShouldBe(true);
@@ -315,10 +316,10 @@ public void UpdateProjectFilesWithMultipleFilenames()
315316
using var repo = new EmptyRepositoryFixture();
316317

317318
var assemblyFile1 = PathHelper.Combine(repo.RepositoryPath, "CommonAssemblyInfo.csproj");
318-
using var file = File.Create(assemblyFile1);
319+
using var file = this.fileSystem.File.Create(assemblyFile1);
319320

320321
var assemblyFile2 = PathHelper.Combine(repo.RepositoryPath, "VersionAssemblyInfo.csproj");
321-
using var file2 = File.Create(assemblyFile2);
322+
using var file2 = this.fileSystem.File.Create(assemblyFile2);
322323

323324
var arguments = this.argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateProjectFiles CommonAssemblyInfo.csproj VersionAssemblyInfo.csproj");
324325
arguments.UpdateProjectFiles.ShouldBe(true);
@@ -333,16 +334,16 @@ public void UpdateAssemblyInfoWithMultipleFilenamesMatchingGlobbing()
333334
using var repo = new EmptyRepositoryFixture();
334335

335336
var assemblyFile1 = PathHelper.Combine(repo.RepositoryPath, "CommonAssemblyInfo.cs");
336-
using var file = File.Create(assemblyFile1);
337+
using var file = this.fileSystem.File.Create(assemblyFile1);
337338

338339
var assemblyFile2 = PathHelper.Combine(repo.RepositoryPath, "VersionAssemblyInfo.cs");
339-
using var file2 = File.Create(assemblyFile2);
340+
using var file2 = this.fileSystem.File.Create(assemblyFile2);
340341

341342
var subdir = PathHelper.Combine(repo.RepositoryPath, "subdir");
342343

343-
this.fileSystem.CreateDirectory(subdir);
344+
this.fileSystem.Directory.CreateDirectory(subdir);
344345
var assemblyFile3 = PathHelper.Combine(subdir, "LocalAssemblyInfo.cs");
345-
using var file3 = File.Create(assemblyFile3);
346+
using var file3 = this.fileSystem.File.Create(assemblyFile3);
346347

347348
var arguments = this.argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateAssemblyInfo **/*AssemblyInfo.cs");
348349
arguments.UpdateAssemblyInfo.ShouldBe(true);
@@ -358,10 +359,10 @@ public void UpdateAssemblyInfoWithRelativeFilename()
358359
using var repo = new EmptyRepositoryFixture();
359360

360361
var assemblyFile = PathHelper.Combine(repo.RepositoryPath, "CommonAssemblyInfo.cs");
361-
using var file = File.Create(assemblyFile);
362+
using var file = this.fileSystem.File.Create(assemblyFile);
362363

363364
var targetPath = PathHelper.Combine(repo.RepositoryPath, "subdir1", "subdir2");
364-
this.fileSystem.CreateDirectory(targetPath);
365+
this.fileSystem.Directory.CreateDirectory(targetPath);
365366

366367
var arguments = this.argumentParser.ParseArguments($"-targetpath {targetPath} -updateAssemblyInfo ..\\..\\CommonAssemblyInfo.cs");
367368
arguments.UpdateAssemblyInfo.ShouldBe(true);
@@ -765,9 +766,9 @@ public void ThrowIfConfigurationFileDoesNotExist(string configFile) =>
765766
public void EnsureConfigurationFileIsSet()
766767
{
767768
var configFile = PathHelper.GetTempPath() + Guid.NewGuid() + ".yaml";
768-
File.WriteAllText(configFile, "next-version: 1.0.0");
769+
this.fileSystem.File.WriteAllText(configFile, "next-version: 1.0.0");
769770
var arguments = this.argumentParser.ParseArguments($"-config {configFile}");
770771
arguments.ConfigurationFile.ShouldBe(configFile);
771-
File.Delete(configFile);
772+
this.fileSystem.File.Delete(configFile);
772773
}
773774
}

src/GitVersion.App/ArgumentParser.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO.Abstractions;
12
using GitVersion.Agents;
23
using GitVersion.Extensions;
34
using GitVersion.Helpers;
@@ -108,13 +109,13 @@ private void ValidateConfigurationFile(Arguments arguments)
108109

109110
if (Path.IsPathRooted(arguments.ConfigurationFile))
110111
{
111-
if (!this.fileSystem.Exists(arguments.ConfigurationFile)) throw new WarningException($"Could not find config file at '{arguments.ConfigurationFile}'");
112+
if (!this.fileSystem.File.Exists(arguments.ConfigurationFile)) throw new WarningException($"Could not find config file at '{arguments.ConfigurationFile}'");
112113
arguments.ConfigurationFile = Path.GetFullPath(arguments.ConfigurationFile);
113114
}
114115
else
115116
{
116117
var configFilePath = Path.GetFullPath(PathHelper.Combine(arguments.TargetPath, arguments.ConfigurationFile));
117-
if (!this.fileSystem.Exists(configFilePath)) throw new WarningException($"Could not find config file at '{configFilePath}'");
118+
if (!this.fileSystem.File.Exists(configFilePath)) throw new WarningException($"Could not find config file at '{configFilePath}'");
118119
arguments.ConfigurationFile = configFilePath;
119120
}
120121
}
@@ -166,7 +167,7 @@ private void ParseTargetPath(Arguments arguments, string? name, IReadOnlyList<st
166167
{
167168
EnsureArgumentValueCount(values);
168169
arguments.TargetPath = value;
169-
if (string.IsNullOrWhiteSpace(value) || !this.fileSystem.DirectoryExists(value))
170+
if (string.IsNullOrWhiteSpace(value) || !this.fileSystem.Directory.Exists(value))
170171
{
171172
this.console.WriteLine($"The working directory '{value}' does not exist.");
172173
}

src/GitVersion.App/FileAppender.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
using System.IO.Abstractions;
2+
using GitVersion.Extensions;
13
using GitVersion.Helpers;
24
using GitVersion.Logging;
35

46
namespace GitVersion;
57

68
internal class FileAppender : ILogAppender
79
{
10+
private readonly IFileSystem fileSystem;
811
private readonly string filePath;
912

10-
public FileAppender(string filePath)
13+
public FileAppender(IFileSystem fileSystem, string filePath)
1114
{
15+
this.fileSystem = fileSystem.NotNull();
1216
this.filePath = filePath;
1317

14-
var logFile = new FileInfo(Path.GetFullPath(filePath));
18+
var logFile = this.fileSystem.FileInfo.New(Path.GetFullPath(filePath));
1519

1620
logFile.Directory?.Create();
1721
if (logFile.Exists) return;
@@ -31,9 +35,9 @@ public void WriteTo(LogLevel level, string message)
3135
}
3236
}
3337

34-
private static void WriteLogEntry(string logFilePath, string str)
38+
private void WriteLogEntry(string logFilePath, string str)
3539
{
3640
var contents = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}\t\t{str}{PathHelper.NewLine}";
37-
File.AppendAllText(logFilePath, contents);
41+
this.fileSystem.File.AppendAllText(logFilePath, contents);
3842
}
3943
}

src/GitVersion.App/GitVersionExecutor.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO.Abstractions;
12
using GitVersion.Configuration;
23
using GitVersion.Extensions;
34
using GitVersion.Git;
@@ -125,15 +126,15 @@ private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int e
125126
gitVersionOptions.Output.Add(OutputType.BuildServer);
126127
}
127128

128-
ConfigureLogging(gitVersionOptions, this.log);
129+
ConfigureLogging(gitVersionOptions, this.log, this.fileSystem);
129130

130131
var workingDirectory = gitVersionOptions.WorkingDirectory;
131132
if (gitVersionOptions.Diag)
132133
{
133134
GitExtensions.DumpGraphLog(logMessage => this.log.Info(logMessage));
134135
}
135136

136-
if (!this.fileSystem.DirectoryExists(workingDirectory))
137+
if (!this.fileSystem.Directory.Exists(workingDirectory))
137138
{
138139
this.log.Warning($"The working directory '{workingDirectory}' does not exist.");
139140
}
@@ -159,7 +160,7 @@ private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int e
159160
return false;
160161
}
161162

162-
private static void ConfigureLogging(GitVersionOptions gitVersionOptions, ILog log)
163+
private static void ConfigureLogging(GitVersionOptions gitVersionOptions, ILog log, IFileSystem fileSystem)
163164
{
164165
if (gitVersionOptions.Output.Contains(OutputType.BuildServer) || gitVersionOptions.LogFilePath == "console")
165166
{
@@ -168,7 +169,7 @@ private static void ConfigureLogging(GitVersionOptions gitVersionOptions, ILog l
168169

169170
if (gitVersionOptions.LogFilePath != null && gitVersionOptions.LogFilePath != "console")
170171
{
171-
log.AddLogAppender(new FileAppender(gitVersionOptions.LogFilePath));
172+
log.AddLogAppender(new FileAppender(fileSystem, gitVersionOptions.LogFilePath));
172173
}
173174
}
174175
}

src/GitVersion.BuildAgents.Tests/Agents/BitBucketPipelinesTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO.Abstractions;
12
using GitVersion.Configuration;
23
using GitVersion.Core.Tests.Helpers;
34
using GitVersion.Helpers;
@@ -10,6 +11,7 @@ namespace GitVersion.Agents.Tests;
1011
public class BitBucketPipelinesTests : TestBase
1112
{
1213
private IEnvironment environment;
14+
private IFileSystem fileSystem;
1315
private BitBucketPipelines buildServer;
1416
private IServiceProvider sp;
1517

@@ -18,6 +20,7 @@ public void SetEnvironmentVariableForTest()
1820
{
1921
this.sp = ConfigureServices(services => services.AddSingleton<BitBucketPipelines>());
2022
this.environment = this.sp.GetRequiredService<IEnvironment>();
23+
this.fileSystem = this.sp.GetRequiredService<IFileSystem>();
2124
this.buildServer = this.sp.GetRequiredService<BitBucketPipelines>();
2225

2326
this.environment.SetEnvironmentVariable(BitBucketPipelines.EnvironmentVariableName, "MyWorkspace");
@@ -128,8 +131,8 @@ public void WriteAllVariablesToTheTextWriter()
128131
}
129132
finally
130133
{
131-
File.Delete(propertyFile);
132-
File.Delete(ps1File);
134+
this.fileSystem.File.Delete(propertyFile);
135+
this.fileSystem.File.Delete(ps1File);
133136
}
134137
}
135138

@@ -156,18 +159,18 @@ private void AssertVariablesAreWrittenToFile(string propertyFile, string ps1File
156159

157160
writes[1].ShouldBe("1.2.3-beta.1+5");
158161

159-
File.Exists(propertyFile).ShouldBe(true);
162+
this.fileSystem.File.Exists(propertyFile).ShouldBe(true);
160163

161-
var props = File.ReadAllText(propertyFile);
164+
var props = this.fileSystem.File.ReadAllText(propertyFile);
162165

163166
props.ShouldContain("export GITVERSION_MAJOR=1");
164167
props.ShouldContain("export GITVERSION_MINOR=2");
165168
props.ShouldContain("export GITVERSION_SHA=f28807e615e9f06aec8a33c87780374e0c1f6fb8");
166169
props.ShouldContain("export GITVERSION_COMMITDATE=2022-04-06");
167170

168-
File.Exists(ps1File).ShouldBe(true);
171+
this.fileSystem.File.Exists(ps1File).ShouldBe(true);
169172

170-
var psProps = File.ReadAllText(ps1File);
173+
var psProps = this.fileSystem.File.ReadAllText(ps1File);
171174

172175
psProps.ShouldContain("$GITVERSION_MAJOR = \"1\"");
173176
psProps.ShouldContain("$GITVERSION_MINOR = \"2\"");

src/GitVersion.BuildAgents.Tests/Agents/BuildServerBaseTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO.Abstractions;
12
using GitVersion.Configuration;
23
using GitVersion.Core.Tests.Helpers;
34
using GitVersion.Logging;

src/GitVersion.BuildAgents.Tests/Agents/CodeBuildTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO.Abstractions;
12
using GitVersion.Configuration;
23
using GitVersion.Core.Tests.Helpers;
34
using GitVersion.Helpers;
@@ -10,6 +11,7 @@ namespace GitVersion.Agents.Tests;
1011
public sealed class CodeBuildTests : TestBase
1112
{
1213
private IEnvironment environment;
14+
private IFileSystem fileSystem;
1315
private IServiceProvider sp;
1416
private CodeBuild buildServer;
1517

@@ -18,6 +20,7 @@ public void SetUp()
1820
{
1921
this.sp = ConfigureServices(services => services.AddSingleton<CodeBuild>());
2022
this.environment = this.sp.GetRequiredService<IEnvironment>();
23+
this.fileSystem = this.sp.GetRequiredService<IFileSystem>();
2124
this.buildServer = this.sp.GetRequiredService<CodeBuild>();
2225
}
2326

@@ -62,7 +65,7 @@ public void WriteAllVariablesToTheTextWriter()
6265
}
6366
finally
6467
{
65-
File.Delete(f);
68+
this.fileSystem.File.Delete(f);
6669
}
6770
}
6871

@@ -92,9 +95,9 @@ private void AssertVariablesAreWrittenToFile(string file)
9295

9396
writes[1].ShouldBe("1.2.3-beta.1+5");
9497

95-
File.Exists(file).ShouldBe(true);
98+
this.fileSystem.File.Exists(file).ShouldBe(true);
9699

97-
var props = File.ReadAllText(file);
100+
var props = this.fileSystem.File.ReadAllText(file);
98101

99102
props.ShouldContain("GitVersion_Major=1");
100103
props.ShouldContain("GitVersion_Minor=2");

src/GitVersion.BuildAgents.Tests/Agents/EnvRunTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO.Abstractions;
12
using GitVersion.Core.Tests.Helpers;
23
using GitVersion.Helpers;
34
using Microsoft.Extensions.DependencyInjection;
@@ -10,26 +11,28 @@ public class EnvRunTests : TestBase
1011
private const string EnvVarName = "ENVRUN_DATABASE";
1112
private string mFilePath;
1213
private IEnvironment environment;
14+
private IFileSystem fileSystem;
1315
private EnvRun buildServer;
1416

1517
[SetUp]
1618
public void SetEnvironmentVariableForTest()
1719
{
1820
var sp = ConfigureServices(services => services.AddSingleton<EnvRun>());
1921
this.environment = sp.GetRequiredService<IEnvironment>();
22+
this.fileSystem = sp.GetRequiredService<IFileSystem>();
2023
this.buildServer = sp.GetRequiredService<EnvRun>();
2124

2225
// set environment variable and create an empty envrun file to indicate that EnvRun is running...
2326
this.mFilePath = PathHelper.Combine(PathHelper.GetTempPath(), "envrun.db");
2427
this.environment.SetEnvironmentVariable(EnvVarName, this.mFilePath);
25-
File.OpenWrite(this.mFilePath).Dispose();
28+
this.fileSystem.File.OpenWrite(this.mFilePath).Dispose();
2629
}
2730

2831
[TearDown]
2932
public void ClearEnvironmentVariableForTest()
3033
{
3134
this.environment.SetEnvironmentVariable(EnvVarName, null);
32-
File.Delete(this.mFilePath);
35+
this.fileSystem.File.Delete(this.mFilePath);
3336
}
3437

3538
[Test]

0 commit comments

Comments
 (0)