-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Cannot create commit with Repository mock on existing branch (#760)
* fix: Cannot create commit with Repository mock on existing branch * Add tests to Repository mock * Fix test * Fix test
- Loading branch information
1 parent
16bca23
commit 8bb0a06
Showing
3 changed files
with
273 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using FluentAssertions; | ||
using NGitLab.Mock.Clients; | ||
using NGitLab.Models; | ||
using NUnit.Framework; | ||
|
||
namespace NGitLab.Mock.Tests; | ||
|
||
public class RepositoryMockTests | ||
{ | ||
[Test] | ||
public void Test_create_commit_in_new_branch_fails_if_both_start_branch_and_sha_specified() | ||
{ | ||
// Arrange | ||
using var server = new GitLabServer(); | ||
var user = server.Users.AddNew(); | ||
var project = user.Namespace.Projects.AddNew(project => project.Visibility = VisibilityLevel.Internal); | ||
var initCommit = project.Repository.Commit(user, "Initial commit"); | ||
|
||
var startBranch = project.DefaultBranch; | ||
|
||
Assert.That(() => project.Repository.Commit(new() | ||
{ | ||
Branch = "new-branch", | ||
StartBranch = startBranch, | ||
StartSha = initCommit.Sha, | ||
CommitMessage = "First commit in new branch", | ||
Actions = new List<CreateCommitAction> | ||
{ | ||
new() | ||
{ | ||
Action = "update", | ||
Content = "This is in a new branch", | ||
FilePath = "README.md", | ||
}, | ||
}, | ||
}), Throws.TypeOf<GitLabBadRequestException>() | ||
.With.Message.Contains("GitLab server returned an error (BadRequest): start_branch, start_sha are mutually exclusive.")); | ||
} | ||
|
||
[Test] | ||
public void Test_create_a_new_commit_with_start_branch_fails_if_branch_already_exists() | ||
{ | ||
// Arrange | ||
using var server = new GitLabServer(); | ||
var user = server.Users.AddNew(); | ||
var project = user.Namespace.Projects.AddNew(project => project.Visibility = VisibilityLevel.Internal); | ||
var initCommit = project.Repository.Commit(user, "Initial commit"); | ||
|
||
project.Repository.CreateBranch("new-branch", initCommit.Sha); | ||
|
||
var startBranch = project.DefaultBranch; | ||
var newBranch = "new-branch"; | ||
|
||
// Act & Assert | ||
Assert.That(() => project.Repository.Commit(new() | ||
{ | ||
Branch = newBranch, | ||
StartBranch = startBranch, | ||
CommitMessage = "First commit in new branch", | ||
Actions = new List<CreateCommitAction> | ||
{ | ||
new() | ||
{ | ||
Action = "update", | ||
Content = "This is in a new branch", | ||
FilePath = "README.md", | ||
}, | ||
}, | ||
}), Throws.TypeOf<GitLabBadRequestException>() | ||
.With.Message.Contains($"A branch called '{newBranch}' already exists.")); | ||
} | ||
|
||
[Test] | ||
public void Test_create_a_new_commit_with_start_sha_fails_if_branch_already_exists() | ||
{ | ||
// Arrange | ||
using var server = new GitLabServer(); | ||
var user = server.Users.AddNew(); | ||
var project = user.Namespace.Projects.AddNew(project => project.Visibility = VisibilityLevel.Internal); | ||
var initCommit = project.Repository.Commit(user, "Initial commit"); | ||
|
||
project.Repository.CreateBranch("new-branch", initCommit.Sha); | ||
|
||
var startBranch = project.DefaultBranch; | ||
var newBranch = "new-branch"; | ||
|
||
// Act & Assert | ||
Assert.That(() => project.Repository.Commit(new() | ||
{ | ||
Branch = newBranch, | ||
StartSha = initCommit.Sha, | ||
CommitMessage = "First commit in new branch", | ||
Actions = new List<CreateCommitAction> | ||
{ | ||
new() | ||
{ | ||
Action = "create", | ||
Content = "This is in a new branch", | ||
FilePath = "README.md", | ||
}, | ||
}, | ||
}), Throws.TypeOf<GitLabBadRequestException>() | ||
.With.Message.Contains($"A branch called '{newBranch}' already exists.")); | ||
} | ||
|
||
[Test] | ||
public void Test_create_a_new_commit_on_new_branch_with_start_branch() | ||
{ | ||
// Arrange | ||
using var server = new GitLabServer(); | ||
var user = server.Users.AddNew(); | ||
var project = user.Namespace.Projects.AddNew(project => project.Visibility = VisibilityLevel.Internal); | ||
var initCommit = project.Repository.Commit(user, "Initial commit"); | ||
|
||
var startBranch = project.DefaultBranch; | ||
var newBranch = "new-branch"; | ||
|
||
// Act & Assert | ||
var commitMessage = "First commit in new branch"; | ||
var newCommit = project.Repository.Commit(new() | ||
{ | ||
Branch = newBranch, | ||
StartBranch = startBranch, | ||
CommitMessage = commitMessage, | ||
Actions = new List<CreateCommitAction> | ||
{ | ||
new() | ||
{ | ||
Action = "create", | ||
Content = "This is in a new branch", | ||
FilePath = "README.md", | ||
}, | ||
}, | ||
}); | ||
newCommit.Message.Trim().Should().Be(commitMessage); | ||
} | ||
|
||
[Test] | ||
public void Test_create_a_new_commit_on_new_branch_with_start_sha() | ||
{ | ||
// Arrange | ||
using var server = new GitLabServer(); | ||
var user = server.Users.AddNew(); | ||
var project = user.Namespace.Projects.AddNew(project => project.Visibility = VisibilityLevel.Internal); | ||
var initCommit = project.Repository.Commit(user, "Initial commit"); | ||
|
||
var newBranch = "new-branch"; | ||
|
||
// Act & Assert | ||
var commitMessage = "First commit in new branch"; | ||
var newCommit = project.Repository.Commit(new() | ||
{ | ||
Branch = newBranch, | ||
StartSha = initCommit.Sha, | ||
CommitMessage = commitMessage, | ||
Actions = new List<CreateCommitAction> | ||
{ | ||
new() | ||
{ | ||
Action = "create", | ||
Content = "This is in a new branch", | ||
FilePath = "README.md", | ||
}, | ||
}, | ||
}); | ||
newCommit.Message.Trim().Should().Be(commitMessage); | ||
} | ||
|
||
[Test] | ||
public void Test_create_a_new_commit_on_nonexistent_branch() | ||
{ | ||
// Arrange | ||
using var server = new GitLabServer(); | ||
var user = server.Users.AddNew(); | ||
var project = user.Namespace.Projects.AddNew(project => project.Visibility = VisibilityLevel.Internal); | ||
var initCommit = project.Repository.Commit(user, "Initial commit"); | ||
|
||
// Act & Assert | ||
var commitMessage = "First commit in non existing branch"; | ||
Assert.That(() => project.Repository.Commit(new() | ||
{ | ||
Branch = "new-branch", | ||
CommitMessage = commitMessage, | ||
Actions = new List<CreateCommitAction> | ||
{ | ||
new() | ||
{ | ||
Action = "create", | ||
Content = "This is in a new branch", | ||
FilePath = "README.md", | ||
}, | ||
}, | ||
}), Throws.TypeOf<GitLabBadRequestException>() | ||
.With.Message.Contains("You can only create or edit files when you are on a branch.")); | ||
} | ||
|
||
[Test] | ||
public void Test_create_a_new_commit_on_existing_branch() | ||
{ | ||
// Arrange | ||
using var server = new GitLabServer(); | ||
var user = server.Users.AddNew(); | ||
var project = user.Namespace.Projects.AddNew(project => project.Visibility = VisibilityLevel.Internal); | ||
var initCommit = project.Repository.Commit(user, "Initial commit"); | ||
|
||
// Act & Assert | ||
var commitMessage = "First commit in existing branch"; | ||
var newCommit = project.Repository.Commit(new() | ||
{ | ||
Branch = project.DefaultBranch, | ||
CommitMessage = commitMessage, | ||
Actions = new List<CreateCommitAction> | ||
{ | ||
new() | ||
{ | ||
Action = "create", | ||
Content = "This is in an existing branch", | ||
FilePath = "README.md", | ||
}, | ||
}, | ||
}); | ||
newCommit.Message.Trim().Should().Be(commitMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters