From 1052d09edf6adcf3550bb94f1322157d41e6a0ad Mon Sep 17 00:00:00 2001 From: Mordechai Zuber Date: Thu, 4 Feb 2016 12:42:39 +0200 Subject: [PATCH 1/4] Add overloads to specify branch for ContentRequest --- Octokit/Models/Request/CreateFileRequest.cs | 62 ++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/Octokit/Models/Request/CreateFileRequest.cs b/Octokit/Models/Request/CreateFileRequest.cs index 275d6fbf30..b804ac73cf 100644 --- a/Octokit/Models/Request/CreateFileRequest.cs +++ b/Octokit/Models/Request/CreateFileRequest.cs @@ -21,6 +21,18 @@ protected ContentRequest(string message) Message = message; } + /// + /// Initializes a new instance of the class. + /// + /// The message. + /// The branch the request is for. + protected ContentRequest(string message, string branch): this(message) + { + Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); + + Branch = branch; + } + /// /// The commit message. This is required. /// @@ -54,6 +66,19 @@ public class DeleteFileRequest : ContentRequest /// The message. /// The sha. public DeleteFileRequest(string message, string sha) : base(message) + { + Ensure.ArgumentNotNullOrEmptyString(sha, "content"); + + Sha = sha; + } + + /// + /// Initializes a new instance of the class. + /// + /// The message. + /// The sha. + /// The branch the request is for. + public DeleteFileRequest(string message, string sha, string branch) : base(message, branch) { Ensure.ArgumentNotNullOrEmptyString(sha, "sha"); @@ -81,8 +106,8 @@ public class CreateFileRequest : ContentRequest /// /// Creates an instance of a . /// - /// - /// + /// The message. + /// The content. public CreateFileRequest(string message, string content) : base(message) { Ensure.ArgumentNotNull(content, "content"); @@ -90,6 +115,18 @@ public CreateFileRequest(string message, string content) : base(message) Content = content; } + /// + /// Initializes a new instance of the class. + /// + /// The message. + /// The content. + /// The branch the request is for. + public CreateFileRequest(string message, string content, string branch) : base(message, branch) + { + Ensure.ArgumentNotNullOrEmptyString(content, "content"); + + Content = content; + } /// /// The contents of the file to create. This is required. /// @@ -111,6 +148,12 @@ internal virtual string DebuggerDisplay [DebuggerDisplay("{DebuggerDisplay,nq}")] public class UpdateFileRequest : CreateFileRequest { + /// + /// Creates an instance of a . + /// + /// The message. + /// The content. + /// The sha. public UpdateFileRequest(string message, string content, string sha) : base(message, content) { @@ -119,6 +162,21 @@ public UpdateFileRequest(string message, string content, string sha) Sha = sha; } + /// + /// Creates an instance of a . + /// + /// The message. + /// The content. + /// The sha. + /// The branch the request is for. + public UpdateFileRequest(string message, string content, string sha, string branch) + : base(message, content, branch) + { + Ensure.ArgumentNotNullOrEmptyString(sha, "sha"); + + Sha = sha; + } + /// /// The blob SHA of the file being replaced. /// From 84c77464bed593da553b469f04e99975ff14cbab Mon Sep 17 00:00:00 2001 From: Mordechai Zuber Date: Mon, 8 Feb 2016 12:16:34 +0200 Subject: [PATCH 2/4] Add integration test --- .../Clients/RepositoryContentsClientTests.cs | 47 +++++++++++++++++++ Octokit/Models/Request/CreateFileRequest.cs | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs index 2cb44e7ff8..5e9ddbf7bb 100644 --- a/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs @@ -111,6 +111,53 @@ await Assert.ThrowsAsync( } } + [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( + async () => await fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt")); + } + } + [IntegrationTest(Skip = "this will probably take too long")] public async Task GetsArchiveAsTarball() { diff --git a/Octokit/Models/Request/CreateFileRequest.cs b/Octokit/Models/Request/CreateFileRequest.cs index b804ac73cf..b9529e3bff 100644 --- a/Octokit/Models/Request/CreateFileRequest.cs +++ b/Octokit/Models/Request/CreateFileRequest.cs @@ -116,7 +116,7 @@ public CreateFileRequest(string message, string content) : base(message) } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The message. /// The content. From 1bfa625f085b01e8dc85f058fae5bd0f7b5cf38a Mon Sep 17 00:00:00 2001 From: Ryan Gribble Date: Mon, 8 Feb 2016 22:46:47 +1000 Subject: [PATCH 3/4] Add unit tests for CreateFile DeleteFile and UpdateFile --- .../Clients/RepositoryContentsClientTests.cs | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/Octokit.Tests/Clients/RepositoryContentsClientTests.cs b/Octokit.Tests/Clients/RepositoryContentsClientTests.cs index 46324cbe19..85707ee9f7 100644 --- a/Octokit.Tests/Clients/RepositoryContentsClientTests.cs +++ b/Octokit.Tests/Clients/RepositoryContentsClientTests.cs @@ -89,5 +89,135 @@ public async Task ReturnsContents() Assert.Equal(1, contents.Count); } } + + public class TheCreateFileMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + 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(Arg.Is(u => u.ToString() == expectedUri), Arg.Any()); + } + + [Fact] + public void PassesRequestObject() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")); + + connection.Received().Put( + Arg.Any(), + Arg.Is(a => + a.Message == "message" + && a.Content == "myfilecontents" + && a.Branch == "mybranch")); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + await Assert.ThrowsAsync(() => client.CreateFile(null, "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"))); + await Assert.ThrowsAsync(() => client.CreateFile("org", null, "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"))); + await Assert.ThrowsAsync(() => client.CreateFile("org", "repo", null, new CreateFileRequest("message", "myfilecontents", "mybranch"))); + await Assert.ThrowsAsync(() => client.CreateFile("org", "repo", "path/to/file", null)); + } + } + + public class TheDeleteFileMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + 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(u => u.ToString() == expectedUri), Arg.Any()); + } + + [Fact] + public void PassesRequestObject() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")); + + connection.Received().Delete( + Arg.Any(), + Arg.Is(a => + a.Message == "message" + && a.Sha == "1234abc" + && a.Branch == "mybranch")); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + await Assert.ThrowsAsync(() => client.DeleteFile(null, "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.DeleteFile("org", null, "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.DeleteFile("org", "repo", null, new DeleteFileRequest("message", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.DeleteFile("org", "repo", "path/to/file", null)); + } + } + + public class TheUpdateFileMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + 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(Arg.Is(u => u.ToString() == expectedUri), Arg.Any()); + } + + [Fact] + public void PassesRequestObject() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")); + + connection.Received().Put( + Arg.Any(), + Arg.Is(a => + a.Message == "message" + && a.Content == "myfilecontents" + && a.Sha == "1234abc" + && a.Branch == "mybranch")); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + await Assert.ThrowsAsync(() => client.UpdateFile(null, "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.UpdateFile("org", null, "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.UpdateFile("org", "repo", null, new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.UpdateFile("org", "repo", "path/to/file", null)); + } + } } } \ No newline at end of file From 5cad1ab2f0aaf98dc25506549d09a3c761d99617 Mon Sep 17 00:00:00 2001 From: Mordechai Zuber Date: Thu, 11 Feb 2016 01:49:10 +0200 Subject: [PATCH 4/4] Remove unneeded async/await calls --- .../Clients/RepositoryContentsClientTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs index 5e9ddbf7bb..91c8fd915f 100644 --- a/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs @@ -107,7 +107,7 @@ await fixture.DeleteFile( new DeleteFileRequest("Deleted file", fileSha)); await Assert.ThrowsAsync( - async () => await fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt")); + () => fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt")); } } @@ -154,7 +154,7 @@ await fixture.DeleteFile( new DeleteFileRequest("Deleted file", fileSha, branchName)); await Assert.ThrowsAsync( - async () => await fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt")); + () => fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt")); } }