Skip to content

Commit 1aefeed

Browse files
authored
Merge pull request #4340 from arturcic/feature/filesystem
Improve IFileSystem usage in the app (part 1)
2 parents e4c3745 + e2734f8 commit 1aefeed

File tree

27 files changed

+163
-268
lines changed

27 files changed

+163
-268
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.Tests/GitVersion.App.Tests.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
<Compile Include="..\GitVersion.Core.Tests\Helpers\ExecutableHelper.cs" Link="Helpers\ExecutableHelper.cs" />
2020
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestConsoleAdapter.cs" Link="Helpers\TestConsoleAdapter.cs" />
2121
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestEnvironment.cs" Link="Helpers\TestEnvironment.cs" />
22-
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestFileSystem.cs" Link="Helpers\TestFileSystem.cs" />
2322
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestLogAppender.cs" Link="Helpers\TestLogAppender.cs" />
24-
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestStream.cs" Link="Helpers\TestStream.cs" />
2523
<Compile Include="..\GitVersion.Core.Tests\Helpers\TestBase.cs" Link="Helpers\TestBase.cs" />
2624
<Compile Include="..\GitVersion.Core.Tests\Helpers\GitVersionCoreTestModule.cs" Link="Helpers\GitVersionCoreTestModule.cs" />
2725
<Compile Include="..\GitVersion.Core.Tests\Extensions\GitVersionVariablesExtensions.cs" Link="Extensions\GitVersionVariablesExtensions.cs" />

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 (string.IsNullOrWhiteSpace(value) || !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.Tests/Configuration/ConfigurationProviderTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ public void NoWarnOnGitVersionYmlFile()
285285

286286
this.configurationProvider.ProvideForDirectory(this.repoPath);
287287

288-
stringLogger.Length.ShouldBe(0);
288+
var filePath = PathHelper.Combine(this.repoPath, ConfigurationFileLocator.DefaultFileName);
289+
stringLogger.ShouldContain($"Found configuration file at '{filePath}'");
289290
}
290291

291292
[Test]

src/GitVersion.Configuration.Tests/Configuration/Extensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public static IDisposable<string> SetupConfigFile(this IFileSystem fileSystem, s
1313
}
1414

1515
var fullPath = PathHelper.Combine(path, fileName);
16+
var directory = PathHelper.GetDirectoryName(fullPath);
17+
if (!fileSystem.DirectoryExists(directory))
18+
{
19+
fileSystem.CreateDirectory(directory);
20+
}
21+
1622
fileSystem.WriteAllText(fullPath, text);
1723

1824
return Disposable.Create(fullPath, () => fileSystem.Delete(fullPath));

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: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,18 @@ public class DynamicRepositoryTests : TestBase
1010
{
1111
private string? workDirectory;
1212

13-
private static void ClearReadOnly(DirectoryInfo parentDirectory)
14-
{
15-
parentDirectory.Attributes = FileAttributes.Normal;
16-
foreach (var fi in parentDirectory.GetFiles())
17-
{
18-
fi.Attributes = FileAttributes.Normal;
19-
}
20-
foreach (var di in parentDirectory.GetDirectories())
21-
{
22-
ClearReadOnly(di);
23-
}
24-
}
13+
[SetUp]
14+
public void SetUp() => this.workDirectory = PathHelper.Combine(Path.GetTempPath(), "GV");
2515

26-
[OneTimeSetUp]
27-
public void CreateTemporaryRepository()
28-
{
29-
// Note: we can't use guid because paths will be too long
30-
this.workDirectory = PathHelper.Combine(Path.GetTempPath(), "GV");
31-
32-
// Clean directory upfront, some build agents are having troubles
33-
if (Directory.Exists(this.workDirectory))
34-
{
35-
var di = new DirectoryInfo(this.workDirectory);
36-
ClearReadOnly(di);
37-
38-
Directory.Delete(this.workDirectory, true);
39-
}
40-
41-
Directory.CreateDirectory(this.workDirectory);
42-
}
43-
44-
[OneTimeTearDown]
45-
public void Cleanup()
16+
[TearDown]
17+
public void TearDown()
4618
{
4719
}
4820

4921
// Note: use same name twice to see if changing commits works on same (cached) repository
5022
[NonParallelizable]
51-
[TestCase("GV_main", "https://github.com/GitTools/GitVersion", MainBranch, "efddf2f92c539a9c27f1904d952dcab8fb955f0e", "5.8.2-56")]
5223
[TestCase("GV_main", "https://github.com/GitTools/GitVersion", MainBranch, "2dc142a4a4df77db61a00d9fb7510b18b3c2c85a", "5.8.2-47")]
24+
[TestCase("GV_main", "https://github.com/GitTools/GitVersion", MainBranch, "efddf2f92c539a9c27f1904d952dcab8fb955f0e", "5.8.2-56")]
5325
public void FindsVersionInDynamicRepo(string name, string url, string targetBranch, string commitId, string expectedFullSemVer)
5426
{
5527
var root = PathHelper.Combine(this.workDirectory, name);
@@ -64,21 +36,22 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran
6436
TargetBranch = targetBranch,
6537
CommitId = commitId
6638
},
67-
Settings = { NoFetch = false },
39+
Settings = { NoFetch = false, NoCache = true },
6840
WorkingDirectory = workingDirectory
6941
};
7042
var options = Options.Create(gitVersionOptions);
7143

72-
Directory.CreateDirectory(dynamicDirectory);
73-
Directory.CreateDirectory(workingDirectory);
74-
7544
var sp = ConfigureServices(services => services.AddSingleton(options));
7645

7746
sp.DiscoverRepository();
7847

7948
var gitPreparer = sp.GetRequiredService<IGitPreparer>();
8049
gitPreparer.Prepare();
8150

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

8457
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.Tests/Helpers/GitVersionCoreTestModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void RegisterTypes(IServiceCollection services)
1717
services.AddModule(new GitVersionConfigurationModule());
1818
services.AddModule(new GitVersionCoreModule());
1919

20-
services.AddSingleton<IFileSystem, TestFileSystem>();
20+
services.AddSingleton<IFileSystem, FileSystem>();
2121
services.AddSingleton<IEnvironment, TestEnvironment>();
2222
services.AddSingleton<ILog, NullLog>();
2323
}

0 commit comments

Comments
 (0)