Skip to content

Commit 9a813b8

Browse files
committedApr 14, 2025
加入GitLab的单元测试
1 parent 0513340 commit 9a813b8

File tree

7 files changed

+99
-9
lines changed

7 files changed

+99
-9
lines changed
 

‎Extensions.Configuration.GitRepository.GitLabProvider/GitLabProviderExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public static GitRepositoryConfigurationOptions WithGitLab([NotNull] this GitRep
1616
{
1717
throw new ArgumentNullException(nameof(options));
1818
}
19-
var gitlabClient = new GitLabRepositoryClient(options.HostUrl, options.AuthenticationToken, options.RepositoryPath);
20-
options.GitRepositoryClient = gitlabClient;
19+
options.GitRepositoryClient = new GitLabRepositoryClient(options);
2120
return options;
2221
}
2322
}

‎Extensions.Configuration.GitRepository.GitLabProvider/GitLabRepositoryClient.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,27 @@ namespace Extensions.Configuration.GitRepository.GitLabProvider
77
internal class GitLabRepositoryClient : IGitRepositoryClient
88
{
99

10-
private readonly NGitLab.GitLabClient client;
11-
private readonly string _repoNamespaced;
10+
private NGitLab.GitLabClient client;
11+
private readonly GitRepositoryConfigurationOptions _options;
1212
private Project project;
1313
private IRepositoryClient repo;
14-
15-
public GitLabRepositoryClient(string hostUrl, string authenticationToken, string repoNamespaced)
14+
public GitLabRepositoryClient(GitRepositoryConfigurationOptions options)
1615
{
17-
client = new NGitLab.GitLabClient(hostUrl, authenticationToken);
18-
_repoNamespaced = repoNamespaced;
16+
_options = options;
1917
}
2018

19+
2120
private void check_connect()
2221
{
22+
if (client == null)
23+
{
24+
client = new NGitLab.GitLabClient(_options.HostUrl, _options.AuthenticationToken);
25+
}
2326
if (project == null || repo == null)
2427
{
2528
try
2629
{
27-
project = client.Projects.GetByNamespacedPathAsync(_repoNamespaced).GetAwaiter().GetResult();
30+
project = client.Projects.GetByNamespacedPathAsync(_options.RepositoryPath).GetAwaiter().GetResult();
2831
repo = client.GetRepository(new ProjectId(project.Id));
2932
}
3033
catch (System.Exception ex)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="MSTest.Sdk/3.6.4">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<!--
9+
Displays error on console in addition to the log file. Note that this feature comes with a performance impact.
10+
For more information, visit https://learn.microsoft.com/dotnet/core/testing/unit-testing-platform-integration-dotnet-test#show-failure-per-test
11+
-->
12+
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
13+
<UserSecretsId>c0ba2e9f-65d7-4a51-8c1d-4392c9867f27</UserSecretsId>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.4" />
18+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\Extensions.Configuration.GitRepository.GitLabProvider\Extensions.Configuration.GitRepository.GitLabProvider.csproj" />
23+
<ProjectReference Include="..\Extensions.Configuration.GitRepository\Extensions.Configuration.GitRepository.csproj" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"secrets1": {
4+
"type": "secrets"
5+
}
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"secrets1": {
4+
"type": "secrets.user"
5+
}
6+
}
7+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Testing.Platform.Builder;
3+
using System.ComponentModel;
4+
using System.Threading.Tasks;
5+
6+
namespace Extensions.Configuration.GitRepository.TestProject
7+
{
8+
[TestClass]
9+
public sealed class TestGitRepositoryProvides
10+
{
11+
[TestInitialize]
12+
public void TestInit()
13+
{
14+
}
15+
16+
[TestCleanup]
17+
public void TestCleanup()
18+
{
19+
// This method is called after each test method.
20+
}
21+
22+
[TestMethod]
23+
[DataRow("GitLab", "https://gitlab.com/", "maikebing/gitcfg", "WithGitLab", typeof(GitLabProviderExtensions), DisplayName = "GitLabProvider")]
24+
public void TestGitLabProvider(string _proveiderName, string hosturl, string repoPath, string setProveiderMethodName, Type extType)
25+
{
26+
IConfigurationBuilder _builder;
27+
IConfigurationRoot config;
28+
var cfgfilename = Path.GetTempFileName();
29+
_builder = new ConfigurationBuilder()
30+
.AddUserSecrets<TestGitRepositoryProvides>();
31+
config = _builder.Build();
32+
System.IO.File.WriteAllText(cfgfilename, $"{{\"{_proveiderName}\":\"{_proveiderName}\"}}");
33+
_builder.AddGitRepository(cfg =>
34+
{
35+
cfg = cfg.WithHostUrl(hosturl)
36+
.WithRepositoryPath(repoPath)
37+
.WithAuthenticationToken(config.GetValue<string>(_proveiderName))
38+
.WithFileName($"{_proveiderName}.json")
39+
.WithCache(cfgfilename);
40+
extType.GetMethod(setProveiderMethodName)?.Invoke(null, [cfg]);
41+
});
42+
var cfg = _builder.Build();
43+
Assert.IsNotNull(cfg);
44+
Assert.AreEqual(cfg.GetValue<string>(_proveiderName), _proveiderName);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)
Please sign in to comment.