Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for IRepositoryClient.GetArchive #680

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 93 additions & 59 deletions NGitLab.Tests/RepositoryClient/RepositoryClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Formats.Tar;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using NGitLab.Models;
Expand All @@ -11,65 +14,6 @@ namespace NGitLab.Tests.RepositoryClient;

public class RepositoryClientTests
{
private sealed class RepositoryClientTestsContext : IDisposable
{
public const string SubfolderName = "subfolder";

public RepositoryClientTestsContext(GitLabTestContext context, Project project, Commit[] commits, IRepositoryClient repositoryClient)
{
Context = context;
Project = project;
Commits = commits;
RepositoryClient = repositoryClient;
}

public GitLabTestContext Context { get; }

public Project Project { get; }

public Commit[] Commits { get; }

public IRepositoryClient RepositoryClient { get; }

public void Dispose()
{
Context.Dispose();
}

public static async Task<RepositoryClientTestsContext> CreateAsync(int commitCount)
{
var context = await GitLabTestContext.CreateAsync().ConfigureAwait(false);
var project = context.CreateProject();
var repositoryClient = context.Client.GetRepository(project.Id);

var commits = new Commit[commitCount];
for (var i = 0; i < commits.Length; i++)
{
commits[i] = context.Client.GetCommits(project.Id).Create(new CommitCreate
{
Branch = project.DefaultBranch,
CommitMessage = context.GetUniqueRandomString(),
AuthorEmail = "a@example.com",
AuthorName = "a",
Actions =
{
new CreateCommitAction
{
Action = "create",
Content = "test",
FilePath = // Spread files among the root directory and its 'subfolder'
i % 2 == 0 ?
$"test{i.ToString(CultureInfo.InvariantCulture)}.md" :
$"{SubfolderName}/test{i.ToString(CultureInfo.InvariantCulture)}.md",
},
},
});
}

return new RepositoryClientTestsContext(context, project, commits, repositoryClient);
}
}

[Test]
[NGitLabRetry]
public async Task GetAllCommits()
Expand Down Expand Up @@ -353,4 +297,94 @@ public async Task GetCommitRefs(CommitRefType type)
Assert.That(commitRefs, Is.Not.Empty);
}
}

[Test]
[NGitLabRetry]
public async Task GetArchive()
{
// Arrange
const int commitCount = 4;
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount);

var archiveItems = new List<KeyValuePair<string, TarEntryType>>();

// Act
context.RepositoryClient.GetArchive(tgzStream =>
{
using var gzipDecompressor = new GZipStream(tgzStream, CompressionMode.Decompress);
using var unzippedStream = new MemoryStream();

gzipDecompressor.CopyTo(unzippedStream);
unzippedStream.Seek(0, SeekOrigin.Begin);

using var reader = new TarReader(unzippedStream);

while (reader.GetNextEntry() is TarEntry entry)
{
archiveItems.Add(new KeyValuePair<string, TarEntryType>(entry.Name, entry.EntryType));
}
});

// Assert
Assert.That(archiveItems.Where(item => item.Value is TarEntryType.RegularFile).ToList(), Has.Count.EqualTo(commitCount));
}

private sealed class RepositoryClientTestsContext : IDisposable
{
public const string SubfolderName = "subfolder";

public RepositoryClientTestsContext(GitLabTestContext context, Project project, Commit[] commits, IRepositoryClient repositoryClient)
{
Context = context;
Project = project;
Commits = commits;
RepositoryClient = repositoryClient;
}

public GitLabTestContext Context { get; }

public Project Project { get; }

public Commit[] Commits { get; }

public IRepositoryClient RepositoryClient { get; }

public void Dispose()
{
Context.Dispose();
}

public static async Task<RepositoryClientTestsContext> CreateAsync(int commitCount)
{
var context = await GitLabTestContext.CreateAsync().ConfigureAwait(false);
var project = context.CreateProject();
var repositoryClient = context.Client.GetRepository(project.Id);

var commits = new Commit[commitCount];
for (var i = 0; i < commits.Length; i++)
{
commits[i] = context.Client.GetCommits(project.Id).Create(new CommitCreate
{
Branch = project.DefaultBranch,
CommitMessage = context.GetUniqueRandomString(),
AuthorEmail = "a@example.com",
AuthorName = "a",
Actions =
{
new CreateCommitAction
{
Action = "create",
Content = "test",
FilePath = // Spread files among the root directory and its 'subfolder'
i % 2 == 0 ?
$"test{i.ToString(CultureInfo.InvariantCulture)}.md" :
$"{SubfolderName}/test{i.ToString(CultureInfo.InvariantCulture)}.md",
},
},
});
}

return new RepositoryClientTestsContext(context, project, commits, repositoryClient);
}
}
}