Skip to content

Commit fc30dbb

Browse files
arturcicBi0T1N
authored andcommitted
replace Path.* usage with PathHelper.*
1 parent 9057641 commit fc30dbb

File tree

26 files changed

+81
-60
lines changed

26 files changed

+81
-60
lines changed

src/GitVersion.App.Tests/ArgumentParserTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void UpdateAssemblyInfoWithFilename()
289289
var arguments = this.argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateAssemblyInfo CommonAssemblyInfo.cs");
290290
arguments.UpdateAssemblyInfo.ShouldBe(true);
291291
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(1);
292-
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
292+
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => PathHelper.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
293293
}
294294

295295
[Test]
@@ -306,8 +306,8 @@ public void UpdateAssemblyInfoWithMultipleFilenames()
306306
var arguments = this.argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateAssemblyInfo CommonAssemblyInfo.cs VersionAssemblyInfo.cs");
307307
arguments.UpdateAssemblyInfo.ShouldBe(true);
308308
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(2);
309-
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
310-
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("VersionAssemblyInfo.cs"));
309+
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => PathHelper.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
310+
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => PathHelper.GetFileName(x).Equals("VersionAssemblyInfo.cs"));
311311
}
312312

313313
[Test]
@@ -324,8 +324,8 @@ public void UpdateProjectFilesWithMultipleFilenames()
324324
var arguments = this.argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateProjectFiles CommonAssemblyInfo.csproj VersionAssemblyInfo.csproj");
325325
arguments.UpdateProjectFiles.ShouldBe(true);
326326
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(2);
327-
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("CommonAssemblyInfo.csproj"));
328-
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("VersionAssemblyInfo.csproj"));
327+
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => PathHelper.GetFileName(x).Equals("CommonAssemblyInfo.csproj"));
328+
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => PathHelper.GetFileName(x).Equals("VersionAssemblyInfo.csproj"));
329329
}
330330

331331
[Test]
@@ -348,9 +348,9 @@ public void UpdateAssemblyInfoWithMultipleFilenamesMatchingGlobbing()
348348
var arguments = this.argumentParser.ParseArguments($"-targetpath {repo.RepositoryPath} -updateAssemblyInfo **/*AssemblyInfo.cs");
349349
arguments.UpdateAssemblyInfo.ShouldBe(true);
350350
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(3);
351-
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
352-
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("VersionAssemblyInfo.cs"));
353-
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("LocalAssemblyInfo.cs"));
351+
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => PathHelper.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
352+
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => PathHelper.GetFileName(x).Equals("VersionAssemblyInfo.cs"));
353+
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => PathHelper.GetFileName(x).Equals("LocalAssemblyInfo.cs"));
354354
}
355355

356356
[Test]
@@ -367,7 +367,7 @@ public void UpdateAssemblyInfoWithRelativeFilename()
367367
var arguments = this.argumentParser.ParseArguments($"-targetpath {targetPath} -updateAssemblyInfo ..\\..\\CommonAssemblyInfo.cs");
368368
arguments.UpdateAssemblyInfo.ShouldBe(true);
369369
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(1);
370-
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => Path.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
370+
arguments.UpdateAssemblyInfoFileName.ShouldContain(x => PathHelper.GetFileName(x).Equals("CommonAssemblyInfo.cs"));
371371
}
372372

373373
[Test]

src/GitVersion.App/ArgumentParser.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ private void ValidateConfigurationFile(Arguments arguments)
107107
{
108108
if (arguments.ConfigurationFile.IsNullOrWhiteSpace()) return;
109109

110-
if (Path.IsPathRooted(arguments.ConfigurationFile))
110+
if (PathHelper.IsPathRooted(arguments.ConfigurationFile))
111111
{
112112
if (!this.fileSystem.File.Exists(arguments.ConfigurationFile)) throw new WarningException($"Could not find config file at '{arguments.ConfigurationFile}'");
113-
arguments.ConfigurationFile = Path.GetFullPath(arguments.ConfigurationFile);
113+
arguments.ConfigurationFile = PathHelper.GetFullPath(arguments.ConfigurationFile);
114114
}
115115
else
116116
{
117-
var configFilePath = Path.GetFullPath(PathHelper.Combine(arguments.TargetPath, arguments.ConfigurationFile));
117+
var configFilePath = PathHelper.GetFullPath(PathHelper.Combine(arguments.TargetPath, arguments.ConfigurationFile));
118118
if (!this.fileSystem.File.Exists(configFilePath)) throw new WarningException($"Could not find config file at '{configFilePath}'");
119119
arguments.ConfigurationFile = configFilePath;
120120
}
@@ -156,7 +156,7 @@ private IEnumerable<string> ResolveFiles(string workingDirectory, ISet<string>?
156156

157157
foreach (var path in paths)
158158
{
159-
yield return Path.GetFullPath(PathHelper.Combine(workingDirectory, path));
159+
yield return PathHelper.GetFullPath(PathHelper.Combine(workingDirectory, path));
160160
}
161161
}
162162
}
@@ -182,7 +182,7 @@ private void ParseTargetPath(Arguments arguments, string? name, IReadOnlyList<st
182182
{
183183
if (name?.StartsWith('/') == true)
184184
{
185-
if (Path.DirectorySeparatorChar == '/' && name.IsValidPath())
185+
if (PathHelper.DirectorySeparatorChar == '/' && name.IsValidPath())
186186
{
187187
arguments.TargetPath = name;
188188
return;

src/GitVersion.App/ArgumentParserExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static bool ArgumentRequiresValue(this string argument, int argumentIndex
7474
var argumentMightRequireValue = !booleanArguments.Contains(argument[1..], StringComparer.OrdinalIgnoreCase);
7575

7676
// If this is the first argument that might be a target path, the argument starts with slash, and we're on an OS that supports paths with slashes, the argument does not require a value.
77-
if (argumentMightRequireValue && argumentIndex == 0 && argument.StartsWith('/') && Path.DirectorySeparatorChar == '/' && argument.IsValidPath())
77+
if (argumentMightRequireValue && argumentIndex == 0 && argument.StartsWith('/') && PathHelper.DirectorySeparatorChar == '/' && argument.IsValidPath())
7878
return false;
7979

8080
return argumentMightRequireValue;

src/GitVersion.App/FileAppender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public FileAppender(IFileSystem fileSystem, string filePath)
1515
this.fileSystem = fileSystem.NotNull();
1616
this.filePath = filePath;
1717

18-
var logFile = this.fileSystem.FileInfo.New(Path.GetFullPath(filePath));
18+
var logFile = this.fileSystem.FileInfo.New(PathHelper.GetFullPath(filePath));
1919

2020
logFile.Directory?.Create();
2121
if (logFile.Exists) return;

src/GitVersion.App/GitVersionExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public int Execute(GitVersionOptions gitVersionOptions)
5656
private int RunGitVersionTool(GitVersionOptions gitVersionOptions)
5757
{
5858
this.gitRepository.DiscoverRepository(gitVersionOptions.WorkingDirectory);
59-
var mutexName = this.repositoryInfo.DotGitDirectory?.Replace(Path.DirectorySeparatorChar.ToString(), "") ?? string.Empty;
59+
var mutexName = this.repositoryInfo.DotGitDirectory?.Replace(PathHelper.DirectorySeparatorChar.ToString(), "") ?? string.Empty;
6060
using var mutex = new Mutex(true, $@"Global\gitversion{mutexName}", out var acquired);
6161

6262
try

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void GetCurrentBranchShouldHandlePullRequests()
120120
[Test]
121121
public void WriteAllVariablesToTheTextWriter()
122122
{
123-
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
123+
var assemblyLocation = PathHelper.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
124124
assemblyLocation.ShouldNotBeNull();
125125
var propertyFile = PathHelper.Combine(assemblyLocation, "gitversion.properties");
126126
var ps1File = PathHelper.Combine(assemblyLocation, "gitversion.ps1");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void PicksUpBranchNameFromEnvironmentFromWebHook()
5555
[Test]
5656
public void WriteAllVariablesToTheTextWriter()
5757
{
58-
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
58+
var assemblyLocation = PathHelper.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
5959
assemblyLocation.ShouldNotBeNull();
6060
var f = PathHelper.Combine(assemblyLocation, "codebuild_this_file_should_be_deleted.properties");
6161

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void SetUp()
2323
this.environment.SetEnvironmentVariable(GitHubActions.EnvironmentVariableName, "true");
2424
this.environment.SetEnvironmentVariable("GITHUB_REF_TYPE", "branch");
2525

26-
this.githubSetEnvironmentTempFilePath = Path.GetTempFileName();
26+
this.githubSetEnvironmentTempFilePath = PathHelper.GetTempFileName();
2727
this.environment.SetEnvironmentVariable(GitHubActions.GitHubSetEnvTempFileEnvironmentVariableName, this.githubSetEnvironmentTempFilePath);
2828
}
2929

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void GetCurrentBranchShouldHandlePullRequests(string branchName, string e
9595
[Test]
9696
public void WriteAllVariablesToTheTextWriter()
9797
{
98-
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
98+
var assemblyLocation = PathHelper.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
9999
assemblyLocation.ShouldNotBeNull();
100100
var f = PathHelper.Combine(assemblyLocation, "jenkins_this_file_should_be_deleted.properties");
101101

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void GenerateMessageTest()
111111
[Test]
112112
public void WriteAllVariablesToTheTextWriter()
113113
{
114-
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
114+
var assemblyLocation = PathHelper.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
115115
assemblyLocation.ShouldNotBeNull();
116116
var f = PathHelper.Combine(assemblyLocation, "gitlab_this_file_should_be_deleted.properties");
117117

src/GitVersion.Configuration/ConfigurationFileLocator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal class ConfigurationFileLocator(
2626

2727
public void Verify(string? workingDirectory, string? projectRootDirectory)
2828
{
29-
if (Path.IsPathRooted(this.ConfigurationFile)) return;
29+
if (PathHelper.IsPathRooted(this.ConfigurationFile)) return;
3030
if (PathHelper.Equal(workingDirectory, projectRootDirectory)) return;
3131
WarnAboutAmbiguousConfigFileSelection(workingDirectory, projectRootDirectory);
3232
}

src/GitVersion.Core.Tests/Core/GitVersionToolDirectoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void SetUp()
2020
var sp = ConfigureServices();
2121
this.fileSystem = sp.GetRequiredService<IFileSystem>();
2222
this.workDirectory = PathHelper.Combine(PathHelper.GetTempPath(), Guid.NewGuid().ToString());
23-
this.gitDirectory = Repository.Init(this.workDirectory).TrimEnd(Path.DirectorySeparatorChar);
23+
this.gitDirectory = Repository.Init(this.workDirectory).TrimEnd(PathHelper.DirectorySeparatorChar);
2424
Assert.That(this.gitDirectory, Is.Not.Null);
2525
}
2626

src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public void HasDirtyFlagWhenUncommittedChangesAreInRepository(bool stageFile, in
111111

112112
for (int i = 0; i < numberOfFiles; i++)
113113
{
114-
var tempFile = Path.GetTempFileName();
115-
var repoFile = PathHelper.Combine(fixture.RepositoryPath, Path.GetFileNameWithoutExtension(tempFile) + ".txt");
114+
var tempFile = PathHelper.GetTempFileName();
115+
var repoFile = PathHelper.Combine(fixture.RepositoryPath, PathHelper.GetFileNameWithoutExtension(tempFile) + ".txt");
116116
fileSystem.File.Move(tempFile, repoFile);
117117
fileSystem.File.WriteAllText(repoFile, $"Hello world / testfile {i}");
118118

src/GitVersion.Core/Extensions/ConfigurationExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration,
100100
foreach (var groupName in regex.GetGroupNames())
101101
{
102102
var groupValue = match.Groups[groupName].Value;
103-
Lazy<string> escapedGroupValueLazy = new(() => EscapeInvalidCharaters(groupValue));
103+
Lazy<string> escapedGroupValueLazy = new(() => EscapeInvalidCharacters(groupValue));
104104
var placeholder = $"{{{groupName}}}";
105105
int index, startIndex = 0;
106106
while ((index = label.IndexOf(placeholder, startIndex, StringComparison.InvariantCulture)) >= 0)
@@ -115,33 +115,33 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration,
115115
return label;
116116
}
117117

118-
private static string EscapeInvalidCharaters(string groupValue) => groupValue.RegexReplace(@"[^a-zA-Z0-9-]", "-");
118+
private static string EscapeInvalidCharacters(string groupValue) => groupValue.RegexReplace("[^a-zA-Z0-9-]", "-");
119119

120-
public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this IFileSystem fileSystem, string path)
120+
public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this IFileSystem fileSystem, string? path)
121121
{
122122
string? startingDir = path;
123123
while (startingDir is not null)
124124
{
125125
var dirOrFilePath = PathHelper.Combine(startingDir, ".git");
126126
if (fileSystem.Directory.Exists(dirOrFilePath))
127127
{
128-
return (dirOrFilePath, Path.GetDirectoryName(dirOrFilePath)!);
128+
return (dirOrFilePath, PathHelper.GetDirectoryName(dirOrFilePath));
129129
}
130130

131131
if (fileSystem.File.Exists(dirOrFilePath))
132132
{
133133
string? relativeGitDirPath = ReadGitDirFromFile(dirOrFilePath);
134134
if (!string.IsNullOrWhiteSpace(relativeGitDirPath))
135135
{
136-
var fullGitDirPath = Path.GetFullPath(PathHelper.Combine(startingDir, relativeGitDirPath));
136+
var fullGitDirPath = PathHelper.GetFullPath(PathHelper.Combine(startingDir, relativeGitDirPath));
137137
if (fileSystem.Directory.Exists(fullGitDirPath))
138138
{
139-
return (fullGitDirPath, Path.GetDirectoryName(dirOrFilePath)!);
139+
return (fullGitDirPath, PathHelper.GetDirectoryName(dirOrFilePath));
140140
}
141141
}
142142
}
143143

144-
startingDir = Path.GetDirectoryName(startingDir);
144+
startingDir = PathHelper.GetDirectoryName(startingDir);
145145
}
146146

147147
return null;

src/GitVersion.Core/Helpers/DirectoryHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void DeleteDirectory(string directoryPath)
4040
"{0}Known and common causes include:" +
4141
"{0}- Windows Search Indexer (go to the Indexing Options, in the Windows Control Panel, and exclude the bin folder of LibGit2Sharp.Tests)" +
4242
"{0}- Antivirus (exclude the bin folder of LibGit2Sharp.Tests from the paths scanned by your real-time antivirus){0}",
43-
PathHelper.NewLine, Path.GetFullPath(directoryPath)));
43+
PathHelper.NewLine, PathHelper.GetFullPath(directoryPath)));
4444
}
4545
}
4646
}

src/GitVersion.Core/Helpers/PathHelper.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace GitVersion.Helpers;
55
internal static class PathHelper
66
{
77
public static string NewLine => SysEnv.NewLine;
8+
public static char DirectorySeparatorChar => Path.DirectorySeparatorChar;
89

910
private static readonly StringComparison OsDependentComparison =
1011
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
@@ -33,13 +34,19 @@ public static string GetDirectoryName(string? path)
3334
return Path.GetDirectoryName(path)!;
3435
}
3536

36-
public static string GetFullPath(string? path)
37+
public static string GetFileName(string? path)
3738
{
3839
ArgumentNullException.ThrowIfNull(path, nameof(path));
3940

40-
return Path.GetFullPath(path);
41+
return Path.GetFileName(path);
4142
}
4243

44+
public static string? GetFileNameWithoutExtension(string? path) => Path.GetFileNameWithoutExtension(path);
45+
46+
public static string? GetExtension(string? path) => Path.GetExtension(path);
47+
48+
public static string GetFullPath(string? path) => Path.GetFullPath(path!);
49+
4350
public static string Combine(string? path1, string? path2)
4451
{
4552
ArgumentException.ThrowIfNullOrWhiteSpace(path1);
@@ -79,4 +86,10 @@ public static bool Equal(string? path, string? otherPath) =>
7986
GetFullPath(path).TrimEnd('\\').TrimEnd('/'),
8087
GetFullPath(otherPath).TrimEnd('\\').TrimEnd('/'),
8188
OsDependentComparison);
89+
90+
public static string GetRandomFileName() => Path.GetRandomFileName();
91+
92+
public static string GetTempFileName() => Path.GetTempFileName();
93+
94+
public static bool IsPathRooted(string? path) => Path.IsPathRooted(path);
8295
}

src/GitVersion.Core/VersionCalculation/Caching/GitVersionCacheKeyFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private List<string> CalculateDirectoryContents(string root)
118118
try
119119
{
120120
if (!this.fileSystem.File.Exists(file)) continue;
121-
result.Add(Path.GetFileName(file));
121+
result.Add(PathHelper.GetFileName(file));
122122
result.Add(this.fileSystem.File.ReadAllText(file));
123123
}
124124
catch (IOException e)

src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public GitRepositoryInfo(IFileSystem fileSystem, IOptions<GitVersionOptions> opt
4040
var targetUrl = repositoryInfo.TargetUrl;
4141
var clonePath = repositoryInfo.ClonePath;
4242

43-
var userTemp = clonePath ?? Path.GetTempPath();
43+
var userTemp = clonePath ?? PathHelper.GetTempPath();
4444
var repositoryName = targetUrl.Split('/', '\\').Last().Replace(".git", string.Empty);
4545
var possiblePath = PathHelper.Combine(userTemp, repositoryName);
4646

0 commit comments

Comments
 (0)