Skip to content

Commit

Permalink
Merge branch 'main' into support-sub-modules-in-mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jairbubbles authored Sep 28, 2023
2 parents 737ecb2 + adea364 commit 1e6706f
Show file tree
Hide file tree
Showing 63 changed files with 1,183 additions and 274 deletions.
29 changes: 21 additions & 8 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ csharp_style_var_for_built_in_types = true : suggestion
csharp_style_var_when_type_is_apparent = true : warning

# Expression-Bodied members
csharp_style_expression_bodied_accessors = true : suggestion
csharp_style_expression_bodied_indexers = true : suggestion
csharp_style_expression_bodied_operators = true : suggestion
csharp_style_expression_bodied_properties = true : suggestion
csharp_style_expression_bodied_accessors = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
# Explicitly disabled due to difference in coding style between source and tests
#csharp_style_expression_bodied_constructors = true : warning
#csharp_style_expression_bodied_methods = true : warning
Expand All @@ -101,7 +101,7 @@ csharp_style_conditional_delegate_call = true : warning
csharp_style_throw_expression = true : warning

# Code block preferences
csharp_prefer_braces = when_multiline : suggestion
csharp_prefer_braces = when_multiline:suggestion

## Formatting conventions
# Dotnet formatting settings:
Expand Down Expand Up @@ -141,6 +141,16 @@ csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping options
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = false
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
dotnet_diagnostic.SA1507.severity = error

## Naming conventions
[*.{cs,vb}]
Expand All @@ -159,7 +169,7 @@ dotnet_naming_style.non_private_static_field_style.capitalization = pascal_case
# Constants are PascalCase
dotnet_naming_rule.constants_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constants_should_be_pascal_case.symbols = constants
dotnet_naming_rule.constants_should_be_pascal_case.style = constant_style
dotnet_naming_rule.constants_should_be_pascal_case.style = non_private_static_field_style

dotnet_naming_symbols.constants.applicable_kinds = field, local
dotnet_naming_symbols.constants.required_modifiers = const
Expand Down Expand Up @@ -198,7 +208,7 @@ dotnet_naming_style.camel_case_style.capitalization = camel_case
# Local functions are PascalCase
dotnet_naming_rule.local_functions_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.local_functions_should_be_pascal_case.symbols = local_functions
dotnet_naming_rule.local_functions_should_be_pascal_case.style = local_function_style
dotnet_naming_rule.local_functions_should_be_pascal_case.style = non_private_static_field_style

dotnet_naming_symbols.local_functions.applicable_kinds = local_function
dotnet_naming_style.local_function_style.capitalization = pascal_case
Expand All @@ -216,7 +226,7 @@ dotnet_naming_symbols.type_parameter_symbol.applicable_accessibilities = *
# By default, name items with PascalCase
dotnet_naming_rule.members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.members_should_be_pascal_case.symbols = all_members
dotnet_naming_rule.members_should_be_pascal_case.style = pascal_case_style
dotnet_naming_rule.members_should_be_pascal_case.style = non_private_static_field_style

dotnet_naming_symbols.all_members.applicable_kinds = *

Expand Down Expand Up @@ -373,3 +383,6 @@ dotnet_diagnostic.SA1636.severity = none

# SA1649: File name should match first type name
dotnet_diagnostic.SA1649.severity = none
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
end_of_line = crlf
10 changes: 3 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
create_nuget:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Get all the history so MinGit can compute the version
- name: Setup .NET Core (latest)
Expand Down Expand Up @@ -51,12 +51,8 @@ jobs:
# Keep in sync with the version in GitLabDockerContainer.cs
# Available tags: https://hub.docker.com/r/gitlab/gitlab-ee/tags
gitlab: [
'gitlab/gitlab-ee:15.0.5-ee.0',
'gitlab/gitlab-ee:15.1.6-ee.0',
'gitlab/gitlab-ee:15.4.6-ee.0',
'gitlab/gitlab-ee:15.6.8-ee.0',
# Several MR-related tests fail against the following version. We need to investigate...
# 'gitlab/gitlab-ee:15.10.0-ee.0',
'gitlab/gitlab-ee:15.11.9-ee.0',
]
configuration: [ Release ]
fail-fast: false
Expand All @@ -69,7 +65,7 @@ jobs:
GITLAB_OMNIBUS_CONFIG: "external_url 'http://localhost:48624/'"
GITLAB_ROOT_PASSWORD: "Pa$$w0rd"
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET Core (latest)
uses: actions/setup-dotnet@v3
- run: |
Expand Down
10 changes: 5 additions & 5 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
<Authors>$(Company), NGitLab contributors</Authors>

<!-- Compile Options -->
<LangVersion>9.0</LangVersion>
<LangVersion>10.0</LangVersion>
<Deterministic>true</Deterministic>
<Features>strict</Features>
<TreatWarningsAsErrors Condition="'$(Configuration)' != 'Debug'">true</TreatWarningsAsErrors>

<RestoreUseStaticGraphEvaluation>true</RestoreUseStaticGraphEvaluation>

<!-- Package info -->
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

Expand All @@ -37,17 +37,17 @@

<!-- Analyzers (Roslyn, Meziantou, StyleCop, ...) -->
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.1">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<PackageReference Include="Meziantou.Analyzer" Version="2.0.57">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.85">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
41 changes: 41 additions & 0 deletions NGitLab.Mock.Tests/BotUserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using NGitLab.Models;
using NUnit.Framework;

namespace NGitLab.Mock.Tests
{
public sealed class BotUserTests
{
[Test]
public void Test_project_bot_user()
{
using var server = new GitLabServer();
var group = new Group("test");
var project = new Project("test-project");
server.Groups.Add(group);
group.Projects.Add(project);

var bot = project.CreateBotUser("token_name", AccessLevel.Maintainer);

Assert.That(bot.Bot, Is.True);
Assert.That(bot.Name, Is.EqualTo("token_name"));
var permissions = project.GetEffectivePermissions();
var botPermission = permissions.GetEffectivePermission(bot);
Assert.That(botPermission.AccessLevel, Is.EqualTo(AccessLevel.Maintainer));
}

[Test]
public void Test_group_bot_user()
{
using var server = new GitLabServer();
var group = new Group("test");
server.Groups.Add(group);

var bot = group.CreateBotUser(AccessLevel.Maintainer);

Assert.That(bot.Bot, Is.True);
var permissions = group.GetEffectivePermissions();
var botPermission = permissions.GetEffectivePermission(bot);
Assert.That(botPermission.AccessLevel, Is.EqualTo(AccessLevel.Maintainer));
}
}
}
86 changes: 86 additions & 0 deletions NGitLab.Mock.Tests/MembersMockTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Linq;
using NGitLab.Mock.Config;
using NUnit.Framework;

namespace NGitLab.Mock.Tests
{
public class MembersMockTests
{
[Test]
public void Test_members_group_all_direct([Values] bool isDefault)
{
using var server = new GitLabConfig()
.WithUser("user1", isDefault: true)
.WithUser("user2")
.WithGroup("G1", 1, addDefaultUserAsMaintainer: true)
.WithGroup("G2", 2, @namespace: "G1", configure: g => g.WithUserPermission("user2", Models.AccessLevel.Maintainer))
.BuildServer();

var client = server.CreateClient("user1");
var members = isDefault
? client.Members.OfGroup("2")
: client.Members.OfGroup("2", includeInheritedMembers: false);

Assert.AreEqual(1, members.Count(), "Membership found are invalid");
}

[Test]
public void Test_members_group_all_inherited()
{
using var server = new GitLabConfig()
.WithUser("user1", isDefault: true)
.WithUser("user2")
.WithProject("Test")
.WithGroup("G1", 1, configure: g => g.WithUserPermission("user1", Models.AccessLevel.Maintainer))
.WithGroup("G2", 2, @namespace: "G1", configure: g => g.WithUserPermission("user2", Models.AccessLevel.Maintainer))
.BuildServer();

var client = server.CreateClient("user1");
var members = client.Members.OfGroup("2", includeInheritedMembers: true);

Assert.AreEqual(2, members.Count(), "Membership found are invalid");
}

[Test]
public void Test_members_project_all_direct([Values] bool isDefault)
{
using var server = new GitLabConfig()
.WithUser("user1", isDefault: true)
.WithUser("user2")
.WithUser("user3")
.WithGroup("G1", 1, addDefaultUserAsMaintainer: true)
.WithGroup("G2", 2, @namespace: "G1", configure: g => g.WithUserPermission("user2", Models.AccessLevel.Maintainer))
.WithProject("Project", @namespace: "G1", configure: g =>
g.WithUserPermission("user3", Models.AccessLevel.Maintainer)
.WithGroupPermission("G2", Models.AccessLevel.Developer))
.BuildServer();

var client = server.CreateClient("user1");
var members = isDefault
? client.Members.OfProject("1")
: client.Members.OfProject("1", includeInheritedMembers: false);

Assert.AreEqual(1, members.Count(), "Membership found are invalid");
}

[Test]
public void Test_members_project_all_inherited()
{
using var server = new GitLabConfig()
.WithUser("user1", isDefault: true)
.WithUser("user2")
.WithUser("user3")
.WithGroup("G1", addDefaultUserAsMaintainer: true)
.WithGroup("G2", @namespace: "G1", configure: g => g.WithUserPermission("user2", Models.AccessLevel.Maintainer))
.WithProject("Project", 1, @namespace: "G1", configure: g =>
g.WithUserPermission("user3", Models.AccessLevel.Maintainer)
.WithGroupPermission("G1/G2", Models.AccessLevel.Developer))
.BuildServer();

var client = server.CreateClient("user1");
var members = client.Members.OfProject("1", includeInheritedMembers: true);

Assert.AreEqual(3, members.Count(), "Membership found are invalid");
}
}
}
42 changes: 42 additions & 0 deletions NGitLab.Mock.Tests/MilestonesMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,47 @@ public void Test_milestones_can_be_closed_and_activated_from_project()
Assert.AreEqual(1, activeMilestones.Length, "Active milestones count is invalid");
Assert.AreEqual(0, closedMilestones.Length, "Closed milestones count is invalid");
}

[Test]
public void Test_projects_merge_request_can_be_found_from_milestone()
{
const int ProjectId = 1;
const int MilestoneId = 1;
using var server = new GitLabConfig()
.WithUser("user1", isDefault: true)
.WithProject("Test", id: ProjectId, addDefaultUserAsMaintainer: true, configure: project => project
.WithMilestone("Milestone 1", id: MilestoneId)
.WithMergeRequest("branch-01", title: "Merge request 1", milestone: "Milestone 1")
.WithMergeRequest("branch-02", title: "Merge request 2", milestone: "Milestone 1")
.WithMergeRequest("branch-03", title: "Merge request 3", milestone: "Milestone 2"))
.BuildServer();

var client = server.CreateClient();
var mergeRequests = client.GetMilestone(ProjectId).GetMergeRequests(MilestoneId).ToArray();
Assert.AreEqual(2, mergeRequests.Length, "Merge requests count is invalid");
}

[Test]
public void Test_groups_merge_request_can_be_found_from_milestone()
{
const int projectId = 1;
const int milestoneId = 1;
using var server = new GitLabConfig()
.WithUser("user1", isDefault: true)
.WithGroup("parentGroup", id: projectId, configure: group => group
.WithMilestone("Milestone 1", id: milestoneId))
.WithGroup("subGroup1", 2, @namespace: "parentGroup")
.WithGroup("subGroup2", 3, @namespace: "parentGroup")
.WithProject("project1", @namespace: "parentGroup/subGroup1", addDefaultUserAsMaintainer: true, configure: project => project
.WithMergeRequest("branch-01", title: "Merge request 1", milestone: "Milestone 1")
.WithMergeRequest("branch-02", title: "Merge request 2", milestone: "Milestone 2"))
.WithProject("project2", @namespace: "parentGroup/subGroup2", addDefaultUserAsMaintainer: true, configure: project => project
.WithMergeRequest("branch-03", title: "Merge request 3", milestone: "Milestone 1"))
.BuildServer();

var client = server.CreateClient();
var mergeRequests = client.GetGroupMilestone(projectId).GetMergeRequests(milestoneId).ToArray();
Assert.AreEqual(2, mergeRequests.Length, "Merge requests count is invalid");
}
}
}
4 changes: 2 additions & 2 deletions NGitLab.Mock.Tests/NGitLab.Mock.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<ProjectReference Include="..\NGitLab.Mock\NGitLab.Mock.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions NGitLab.Mock/Clients/IssueClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ public Models.Issue Get(int projectId, int issueId)
return GetById(issueId);
}

public GitLabCollectionResponse<Models.Issue> LinkedToAsync(int projectId, int issueId)
{
throw new NotImplementedException();
}

public bool CreateLinkBetweenIssues(int sourceProjectId, int sourceIssueId, int targetProjectId, int targetIssueId)
{
throw new NotImplementedException();
}

public Models.Issue GetById(int issueId)
{
using (Context.BeginOperationScope())
Expand Down
21 changes: 20 additions & 1 deletion NGitLab.Mock/Clients/LibGit2SharpExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NGitLab.Models;

Expand Down Expand Up @@ -62,7 +63,25 @@ public static Models.CommitInfo ToCommitInfo(this LibGit2Sharp.Commit commit)

internal static LibGit2Sharp.Commit GetLastCommitForFileChanges(this LibGit2Sharp.Repository repository, string filePath)
{
return repository.Commits.QueryBy(filePath).FirstOrDefault()?.Commit;
try
{
return repository.Commits.QueryBy(filePath).FirstOrDefault()?.Commit;
}
catch (KeyNotFoundException)
{
// LibGit2Sharp sometimes fails with the following exception
// System.Collections.Generic.KeyNotFoundException: The given key '1d08df45e551942eaa70d9f5ab6f5f7665a3f5b3' was not present in the dictionary.
// at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
// at LibGit2Sharp.Core.FileHistory.FullHistory(IRepository repo, String path, CommitFilter filter)+MoveNext() in /_/LibGit2Sharp/Core/FileHistory.cs:line 120
// at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
// at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
// at NGitLab.Mock.Clients.LibGit2SharpExtensions.GetLastCommitForFileChanges(Repository repository, String filePath) in /_/NGitLab.Mock/Clients/LibGit2SharpExtensions.cs:line 65
// at NGitLab.Mock.Repository.GetFile(String filePath, String ref) in /_/NGitLab.Mock/Repository.cs:line 485
// at NGitLab.Mock.Clients.FileClient.Get(String filePath, String ref) in /_/NGitLab.Mock/Clients/FileClient.cs:line 77
// at NGitLab.Mock.Clients.FileClient.GetAsync(String filePath, String ref, CancellationToken cancellationToken) in /_/NGitLab.Mock/Clients/FileClient.cs:line 125
}

return null;
}
}
}
Loading

0 comments on commit 1e6706f

Please sign in to comment.