Skip to content

Commit e41c43e

Browse files
committed
Adding tests to validate that this implementation of ignore is lacking
1 parent 6ca9c61 commit e41c43e

File tree

6 files changed

+735
-1
lines changed

6 files changed

+735
-1
lines changed

Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
1313
<PackageVersion Include="GitignoreParserNet" Version="0.2.0.14" />
1414
<PackageVersion Include="ini-parser-netstandard" Version="2.5.3" />
15+
<PackageVersion Include="LibGit2Sharp" Version="0.31.0" />
1516
<PackageVersion Include="LovettSoftware.XmlDiff" Version="1.1.0" />
1617
<PackageVersion Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="9.0.10" />
1718
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.14.0" />
@@ -28,7 +29,7 @@
2829
<PackageVersion Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
2930
<PackageVersion Include="StrongNamer" Version="0.2.5" />
3031
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
31-
<PackageVersion Include="System.IO.Abstractions" Version="22.0.16" />
32+
<PackageVersion Include="System.IO.Abstractions" Version="22.1.0" />
3233
<PackageVersion Include="System.IO.Abstractions.TestingHelpers" Version="22.0.16" />
3334
<PackageVersion Include="System.IO.Hashing" Version="9.0.10" />
3435
<PackageVersion Include="System.Text.Encoding.CodePages" Version="9.0.10" />

Src/CSharpier.Cli/IgnoreFile.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ async Task<IgnoreWithBasePath> CreateIgnore(string ignoreFilePath, string? overr
6363

6464
var content = await fileSystem.File.ReadAllTextAsync(ignoreFilePath, cancellationToken);
6565

66+
// TODO #1768 Gitignore Parser won't work as is, can maybe modify it. It is an apache license
67+
// try out https://github.com/markashleybell/MAB.DotIgnore to see if that works and is fast
68+
// if I remember correctly the modified version of https://github.com/goelhardik/ignore I originally used was too slow
6669
var (positives, negatives) = GitignoreParserNet.GitignoreParser.Parse(content, true);
6770

6871
ignore.AddPositives(positives.Merged);

Src/CSharpier.Tests/CSharpier.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageReference Include="DiffEngine" />
1313
<PackageReference Include="FluentAssertions" />
1414
<PackageReference Include="GitHubActionsTestLogger" PrivateAssets="all" />
15+
<PackageReference Include="LibGit2Sharp" />
1516
<PackageReference Include="Microsoft.NET.Test.Sdk" />
1617
<PackageReference Include="NUnit" />
1718
<PackageReference Include="NUnit3TestAdapter" />
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System.IO.Abstractions;
2+
using LibGit2Sharp;
3+
4+
namespace CSharpier.Tests.Cli;
5+
6+
public class GitRepository
7+
{
8+
private readonly IFileSystem fileSystem;
9+
public string RepoPath { get; }
10+
11+
public GitRepository(IFileSystem fileSystem)
12+
{
13+
this.fileSystem = fileSystem;
14+
var tempPath = this.fileSystem.Path.GetTempPath();
15+
this.RepoPath = this.fileSystem.Path.Combine(tempPath, $"fakeRepo-{Guid.NewGuid()}");
16+
if (this.fileSystem.Directory.Exists(this.RepoPath))
17+
{
18+
this.fileSystem.Directory.Delete(this.RepoPath, true);
19+
}
20+
21+
this.fileSystem.Directory.CreateDirectory(this.RepoPath);
22+
23+
Repository.Init(this.RepoPath);
24+
}
25+
26+
public void AddUntrackedFileToRepo(string relativePath, int numLines = 1)
27+
{
28+
string lineContent = $"Fake content line.{Environment.NewLine}";
29+
this.AddUntrackedFileToRepo(
30+
relativePath,
31+
string.Concat(Enumerable.Repeat(lineContent, numLines))
32+
);
33+
}
34+
35+
public void AddTrackedFileToRepo(string relativePath, string content)
36+
{
37+
this.AddUntrackedFileToRepo(relativePath, content);
38+
this.CommitPathToRepo(relativePath);
39+
}
40+
41+
public void AddUntrackedDirToRepo(string relativePath)
42+
{
43+
var filePath = this.fileSystem.Path.Combine(this.RepoPath, relativePath);
44+
var directoryInfo = Directory.GetParent(filePath);
45+
Directory.CreateDirectory(directoryInfo.FullName);
46+
}
47+
48+
public IEnumerable<StatusEntry> GetUntrackedFiles()
49+
{
50+
using var repo = new Repository(this.RepoPath);
51+
var status = repo.RetrieveStatus();
52+
return status.Untracked;
53+
}
54+
55+
public void AddUntrackedFileToRepo(string relativePath, string content)
56+
{
57+
var filePath = this.fileSystem.Path.Combine(this.RepoPath, relativePath);
58+
var directoryInfo = Directory.GetParent(filePath);
59+
Directory.CreateDirectory(directoryInfo.FullName);
60+
this.fileSystem.File.WriteAllText(
61+
this.fileSystem.Path.Combine(this.RepoPath, relativePath),
62+
content
63+
);
64+
}
65+
66+
public void DeleteRepoDirectory()
67+
{
68+
var dirInfo = this.fileSystem.DirectoryInfo.New(this.RepoPath);
69+
if (dirInfo.Exists)
70+
{
71+
SetNormalAttribute(dirInfo);
72+
}
73+
74+
dirInfo.Delete(true);
75+
}
76+
77+
private static void SetNormalAttribute(IDirectoryInfo dirInfo)
78+
{
79+
foreach (var subDir in dirInfo.GetDirectories())
80+
{
81+
SetNormalAttribute(subDir);
82+
}
83+
84+
foreach (var file in dirInfo.GetFiles())
85+
{
86+
file.Attributes = FileAttributes.Normal;
87+
}
88+
}
89+
90+
private void CommitPathToRepo(string relativePath)
91+
{
92+
var repo = new Repository(this.RepoPath);
93+
Commands.Stage(repo, relativePath);
94+
var author = new Signature("FakeUser", "[email protected]", DateTimeOffset.Now);
95+
repo.Commit("Adding files", author, author);
96+
}
97+
}

0 commit comments

Comments
 (0)