Merge pull request #1093 from M-Zuber/Ctor_Overload

Add overloads to specify branch for ContentRequest
This commit is contained in:
Brendan Forster
2016-02-11 14:20:28 +11:00
3 changed files with 238 additions and 3 deletions
@@ -107,7 +107,54 @@ namespace Octokit.Tests.Integration.Clients
new DeleteFileRequest("Deleted file", fileSha));
await Assert.ThrowsAsync<NotFoundException>(
async () => await fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt"));
() => fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt"));
}
}
[IntegrationTest]
public async Task CrudTestWithNamedBranch()
{
var client = Helper.GetAuthenticatedClient();
var fixture = client.Repository.Content;
var repoName = Helper.MakeNameWithTimestamp("source-repo");
var branchName = "other-branch";
using (var context = await client.CreateRepositoryContext(new NewRepository(repoName) { AutoInit = true }))
{
var repository = context.Repository;
var master = await client.Git.Reference.Get(Helper.UserName, repository.Name, "heads/master");
await client.Git.Reference.Create(Helper.UserName, repository.Name, new NewReference("refs/heads/" + branchName, master.Object.Sha));
var file = await fixture.CreateFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new CreateFileRequest("Test commit", "Some Content", branchName));
Assert.Equal("somefile.txt", file.Content.Name);
var contents = await fixture.GetAllContentsByRef(repository.Owner.Login, repository.Name, "somefile.txt", branchName);
string fileSha = contents.First().Sha;
Assert.Equal("Some Content", contents.First().Content);
var update = await fixture.UpdateFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new UpdateFileRequest("Updating file", "New Content", fileSha, branchName));
Assert.Equal("somefile.txt", update.Content.Name);
contents = await fixture.GetAllContentsByRef(repository.Owner.Login, repository.Name, "somefile.txt", branchName);
Assert.Equal("New Content", contents.First().Content);
fileSha = contents.First().Sha;
await fixture.DeleteFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new DeleteFileRequest("Deleted file", fileSha, branchName));
await Assert.ThrowsAsync<NotFoundException>(
() => fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt"));
}
}
@@ -89,5 +89,135 @@ namespace Octokit.Tests.Clients
Assert.Equal(1, contents.Count);
}
}
public class TheCreateFileMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);
string expectedUri = "repos/org/repo/contents/path/to/file";
client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));
connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
}
[Fact]
public void PassesRequestObject()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);
client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));
connection.Received().Put<RepositoryContentChangeSet>(
Arg.Any<Uri>(),
Arg.Is<CreateFileRequest>(a =>
a.Message == "message"
&& a.Content == "myfilecontents"
&& a.Branch == "mybranch"));
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);
await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile(null, "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile("org", null, "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile("org", "repo", null, new CreateFileRequest("message", "myfilecontents", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile("org", "repo", "path/to/file", null));
}
}
public class TheDeleteFileMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);
string expectedUri = "repos/org/repo/contents/path/to/file";
client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));
connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
}
[Fact]
public void PassesRequestObject()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);
client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));
connection.Received().Delete(
Arg.Any<Uri>(),
Arg.Is<DeleteFileRequest>(a =>
a.Message == "message"
&& a.Sha == "1234abc"
&& a.Branch == "mybranch"));
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);
await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile(null, "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile("org", null, "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile("org", "repo", null, new DeleteFileRequest("message", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile("org", "repo", "path/to/file", null));
}
}
public class TheUpdateFileMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);
string expectedUri = "repos/org/repo/contents/path/to/file";
client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));
connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
}
[Fact]
public void PassesRequestObject()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);
client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));
connection.Received().Put<RepositoryContentChangeSet>(
Arg.Any<Uri>(),
Arg.Is<UpdateFileRequest>(a =>
a.Message == "message"
&& a.Content == "myfilecontents"
&& a.Sha == "1234abc"
&& a.Branch == "mybranch"));
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile(null, "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile("org", null, "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile("org", "repo", null, new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile("org", "repo", "path/to/file", null));
}
}
}
}
+60 -2
View File
@@ -21,6 +21,18 @@ namespace Octokit
Message = message;
}
/// <summary>
/// Initializes a new instance of the <see cref="ContentRequest"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="branch">The branch the request is for.</param>
protected ContentRequest(string message, string branch): this(message)
{
Ensure.ArgumentNotNullOrEmptyString(branch, "branch");
Branch = branch;
}
/// <summary>
/// The commit message. This is required.
/// </summary>
@@ -54,6 +66,19 @@ namespace Octokit
/// <param name="message">The message.</param>
/// <param name="sha">The sha.</param>
public DeleteFileRequest(string message, string sha) : base(message)
{
Ensure.ArgumentNotNullOrEmptyString(sha, "content");
Sha = sha;
}
/// <summary>
/// Initializes a new instance of the <see cref="DeleteFileRequest"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="sha">The sha.</param>
/// <param name="branch">The branch the request is for.</param>
public DeleteFileRequest(string message, string sha, string branch) : base(message, branch)
{
Ensure.ArgumentNotNullOrEmptyString(sha, "sha");
@@ -81,8 +106,8 @@ namespace Octokit
/// <summary>
/// Creates an instance of a <see cref="CreateFileRequest" />.
/// </summary>
/// <param name="message"></param>
/// <param name="content"></param>
/// <param name="message">The message.</param>
/// <param name="content">The content.</param>
public CreateFileRequest(string message, string content) : base(message)
{
Ensure.ArgumentNotNull(content, "content");
@@ -90,6 +115,18 @@ namespace Octokit
Content = content;
}
/// <summary>
/// Initializes a new instance of the <see cref="CreateFileRequest"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="content">The content.</param>
/// <param name="branch">The branch the request is for.</param>
public CreateFileRequest(string message, string content, string branch) : base(message, branch)
{
Ensure.ArgumentNotNullOrEmptyString(content, "content");
Content = content;
}
/// <summary>
/// The contents of the file to create. This is required.
/// </summary>
@@ -111,6 +148,12 @@ namespace Octokit
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class UpdateFileRequest : CreateFileRequest
{
/// <summary>
/// Creates an instance of a <see cref="UpdateFileRequest" />.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="content">The content.</param>
/// <param name="sha">The sha.</param>
public UpdateFileRequest(string message, string content, string sha)
: base(message, content)
{
@@ -119,6 +162,21 @@ namespace Octokit
Sha = sha;
}
/// <summary>
/// Creates an instance of a <see cref="UpdateFileRequest" />.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="content">The content.</param>
/// <param name="sha">The sha.</param>
/// <param name="branch">The branch the request is for.</param>
public UpdateFileRequest(string message, string content, string sha, string branch)
: base(message, content, branch)
{
Ensure.ArgumentNotNullOrEmptyString(sha, "sha");
Sha = sha;
}
/// <summary>
/// The blob SHA of the file being replaced.
/// </summary>