diff --git a/Octokit.Tests.Integration/Clients/AssigneesClientTests.cs b/Octokit.Tests.Integration/Clients/AssigneesClientTests.cs index e902b52560..9869c24ed9 100644 --- a/Octokit.Tests.Integration/Clients/AssigneesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/AssigneesClientTests.cs @@ -2,33 +2,32 @@ using System.Threading.Tasks; using Octokit; using Octokit.Tests.Integration; +using Octokit.Tests.Integration.Helpers; using Xunit; public class AssigneesClientTests { - readonly IGitHubClient _gitHubClient; - readonly Repository _repository; - readonly string _owner; + readonly IGitHubClient _github; + readonly RepositoryContext _context; public AssigneesClientTests() { - _gitHubClient = Helper.GetAuthenticatedClient(); + _github = Helper.GetAuthenticatedClient(); var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = _gitHubClient.Repository.Create(new NewRepository(repoName)).Result; - _owner = _repository.Owner.Login; + _context = _github.CreateRepositoryContext(new NewRepository(repoName)).Result; } [IntegrationTest] public async Task CanCheckAssignees() { var isAssigned = await - _gitHubClient.Issue.Assignee.CheckAssignee(_owner, _repository.Name, "FakeHaacked"); + _github.Issue.Assignee.CheckAssignee(_context.RepositoryOwner, _context.RepositoryName, "FakeHaacked"); Assert.False(isAssigned); - + // Repository owner is always an assignee isAssigned = await - _gitHubClient.Issue.Assignee.CheckAssignee(_owner, _repository.Name, _owner); + _github.Issue.Assignee.CheckAssignee(_context.RepositoryOwner, _context.RepositoryName, _context.RepositoryOwner); Assert.True(isAssigned); } @@ -36,12 +35,7 @@ public async Task CanCheckAssignees() public async Task CanListAssignees() { // Repository owner is always an assignee - var assignees = await _gitHubClient.Issue.Assignee.GetAllForRepository(_owner, _repository.Name); + var assignees = await _github.Issue.Assignee.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName); Assert.True(assignees.Any(u => u.Login == Helper.UserName)); } - - public void Dispose() - { - Helper.DeleteRepo(_repository); - } -} +} \ No newline at end of file diff --git a/Octokit.Tests.Integration/Clients/AuthorizationClientTests.cs b/Octokit.Tests.Integration/Clients/AuthorizationClientTests.cs index 2c9a018c38..213326e124 100644 --- a/Octokit.Tests.Integration/Clients/AuthorizationClientTests.cs +++ b/Octokit.Tests.Integration/Clients/AuthorizationClientTests.cs @@ -10,14 +10,14 @@ public class AuthorizationClientTests [ApplicationTest] public async Task CanCreateAndGetAuthorizationWithoutFingerPrint() { - var client = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); var note = Helper.MakeNameWithTimestamp("Testing authentication"); var newAuthorization = new NewAuthorization( note, new[] { "user" }); // the first call will create the authorization - var created = await client.Authorization.GetOrCreateApplicationAuthentication( + var created = await github.Authorization.GetOrCreateApplicationAuthentication( Helper.ClientId, Helper.ClientSecret, newAuthorization); @@ -27,14 +27,14 @@ public async Task CanCreateAndGetAuthorizationWithoutFingerPrint() Assert.False(String.IsNullOrWhiteSpace(created.HashedToken)); // we can then query it through the regular API - var get = await client.Authorization.Get(created.Id); + var get = await github.Authorization.Get(created.Id); Assert.Equal(created.Id, get.Id); Assert.Equal(created.Note, get.Note); // but the second time we call this API we get // a different set of data - var getExisting = await client.Authorization.GetOrCreateApplicationAuthentication( + var getExisting = await github.Authorization.GetOrCreateApplicationAuthentication( Helper.ClientId, Helper.ClientSecret, newAuthorization); @@ -47,13 +47,13 @@ public async Task CanCreateAndGetAuthorizationWithoutFingerPrint() Assert.False(String.IsNullOrWhiteSpace(getExisting.TokenLastEight)); Assert.False(String.IsNullOrWhiteSpace(getExisting.HashedToken)); - await client.Authorization.Delete(created.Id); + await github.Authorization.Delete(created.Id); } [ApplicationTest] public async Task CanCreateAndGetAuthorizationByFingerprint() { - var client = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing"); var note = Helper.MakeNameWithTimestamp("Testing authentication"); var newAuthorization = new NewAuthorization( @@ -61,7 +61,7 @@ public async Task CanCreateAndGetAuthorizationByFingerprint() new[] { "user" }, fingerprint); - var created = await client.Authorization.GetOrCreateApplicationAuthentication( + var created = await github.Authorization.GetOrCreateApplicationAuthentication( Helper.ClientId, Helper.ClientSecret, newAuthorization); @@ -70,14 +70,14 @@ public async Task CanCreateAndGetAuthorizationByFingerprint() Assert.False(String.IsNullOrWhiteSpace(created.Token)); // we can then query it through the regular API - var get = await client.Authorization.Get(created.Id); + var get = await github.Authorization.Get(created.Id); Assert.Equal(created.Id, get.Id); Assert.Equal(created.Note, get.Note); // but the second time we call this API we get // a different set of data - var getExisting = await client.Authorization.GetOrCreateApplicationAuthentication( + var getExisting = await github.Authorization.GetOrCreateApplicationAuthentication( Helper.ClientId, Helper.ClientSecret, newAuthorization); @@ -93,13 +93,13 @@ public async Task CanCreateAndGetAuthorizationByFingerprint() Assert.False(String.IsNullOrWhiteSpace(getExisting.TokenLastEight)); Assert.False(String.IsNullOrWhiteSpace(getExisting.HashedToken)); - await client.Authorization.Delete(created.Id); + await github.Authorization.Delete(created.Id); } [ApplicationTest] public async Task CanCheckApplicationAuthentication() { - var client = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing"); var note = Helper.MakeNameWithTimestamp("Testing authentication"); var newAuthorization = new NewAuthorization( @@ -107,7 +107,7 @@ public async Task CanCheckApplicationAuthentication() new[] { "user" }, fingerprint); - var created = await client.Authorization.GetOrCreateApplicationAuthentication( + var created = await github.Authorization.GetOrCreateApplicationAuthentication( Helper.ClientId, Helper.ClientSecret, newAuthorization); @@ -118,14 +118,14 @@ public async Task CanCheckApplicationAuthentication() Assert.NotNull(applicationAuthorization); Assert.Equal(created.Token, applicationAuthorization.Token); - await client.Authorization.Delete(created.Id); - Assert.ThrowsAsync(() => client.Authorization.Get(created.Id)); + await github.Authorization.Delete(created.Id); + Assert.ThrowsAsync(() => github.Authorization.Get(created.Id)); } [ApplicationTest] public async Task CanResetApplicationAuthentication() { - var client = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing"); var note = Helper.MakeNameWithTimestamp("Testing authentication"); var newAuthorization = new NewAuthorization( @@ -133,7 +133,7 @@ public async Task CanResetApplicationAuthentication() new[] { "user" }, fingerprint); - var created = await client.Authorization.GetOrCreateApplicationAuthentication( + var created = await github.Authorization.GetOrCreateApplicationAuthentication( Helper.ClientId, Helper.ClientSecret, newAuthorization); @@ -144,14 +144,14 @@ public async Task CanResetApplicationAuthentication() Assert.NotNull(applicationAuthorization); Assert.NotEqual(created.Token, applicationAuthorization.Token); - await client.Authorization.Delete(created.Id); - Assert.ThrowsAsync(() => client.Authorization.Get(created.Id)); + await github.Authorization.Delete(created.Id); + Assert.ThrowsAsync(() => github.Authorization.Get(created.Id)); } [ApplicationTest] public async Task CanRevokeApplicationAuthentication() { - var client = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing"); var note = Helper.MakeNameWithTimestamp("Testing authentication"); var newAuthorization = new NewAuthorization( @@ -159,7 +159,7 @@ public async Task CanRevokeApplicationAuthentication() new[] { "user" }, fingerprint); - var created = await client.Authorization.GetOrCreateApplicationAuthentication( + var created = await github.Authorization.GetOrCreateApplicationAuthentication( Helper.ClientId, Helper.ClientSecret, newAuthorization); @@ -168,17 +168,17 @@ public async Task CanRevokeApplicationAuthentication() await applicationClient.Authorization.RevokeApplicationAuthentication(Helper.ClientId, created.Token); Assert.ThrowsAsync(() => applicationClient.Authorization.CheckApplicationAuthentication(Helper.ClientId, created.Token)); - Assert.ThrowsAsync(() => client.Authorization.Get(created.Id)); + Assert.ThrowsAsync(() => github.Authorization.Get(created.Id)); } [ApplicationTest] public async Task CanRevokeAllApplicationAuthentications() { - var client = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing"); var note = Helper.MakeNameWithTimestamp("Testing authentication"); - var token1 = await client.Authorization.GetOrCreateApplicationAuthentication( + var token1 = await github.Authorization.GetOrCreateApplicationAuthentication( Helper.ClientId, Helper.ClientSecret, new NewAuthorization( @@ -188,7 +188,7 @@ public async Task CanRevokeAllApplicationAuthentications() fingerprint = Helper.MakeNameWithTimestamp("authorization-testing-2"); note = Helper.MakeNameWithTimestamp("Testing authentication 2"); - var token2 = await client.Authorization.GetOrCreateApplicationAuthentication( + var token2 = await github.Authorization.GetOrCreateApplicationAuthentication( Helper.ClientId, Helper.ClientSecret, new NewAuthorization( @@ -204,8 +204,8 @@ public async Task CanRevokeAllApplicationAuthentications() Assert.ThrowsAsync(async () => await applicationClient.Authorization.CheckApplicationAuthentication(Helper.ClientId, token2.Token)); - Assert.ThrowsAsync(() => client.Authorization.Get(token1.Id)); - Assert.ThrowsAsync(() => client.Authorization.Get(token2.Id)); + Assert.ThrowsAsync(() => github.Authorization.Get(token1.Id)); + Assert.ThrowsAsync(() => github.Authorization.Get(token2.Id)); } } } diff --git a/Octokit.Tests.Integration/Clients/BlobClientTests.cs b/Octokit.Tests.Integration/Clients/BlobClientTests.cs index 74195ecc98..7b82bba929 100644 --- a/Octokit.Tests.Integration/Clients/BlobClientTests.cs +++ b/Octokit.Tests.Integration/Clients/BlobClientTests.cs @@ -4,21 +4,19 @@ using Octokit.Tests.Integration; using System.Threading.Tasks; using Xunit; +using Octokit.Tests.Integration.Helpers; public class BlobClientTests : IDisposable { - readonly IBlobsClient _fixture; - readonly Repository _repository; - readonly string _owner; + private readonly IBlobsClient _fixture; + private readonly RepositoryContext _context; public BlobClientTests() { - var client = Helper.GetAuthenticatedClient(); - _fixture = client.GitDatabase.Blob; + var github = Helper.GetAuthenticatedClient(); + _fixture = github.GitDatabase.Blob; - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = client.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - _owner = _repository.Owner.Login; + _context = github.CreateRepositoryContext("public-repo").Result; } [IntegrationTest] @@ -30,7 +28,7 @@ public async Task CanCreateABlob() Encoding = EncodingType.Utf8 }; - var result = await _fixture.Create(_owner, _repository.Name, blob); + var result = await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, blob); Assert.False(String.IsNullOrWhiteSpace(result.Sha)); } @@ -47,7 +45,7 @@ public async Task CanCreateABlobWithBase64Contents() Encoding = EncodingType.Base64 }; - var result = await _fixture.Create(_owner, _repository.Name, blob); + var result = await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, blob); Assert.False(String.IsNullOrWhiteSpace(result.Sha)); } @@ -61,8 +59,8 @@ public async Task CanGetABlob() Encoding = EncodingType.Utf8 }; - var result = await _fixture.Create(_owner, _repository.Name, newBlob); - var blob = await _fixture.Get(_owner, _repository.Name, result.Sha); + var result = await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, newBlob); + var blob = await _fixture.Get(_context.RepositoryOwner, _context.RepositoryName, result.Sha); Assert.Equal(result.Sha, blob.Sha); Assert.Equal(EncodingType.Base64, blob.Encoding); @@ -84,8 +82,8 @@ public async Task CanGetABlobWithBase64Text() Encoding = EncodingType.Base64 }; - var result = await _fixture.Create(_owner, _repository.Name, newBlob); - var blob = await _fixture.Get(_owner, _repository.Name, result.Sha); + var result = await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, newBlob); + var blob = await _fixture.Get(_context.RepositoryOwner, _context.RepositoryName, result.Sha); Assert.Equal(result.Sha, blob.Sha); Assert.Equal(EncodingType.Base64, blob.Encoding); @@ -98,6 +96,6 @@ public async Task CanGetABlobWithBase64Text() public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/BranchesClientTests.cs b/Octokit.Tests.Integration/Clients/BranchesClientTests.cs index f26ef1ea66..74507996ce 100644 --- a/Octokit.Tests.Integration/Clients/BranchesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/BranchesClientTests.cs @@ -2,34 +2,27 @@ using System.Threading.Tasks; using Octokit; using Octokit.Tests.Integration; +using Octokit.Tests.Integration.Helpers; using Xunit; public class BranchesClientTests { - public class TheGetBranchesMethod : IDisposable + public class TheGetBranchesMethod { - readonly Repository _repository; - readonly IGitHubClient _github; - - public TheGetBranchesMethod() - { - _github = Helper.GetAuthenticatedClient(); - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = _github.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - } + public TheGetBranchesMethod() { } [IntegrationTest] public async Task ReturnsBranches() { - var branches = await _github.Repository.GetAllBranches(_repository.Owner.Login, _repository.Name); - - Assert.NotEmpty(branches); - Assert.Equal(branches[0].Name, "master"); - } + var github = Helper.GetAuthenticatedClient(); - public void Dispose() - { - Helper.DeleteRepo(_repository); + using (var context = await github.CreateRepositoryContext("public-repo")) + { + var branches = await github.Repository.GetAllBranches(context.Repository.Owner.Login, context.Repository.Name); + + Assert.NotEmpty(branches); + Assert.Equal(branches[0].Name, "master"); + } } } } \ No newline at end of file diff --git a/Octokit.Tests.Integration/Clients/CommitStatusClientTests.cs b/Octokit.Tests.Integration/Clients/CommitStatusClientTests.cs index 894b412f28..a3fe99c0f4 100644 --- a/Octokit.Tests.Integration/Clients/CommitStatusClientTests.cs +++ b/Octokit.Tests.Integration/Clients/CommitStatusClientTests.cs @@ -4,6 +4,7 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class CommitStatusClientTests { @@ -15,8 +16,8 @@ public async Task CanRetrieveStatuses() // Figured it was easier to grab the public status of a public repository for now than // to go through the rigamarole of creating it all. But ideally, that's exactly what we'd do. - var githubClient = Helper.GetAuthenticatedClient(); - var statuses = await githubClient.Repository.CommitStatus.GetAll( + var github = Helper.GetAuthenticatedClient(); + var statuses = await github.Repository.CommitStatus.GetAll( "rails", "rails", "94b857899506612956bb542e28e292308accb908"); @@ -31,8 +32,8 @@ public class TheGetCombinedMethod [IntegrationTest] public async Task CanRetrieveCombinedStatus() { - var githubClient = Helper.GetAuthenticatedClient(); - var status = await githubClient.Repository.CommitStatus.GetCombined( + var github = Helper.GetAuthenticatedClient(); + var status = await github.Repository.CommitStatus.GetCombined( "libgit2", "libgit2sharp", "f54529997b6ad841be524654d9e9074ab8e7d41d"); @@ -47,23 +48,20 @@ public async Task CanRetrieveCombinedStatus() public class TheCreateMethod : IDisposable { - readonly IGitHubClient _client; - readonly Repository _repository; - readonly string _owner; + private readonly IGitHubClient _github; + private readonly RepositoryContext _context; public TheCreateMethod() { - _client = Helper.GetAuthenticatedClient(); + _github = Helper.GetAuthenticatedClient(); - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = _client.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - _owner = _repository.Owner.Login; + _context = _github.CreateRepositoryContext("public-repo").Result; } [IntegrationTest] public async Task CanAssignPendingToCommit() { - var commit = await SetupCommitForRepository(_client); + var commit = await SetupCommitForRepository(_github); var status = new NewCommitStatus { @@ -71,7 +69,7 @@ public async Task CanAssignPendingToCommit() Description = "this is a test status" }; - var result = await _client.Repository.CommitStatus.Create(_owner, _repository.Name, commit.Sha, status); + var result = await _github.Repository.CommitStatus.Create(_context.RepositoryOwner, _context.RepositoryName, commit.Sha, status); Assert.Equal(CommitState.Pending, result.State); } @@ -79,7 +77,7 @@ public async Task CanAssignPendingToCommit() [IntegrationTest] public async Task CanRetrievePendingStatus() { - var commit = await SetupCommitForRepository(_client); + var commit = await SetupCommitForRepository(_github); var status = new NewCommitStatus { @@ -87,9 +85,9 @@ public async Task CanRetrievePendingStatus() Description = "this is a test status" }; - await _client.Repository.CommitStatus.Create(_owner, _repository.Name, commit.Sha, status); + await _github.Repository.CommitStatus.Create(_context.RepositoryOwner, _context.RepositoryName, commit.Sha, status); - var statuses = await _client.Repository.CommitStatus.GetAll(_owner, _repository.Name, commit.Sha); + var statuses = await _github.Repository.CommitStatus.GetAll(_context.RepositoryOwner, _context.RepositoryName, commit.Sha); Assert.Equal(1, statuses.Count); Assert.Equal(CommitState.Pending, statuses[0].State); @@ -98,7 +96,7 @@ public async Task CanRetrievePendingStatus() [IntegrationTest] public async Task CanUpdatePendingStatusToSuccess() { - var commit = await SetupCommitForRepository(_client); + var commit = await SetupCommitForRepository(_github); var status = new NewCommitStatus { @@ -106,13 +104,13 @@ public async Task CanUpdatePendingStatusToSuccess() Description = "this is a test status" }; - await _client.Repository.CommitStatus.Create(_owner, _repository.Name, commit.Sha, status); + await _github.Repository.CommitStatus.Create(_context.RepositoryOwner, _context.RepositoryName, commit.Sha, status); status.State = CommitState.Success; - await _client.Repository.CommitStatus.Create(_owner, _repository.Name, commit.Sha, status); + await _github.Repository.CommitStatus.Create(_context.RepositoryOwner, _context.RepositoryName, commit.Sha, status); - var statuses = await _client.Repository.CommitStatus.GetAll(_owner, _repository.Name, commit.Sha); + var statuses = await _github.Repository.CommitStatus.GetAll(_context.RepositoryOwner, _context.RepositoryName, commit.Sha); Assert.Equal(2, statuses.Count); Assert.Equal(CommitState.Success, statuses[0].State); @@ -121,7 +119,7 @@ public async Task CanUpdatePendingStatusToSuccess() [IntegrationTest] public async Task CanProvideACommitStatusWithoutRequiringAContext() { - var commit = await SetupCommitForRepository(_client); + var commit = await SetupCommitForRepository(_github); var status = new NewCommitStatus { @@ -129,9 +127,9 @@ public async Task CanProvideACommitStatusWithoutRequiringAContext() Description = "this is a test status" }; - await _client.Repository.CommitStatus.Create(_owner, _repository.Name, commit.Sha, status); + await _github.Repository.CommitStatus.Create(_context.RepositoryOwner, _context.RepositoryName, commit.Sha, status); - var statuses = await _client.Repository.CommitStatus.GetAll(_owner, _repository.Name, commit.Sha); + var statuses = await _github.Repository.CommitStatus.GetAll(_context.RepositoryOwner, _context.RepositoryName, commit.Sha); Assert.Equal(1, statuses.Count); Assert.Equal("default", statuses[0].Context); @@ -140,7 +138,7 @@ public async Task CanProvideACommitStatusWithoutRequiringAContext() [IntegrationTest] public async Task CanCreateStatusesForDifferentContexts() { - var commit = await SetupCommitForRepository(_client); + var commit = await SetupCommitForRepository(_github); var status = new NewCommitStatus { @@ -149,27 +147,27 @@ public async Task CanCreateStatusesForDifferentContexts() Context = "System A" }; - await _client.Repository.CommitStatus.Create(_owner, _repository.Name, commit.Sha, status); + await _github.Repository.CommitStatus.Create(_context.RepositoryOwner, _context.RepositoryName, commit.Sha, status); status.Context = "System B"; - await _client.Repository.CommitStatus.Create(_owner, _repository.Name, commit.Sha, status); + await _github.Repository.CommitStatus.Create(_context.RepositoryOwner, _context.RepositoryName, commit.Sha, status); - var statuses = await _client.Repository.CommitStatus.GetAll(_owner, _repository.Name, commit.Sha); + var statuses = await _github.Repository.CommitStatus.GetAll(_context.RepositoryOwner, _context.RepositoryName, commit.Sha); Assert.Equal(2, statuses.Count); Assert.Equal("System B", statuses[0].Context); Assert.Equal("System A", statuses[1].Context); } - async Task SetupCommitForRepository(IGitHubClient client) + private async Task SetupCommitForRepository(IGitHubClient client) { var blob = new NewBlob { Content = "Hello World!", Encoding = EncodingType.Utf8 }; - var blobResult = await client.GitDatabase.Blob.Create(_owner, _repository.Name, blob); + var blobResult = await client.GitDatabase.Blob.Create(_context.RepositoryOwner, _context.RepositoryName, blob); var newTree = new NewTree(); newTree.Tree.Add(new NewTreeItem @@ -180,16 +178,16 @@ async Task SetupCommitForRepository(IGitHubClient client) Sha = blobResult.Sha }); - var treeResult = await client.GitDatabase.Tree.Create(_owner, _repository.Name, newTree); + var treeResult = await client.GitDatabase.Tree.Create(_context.RepositoryOwner, _context.RepositoryName, newTree); var newCommit = new NewCommit("test-commit", treeResult.Sha); - return await client.GitDatabase.Commit.Create(_owner, _repository.Name, newCommit); + return await client.GitDatabase.Commit.Create(_context.RepositoryOwner, _context.RepositoryName, newCommit); } public void Dispose() { - _client.Repository.Delete(_owner, _repository.Name).Wait(); + _context.Dispose(); } } } diff --git a/Octokit.Tests.Integration/Clients/CommitsClientTests.cs b/Octokit.Tests.Integration/Clients/CommitsClientTests.cs index 4a5d891184..2e781c87c3 100644 --- a/Octokit.Tests.Integration/Clients/CommitsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/CommitsClientTests.cs @@ -1,58 +1,49 @@ using System; using System.Threading.Tasks; using Octokit; +using Octokit.Tests.Integration.Helpers; using Octokit.Tests.Integration; using Xunit; -public class CommitsClientTests : IDisposable +public class CommitsClientTests { - readonly IGitHubClient _client; - readonly Repository _repository; - readonly ICommitsClient _fixture; - readonly string _owner; - - public CommitsClientTests() - { - _client = Helper.GetAuthenticatedClient(); - - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _fixture = _client.GitDatabase.Commit; - _repository = _client.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - _owner = _repository.Owner.Login; - } + public CommitsClientTests() { } [IntegrationTest] public async Task CanCreateAndRetrieveCommit() { - var blob = new NewBlob + var github = Helper.GetAuthenticatedClient(); + var fixture = github.GitDatabase.Commit; + + using (var context = await github.CreateRepositoryContext("public-repo")) { - Content = "Hello World!", - Encoding = EncodingType.Utf8 - }; - var blobResult = await _client.GitDatabase.Blob.Create(_owner, _repository.Name, blob); - - var newTree = new NewTree(); - newTree.Tree.Add(new NewTreeItem - { - Type = TreeType.Blob, - Mode = FileMode.File, - Path = "README.md", - Sha = blobResult.Sha - }); - - var treeResult = await _client.GitDatabase.Tree.Create(_owner, _repository.Name, newTree); - - var newCommit = new NewCommit("test-commit", treeResult.Sha); - - var commit = await _fixture.Create(_owner, _repository.Name, newCommit); - Assert.NotNull(commit); - - var retrieved = await _fixture.Get(_owner, _repository.Name, commit.Sha); - Assert.NotNull(retrieved); - } - - public void Dispose() - { - Helper.DeleteRepo(_repository); + var owner = context.Repository.Owner.Login; + + var blob = new NewBlob + { + Content = "Hello World!", + Encoding = EncodingType.Utf8 + }; + var blobResult = await github.GitDatabase.Blob.Create(owner, context.Repository.Name, blob); + + var newTree = new NewTree(); + newTree.Tree.Add(new NewTreeItem + { + Type = TreeType.Blob, + Mode = FileMode.File, + Path = "README.md", + Sha = blobResult.Sha + }); + + var treeResult = await github.GitDatabase.Tree.Create(owner, context.Repository.Name, newTree); + + var newCommit = new NewCommit("test-commit", treeResult.Sha); + + var commit = await fixture.Create(owner, context.Repository.Name, newCommit); + Assert.NotNull(commit); + + var retrieved = await fixture.Get(owner, context.Repository.Name, commit.Sha); + Assert.NotNull(retrieved); + } } -} +} \ No newline at end of file diff --git a/Octokit.Tests.Integration/Clients/DeploymentStatusClientTests.cs b/Octokit.Tests.Integration/Clients/DeploymentStatusClientTests.cs index 0ab5edd67f..302c83508f 100644 --- a/Octokit.Tests.Integration/Clients/DeploymentStatusClientTests.cs +++ b/Octokit.Tests.Integration/Clients/DeploymentStatusClientTests.cs @@ -3,26 +3,20 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class DeploymentStatusClientTests : IDisposable { - readonly IDeploymentsClient _deploymentsClient; - readonly Repository _repository; - readonly Deployment _deployment; - readonly string _repositoryOwner; + private readonly IDeploymentsClient _deploymentsClient; + private readonly RepositoryContext _context; + private readonly Deployment _deployment; public DeploymentStatusClientTests() { - var gitHubClient = Helper.GetAuthenticatedClient(); - _deploymentsClient = gitHubClient.Repository.Deployment; + var github = Helper.GetAuthenticatedClient(); - var newRepository = new NewRepository(Helper.MakeNameWithTimestamp("public-repo")) - { - AutoInit = true - }; - - _repository = gitHubClient.Repository.Create(newRepository).Result; - _repositoryOwner = _repository.Owner.Login; + _deploymentsClient = github.Repository.Deployment; + _context = github.CreateRepositoryContext("public-repo").Result; var blob = new NewBlob { @@ -30,7 +24,7 @@ public DeploymentStatusClientTests() Encoding = EncodingType.Utf8 }; - var blobResult = gitHubClient.GitDatabase.Blob.Create(_repositoryOwner, _repository.Name, blob).Result; + var blobResult = github.GitDatabase.Blob.Create(_context.RepositoryOwner, _context.RepositoryName, blob).Result; var newTree = new NewTree(); newTree.Tree.Add(new NewTreeItem @@ -41,12 +35,13 @@ public DeploymentStatusClientTests() Sha = blobResult.Sha }); - var treeResult = gitHubClient.GitDatabase.Tree.Create(_repositoryOwner, _repository.Name, newTree).Result; + var treeResult = github.GitDatabase.Tree.Create(_context.RepositoryOwner, _context.RepositoryName, newTree).Result; var newCommit = new NewCommit("test-commit", treeResult.Sha); - var commit = gitHubClient.GitDatabase.Commit.Create(_repositoryOwner, _repository.Name, newCommit).Result; + + var commit = github.GitDatabase.Commit.Create(_context.RepositoryOwner, _context.RepositoryName, newCommit).Result; var newDeployment = new NewDeployment(commit.Sha) { AutoMerge = false }; - _deployment = _deploymentsClient.Create(_repositoryOwner, _repository.Name, newDeployment).Result; + _deployment = _deploymentsClient.Create(_context.RepositoryOwner, _context.RepositoryName, newDeployment).Result; } [IntegrationTest] @@ -54,7 +49,7 @@ public async Task CanCreateDeploymentStatus() { var newStatus = new NewDeploymentStatus(DeploymentState.Success); - var status = await _deploymentsClient.Status.Create(_repositoryOwner, _repository.Name, _deployment.Id, newStatus); + var status = await _deploymentsClient.Status.Create(_context.RepositoryOwner, _context.RepositoryName, _deployment.Id, newStatus); Assert.NotNull(status); Assert.Equal(DeploymentState.Success, status.State); @@ -64,9 +59,9 @@ public async Task CanCreateDeploymentStatus() public async Task CanReadDeploymentStatuses() { var newStatus = new NewDeploymentStatus(DeploymentState.Success); - await _deploymentsClient.Status.Create(_repositoryOwner, _repository.Name, _deployment.Id, newStatus); + await _deploymentsClient.Status.Create(_context.RepositoryOwner, _context.RepositoryName, _deployment.Id, newStatus); - var statuses = await _deploymentsClient.Status.GetAll(_repositoryOwner, _repository.Name, _deployment.Id); + var statuses = await _deploymentsClient.Status.GetAll(_context.RepositoryOwner, _context.RepositoryName, _deployment.Id); Assert.NotEmpty(statuses); Assert.Equal(DeploymentState.Success, statuses[0].State); @@ -74,7 +69,7 @@ public async Task CanReadDeploymentStatuses() public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/DeploymentsClientTests.cs b/Octokit.Tests.Integration/Clients/DeploymentsClientTests.cs index 00743ecd6b..46945a6ffd 100644 --- a/Octokit.Tests.Integration/Clients/DeploymentsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/DeploymentsClientTests.cs @@ -3,27 +3,20 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class DeploymentsClientTests : IDisposable { - readonly IDeploymentsClient _deploymentsClient; - readonly Repository _repository; - readonly Commit _commit; - readonly string _repositoryOwner; + private readonly IDeploymentsClient _deploymentsClient; + private readonly RepositoryContext _context; + private readonly Commit _commit; public DeploymentsClientTests() { - var gitHubClient = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); - _deploymentsClient = gitHubClient.Repository.Deployment; - - var newRepository = new NewRepository(Helper.MakeNameWithTimestamp("public-repo")) - { - AutoInit = true - }; - - _repository = gitHubClient.Repository.Create(newRepository).Result; - _repositoryOwner = _repository.Owner.Login; + _deploymentsClient = github.Repository.Deployment; + _context = github.CreateRepositoryContext("public-repo").Result; var blob = new NewBlob { @@ -31,7 +24,7 @@ public DeploymentsClientTests() Encoding = EncodingType.Utf8 }; - var blobResult = gitHubClient.GitDatabase.Blob.Create(_repositoryOwner, _repository.Name, blob).Result; + var blobResult = github.GitDatabase.Blob.Create(_context.RepositoryOwner, _context.RepositoryName, blob).Result; var newTree = new NewTree(); newTree.Tree.Add(new NewTreeItem @@ -42,9 +35,9 @@ public DeploymentsClientTests() Sha = blobResult.Sha }); - var treeResult = gitHubClient.GitDatabase.Tree.Create(_repositoryOwner, _repository.Name, newTree).Result; + var treeResult = github.GitDatabase.Tree.Create(_context.RepositoryOwner, _context.RepositoryName, newTree).Result; var newCommit = new NewCommit("test-commit", treeResult.Sha); - _commit = gitHubClient.GitDatabase.Commit.Create(_repositoryOwner, _repository.Name, newCommit).Result; + _commit = github.GitDatabase.Commit.Create(_context.RepositoryOwner, _context.RepositoryName, newCommit).Result; } [IntegrationTest] @@ -52,7 +45,7 @@ public async Task CanCreateDeployment() { var newDeployment = new NewDeployment(_commit.Sha) { AutoMerge = false }; - var deployment = await _deploymentsClient.Create(_repositoryOwner, _repository.Name, newDeployment); + var deployment = await _deploymentsClient.Create(_context.RepositoryOwner, _context.RepositoryName, newDeployment); Assert.NotNull(deployment); } @@ -61,15 +54,15 @@ public async Task CanCreateDeployment() public async Task CanGetDeployments() { var newDeployment = new NewDeployment(_commit.Sha) { AutoMerge = false }; - await _deploymentsClient.Create(_repositoryOwner, _repository.Name, newDeployment); - - var deployments = await _deploymentsClient.GetAll(_repositoryOwner, _repository.Name); + await _deploymentsClient.Create(_context.RepositoryOwner, _context.RepositoryName, newDeployment); + + var deployments = await _deploymentsClient.GetAll(_context.RepositoryOwner, _context.RepositoryName); Assert.NotEmpty(deployments); } public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/GistsClientTests.cs b/Octokit.Tests.Integration/Clients/GistsClientTests.cs index 5b3c0ed085..56ef19f1f6 100644 --- a/Octokit.Tests.Integration/Clients/GistsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/GistsClientTests.cs @@ -13,9 +13,9 @@ public class GistsClientTests public GistsClientTests() { - var client = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); - _fixture = client.Gist; + _fixture = github.Gist; } [IntegrationTest] diff --git a/Octokit.Tests.Integration/Clients/GitHubClientTests.cs b/Octokit.Tests.Integration/Clients/GitHubClientTests.cs index aa7c6fcebb..5cc9ce9027 100644 --- a/Octokit.Tests.Integration/Clients/GitHubClientTests.cs +++ b/Octokit.Tests.Integration/Clients/GitHubClientTests.cs @@ -1,5 +1,6 @@ using Octokit; using Octokit.Tests.Integration; +using Octokit.Tests.Integration.Helpers; using System; using System.Collections.Generic; using System.Linq; @@ -19,10 +20,10 @@ public async Task CanRetrieveLastApiInfoWithEtag() var github = Helper.GetAuthenticatedClient(); var repoName = Helper.MakeNameWithTimestamp("public-repo"); - var createdRepository = await github.Repository.Create(new NewRepository(repoName)); - - try + using (var context = await github.CreateRepositoryContext(new NewRepository(repoName))) { + var createdRepository = context.Repository; + var result = github.GetLastApiInfo(); Assert.True(result.Links.Count == 0); @@ -33,10 +34,6 @@ public async Task CanRetrieveLastApiInfoWithEtag() Assert.True(result.RateLimit.Remaining > -1); Assert.NotNull(result.RateLimit.Reset); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [IntegrationTest] diff --git a/Octokit.Tests.Integration/Clients/IssuesClientTests.cs b/Octokit.Tests.Integration/Clients/IssuesClientTests.cs index 7517e7da9b..2193a1c090 100644 --- a/Octokit.Tests.Integration/Clients/IssuesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/IssuesClientTests.cs @@ -7,40 +7,38 @@ using Octokit.Tests.Helpers; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class IssuesClientTests : IDisposable { - readonly IGitHubClient _gitHubClient; - readonly Repository _repository; - readonly IIssuesClient _issuesClient; + private readonly RepositoryContext _context; + private readonly IIssuesClient _issuesClient; public IssuesClientTests() { - _gitHubClient = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _issuesClient = _gitHubClient.Issue; - _repository = _gitHubClient.Repository.Create(new NewRepository(repoName)).Result; + _issuesClient = github.Issue; + _context = github.CreateRepositoryContext(new NewRepository(repoName)).Result; } [IntegrationTest] public async Task CanCreateRetrieveAndCloseIssue() { - string owner = _repository.Owner.Login; - var newIssue = new NewIssue("a test issue") { Body = "A new unassigned issue" }; - var issue = await _issuesClient.Create(owner, _repository.Name, newIssue); + var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); try { Assert.NotNull(issue); - var retrieved = await _issuesClient.Get(owner, _repository.Name, issue.Number); - var all = await _issuesClient.GetAllForRepository(owner, _repository.Name); + var retrieved = await _issuesClient.Get(_context.RepositoryOwner, _context.RepositoryName, issue.Number); + var all = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName); Assert.NotNull(retrieved); Assert.True(all.Any(i => i.Number == retrieved.Number)); } finally { - var closed = _issuesClient.Update(owner, _repository.Name, issue.Number, + var closed = _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue.Number, new IssueUpdate { State = ItemState.Closed }) .Result; Assert.NotNull(closed); @@ -50,21 +48,20 @@ public async Task CanCreateRetrieveAndCloseIssue() [IntegrationTest] public async Task CanListOpenIssuesWithDefaultSort() { - string owner = _repository.Owner.Login; var newIssue1 = new NewIssue("A test issue1") { Body = "A new unassigned issue" }; var newIssue2 = new NewIssue("A test issue2") { Body = "A new unassigned issue" }; var newIssue3 = new NewIssue("A test issue3") { Body = "A new unassigned issue" }; var newIssue4 = new NewIssue("A test issue4") { Body = "A new unassigned issue" }; - await _issuesClient.Create(owner, _repository.Name, newIssue1); - Thread.Sleep(1000); - await _issuesClient.Create(owner, _repository.Name, newIssue2); - Thread.Sleep(1000); - await _issuesClient.Create(owner, _repository.Name, newIssue3); - var closed = await _issuesClient.Create(owner, _repository.Name, newIssue4); - await _issuesClient.Update(owner, _repository.Name, closed.Number, + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue1); + Thread.Sleep(1000); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue2); + Thread.Sleep(1000); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue3); + var closed = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue4); + await _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, closed.Number, new IssueUpdate { State = ItemState.Closed }); - var issues = await _issuesClient.GetAllForRepository(owner, _repository.Name); + var issues = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName); Assert.Equal(3, issues.Count); Assert.Equal("A test issue3", issues[0].Title); @@ -75,22 +72,20 @@ await _issuesClient.Update(owner, _repository.Name, closed.Number, [IntegrationTest] public async Task CanListIssuesWithAscendingSort() { - string owner = _repository.Owner.Login; - var newIssue1 = new NewIssue("A test issue1") { Body = "A new unassigned issue" }; var newIssue2 = new NewIssue("A test issue2") { Body = "A new unassigned issue" }; var newIssue3 = new NewIssue("A test issue3") { Body = "A new unassigned issue" }; var newIssue4 = new NewIssue("A test issue4") { Body = "A new unassigned issue" }; - await _issuesClient.Create(owner, _repository.Name, newIssue1); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue1); Thread.Sleep(1000); - await _issuesClient.Create(owner, _repository.Name, newIssue2); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue2); Thread.Sleep(1000); - await _issuesClient.Create(owner, _repository.Name, newIssue3); - var closed = await _issuesClient.Create(owner, _repository.Name, newIssue4); - await _issuesClient.Update(owner, _repository.Name, closed.Number, + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue3); + var closed = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue4); + await _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, closed.Number, new IssueUpdate { State = ItemState.Closed }); - var issues = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var issues = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest {SortDirection = SortDirection.Ascending}); Assert.Equal(3, issues.Count); @@ -102,17 +97,15 @@ await _issuesClient.Update(owner, _repository.Name, closed.Number, [IntegrationTest] public async Task CanListClosedIssues() { - string owner = _repository.Owner.Login; - var newIssue1 = new NewIssue("A test issue1") { Body = "A new unassigned issue" }; var newIssue2 = new NewIssue("A closed issue") { Body = "A new unassigned issue" }; - await _issuesClient.Create(owner, _repository.Name, newIssue1); - await _issuesClient.Create(owner, _repository.Name, newIssue2); - var closed = await _issuesClient.Create(owner, _repository.Name, newIssue2); - await _issuesClient.Update(owner, _repository.Name, closed.Number, + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue1); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue2); + var closed = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue2); + await _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, closed.Number, new IssueUpdate { State = ItemState.Closed }); - var issues = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var issues = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest { State = ItemState.Closed }); Assert.Equal(1, issues.Count); @@ -122,14 +115,13 @@ await _issuesClient.Update(owner, _repository.Name, closed.Number, [IntegrationTest] public async Task CanListMilestoneIssues() { - string owner = _repository.Owner.Login; - var milestone = await _issuesClient.Milestone.Create(owner, _repository.Name, new NewMilestone("milestone")); + var milestone = await _issuesClient.Milestone.Create(_context.RepositoryOwner, _context.RepositoryName, new NewMilestone("milestone")); var newIssue1 = new NewIssue("A test issue1") { Body = "A new unassigned issue" }; var newIssue2 = new NewIssue("A milestone issue") { Body = "A new unassigned issue", Milestone = milestone.Number }; - await _issuesClient.Create(owner, _repository.Name, newIssue1); - await _issuesClient.Create(owner, _repository.Name, newIssue2); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue1); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue2); - var issues = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var issues = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest { Milestone = milestone.Number.ToString(CultureInfo.InvariantCulture) }); Assert.Equal(1, issues.Count); @@ -139,19 +131,18 @@ public async Task CanListMilestoneIssues() [IntegrationTest(Skip = "This is paging for a long long time")] public async Task CanRetrieveAllIssues() { - string owner = _repository.Owner.Login; var newIssue1 = new NewIssue("A test issue1") { Body = "A new unassigned issue" }; var newIssue2 = new NewIssue("A test issue2") { Body = "A new unassigned issue" }; var newIssue3 = new NewIssue("A test issue3") { Body = "A new unassigned issue" }; var newIssue4 = new NewIssue("A test issue4") { Body = "A new unassigned issue" }; - var issue1 = await _issuesClient.Create(owner, _repository.Name, newIssue1); - var issue2 = await _issuesClient.Create(owner, _repository.Name, newIssue2); - var issue3 = await _issuesClient.Create(owner, _repository.Name, newIssue3); - var issue4 = await _issuesClient.Create(owner, _repository.Name, newIssue4); - await _issuesClient.Update(owner, _repository.Name, issue4.Number, + var issue1 = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue1); + var issue2 = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue2); + var issue3 = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue3); + var issue4 = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue4); + await _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue4.Number, new IssueUpdate { State = ItemState.Closed }); - var retrieved = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var retrieved = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest { State = ItemState.All }); Assert.True(retrieved.Count >= 4); @@ -164,24 +155,23 @@ await _issuesClient.Update(owner, _repository.Name, issue4.Number, [IntegrationTest] public async Task CanFilterByAssigned() { - var owner = _repository.Owner.Login; - var newIssue1 = new NewIssue("An assigned issue") { Body = "Assigning this to myself", Assignee = owner }; + var newIssue1 = new NewIssue("An assigned issue") { Body = "Assigning this to myself", Assignee = _context.RepositoryOwner }; var newIssue2 = new NewIssue("An unassigned issue") { Body = "A new unassigned issue" }; - await _issuesClient.Create(owner, _repository.Name, newIssue1); - await _issuesClient.Create(owner, _repository.Name, newIssue2); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue1); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue2); - var allIssues = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var allIssues = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest()); Assert.Equal(2, allIssues.Count); - var assignedIssues = await _issuesClient.GetAllForRepository(owner, _repository.Name, - new RepositoryIssueRequest { Assignee = owner }); + var assignedIssues = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, + new RepositoryIssueRequest { Assignee = _context.RepositoryOwner }); Assert.Equal(1, assignedIssues.Count); Assert.Equal("An assigned issue", assignedIssues[0].Title); - var unassignedIssues = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var unassignedIssues = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest { Assignee = "none" }); Assert.Equal(1, unassignedIssues.Count); @@ -191,23 +181,22 @@ public async Task CanFilterByAssigned() [IntegrationTest] public async Task CanFilterByCreator() { - var owner = _repository.Owner.Login; var newIssue1 = new NewIssue("An issue") { Body = "words words words" }; var newIssue2 = new NewIssue("Another issue") { Body = "some other words" }; - await _issuesClient.Create(owner, _repository.Name, newIssue1); - await _issuesClient.Create(owner, _repository.Name, newIssue2); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue1); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue2); - var allIssues = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var allIssues = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest()); Assert.Equal(2, allIssues.Count); - var issuesCreatedByOwner = await _issuesClient.GetAllForRepository(owner, _repository.Name, - new RepositoryIssueRequest { Creator = owner }); + var issuesCreatedByOwner = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, + new RepositoryIssueRequest { Creator = _context.RepositoryOwner }); Assert.Equal(2, issuesCreatedByOwner.Count); - var issuesCreatedByExternalUser = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var issuesCreatedByExternalUser = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest { Creator = "shiftkey" }); Assert.Equal(0, issuesCreatedByExternalUser.Count); @@ -216,23 +205,22 @@ public async Task CanFilterByCreator() [IntegrationTest] public async Task CanFilterByMentioned() { - var owner = _repository.Owner.Login; var newIssue1 = new NewIssue("An issue") { Body = "words words words hello there @shiftkey" }; var newIssue2 = new NewIssue("Another issue") { Body = "some other words" }; - await _issuesClient.Create(owner, _repository.Name, newIssue1); - await _issuesClient.Create(owner, _repository.Name, newIssue2); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue1); + await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue2); - var allIssues = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var allIssues = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest()); Assert.Equal(2, allIssues.Count); - var mentionsWithShiftkey = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var mentionsWithShiftkey = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest { Mentioned = "shiftkey" }); Assert.Equal(1, mentionsWithShiftkey.Count); - var mentionsWithHaacked = await _issuesClient.GetAllForRepository(owner, _repository.Name, + var mentionsWithHaacked = await _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest { Mentioned = "haacked" }); Assert.Equal(0, mentionsWithHaacked.Count); @@ -241,34 +229,30 @@ public async Task CanFilterByMentioned() [IntegrationTest] public async Task FilteringByInvalidAccountThrowsError() { - var owner = _repository.Owner.Login; - await Assert.ThrowsAsync( - () => _issuesClient.GetAllForRepository(owner, _repository.Name, + () => _issuesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest { Assignee = "some-random-account" })); } [IntegrationTest] public async Task CanAssignAndUnassignMilestone() { - var owner = _repository.Owner.Login; - var newMilestone = new NewMilestone("a milestone"); - var milestone = await _issuesClient.Milestone.Create(owner, _repository.Name, newMilestone); + var milestone = await _issuesClient.Milestone.Create(_context.RepositoryOwner, _context.RepositoryName, newMilestone); var newIssue1 = new NewIssue("A test issue1") { Body = "A new unassigned issue", Milestone = milestone.Number }; - var issue = await _issuesClient.Create(owner, _repository.Name, newIssue1); + var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue1); Assert.NotNull(issue.Milestone); var issueUpdate = issue.ToUpdate(); issueUpdate.Milestone = null; - var updatedIssue = await _issuesClient.Update(owner, _repository.Name, issue.Number, issueUpdate); + var updatedIssue = await _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue.Number, issueUpdate); Assert.Null(updatedIssue.Milestone); } @@ -276,9 +260,7 @@ public async Task CanAssignAndUnassignMilestone() [IntegrationTest] public async Task DoesNotChangeLabelsByDefault() { - var owner = _repository.Owner.Login; - - await _issuesClient.Labels.Create(owner, _repository.Name, new NewLabel("something", "FF0000")); + await _issuesClient.Labels.Create(_context.RepositoryOwner, _context.RepositoryName, new NewLabel("something", "FF0000")); var newIssue = new NewIssue("A test issue1") { @@ -286,11 +268,11 @@ public async Task DoesNotChangeLabelsByDefault() }; newIssue.Labels.Add("something"); - var issue = await _issuesClient.Create(owner, _repository.Name, newIssue); + var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); var issueUpdate = issue.ToUpdate(); - var updatedIssue = await _issuesClient.Update(owner, _repository.Name, issue.Number, issueUpdate); + var updatedIssue = await _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue.Number, issueUpdate); Assert.Equal(1, updatedIssue.Labels.Count); } @@ -298,11 +280,9 @@ public async Task DoesNotChangeLabelsByDefault() [IntegrationTest] public async Task CanUpdateLabelForAnIssue() { - var owner = _repository.Owner.Login; - // create some labels - await _issuesClient.Labels.Create(owner, _repository.Name, new NewLabel("something", "FF0000")); - await _issuesClient.Labels.Create(owner, _repository.Name, new NewLabel("another thing", "0000FF")); + await _issuesClient.Labels.Create(_context.RepositoryOwner, _context.RepositoryName, new NewLabel("something", "FF0000")); + await _issuesClient.Labels.Create(_context.RepositoryOwner, _context.RepositoryName, new NewLabel("another thing", "0000FF")); // setup us the issue var newIssue = new NewIssue("A test issue1") @@ -311,13 +291,13 @@ public async Task CanUpdateLabelForAnIssue() }; newIssue.Labels.Add("something"); - var issue = await _issuesClient.Create(owner, _repository.Name, newIssue); + var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); // update the issue var issueUpdate = issue.ToUpdate(); issueUpdate.AddLabel("another thing"); - var updatedIssue = await _issuesClient.Update(owner, _repository.Name, issue.Number, issueUpdate); + var updatedIssue = await _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue.Number, issueUpdate); Assert.Equal("another thing", updatedIssue.Labels[0].Name); } @@ -325,11 +305,9 @@ public async Task CanUpdateLabelForAnIssue() [IntegrationTest] public async Task CanClearLabelsForAnIssue() { - var owner = _repository.Owner.Login; - // create some labels - await _issuesClient.Labels.Create(owner, _repository.Name, new NewLabel("something", "FF0000")); - await _issuesClient.Labels.Create(owner, _repository.Name, new NewLabel("another thing", "0000FF")); + await _issuesClient.Labels.Create(_context.RepositoryOwner, _context.RepositoryName, new NewLabel("something", "FF0000")); + await _issuesClient.Labels.Create(_context.RepositoryOwner, _context.RepositoryName, new NewLabel("another thing", "0000FF")); // setup us the issue var newIssue = new NewIssue("A test issue1") @@ -339,14 +317,14 @@ public async Task CanClearLabelsForAnIssue() newIssue.Labels.Add("something"); newIssue.Labels.Add("another thing"); - var issue = await _issuesClient.Create(owner, _repository.Name, newIssue); + var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); Assert.Equal(2, issue.Labels.Count); // update the issue var issueUpdate = issue.ToUpdate(); issueUpdate.ClearLabels(); - var updatedIssue = await _issuesClient.Update(owner, _repository.Name, issue.Number, issueUpdate); + var updatedIssue = await _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue.Number, issueUpdate); Assert.Empty(updatedIssue.Labels); } @@ -355,23 +333,22 @@ public async Task CanClearLabelsForAnIssue() public async Task CanAccessUrls() { var expctedUri = "https://api.github.com/repos/{0}/{1}/issues/{2}/{3}"; - var owner = _repository.Owner.Login; var newIssue = new NewIssue("A test issue") { Body = "A new unassigned issue", }; - var issue = await _issuesClient.Create(owner, _repository.Name, newIssue); + var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); Assert.NotNull(issue.CommentsUrl); - Assert.Equal(new Uri(string.Format(expctedUri, owner, _repository.Name, issue.Number, "comments")), issue.CommentsUrl); + Assert.Equal(new Uri(string.Format(expctedUri, _context.RepositoryOwner, _context.RepositoryName, issue.Number, "comments")), issue.CommentsUrl); Assert.NotNull(issue.EventsUrl); - Assert.Equal(new Uri(string.Format(expctedUri, owner, _repository.Name, issue.Number, "events")), issue.EventsUrl); + Assert.Equal(new Uri(string.Format(expctedUri, _context.RepositoryOwner, _context.RepositoryName, issue.Number, "events")), issue.EventsUrl); } public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/IssuesEventsClientTests.cs b/Octokit.Tests.Integration/Clients/IssuesEventsClientTests.cs index 63ccdf7536..3d6fea88e3 100644 --- a/Octokit.Tests.Integration/Clients/IssuesEventsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/IssuesEventsClientTests.cs @@ -5,42 +5,38 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class IssuesEventsClientTests : IDisposable { - readonly IGitHubClient _gitHubClient; - readonly IIssuesEventsClient _issuesEventsClientClient; - readonly IIssuesClient _issuesClient; - readonly Repository _repository; - readonly string _repositoryOwner; - readonly string _repositoryName; + private readonly IIssuesEventsClient _issuesEventsClientClient; + private readonly IIssuesClient _issuesClient; + private readonly RepositoryContext _context; public IssuesEventsClientTests() { - _gitHubClient = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); - _issuesEventsClientClient = _gitHubClient.Issue.Events; - _issuesClient = _gitHubClient.Issue; + _issuesEventsClientClient = github.Issue.Events; + _issuesClient = github.Issue; var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = _gitHubClient.Repository.Create(new NewRepository(repoName)).Result; - _repositoryOwner = _repository.Owner.Login; - _repositoryName = _repository.Name; + _context = github.CreateRepositoryContext(new NewRepository(repoName)).Result; } [IntegrationTest] public async Task CanListEventInfoForAnIssue() { var newIssue = new NewIssue("a test issue") { Body = "A new unassigned issue" }; - var issue = await _issuesClient.Create(_repositoryOwner, _repositoryName, newIssue); + var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); - var issueEventInfo = await _issuesEventsClientClient.GetAllForIssue(_repositoryOwner, _repositoryName, issue.Number); + var issueEventInfo = await _issuesEventsClientClient.GetAllForIssue(_context.RepositoryOwner, _context.RepositoryName, issue.Number); Assert.Empty(issueEventInfo); - var closed = _issuesClient.Update(_repositoryOwner, _repository.Name, issue.Number, new IssueUpdate { State = ItemState.Closed }) + var closed = _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue.Number, new IssueUpdate { State = ItemState.Closed }) .Result; Assert.NotNull(closed); - issueEventInfo = await _issuesEventsClientClient.GetAllForIssue(_repositoryOwner, _repositoryName, issue.Number); + issueEventInfo = await _issuesEventsClientClient.GetAllForIssue(_context.RepositoryOwner, _context.RepositoryName, issue.Number); Assert.Equal(1, issueEventInfo.Count); Assert.Equal(EventInfoState.Closed, issueEventInfo[0].Event); @@ -52,26 +48,26 @@ public async Task CanListIssueEventsForARepository() // create 2 new issues var newIssue1 = new NewIssue("A test issue1") { Body = "Everything's coming up Millhouse" }; var newIssue2 = new NewIssue("A test issue2") { Body = "A new unassigned issue" }; - - var issue1 = await _issuesClient.Create(_repositoryOwner, _repository.Name, newIssue1); + + var issue1 = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue1); Thread.Sleep(1000); - var issue2 = await _issuesClient.Create(_repositoryOwner, _repository.Name, newIssue2); + var issue2 = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue2); Thread.Sleep(1000); // close and open issue1 - var closed1 = _issuesClient.Update(_repositoryOwner, _repository.Name, issue1.Number,new IssueUpdate { State = ItemState.Closed }) + var closed1 = _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue1.Number, new IssueUpdate { State = ItemState.Closed }) .Result; Assert.NotNull(closed1); - var reopened1 = _issuesClient.Update(_repositoryOwner, _repository.Name, issue1.Number, new IssueUpdate { State = ItemState.Open }) + var reopened1 = _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue1.Number, new IssueUpdate { State = ItemState.Open }) .Result; Assert.NotNull(reopened1); // close issue2 - var closed2 = _issuesClient.Update(_repositoryOwner, _repository.Name, issue2.Number, new IssueUpdate { State = ItemState.Closed }) + var closed2 = _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue2.Number, new IssueUpdate { State = ItemState.Closed }) .Result; Assert.NotNull(closed2); - var issueEvents = await _issuesEventsClientClient.GetAllForRepository(_repositoryOwner, _repositoryName); + var issueEvents = await _issuesEventsClientClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName); Assert.Equal(3, issueEvents.Count); Assert.Equal(2, issueEvents.Count(issueEvent => issueEvent.Issue.Body == "Everything's coming up Millhouse")); @@ -81,14 +77,14 @@ public async Task CanListIssueEventsForARepository() public async Task CanRetrieveIssueEventById() { var newIssue = new NewIssue("a test issue") { Body = "A new unassigned issue" }; - var issue = await _issuesClient.Create(_repositoryOwner, _repositoryName, newIssue); - var closed = _issuesClient.Update(_repositoryOwner, _repository.Name, issue.Number, new IssueUpdate { State = ItemState.Closed }) + var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); + var closed = _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue.Number, new IssueUpdate { State = ItemState.Closed }) .Result; Assert.NotNull(closed); - var issueEvents = await _issuesEventsClientClient.GetAllForRepository(_repositoryOwner, _repositoryName); + var issueEvents = await _issuesEventsClientClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName); int issueEventId = issueEvents[0].Id; - var issueEventLookupById = await _issuesEventsClientClient.Get(_repositoryOwner, _repositoryName, issueEventId); + var issueEventLookupById = await _issuesEventsClientClient.Get(_context.RepositoryOwner, _context.RepositoryName, issueEventId); Assert.Equal(issueEventId, issueEventLookupById.Id); Assert.Equal(issueEvents[0].Event, issueEventLookupById.Event); @@ -104,6 +100,6 @@ public async Task CanDeserializeUnsubscribeEvent() public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/IssuesLabelsClientTests.cs b/Octokit.Tests.Integration/Clients/IssuesLabelsClientTests.cs index 4555858006..16900b98e7 100644 --- a/Octokit.Tests.Integration/Clients/IssuesLabelsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/IssuesLabelsClientTests.cs @@ -3,27 +3,23 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class IssuesLabelsClientTests : IDisposable { - readonly IGitHubClient _gitHubClient; - readonly IIssuesLabelsClient _issuesLabelsClient; - readonly IIssuesClient _issuesClient; - readonly Repository _repository; - readonly string _repositoryOwner; - readonly string _repositoryName; + private readonly IIssuesLabelsClient _issuesLabelsClient; + private readonly IIssuesClient _issuesClient; + private readonly RepositoryContext _context; public IssuesLabelsClientTests() { - _gitHubClient = Helper.GetAuthenticatedClient(); + var github = Helper.GetAuthenticatedClient(); - _issuesLabelsClient= _gitHubClient.Issue.Labels; - _issuesClient = _gitHubClient.Issue; + _issuesLabelsClient= github.Issue.Labels; + _issuesClient = github.Issue; var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = _gitHubClient.Repository.Create(new NewRepository(repoName)).Result; - _repositoryOwner = _repository.Owner.Login; - _repositoryName = _repository.Name; + _context = github.CreateRepositoryContext(new NewRepository(repoName)).Result; } [IntegrationTest] @@ -32,17 +28,17 @@ public async Task CanListLabelsForAnIssue() var newIssue = new NewIssue("A test issue") { Body = "A new unassigned issue" }; var newLabel = new NewLabel("test label", "FFFFFF"); - var label = await _issuesLabelsClient.Create(_repositoryOwner, _repository.Name, newLabel); - var issue = await _issuesClient.Create(_repositoryOwner, _repositoryName, newIssue); + var label = await _issuesLabelsClient.Create(_context.RepositoryOwner, _context.RepositoryName, newLabel); + var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); - var issueLabelsInfo = await _issuesLabelsClient.GetAllForIssue(_repositoryOwner, _repositoryName, issue.Number); + var issueLabelsInfo = await _issuesLabelsClient.GetAllForIssue(_context.RepositoryOwner, _context.RepositoryName, issue.Number); Assert.Empty(issueLabelsInfo); var issueUpdate = new IssueUpdate(); issueUpdate.AddLabel(label.Name); - var updated = await _issuesClient.Update(_repositoryOwner, _repository.Name, issue.Number, issueUpdate); + var updated = await _issuesClient.Update(_context.RepositoryOwner, _context.RepositoryName, issue.Number, issueUpdate); Assert.NotNull(updated); - issueLabelsInfo = await _issuesLabelsClient.GetAllForIssue(_repositoryOwner, _repositoryName, issue.Number); + issueLabelsInfo = await _issuesLabelsClient.GetAllForIssue(_context.RepositoryOwner, _context.RepositoryName, issue.Number); Assert.Equal(1, issueLabelsInfo.Count); Assert.Equal(newLabel.Color, issueLabelsInfo[0].Color); @@ -54,12 +50,12 @@ public async Task CanListIssueLabelsForARepository() var newLabel1 = new NewLabel("test label 1", "FFFFFF"); var newLabel2 = new NewLabel("test label 2", "FFFFFF"); - var originalIssueLabels = await _issuesLabelsClient.GetAllForRepository(_repositoryOwner, _repositoryName); + var originalIssueLabels = await _issuesLabelsClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName); - await _issuesLabelsClient.Create(_repositoryOwner, _repository.Name, newLabel1); - await _issuesLabelsClient.Create(_repositoryOwner, _repository.Name, newLabel2); + await _issuesLabelsClient.Create(_context.RepositoryOwner, _context.RepositoryName, newLabel1); + await _issuesLabelsClient.Create(_context.RepositoryOwner, _context.RepositoryName, newLabel2); - var issueLabels = await _issuesLabelsClient.GetAllForRepository(_repositoryOwner, _repositoryName); + var issueLabels = await _issuesLabelsClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName); Assert.Equal(originalIssueLabels.Count + 2, issueLabels.Count); } @@ -68,10 +64,10 @@ public async Task CanListIssueLabelsForARepository() public async Task CanRetrieveIssueLabelByName() { var newLabel = new NewLabel("test label 1b", "FFFFFF"); - var label = await _issuesLabelsClient.Create(_repositoryOwner, _repository.Name, newLabel); + var label = await _issuesLabelsClient.Create(_context.RepositoryOwner, _context.RepositoryName, newLabel); Assert.NotNull(label); - var issueLabelLookupByName = await _issuesLabelsClient.Get(_repositoryOwner, _repositoryName, label.Name); + var issueLabelLookupByName = await _issuesLabelsClient.Get(_context.RepositoryOwner, _context.RepositoryName, label.Name); Assert.Equal(label.Name, issueLabelLookupByName.Name); Assert.Equal(label.Color, issueLabelLookupByName.Color); @@ -79,6 +75,6 @@ public async Task CanRetrieveIssueLabelByName() public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/MergingClientTests.cs b/Octokit.Tests.Integration/Clients/MergingClientTests.cs index 4e7cb49261..c746afb075 100644 --- a/Octokit.Tests.Integration/Clients/MergingClientTests.cs +++ b/Octokit.Tests.Integration/Clients/MergingClientTests.cs @@ -4,27 +4,25 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class MergingClientTests : IDisposable { - readonly IGitHubClient _client; - readonly Repository _repository; - readonly string _owner; - readonly IMergingClient _fixture; + private readonly IGitHubClient _github; + private readonly IMergingClient _fixture; + private readonly RepositoryContext _context; const string branchName = "my-branch"; public MergingClientTests() { - _client = new GitHubClient(new ProductHeaderValue("OctokitTests")) + _github = new GitHubClient(new ProductHeaderValue("OctokitTests")) { Credentials = Helper.Credentials }; - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _fixture = _client.Repository.Merging; - _repository = _client.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - _owner = _repository.Owner.Login; + _fixture = _github.Repository.Merging; + _context = _github.CreateRepositoryContext("public-repo").Result; } [IntegrationTest] @@ -34,7 +32,7 @@ public async Task CanCreateMerge() var newMerge = new NewMerge("master", branchName) { CommitMessage = "merge commit to master from integrationtests" }; - var merge = await _fixture.Create(_owner, _repository.Name, newMerge); + var merge = await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, newMerge); Assert.NotNull(merge); Assert.NotNull(merge.Commit); Assert.Equal("merge commit to master from integrationtests", merge.Commit.Message); @@ -42,21 +40,21 @@ public async Task CanCreateMerge() async Task CreateTheWorld() { - var master = await _client.GitDatabase.Reference.Get(Helper.UserName, _repository.Name, "heads/master"); + var master = await _github.GitDatabase.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master"); // create new commit for master branch var newMasterTree = await CreateTree(new Dictionary { { "README.md", "Hello World! I want to be overwritten by featurebranch!" } }); var newMaster = await CreateCommit("baseline for merge", newMasterTree.Sha, master.Object.Sha); // update master - await _client.GitDatabase.Reference.Update(Helper.UserName, _repository.Name, "heads/master", new ReferenceUpdate(newMaster.Sha)); + await _github.GitDatabase.Reference.Update(Helper.UserName, _context.RepositoryName, "heads/master", new ReferenceUpdate(newMaster.Sha)); // create new commit for feature branch var featureBranchTree = await CreateTree(new Dictionary { { "README.md", "I am overwriting this blob with something new" } }); var featureBranchCommit = await CreateCommit("this is the commit to merge", featureBranchTree.Sha, newMaster.Sha); // create branch - await _client.GitDatabase.Reference.Create(Helper.UserName, _repository.Name, new NewReference("refs/heads/my-branch", featureBranchCommit.Sha)); + await _github.GitDatabase.Reference.Create(Helper.UserName, _context.RepositoryName, new NewReference("refs/heads/my-branch", featureBranchCommit.Sha)); } async Task CreateTree(IEnumerable> treeContents) @@ -70,7 +68,7 @@ async Task CreateTree(IEnumerable> tr Content = c.Value, Encoding = EncodingType.Utf8 }; - var baselineBlobResult = await _client.GitDatabase.Blob.Create(Helper.UserName, _repository.Name, baselineBlob); + var baselineBlobResult = await _github.GitDatabase.Blob.Create(Helper.UserName, _context.RepositoryName, baselineBlob); collection.Add(new NewTreeItem { @@ -87,17 +85,17 @@ async Task CreateTree(IEnumerable> tr newTree.Tree.Add(item); } - return await _client.GitDatabase.Tree.Create(Helper.UserName, _repository.Name, newTree); + return await _github.GitDatabase.Tree.Create(Helper.UserName, _context.RepositoryName, newTree); } async Task CreateCommit(string message, string sha, string parent) { var newCommit = new NewCommit(message, sha, parent); - return await _client.GitDatabase.Commit.Create(Helper.UserName, _repository.Name, newCommit); + return await _github.GitDatabase.Commit.Create(Helper.UserName, _context.RepositoryName, newCommit); } public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/MilestonesClientTests.cs b/Octokit.Tests.Integration/Clients/MilestonesClientTests.cs index 128affd4a5..b4cc1a1a0e 100644 --- a/Octokit.Tests.Integration/Clients/MilestonesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/MilestonesClientTests.cs @@ -4,34 +4,31 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class MilestonesClientTests : IDisposable { - readonly IGitHubClient _gitHubClient; - readonly IMilestonesClient _milestonesClient; - readonly Repository _repository; - readonly string _repositoryOwner; - readonly string _repositoryName; + private readonly IGitHubClient _github; + private readonly IMilestonesClient _milestonesClient; + private readonly RepositoryContext _context; public MilestonesClientTests() { - _gitHubClient = Helper.GetAuthenticatedClient(); + _github = Helper.GetAuthenticatedClient(); - _milestonesClient = _gitHubClient.Issue.Milestone; + _milestonesClient = _github.Issue.Milestone; var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = _gitHubClient.Repository.Create(new NewRepository(repoName)).Result; - _repositoryOwner = _repository.Owner.Login; - _repositoryName = _repository.Name; + _context = _github.CreateRepositoryContext(new NewRepository(repoName)).Result; } [IntegrationTest] public async Task CanRetrieveOneMilestone() { var newMilestone = new NewMilestone("a milestone") { DueOn = DateTime.Now }; - var created = await _milestonesClient.Create(_repositoryOwner, _repositoryName, newMilestone); + var created = await _milestonesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newMilestone); - var result = await _milestonesClient.Get(_repositoryOwner, _repositoryName, created.Number); + var result = await _milestonesClient.Get(_context.RepositoryOwner, _context.RepositoryName, created.Number); Assert.Equal("a milestone", result.Title); } @@ -39,7 +36,7 @@ public async Task CanRetrieveOneMilestone() [IntegrationTest] public async Task CanListEmptyMilestones() { - var milestones = await _milestonesClient.GetAllForRepository(_repositoryOwner, _repositoryName); + var milestones = await _milestonesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName); Assert.Empty(milestones); } @@ -50,11 +47,11 @@ public async Task CanListMilestonesWithDefaultSortByDueDateAsc() var milestone1 = new NewMilestone("milestone 1") { DueOn = DateTime.Now }; var milestone2 = new NewMilestone("milestone 2") { DueOn = DateTime.Now.AddDays(1) }; var milestone3 = new NewMilestone("milestone 3") { DueOn = DateTime.Now.AddDays(3), State = ItemState.Closed }; - await _milestonesClient.Create(_repositoryOwner, _repositoryName, milestone1); - await _milestonesClient.Create(_repositoryOwner, _repositoryName, milestone2); - await _milestonesClient.Create(_repositoryOwner, _repositoryName, milestone3); + await _milestonesClient.Create(_context.RepositoryOwner, _context.RepositoryName, milestone1); + await _milestonesClient.Create(_context.RepositoryOwner, _context.RepositoryName, milestone2); + await _milestonesClient.Create(_context.RepositoryOwner, _context.RepositoryName, milestone3); - var milestones = await _milestonesClient.GetAllForRepository(_repositoryOwner, _repositoryName); + var milestones = await _milestonesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName); Assert.Equal(2, milestones.Count); Assert.Equal("milestone 1", milestones[0].Title); Assert.Equal("milestone 2", milestones[1].Title); @@ -66,11 +63,11 @@ public async Task CanListMilestonesWithSortByDueDateDesc() var milestone1 = new NewMilestone("milestone 1") { DueOn = DateTime.Now }; var milestone2 = new NewMilestone("milestone 2") { DueOn = DateTime.Now.AddDays(1) }; var milestone3 = new NewMilestone("milestone 3") { DueOn = DateTime.Now.AddDays(3), State = ItemState.Closed }; - await _milestonesClient.Create(_repositoryOwner, _repositoryName, milestone1); - await _milestonesClient.Create(_repositoryOwner, _repositoryName, milestone2); - await _milestonesClient.Create(_repositoryOwner, _repositoryName, milestone3); + await _milestonesClient.Create(_context.RepositoryOwner, _context.RepositoryName, milestone1); + await _milestonesClient.Create(_context.RepositoryOwner, _context.RepositoryName, milestone2); + await _milestonesClient.Create(_context.RepositoryOwner, _context.RepositoryName, milestone3); - var milestones = await _milestonesClient.GetAllForRepository(_repositoryOwner, _repositoryName, + var milestones = await _milestonesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new MilestoneRequest { SortDirection = SortDirection.Descending }); Assert.Equal(2, milestones.Count); Assert.Equal("milestone 2", milestones[0].Title); @@ -83,11 +80,11 @@ public async Task CanListClosedMilestones() var milestone1 = new NewMilestone("milestone 1") { DueOn = DateTime.Now }; var milestone2 = new NewMilestone("milestone 2") { DueOn = DateTime.Now.AddDays(1) }; var milestone3 = new NewMilestone("milestone 3") { DueOn = DateTime.Now.AddDays(3), State = ItemState.Closed }; - await _milestonesClient.Create(_repositoryOwner, _repositoryName, milestone1); - await _milestonesClient.Create(_repositoryOwner, _repositoryName, milestone2); - await _milestonesClient.Create(_repositoryOwner, _repositoryName, milestone3); + await _milestonesClient.Create(_context.RepositoryOwner, _context.RepositoryName, milestone1); + await _milestonesClient.Create(_context.RepositoryOwner, _context.RepositoryName, milestone2); + await _milestonesClient.Create(_context.RepositoryOwner, _context.RepositoryName, milestone3); - var milestones = await _milestonesClient.GetAllForRepository(_repositoryOwner, _repositoryName, + var milestones = await _milestonesClient.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new MilestoneRequest { State = ItemState.Closed }); Assert.Equal(1, milestones.Count); @@ -97,17 +94,15 @@ public async Task CanListClosedMilestones() [IntegrationTest] public async Task CanRetrieveClosedIssues() { - string owner = _repository.Owner.Login; - var newIssue = new NewIssue("A test issue") { Body = "A new unassigned issue" }; - var issue1 = await _gitHubClient.Issue.Create(owner, _repository.Name, newIssue); - var issue2 = await _gitHubClient.Issue.Create(owner, _repository.Name, newIssue); - await _gitHubClient.Issue.Update(owner, _repository.Name, issue1.Number, + var issue1 = await _github.Issue.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); + var issue2 = await _github.Issue.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); + await _github.Issue.Update(_context.RepositoryOwner, _context.RepositoryName, issue1.Number, new IssueUpdate { State = ItemState.Closed }); - await _gitHubClient.Issue.Update(owner, _repository.Name, issue2.Number, + await _github.Issue.Update(_context.RepositoryOwner, _context.RepositoryName, issue2.Number, new IssueUpdate { State = ItemState.Closed }); - var retrieved = await _gitHubClient.Issue.GetAllForRepository(owner, _repository.Name, + var retrieved = await _github.Issue.GetAllForRepository(_context.RepositoryOwner, _context.RepositoryName, new RepositoryIssueRequest { State = ItemState.Closed }); Assert.True(retrieved.Count >= 2); @@ -117,6 +112,6 @@ await _gitHubClient.Issue.Update(owner, _repository.Name, issue2.Number, public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/PullRequestReviewCommentsClientTests.cs b/Octokit.Tests.Integration/Clients/PullRequestReviewCommentsClientTests.cs index 6aec24623e..45011f52c2 100644 --- a/Octokit.Tests.Integration/Clients/PullRequestReviewCommentsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/PullRequestReviewCommentsClientTests.cs @@ -5,12 +5,13 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class PullRequestReviewCommentsClientTests : IDisposable { - readonly IGitHubClient _gitHubClient; - readonly IPullRequestReviewCommentsClient _client; - readonly Repository _repository; + private readonly IGitHubClient _github; + private readonly IPullRequestReviewCommentsClient _client; + private readonly RepositoryContext _context; const string branchName = "new-branch"; const string branchHead = "heads/" + branchName; @@ -19,26 +20,25 @@ public class PullRequestReviewCommentsClientTests : IDisposable public PullRequestReviewCommentsClientTests() { - _gitHubClient = Helper.GetAuthenticatedClient(); + _github = Helper.GetAuthenticatedClient(); - _client = _gitHubClient.PullRequest.Comment; + _client = _github.PullRequest.Comment; // We'll create a pull request that can be used by most tests - var repoName = Helper.MakeNameWithTimestamp("test-repo"); - _repository = CreateRepository(repoName).Result; + _context = _github.CreateRepositoryContext("test-repo").Result; } [IntegrationTest] public async Task CanCreateAndRetrieveComment() { - var pullRequest = await CreatePullRequest(_repository); + var pullRequest = await CreatePullRequest(_context); const string body = "A review comment message"; const int position = 1; var createdComment = await CreateComment(body, position, pullRequest.Sha, pullRequest.Number); - var commentFromGitHub = await _client.GetComment(Helper.UserName, _repository.Name, createdComment.Id); + var commentFromGitHub = await _client.GetComment(Helper.UserName, _context.RepositoryName, createdComment.Id); AssertComment(commentFromGitHub, body, position); } @@ -46,7 +46,7 @@ public async Task CanCreateAndRetrieveComment() [IntegrationTest] public async Task CanEditComment() { - var pullRequest = await CreatePullRequest(_repository); + var pullRequest = await CreatePullRequest(_context); const string body = "A new review comment message"; const int position = 1; @@ -55,9 +55,9 @@ public async Task CanEditComment() var edit = new PullRequestReviewCommentEdit("Edited Comment"); - var editedComment = await _client.Edit(Helper.UserName, _repository.Name, createdComment.Id, edit); + var editedComment = await _client.Edit(Helper.UserName, _context.RepositoryName, createdComment.Id, edit); - var commentFromGitHub = await _client.GetComment(Helper.UserName, _repository.Name, editedComment.Id); + var commentFromGitHub = await _client.GetComment(Helper.UserName, _context.RepositoryName, editedComment.Id); AssertComment(commentFromGitHub, edit.Body, position); } @@ -65,7 +65,7 @@ public async Task CanEditComment() [IntegrationTest] public async Task TimestampsAreUpdated() { - var pullRequest = await CreatePullRequest(_repository); + var pullRequest = await CreatePullRequest(_context); const string body = "A new review comment message"; const int position = 1; @@ -78,7 +78,7 @@ public async Task TimestampsAreUpdated() var edit = new PullRequestReviewCommentEdit("Edited Comment"); - var editedComment = await _client.Edit(Helper.UserName, _repository.Name, createdComment.Id, edit); + var editedComment = await _client.Edit(Helper.UserName, _context.RepositoryName, createdComment.Id, edit); Assert.NotEqual(editedComment.UpdatedAt, editedComment.CreatedAt); } @@ -86,20 +86,20 @@ public async Task TimestampsAreUpdated() [IntegrationTest] public async Task CanDeleteComment() { - var pullRequest = await CreatePullRequest(_repository); + var pullRequest = await CreatePullRequest(_context); const string body = "A new review comment message"; const int position = 1; var createdComment = await CreateComment(body, position, pullRequest.Sha, pullRequest.Number); - await _client.Delete(Helper.UserName, _repository.Name, createdComment.Id); + await _client.Delete(Helper.UserName, _context.RepositoryName, createdComment.Id); } [IntegrationTest] public async Task CanCreateReply() { - var pullRequest = await CreatePullRequest(_repository); + var pullRequest = await CreatePullRequest(_context); const string body = "Reply me!"; const int position = 1; @@ -107,8 +107,8 @@ public async Task CanCreateReply() var createdComment = await CreateComment(body, position, pullRequest.Sha, pullRequest.Number); var reply = new PullRequestReviewCommentReplyCreate("Replied", createdComment.Id); - var createdReply = await _client.CreateReply(Helper.UserName, _repository.Name, pullRequest.Number, reply); - var createdReplyFromGitHub = await _client.GetComment(Helper.UserName, _repository.Name, createdReply.Id); + var createdReply = await _client.CreateReply(Helper.UserName, _context.RepositoryName, pullRequest.Number, reply); + var createdReplyFromGitHub = await _client.GetComment(Helper.UserName, _context.RepositoryName, createdReply.Id); AssertComment(createdReplyFromGitHub, reply.Body, position); } @@ -116,14 +116,14 @@ public async Task CanCreateReply() [IntegrationTest] public async Task CanGetForPullRequest() { - var pullRequest = await CreatePullRequest(_repository); + var pullRequest = await CreatePullRequest(_context); const int position = 1; var commentsToCreate = new List { "Comment 1", "Comment 2", "Comment 3" }; - await CreateComments(commentsToCreate, position, _repository.Name, pullRequest.Sha, pullRequest.Number); + await CreateComments(commentsToCreate, position, _context.RepositoryName, pullRequest.Sha, pullRequest.Number); - var pullRequestComments = await _client.GetAll(Helper.UserName, _repository.Name, pullRequest.Number); + var pullRequestComments = await _client.GetAll(Helper.UserName, _context.RepositoryName, pullRequest.Number); AssertComments(pullRequestComments, commentsToCreate, position); } @@ -131,14 +131,14 @@ public async Task CanGetForPullRequest() [IntegrationTest] public async Task CanGetForRepository() { - var pullRequest = await CreatePullRequest(_repository); + var pullRequest = await CreatePullRequest(_context); const int position = 1; var commentsToCreate = new List { "Comment One", "Comment Two" }; - await CreateComments(commentsToCreate, position, _repository.Name, pullRequest.Sha, pullRequest.Number); + await CreateComments(commentsToCreate, position, _context.RepositoryName, pullRequest.Sha, pullRequest.Number); - var pullRequestComments = await _client.GetAllForRepository(Helper.UserName, _repository.Name); + var pullRequestComments = await _client.GetAllForRepository(Helper.UserName, _context.RepositoryName); AssertComments(pullRequestComments, commentsToCreate, position); } @@ -146,14 +146,14 @@ public async Task CanGetForRepository() [IntegrationTest] public async Task CanGetForRepositoryAscendingSort() { - var pullRequest = await CreatePullRequest(_repository); + var pullRequest = await CreatePullRequest(_context); const int position = 1; var commentsToCreate = new [] { "Comment One", "Comment Two", "Comment Three" }; - await CreateComments(commentsToCreate, position, _repository.Name, pullRequest.Sha, pullRequest.Number); + await CreateComments(commentsToCreate, position, _context.RepositoryName, pullRequest.Sha, pullRequest.Number); - var pullRequestComments = await _client.GetAllForRepository(Helper.UserName, _repository.Name, new PullRequestReviewCommentRequest { Direction = SortDirection.Ascending }); + var pullRequestComments = await _client.GetAllForRepository(Helper.UserName, _context.RepositoryName, new PullRequestReviewCommentRequest { Direction = SortDirection.Ascending }); Assert.Equal(pullRequestComments.Select(x => x.Body), commentsToCreate); } @@ -161,26 +161,26 @@ public async Task CanGetForRepositoryAscendingSort() [IntegrationTest] public async Task CanGetForRepositoryDescendingSort() { - var pullRequest = await CreatePullRequest(_repository); + var pullRequest = await CreatePullRequest(_context); const int position = 1; var commentsToCreate = new [] { "Comment One", "Comment Two", "Comment Three", "Comment Four" }; - await CreateComments(commentsToCreate, position, _repository.Name, pullRequest.Sha, pullRequest.Number); + await CreateComments(commentsToCreate, position, _context.RepositoryName, pullRequest.Sha, pullRequest.Number); - var pullRequestComments = await _client.GetAllForRepository(Helper.UserName, _repository.Name, new PullRequestReviewCommentRequest { Direction = SortDirection.Descending }); + var pullRequestComments = await _client.GetAllForRepository(Helper.UserName, _context.RepositoryName, new PullRequestReviewCommentRequest { Direction = SortDirection.Descending }); Assert.Equal(pullRequestComments.Select(x => x.Body), commentsToCreate.Reverse()); } public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } async Task CreateComment(string body, int position, string commitId, int number) { - return await CreateComment(body, position, _repository.Name, commitId, number); + return await CreateComment(body, position, _context.RepositoryName, commitId, number); } async Task CreateComment(string body, int position, string repoName, string pullRequestCommitId, int pullRequestNumber) @@ -220,18 +220,13 @@ static void AssertComments(IReadOnlyList comments, Lis } } - async Task CreateRepository(string repoName) - { - return await _gitHubClient.Repository.Create(new NewRepository(repoName) { AutoInit = true }); - } - /// /// Creates the base state for testing (creates a repo, a commit in master, a branch, a commit in the branch and a pull request) /// /// - async Task CreatePullRequest(Repository repository) + async Task CreatePullRequest(RepositoryContext context) { - var repoName = repository.Name; + var repoName = context.RepositoryName; // Creating a commit in master @@ -240,7 +235,7 @@ async Task CreatePullRequest(Repository repository) // Creating a branch var newBranch = new NewReference(branchRef, createdCommitInMaster.Sha); - await _gitHubClient.GitDatabase.Reference.Create(Helper.UserName, repoName, newBranch); + await _github.GitDatabase.Reference.Create(Helper.UserName, repoName, newBranch); // Creating a commit in the branch @@ -249,7 +244,7 @@ async Task CreatePullRequest(Repository repository) // Creating a pull request var pullRequest = new NewPullRequest("Nice title for the pull request", branchName, "master"); - var createdPullRequest = await _gitHubClient.PullRequest.Create(Helper.UserName, repoName, pullRequest); + var createdPullRequest = await _github.PullRequest.Create(Helper.UserName, repoName, pullRequest); var data = new PullRequestData { @@ -269,7 +264,7 @@ async Task CreateCommit(string repoName, string blobContent, string tree Encoding = EncodingType.Utf8 }; - var createdBlob = await _gitHubClient.GitDatabase.Blob.Create(Helper.UserName, repoName, blob); + var createdBlob = await _github.GitDatabase.Blob.Create(Helper.UserName, repoName, blob); // Creating a tree var newTree = new NewTree(); @@ -281,15 +276,15 @@ async Task CreateCommit(string repoName, string blobContent, string tree Sha = createdBlob.Sha, }); - var createdTree = await _gitHubClient.GitDatabase.Tree.Create(Helper.UserName, repoName, newTree); + var createdTree = await _github.GitDatabase.Tree.Create(Helper.UserName, repoName, newTree); var treeSha = createdTree.Sha; // Creating a commit - var parent = await _gitHubClient.GitDatabase.Reference.Get(Helper.UserName, repoName, reference); + var parent = await _github.GitDatabase.Reference.Get(Helper.UserName, repoName, reference); var commit = new NewCommit(commitMessage, treeSha, parent.Object.Sha); - var createdCommit = await _gitHubClient.GitDatabase.Commit.Create(Helper.UserName, repoName, commit); - await _gitHubClient.GitDatabase.Reference.Update(Helper.UserName, repoName, reference, new ReferenceUpdate(createdCommit.Sha)); + var createdCommit = await _github.GitDatabase.Commit.Create(Helper.UserName, repoName, commit); + await _github.GitDatabase.Reference.Update(Helper.UserName, repoName, reference, new ReferenceUpdate(createdCommit.Sha)); return createdCommit; } diff --git a/Octokit.Tests.Integration/Clients/PullRequestsClientTests.cs b/Octokit.Tests.Integration/Clients/PullRequestsClientTests.cs index 576b9e055f..003d9bcb63 100644 --- a/Octokit.Tests.Integration/Clients/PullRequestsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/PullRequestsClientTests.cs @@ -6,27 +6,26 @@ using Octokit.Tests.Helpers; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class PullRequestsClientTests : IDisposable { - readonly IGitHubClient _client; - readonly IPullRequestsClient _fixture; - readonly Repository _repository; - readonly IRepositoryCommentsClient _repositoryCommentsClient; + private readonly IGitHubClient _github; + private readonly IPullRequestsClient _fixture; + private readonly RepositoryContext _context; + private readonly IRepositoryCommentsClient _repositoryCommentsClient; const string branchName = "my-branch"; const string otherBranchName = "my-other-branch"; public PullRequestsClientTests() { - _client = Helper.GetAuthenticatedClient(); + _github = Helper.GetAuthenticatedClient(); - _fixture = _client.Repository.PullRequest; - _repositoryCommentsClient = _client.Repository.RepositoryComments; + _fixture = _github.Repository.PullRequest; + _repositoryCommentsClient = _github.Repository.RepositoryComments; - var repoName = Helper.MakeNameWithTimestamp("source-repo"); - - _repository = _client.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; + _context = _github.CreateRepositoryContext("source-repo").Result; } [IntegrationTest] @@ -35,7 +34,7 @@ public async Task CanCreate() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var result = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var result = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); Assert.Equal("a pull request", result.Title); Assert.False(result.Merged); @@ -47,9 +46,9 @@ public async Task CanGetForRepository() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var result = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var result = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); - var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _repository.Name); + var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _context.RepositoryName); Assert.Equal(1, pullRequests.Count); Assert.Equal(result.Title, pullRequests[0].Title); @@ -61,10 +60,10 @@ public async Task CanGetOpenPullRequest() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var result = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var result = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var openPullRequests = new PullRequestRequest { State = ItemState.Open }; - var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _repository.Name, openPullRequests); + var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _context.RepositoryName, openPullRequests); Assert.Equal(1, pullRequests.Count); Assert.Equal(result.Title, pullRequests[0].Title); @@ -76,10 +75,10 @@ public async Task IgnoresOpenPullRequest() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var openPullRequests = new PullRequestRequest { State = ItemState.Closed }; - var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _repository.Name, openPullRequests); + var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _context.RepositoryName, openPullRequests); Assert.Empty(pullRequests); } @@ -90,10 +89,10 @@ public async Task CanUpdate() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var updatePullRequest = new PullRequestUpdate { Title = "updated title", Body = "Hello New Body" }; - var result = await _fixture.Update(Helper.UserName, _repository.Name, pullRequest.Number, updatePullRequest); + var result = await _fixture.Update(Helper.UserName, _context.RepositoryName, pullRequest.Number, updatePullRequest); Assert.Equal(updatePullRequest.Title, result.Title); Assert.Equal(updatePullRequest.Body, result.Body); @@ -105,10 +104,10 @@ public async Task CanClose() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var updatePullRequest = new PullRequestUpdate { State = ItemState.Closed }; - var result = await _fixture.Update(Helper.UserName, _repository.Name, pullRequest.Number, updatePullRequest); + var result = await _fixture.Update(Helper.UserName, _context.RepositoryName, pullRequest.Number, updatePullRequest); Assert.Equal(ItemState.Closed, result.State); Assert.Equal(pullRequest.Title, result.Title); @@ -121,13 +120,13 @@ public async Task CanFindClosedPullRequest() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var updatePullRequest = new PullRequestUpdate { State = ItemState.Closed }; - await _fixture.Update(Helper.UserName, _repository.Name, pullRequest.Number, updatePullRequest); + await _fixture.Update(Helper.UserName, _context.RepositoryName, pullRequest.Number, updatePullRequest); var closedPullRequests = new PullRequestRequest { State = ItemState.Closed }; - var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _repository.Name, closedPullRequests); + var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _context.RepositoryName, closedPullRequests); Assert.Equal(1, pullRequests.Count); } @@ -138,20 +137,20 @@ public async Task CanSortPullRequests() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var newPullRequest2 = new NewPullRequest("another pull request", otherBranchName, "master"); - var anotherPullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest2); + var anotherPullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest2); var updatePullRequest = new PullRequestUpdate { Body = "This is the body" }; - await _fixture.Update(Helper.UserName, _repository.Name, pullRequest.Number, updatePullRequest); + await _fixture.Update(Helper.UserName, _context.RepositoryName, pullRequest.Number, updatePullRequest); var sortPullRequestsByUpdated = new PullRequestRequest { SortProperty = PullRequestSort.Updated, SortDirection = SortDirection.Ascending }; - var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _repository.Name, sortPullRequestsByUpdated); + var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _context.RepositoryName, sortPullRequestsByUpdated); Assert.Equal(anotherPullRequest.Title, pullRequests[0].Title); var sortPullRequestsByLongRunning = new PullRequestRequest { SortProperty = PullRequestSort.LongRunning }; - var pullRequestsByLongRunning = await _fixture.GetAllForRepository(Helper.UserName, _repository.Name, sortPullRequestsByLongRunning); + var pullRequestsByLongRunning = await _fixture.GetAllForRepository(Helper.UserName, _context.RepositoryName, sortPullRequestsByLongRunning); Assert.Equal(pullRequest.Title, pullRequestsByLongRunning[0].Title); } @@ -161,15 +160,15 @@ public async Task CanSpecifyDirectionOfSort() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var newPullRequest2 = new NewPullRequest("another pull request", otherBranchName, "master"); - var anotherPullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest2); + var anotherPullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest2); - var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _repository.Name, new PullRequestRequest { SortDirection = SortDirection.Ascending }); + var pullRequests = await _fixture.GetAllForRepository(Helper.UserName, _context.RepositoryName, new PullRequestRequest { SortDirection = SortDirection.Ascending }); Assert.Equal(pullRequest.Title, pullRequests[0].Title); - var pullRequestsDescending = await _fixture.GetAllForRepository(Helper.UserName, _repository.Name, new PullRequestRequest()); + var pullRequestsDescending = await _fixture.GetAllForRepository(Helper.UserName, _context.RepositoryName, new PullRequestRequest()); Assert.Equal(anotherPullRequest.Title, pullRequestsDescending[0].Title); } @@ -179,9 +178,9 @@ public async Task IsNotMergedInitially() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); - var result = await _fixture.Merged(Helper.UserName, _repository.Name, pullRequest.Number); + var result = await _fixture.Merged(Helper.UserName, _context.RepositoryName, pullRequest.Number); Assert.False(result); } @@ -192,10 +191,10 @@ public async Task CanBeMerged() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var merge = new MergePullRequest { CommitMessage = "thing the thing" }; - var result = await _fixture.Merge(Helper.UserName, _repository.Name, pullRequest.Number, merge); + var result = await _fixture.Merge(Helper.UserName, _context.RepositoryName, pullRequest.Number, merge); Assert.True(result.Merged); } @@ -206,10 +205,10 @@ public async Task CanBeMergedWithNoOptionalInput() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var merge = new MergePullRequest(); - var result = await _fixture.Merge(Helper.UserName, _repository.Name, pullRequest.Number, merge); + var result = await _fixture.Merge(Helper.UserName, _context.RepositoryName, pullRequest.Number, merge); Assert.True(result.Merged); } @@ -219,10 +218,10 @@ public async Task CanBeMergedWithShaSpecified() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var merge = new MergePullRequest { CommitMessage = "thing the thing", Sha = pullRequest.Head.Sha }; - var result = await _fixture.Merge(Helper.UserName, _repository.Name, pullRequest.Number, merge); + var result = await _fixture.Merge(Helper.UserName, _context.RepositoryName, pullRequest.Number, merge); Assert.True(result.Merged); } @@ -234,10 +233,10 @@ public async Task CannotBeMerged() var fakeSha = new string('f', 40); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var merge = new MergePullRequest { Sha = fakeSha }; - var ex = await Assert.ThrowsAsync(() => _fixture.Merge(Helper.UserName, _repository.Name, pullRequest.Number, merge)); + var ex = await Assert.ThrowsAsync(() => _fixture.Merge(Helper.UserName, _context.RepositoryName, pullRequest.Number, merge)); Assert.True(ex.ApiError.Message.StartsWith("Head branch was modified")); } @@ -248,12 +247,12 @@ public async Task UpdatesMaster() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); var merge = new MergePullRequest { CommitMessage = "thing the thing" }; - var result = await _fixture.Merge(Helper.UserName, _repository.Name, pullRequest.Number, merge); + var result = await _fixture.Merge(Helper.UserName, _context.RepositoryName, pullRequest.Number, merge); - var master = await _client.GitDatabase.Reference.Get(Helper.UserName, _repository.Name, "heads/master"); + var master = await _github.GitDatabase.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master"); Assert.Equal(result.Sha, master.Object.Sha); } @@ -264,9 +263,9 @@ public async Task CanBrowseCommits() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); - var result = await _fixture.Commits(Helper.UserName, _repository.Name, pullRequest.Number); + var result = await _fixture.Commits(Helper.UserName, _context.RepositoryName, pullRequest.Number); Assert.Equal(1, result.Count); Assert.Equal("this is the commit to merge into the pull request", result[0].Commit.Message); @@ -278,24 +277,24 @@ public async Task CanGetCommitsAndCommentCount() await CreateTheWorld(); var newPullRequest = new NewPullRequest("a pull request", branchName, "master"); - var pullRequest = await _fixture.Create(Helper.UserName, _repository.Name, newPullRequest); + var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest); // create new commit for branch const string commitMessage = "Another commit in branch"; - var branch = await _client.GitDatabase.Reference.Get(Helper.UserName, _repository.Name, "heads/" + branchName); + var branch = await _github.GitDatabase.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/" + branchName); var newTree = await CreateTree(new Dictionary { { "README.md", "Hello World!" } }); var newCommit = await CreateCommit(commitMessage, newTree.Sha, branch.Object.Sha); - await _client.GitDatabase.Reference.Update(Helper.UserName, _repository.Name, "heads/" + branchName, new ReferenceUpdate(newCommit.Sha)); + await _github.GitDatabase.Reference.Update(Helper.UserName, _context.RepositoryName, "heads/" + branchName, new ReferenceUpdate(newCommit.Sha)); - await _repositoryCommentsClient.Create(Helper.UserName, _repository.Name, newCommit.Sha, new NewCommitComment("I am a nice comment") { Path = "README.md", Position = 1 }); + await _repositoryCommentsClient.Create(Helper.UserName, _context.RepositoryName, newCommit.Sha, new NewCommitComment("I am a nice comment") { Path = "README.md", Position = 1 }); // don't try this at home await Task.Delay(TimeSpan.FromSeconds(5)); - var result = await _fixture.Commits(Helper.UserName, _repository.Name, pullRequest.Number); + var result = await _fixture.Commits(Helper.UserName, _context.RepositoryName, pullRequest.Number); Assert.Equal(2, result.Count); Assert.Equal("this is the commit to merge into the pull request", result[0].Commit.Message); @@ -330,26 +329,26 @@ public async Task CanBrowseFiles() async Task CreateTheWorld() { - var master = await _client.GitDatabase.Reference.Get(Helper.UserName, _repository.Name, "heads/master"); + var master = await _github.GitDatabase.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master"); // create new commit for master branch var newMasterTree = await CreateTree(new Dictionary { { "README.md", "Hello World!" } }); var newMaster = await CreateCommit("baseline for pull request", newMasterTree.Sha, master.Object.Sha); // update master - await _client.GitDatabase.Reference.Update(Helper.UserName, _repository.Name, "heads/master", new ReferenceUpdate(newMaster.Sha)); + await _github.GitDatabase.Reference.Update(Helper.UserName, _context.RepositoryName, "heads/master", new ReferenceUpdate(newMaster.Sha)); // create new commit for feature branch var featureBranchTree = await CreateTree(new Dictionary { { "README.md", "I am overwriting this blob with something new" } }); var featureBranchCommit = await CreateCommit("this is the commit to merge into the pull request", featureBranchTree.Sha, newMaster.Sha); // create branch - await _client.GitDatabase.Reference.Create(Helper.UserName, _repository.Name, new NewReference("refs/heads/my-branch", featureBranchCommit.Sha)); + await _github.GitDatabase.Reference.Create(Helper.UserName, _context.RepositoryName, new NewReference("refs/heads/my-branch", featureBranchCommit.Sha)); var otherFeatureBranchTree = await CreateTree(new Dictionary { { "README.md", "I am overwriting this blob with something else" } }); var otherFeatureBranchCommit = await CreateCommit("this is the other commit to merge into the other pull request", otherFeatureBranchTree.Sha, newMaster.Sha); - await _client.GitDatabase.Reference.Create(Helper.UserName, _repository.Name, new NewReference("refs/heads/my-other-branch", otherFeatureBranchCommit.Sha)); + await _github.GitDatabase.Reference.Create(Helper.UserName, _context.RepositoryName, new NewReference("refs/heads/my-other-branch", otherFeatureBranchCommit.Sha)); } async Task CreateTree(IEnumerable> treeContents) @@ -363,7 +362,7 @@ async Task CreateTree(IEnumerable> tr Content = c.Value, Encoding = EncodingType.Utf8 }; - var baselineBlobResult = await _client.GitDatabase.Blob.Create(Helper.UserName, _repository.Name, baselineBlob); + var baselineBlobResult = await _github.GitDatabase.Blob.Create(Helper.UserName, _context.RepositoryName, baselineBlob); collection.Add(new NewTreeItem { @@ -380,17 +379,17 @@ async Task CreateTree(IEnumerable> tr newTree.Tree.Add(item); } - return await _client.GitDatabase.Tree.Create(Helper.UserName, _repository.Name, newTree); + return await _github.GitDatabase.Tree.Create(Helper.UserName, _context.RepositoryName, newTree); } async Task CreateCommit(string message, string sha, string parent) { var newCommit = new NewCommit(message, sha, parent); - return await _client.GitDatabase.Commit.Create(Helper.UserName, _repository.Name, newCommit); + return await _github.GitDatabase.Commit.Create(Helper.UserName, _context.RepositoryName, newCommit); } public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/ReferencesClientTests.cs b/Octokit.Tests.Integration/Clients/ReferencesClientTests.cs index 903f84ee18..a2e1e924d4 100644 --- a/Octokit.Tests.Integration/Clients/ReferencesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/ReferencesClientTests.cs @@ -5,23 +5,21 @@ using Octokit.Tests.Helpers; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class ReferencesClientTests : IDisposable { - readonly IReferencesClient _fixture; - readonly Repository _repository; - readonly IGitHubClient _client; - readonly string _owner; + private readonly IGitHubClient _github; + private readonly IReferencesClient _fixture; + private readonly RepositoryContext _context; public ReferencesClientTests() { - _client = Helper.GetAuthenticatedClient(); + _github = Helper.GetAuthenticatedClient(); - _fixture = _client.GitDatabase.Reference; + _fixture = _github.GitDatabase.Reference; - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = _client.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - _owner = _repository.Owner.Login; + _context = _github.CreateRepositoryContext("public-repo").Result; } [IntegrationTest] @@ -79,7 +77,7 @@ public async Task CanCreateAReference() Content = "Hello World!", Encoding = EncodingType.Utf8 }; - var blobResult = await _client.GitDatabase.Blob.Create(_owner, _repository.Name, blob); + var blobResult = await _github.GitDatabase.Blob.Create(_context.RepositoryOwner, _context.RepositoryName, blob); var newTree = new NewTree(); newTree.Tree.Add(new NewTreeItem @@ -90,14 +88,14 @@ public async Task CanCreateAReference() Sha = blobResult.Sha }); - var treeResult = await _client.GitDatabase.Tree.Create(_owner, _repository.Name, newTree); + var treeResult = await _github.GitDatabase.Tree.Create(_context.RepositoryOwner, _context.RepositoryName, newTree); var newCommit = new NewCommit("This is a new commit", treeResult.Sha); - var commitResult = await _client.GitDatabase.Commit.Create(_owner, _repository.Name, newCommit); + var commitResult = await _github.GitDatabase.Commit.Create(_context.RepositoryOwner, _context.RepositoryName, newCommit); var newReference = new NewReference("heads/develop", commitResult.Sha); - var result = await _fixture.Create(_owner, _repository.Name, newReference); + var result = await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, newReference); Assert.Equal(commitResult.Sha, result.Object.Sha); } @@ -110,13 +108,13 @@ public async Task CanUpdateAReference() Content = "Hello World!", Encoding = EncodingType.Utf8 }; - var firstBlobResult = await _client.GitDatabase.Blob.Create(_owner, _repository.Name, firstBlob); + var firstBlobResult = await _github.GitDatabase.Blob.Create(_context.RepositoryOwner, _context.RepositoryName, firstBlob); var secondBlob = new NewBlob { Content = "This is a test!", Encoding = EncodingType.Utf8 }; - var secondBlobResult = await _client.GitDatabase.Blob.Create(_owner, _repository.Name, secondBlob); + var secondBlobResult = await _github.GitDatabase.Blob.Create(_context.RepositoryOwner, _context.RepositoryName, secondBlob); var firstTree = new NewTree(); firstTree.Tree.Add(new NewTreeItem @@ -127,12 +125,12 @@ public async Task CanUpdateAReference() Sha = firstBlobResult.Sha }); - var firstTreeResult = await _client.GitDatabase.Tree.Create(_owner, _repository.Name, firstTree); + var firstTreeResult = await _github.GitDatabase.Tree.Create(_context.RepositoryOwner, _context.RepositoryName, firstTree); var firstCommit = new NewCommit("This is a new commit", firstTreeResult.Sha); - var firstCommitResult = await _client.GitDatabase.Commit.Create(_owner, _repository.Name, firstCommit); + var firstCommitResult = await _github.GitDatabase.Commit.Create(_context.RepositoryOwner, _context.RepositoryName, firstCommit); var newReference = new NewReference("heads/develop", firstCommitResult.Sha); - await _fixture.Create(_owner, _repository.Name, newReference); + await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, newReference); var secondTree = new NewTree(); secondTree.Tree.Add(new NewTreeItem @@ -143,14 +141,14 @@ public async Task CanUpdateAReference() Sha = secondBlobResult.Sha }); - var secondTreeResult = await _client.GitDatabase.Tree.Create(_owner, _repository.Name, secondTree); + var secondTreeResult = await _github.GitDatabase.Tree.Create(_context.RepositoryOwner, _context.RepositoryName, secondTree); var secondCommit = new NewCommit("This is a new commit", secondTreeResult.Sha, firstCommitResult.Sha); - var secondCommitResult = await _client.GitDatabase.Commit.Create(_owner, _repository.Name, secondCommit); + var secondCommitResult = await _github.GitDatabase.Commit.Create(_context.RepositoryOwner, _context.RepositoryName, secondCommit); var referenceUpdate = new ReferenceUpdate(secondCommitResult.Sha); - var result = await _fixture.Update(_owner, _repository.Name, "heads/develop", referenceUpdate); + var result = await _fixture.Update(_context.RepositoryOwner, _context.RepositoryName, "heads/develop", referenceUpdate); Assert.Equal(secondCommitResult.Sha, result.Object.Sha); } @@ -163,7 +161,7 @@ public async Task CanDeleteAReference() Content = "Hello World!", Encoding = EncodingType.Utf8 }; - var blobResult = await _client.GitDatabase.Blob.Create(_owner, _repository.Name, blob); + var blobResult = await _github.GitDatabase.Blob.Create(_context.RepositoryOwner, _context.RepositoryName, blob); var newTree = new NewTree(); newTree.Tree.Add(new NewTreeItem @@ -174,24 +172,24 @@ public async Task CanDeleteAReference() Sha = blobResult.Sha }); - var treeResult = await _client.GitDatabase.Tree.Create(_owner, _repository.Name, newTree); + var treeResult = await _github.GitDatabase.Tree.Create(_context.RepositoryOwner, _context.RepositoryName, newTree); var newCommit = new NewCommit("This is a new commit", treeResult.Sha); - var commitResult = await _client.GitDatabase.Commit.Create(_owner, _repository.Name, newCommit); + var commitResult = await _github.GitDatabase.Commit.Create(_context.RepositoryOwner, _context.RepositoryName, newCommit); var newReference = new NewReference("heads/develop", commitResult.Sha); - await _fixture.Create(_owner, _repository.Name, newReference); - await _fixture.Delete(_owner, _repository.Name, "heads/develop"); + await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, newReference); + await _fixture.Delete(_context.RepositoryOwner, _context.RepositoryName, "heads/develop"); - var all = await _fixture.GetAll(_owner, _repository.Name); + var all = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName); Assert.Empty(all.Where(r => r.Ref == "heads/develop")); } public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/ReleasesClientTests.cs b/Octokit.Tests.Integration/Clients/ReleasesClientTests.cs index a0826051fa..5f280b3796 100644 --- a/Octokit.Tests.Integration/Clients/ReleasesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/ReleasesClientTests.cs @@ -11,20 +11,15 @@ public class ReleasesClientTests { public class TheGetReleasesMethod : IDisposable { - readonly IReleasesClient _releaseClient; - readonly Repository _repository; - readonly string _repositoryOwner; - readonly string _repositoryName; + private readonly IReleasesClient _releaseClient; + private readonly RepositoryContext _context; public TheGetReleasesMethod() { var github = Helper.GetAuthenticatedClient(); _releaseClient = github.Release; - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = github.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - _repositoryOwner = _repository.Owner.Login; - _repositoryName = _repository.Name; + _context = github.CreateRepositoryContext("public-repo").Result; } [IntegrationTest] @@ -41,9 +36,9 @@ public async Task ReturnsReleasesWithNullPublishDate() { // create a release without a publish date var releaseWithNoUpdate = new NewRelease("0.1") { Draft = true }; - await _releaseClient.Create(_repositoryOwner, _repositoryName, releaseWithNoUpdate); + await _releaseClient.Create(_context.RepositoryOwner, _context.RepositoryName, releaseWithNoUpdate); - var releases = await _releaseClient.GetAll(_repositoryOwner, _repositoryName); + var releases = await _releaseClient.GetAll(_context.RepositoryOwner, _context.RepositoryName); Assert.True(releases.Count == 1); Assert.False(releases.First().PublishedAt.HasValue); @@ -51,40 +46,36 @@ public async Task ReturnsReleasesWithNullPublishDate() public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } public class TheEditMethod : IDisposable { - readonly IReleasesClient _releaseClient; - readonly Repository _repository; - readonly string _repositoryOwner; - readonly string _repositoryName; - readonly IGitHubClient github; + private readonly IGitHubClient _github; + private readonly RepositoryContext _context; + private readonly IReleasesClient _releaseClient; public TheEditMethod() { - github = Helper.GetAuthenticatedClient(); - _releaseClient = github.Release; + _github = Helper.GetAuthenticatedClient(); + _releaseClient = _github.Release; + + _context = _github.CreateRepositoryContext("public-repo").Result; - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = github.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - _repositoryOwner = _repository.Owner.Login; - _repositoryName = _repository.Name; } [IntegrationTest] public async Task CanChangeBodyOfRelease() { var releaseWithNoUpdate = new NewRelease("0.1") { Draft = true }; - var release = await _releaseClient.Create(_repositoryOwner, _repositoryName, releaseWithNoUpdate); + var release = await _releaseClient.Create(_context.RepositoryOwner, _context.RepositoryName, releaseWithNoUpdate); var editRelease = release.ToUpdate(); editRelease.Body = "**This is an updated release"; editRelease.Draft = false; - var updatedRelease = await _releaseClient.Edit(_repositoryOwner, _repositoryName, release.Id, editRelease); + var updatedRelease = await _releaseClient.Edit(_context.RepositoryOwner, _context.RepositoryName, release.Id, editRelease); Assert.Equal(release.Id, updatedRelease.Id); Assert.False(updatedRelease.Draft); @@ -96,17 +87,17 @@ public async Task CanChangeBodyOfRelease() public async Task CanChangeCommitIshOfRelease() { var releaseWithNoUpdate = new NewRelease("0.1") { Draft = true }; - var release = await _releaseClient.Create(_repositoryOwner, _repositoryName, releaseWithNoUpdate); + var release = await _releaseClient.Create(_context.RepositoryOwner, _context.RepositoryName, releaseWithNoUpdate); Assert.Equal("master", release.TargetCommitish); - var newHead = await github.CreateTheWorld(_repository); + var newHead = await _github.CreateTheWorld(_context.Repository); var editRelease = release.ToUpdate(); editRelease.Draft = false; editRelease.TargetCommitish = newHead.Object.Sha; - var updatedRelease = await _releaseClient.Edit(_repositoryOwner, _repositoryName, release.Id, editRelease); + var updatedRelease = await _releaseClient.Edit(_context.RepositoryOwner, _context.RepositoryName, release.Id, editRelease); Assert.Equal(release.Id, updatedRelease.Id); Assert.False(updatedRelease.Draft); @@ -115,34 +106,29 @@ public async Task CanChangeCommitIshOfRelease() public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } public class TheUploadAssetMethod : IDisposable { - readonly IReleasesClient _releaseClient; - readonly Repository _repository; - readonly string _repositoryOwner; - readonly string _repositoryName; readonly IGitHubClient _github; + readonly RepositoryContext _context; + readonly IReleasesClient _releaseClient; public TheUploadAssetMethod() { _github = Helper.GetAuthenticatedClient(); _releaseClient = _github.Release; - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = _github.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - _repositoryOwner = _repository.Owner.Login; - _repositoryName = _repository.Name; + _context = _github.CreateRepositoryContext("public-repo").Result; } [IntegrationTest] public async Task CanUploadAndRetrieveAnAsset() { var releaseWithNoUpdate = new NewRelease("0.1") { Draft = true }; - var release = await _releaseClient.Create(_repositoryOwner, _repositoryName, releaseWithNoUpdate); + var release = await _releaseClient.Create(_context.RepositoryOwner, _context.RepositoryName, releaseWithNoUpdate); var stream = Helper.LoadFixture("hello-world.txt"); @@ -152,7 +138,7 @@ public async Task CanUploadAndRetrieveAnAsset() Assert.True(result.Id > 0); - var assets = await _releaseClient.GetAllAssets(_repositoryOwner, _repositoryName, release.Id); + var assets = await _releaseClient.GetAllAssets(_context.RepositoryOwner, _context.RepositoryName, release.Id); Assert.Equal(1, assets.Count); var asset = assets[0]; @@ -165,19 +151,19 @@ public async Task CanUploadAndRetrieveAnAsset() public async Task CanEditAnAssetLabel() { var releaseWithNoUpdate = new NewRelease("0.1") { Draft = true }; - var release = await _releaseClient.Create(_repositoryOwner, _repositoryName, releaseWithNoUpdate); + var release = await _releaseClient.Create(_context.RepositoryOwner, _context.RepositoryName, releaseWithNoUpdate); var stream = Helper.LoadFixture("hello-world.txt"); var newAsset = new ReleaseAssetUpload("hello-world.txt", "text/plain", stream, null); var result = await _releaseClient.UploadAsset(release, newAsset); - var asset = await _releaseClient.GetAsset(_repositoryOwner, _repositoryName, result.Id); + var asset = await _releaseClient.GetAsset(_context.RepositoryOwner, _context.RepositoryName, result.Id); var updateAsset = asset.ToUpdate(); updateAsset.Label = "some other thing"; - var updatedAsset = await _releaseClient.EditAsset(_repositoryOwner, _repositoryName, result.Id, updateAsset); + var updatedAsset = await _releaseClient.EditAsset(_context.RepositoryOwner, _context.RepositoryName, result.Id, updateAsset); Assert.Equal("some other thing", updatedAsset.Label); } @@ -186,7 +172,7 @@ public async Task CanEditAnAssetLabel() public async Task CanDownloadAnAsset() { var releaseWithNoUpdate = new NewRelease("0.1") { Draft = true }; - var release = await _releaseClient.Create(_repositoryOwner, _repositoryName, releaseWithNoUpdate); + var release = await _releaseClient.Create(_context.RepositoryOwner, _context.RepositoryName, releaseWithNoUpdate); var stream = Helper.LoadFixture("hello-world.txt"); @@ -196,7 +182,7 @@ public async Task CanDownloadAnAsset() Assert.True(result.Id > 0); - var asset = await _releaseClient.GetAsset(_repositoryOwner, _repositoryName, result.Id); + var asset = await _releaseClient.GetAsset(_context.RepositoryOwner, _context.RepositoryName, result.Id); Assert.Equal(result.Id, asset.Id); @@ -207,7 +193,7 @@ public async Task CanDownloadAnAsset() public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } } diff --git a/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs index 0c6786b74d..31273268a1 100644 --- a/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Octokit; using Octokit.Tests.Integration; +using Octokit.Tests.Integration.Helpers; using Xunit; public class RepositoriesClientTests @@ -16,10 +17,10 @@ public async Task CreatesANewPublicRepository() var github = Helper.GetAuthenticatedClient(); var repoName = Helper.MakeNameWithTimestamp("public-repo"); - var createdRepository = await github.Repository.Create(new NewRepository(repoName)); - - try + using (var context = await github.CreateRepositoryContext(new NewRepository(repoName))) { + var createdRepository = context.Repository; + var cloneUrl = string.Format("https://github.com/{0}/{1}.git", Helper.UserName, repoName); Assert.Equal(repoName, createdRepository.Name); Assert.False(createdRepository.Private); @@ -34,10 +35,6 @@ public async Task CreatesANewPublicRepository() Assert.Null(repository.Homepage); Assert.NotNull(repository.DefaultBranch); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [PaidAccountTest] @@ -53,26 +50,14 @@ public async Task CreatesANewPrivateRepository() var repoName = Helper.MakeNameWithTimestamp("private-repo"); - Repository createdRepository = null; - - try + using (var context = await github.CreateRepositoryContext(new NewRepository(repoName) { Private = true })) { - createdRepository = await github.Repository.Create(new NewRepository(repoName) - { - Private = true - }); + var createdRepository = context.Repository; Assert.True(createdRepository.Private); var repository = await github.Repository.Get(Helper.UserName, repoName); Assert.True(repository.Private); } - finally - { - if (createdRepository != null) - { - Helper.DeleteRepo(createdRepository); - } - } } [IntegrationTest] @@ -82,21 +67,16 @@ public async Task CreatesARepositoryWithoutDownloads() var repoName = Helper.MakeNameWithTimestamp("repo-without-downloads"); - var createdRepository = await github.Repository.Create(new NewRepository(repoName) - { - HasDownloads = false - }); + var newRepository = new NewRepository(repoName) { HasDownloads = false }; - try + using (var context = await github.CreateRepositoryContext(newRepository)) { + var createdRepository = context.Repository; + Assert.False(createdRepository.HasDownloads); var repository = await github.Repository.Get(Helper.UserName, repoName); Assert.False(repository.HasDownloads); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [IntegrationTest] @@ -106,21 +86,14 @@ public async Task CreatesARepositoryWithoutIssues() var repoName = Helper.MakeNameWithTimestamp("repo-without-issues"); - var createdRepository = await github.Repository.Create(new NewRepository(repoName) + using (var context = await github.CreateRepositoryContext(new NewRepository(repoName) { HasIssues = false })) { - HasIssues = false - }); + var createdRepository = context.Repository; - try - { Assert.False(createdRepository.HasIssues); var repository = await github.Repository.Get(Helper.UserName, repoName); Assert.False(repository.HasIssues); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [IntegrationTest] @@ -130,21 +103,14 @@ public async Task CreatesARepositoryWithoutAWiki() var repoName = Helper.MakeNameWithTimestamp("repo-without-wiki"); - var createdRepository = await github.Repository.Create(new NewRepository(repoName) + using (var context = await github.CreateRepositoryContext(new NewRepository(repoName) { HasWiki = false })) { - HasWiki = false - }); + var createdRepository = context.Repository; - try - { Assert.False(createdRepository.HasWiki); var repository = await github.Repository.Get(Helper.UserName, repoName); Assert.False(repository.HasWiki); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [IntegrationTest] @@ -154,21 +120,14 @@ public async Task CreatesARepositoryWithADescription() var repoName = Helper.MakeNameWithTimestamp("repo-with-description"); - var createdRepository = await github.Repository.Create(new NewRepository(repoName) + using (var context = await github.CreateRepositoryContext(new NewRepository(repoName) { Description = "theDescription" })) { - Description = "theDescription" - }); + var createdRepository = context.Repository; - try - { Assert.Equal("theDescription", createdRepository.Description); var repository = await github.Repository.Get(Helper.UserName, repoName); Assert.Equal("theDescription", repository.Description); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [IntegrationTest] @@ -178,21 +137,14 @@ public async Task CreatesARepositoryWithAHomepage() var repoName = Helper.MakeNameWithTimestamp("repo-with-homepage"); - var createdRepository = await github.Repository.Create(new NewRepository(repoName) + using (var context = await github.CreateRepositoryContext(new NewRepository(repoName) { Homepage = "http://aUrl.to/nowhere" })) { - Homepage = "http://aUrl.to/nowhere" - }); + var createdRepository = context.Repository; - try - { Assert.Equal("http://aUrl.to/nowhere", createdRepository.Homepage); var repository = await github.Repository.Get(Helper.UserName, repoName); Assert.Equal("http://aUrl.to/nowhere", repository.Homepage); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [IntegrationTest] @@ -202,22 +154,15 @@ public async Task CreatesARepositoryWithAutoInit() var repoName = Helper.MakeNameWithTimestamp("repo-with-autoinit"); - var createdRepository = await github.Repository.Create(new NewRepository(repoName) + using (var context = await github.CreateRepositoryContext(new NewRepository(repoName) { AutoInit = true })) { - AutoInit = true - }); + var createdRepository = context.Repository; - try - { // TODO: Once the contents API has been added, check the actual files in the created repo Assert.Equal(repoName, createdRepository.Name); var repository = await github.Repository.Get(Helper.UserName, repoName); Assert.Equal(repoName, repository.Name); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [IntegrationTest] @@ -226,23 +171,21 @@ public async Task CreatesARepositoryWithAGitignoreTemplate() var github = Helper.GetAuthenticatedClient(); var repoName = Helper.MakeNameWithTimestamp("repo-with-gitignore"); - var createdRepository = await github.Repository.Create(new NewRepository(repoName) + var newRepository = new NewRepository(repoName) { AutoInit = true, GitignoreTemplate = "VisualStudio" - }); + }; - try + using (var context = await github.CreateRepositoryContext(newRepository)) { + var createdRepository = context.Repository; + // TODO: Once the contents API has been added, check the actual files in the created repo Assert.Equal(repoName, createdRepository.Name); var repository = await github.Repository.Get(Helper.UserName, repoName); Assert.Equal(repoName, repository.Name); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [IntegrationTest] @@ -251,10 +194,11 @@ public async Task ThrowsRepositoryExistsExceptionForExistingRepository() var github = Helper.GetAuthenticatedClient(); var repoName = Helper.MakeNameWithTimestamp("existing-repo"); var repository = new NewRepository(repoName); - var createdRepository = await github.Repository.Create(repository); - try + using (var context = await github.CreateRepositoryContext(repository)) { + var createdRepository = context.Repository; + var message = string.Format(CultureInfo.InvariantCulture, "There is already a repository named '{0}' for the current account.", repoName); var thrown = await Assert.ThrowsAsync( @@ -265,10 +209,6 @@ public async Task ThrowsRepositoryExistsExceptionForExistingRepository() Assert.Equal(message, thrown.Message); Assert.False(thrown.OwnerIsOrganization); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [PaidAccountTest] @@ -284,7 +224,7 @@ public async Task ThrowsPrivateRepositoryQuotaExceededExceptionWhenOverQuota() throw new Exception("Test cannot complete, account is on free plan"); } - var createRepoTasks = + var createRepoTasks = Enumerable.Range(0, (int)freePrivateSlots) .Select(x => { @@ -319,10 +259,10 @@ public async Task CreatesANewPublicRepository() var repoName = Helper.MakeNameWithTimestamp("public-org-repo"); - var createdRepository = await github.Repository.Create(Helper.Organization, new NewRepository(repoName)); - - try + using (var context = await github.CreateRepositoryContext(Helper.Organization, new NewRepository(repoName))) { + var createdRepository = context.Repository; + var cloneUrl = string.Format("https://github.com/{0}/{1}.git", Helper.Organization, repoName); Assert.Equal(repoName, createdRepository.Name); Assert.False(createdRepository.Private); @@ -336,10 +276,6 @@ public async Task CreatesANewPublicRepository() Assert.True(repository.HasWiki); Assert.Null(repository.Homepage); } - finally - { - Helper.DeleteRepo(createdRepository); - } } [OrganizationTest] @@ -350,10 +286,11 @@ public async Task ThrowsRepositoryExistsExceptionForExistingRepository() var repoName = Helper.MakeNameWithTimestamp("existing-org-repo"); var repository = new NewRepository(repoName); - var createdRepository = await github.Repository.Create(Helper.Organization, repository); - try + using (var context = await github.CreateRepositoryContext(Helper.Organization, repository)) { + var createdRepository = context.Repository; + var repositoryUrl = string.Format(CultureInfo.InvariantCulture, "https://github.com/{0}/{1}", Helper.Organization, repository.Name); var message = string.Format(CultureInfo.InvariantCulture, "There is already a repository named '{0}' in the organization '{1}'.", repository.Name, Helper.Organization); @@ -367,13 +304,9 @@ public async Task ThrowsRepositoryExistsExceptionForExistingRepository() Assert.Equal(Helper.Organization, thrown.Organization); Assert.Equal(repositoryUrl, thrown.ExistingRepositoryWebUrl.ToString()); } - finally - { - Helper.DeleteRepo(createdRepository); - } } - // TODO: Add a test for the team_id param once an overload that takes an oranization is added + // TODO: Add a test for the team_id param once an overload that takes an organization is added } public class TheEditMethod : IDisposable diff --git a/Octokit.Tests.Integration/Clients/RepositoryCommitsClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoryCommitsClientTests.cs index b1a33019a9..fd15b8ca93 100644 --- a/Octokit.Tests.Integration/Clients/RepositoryCommitsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoryCommitsClientTests.cs @@ -5,6 +5,7 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class RepositoryCommitsClientTests { @@ -90,19 +91,17 @@ public async Task CanGetCommitWithRenamedFiles() public class TestsWithNewRepository : IDisposable { - readonly IGitHubClient _client; - readonly IRepositoryCommitsClient _fixture; - readonly Repository _repository; + private readonly IGitHubClient _github; + private readonly IRepositoryCommitsClient _fixture; + private readonly RepositoryContext _context; public TestsWithNewRepository() { - _client = Helper.GetAuthenticatedClient(); - - _fixture = _client.Repository.Commits; + _github = Helper.GetAuthenticatedClient(); - var repoName = Helper.MakeNameWithTimestamp("source-repo"); + _fixture = _github.Repository.Commits; - _repository = _client.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; + _context = _github.CreateRepositoryContext("source-repo").Result; } [IntegrationTest] @@ -110,7 +109,7 @@ public async Task CanCompareReferences() { await CreateTheWorld(); - var result = await _fixture.Compare(Helper.UserName, _repository.Name, "master", "my-branch"); + var result = await _fixture.Compare(Helper.UserName, _context.RepositoryName, "master", "my-branch"); Assert.Equal(1, result.TotalCommits); Assert.Equal(1, result.Commits.Count); @@ -123,7 +122,7 @@ public async Task CanCompareReferencesOtherWayRound() { await CreateTheWorld(); - var result = await _fixture.Compare(Helper.UserName, _repository.Name, "my-branch", "master"); + var result = await _fixture.Compare(Helper.UserName, _context.RepositoryName, "my-branch", "master"); Assert.Equal(0, result.TotalCommits); Assert.Equal(0, result.Commits.Count); @@ -136,7 +135,7 @@ public async Task ReturnsUrlsToResources() { await CreateTheWorld(); - var result = await _fixture.Compare(Helper.UserName, _repository.Name, "my-branch", "master"); + var result = await _fixture.Compare(Helper.UserName, _context.RepositoryName, "my-branch", "master"); Assert.NotNull(result.DiffUrl); Assert.NotNull(result.HtmlUrl); @@ -149,10 +148,10 @@ public async Task CanCompareUsingSha() { await CreateTheWorld(); - var master = await _client.GitDatabase.Reference.Get(Helper.UserName, _repository.Name, "heads/master"); - var branch = await _client.GitDatabase.Reference.Get(Helper.UserName, _repository.Name, "heads/my-branch"); + var master = await _github.GitDatabase.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master"); + var branch = await _github.GitDatabase.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/my-branch"); - var result = await _fixture.Compare(Helper.UserName, _repository.Name, master.Object.Sha, branch.Object.Sha); + var result = await _fixture.Compare(Helper.UserName, _context.RepositoryName, master.Object.Sha, branch.Object.Sha); Assert.Equal(1, result.Commits.Count); Assert.Equal(1, result.AheadBy); @@ -161,21 +160,21 @@ public async Task CanCompareUsingSha() async Task CreateTheWorld() { - var master = await _client.GitDatabase.Reference.Get(Helper.UserName, _repository.Name, "heads/master"); + var master = await _github.GitDatabase.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master"); // create new commit for master branch var newMasterTree = await CreateTree(new Dictionary { { "README.md", "Hello World!" } }); var newMaster = await CreateCommit("baseline for pull request", newMasterTree.Sha, master.Object.Sha); // update master - await _client.GitDatabase.Reference.Update(Helper.UserName, _repository.Name, "heads/master", new ReferenceUpdate(newMaster.Sha)); + await _github.GitDatabase.Reference.Update(Helper.UserName, _context.RepositoryName, "heads/master", new ReferenceUpdate(newMaster.Sha)); // create new commit for feature branch var featureBranchTree = await CreateTree(new Dictionary { { "README.md", "I am overwriting this blob with something new" } }); var newFeature = await CreateCommit("this is the commit to merge into the pull request", featureBranchTree.Sha, newMaster.Sha); // create branch - await _client.GitDatabase.Reference.Create(Helper.UserName, _repository.Name, new NewReference("refs/heads/my-branch", newFeature.Sha)); + await _github.GitDatabase.Reference.Create(Helper.UserName, _context.RepositoryName, new NewReference("refs/heads/my-branch", newFeature.Sha)); } async Task CreateTree(IDictionary treeContents) @@ -189,7 +188,7 @@ async Task CreateTree(IDictionary treeContents) Content = c.Value, Encoding = EncodingType.Utf8 }; - var baselineBlobResult = await _client.GitDatabase.Blob.Create(Helper.UserName, _repository.Name, baselineBlob); + var baselineBlobResult = await _github.GitDatabase.Blob.Create(Helper.UserName, _context.RepositoryName, baselineBlob); collection.Add(new NewTreeItem { @@ -206,18 +205,18 @@ async Task CreateTree(IDictionary treeContents) newTree.Tree.Add(item); } - return await _client.GitDatabase.Tree.Create(Helper.UserName, _repository.Name, newTree); + return await _github.GitDatabase.Tree.Create(Helper.UserName, _context.RepositoryName, newTree); } async Task CreateCommit(string message, string sha, string parent) { var newCommit = new NewCommit(message, sha, parent); - return await _client.GitDatabase.Commit.Create(Helper.UserName, _repository.Name, newCommit); + return await _github.GitDatabase.Commit.Create(Helper.UserName, _context.RepositoryName, newCommit); } public void Dispose() { - _client.Repository.Delete(_repository.Owner.Login, _repository.Name); + _context.Dispose(); } } } \ No newline at end of file diff --git a/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs index cdec191820..2cb44e7ff8 100644 --- a/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Threading.Tasks; +using Octokit.Tests.Integration.Helpers; using Xunit; namespace Octokit.Tests.Integration.Clients @@ -70,13 +71,12 @@ public async Task GetsDirectoryContent() public async Task CrudTest() { var client = Helper.GetAuthenticatedClient(); + var fixture = client.Repository.Content; + var repoName = Helper.MakeNameWithTimestamp("source-repo"); - Repository repository = null; - try + using (var context = await client.CreateRepositoryContext(new NewRepository(repoName) { AutoInit = true })) { - var fixture = client.Repository.Content; - var repoName = Helper.MakeNameWithTimestamp("source-repo"); - repository = await client.Repository.Create(new NewRepository(repoName) { AutoInit = true }); + var repository = context.Repository; var file = await fixture.CreateFile( repository.Owner.Login, @@ -109,10 +109,6 @@ await fixture.DeleteFile( await Assert.ThrowsAsync( async () => await fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt")); } - finally - { - Helper.DeleteRepo(repository); - } } [IntegrationTest(Skip = "this will probably take too long")] diff --git a/Octokit.Tests.Integration/Clients/RepositoryDeployKeysClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoryDeployKeysClientTests.cs index 215923630a..cb490c1129 100644 --- a/Octokit.Tests.Integration/Clients/RepositoryDeployKeysClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoryDeployKeysClientTests.cs @@ -3,25 +3,22 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class RepositoryDeployKeysClientTests : IDisposable { const string _key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDB8IE5+RppLpeW+6lqo0fpfvMunKg6W4bhYCfVJIOYbpKoHP95nTUMZPBT++9NLeB4/YsuNTCrrpnpjc4f2IVpGvloRiVXjAzoJk9QIL6uzn1zRFdvaxSJ3Urhe9LcLHcIgccgZgSdWGzaZI3xtMvGC4diwWNsPjvVc/RyDM/MPqAim0X5XVOQwEFsSsUSraezJ+VgYMYzLYBcKWW0B86HVVhL4ZtmcY/RN2544bljnzw2M3aQvXNPTvkuiUoqLOI+5/qzZ8PfkruO55YtweEd0lkY6oZvrBPMD6dLODEqMHb4tD6htx60wSipNqjPwpOMpzp0Bk3G909unVXi6Fw5"; const string _keyTitle = "octokit@github"; - readonly IRepositoryDeployKeysClient _fixture; - readonly Repository _repository; - readonly string _owner; + private readonly RepositoryContext _context; + private readonly IRepositoryDeployKeysClient _fixture; public RepositoryDeployKeysClientTests() { - var client = Helper.GetAuthenticatedClient(); - - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _fixture = client.Repository.DeployKeys; - _repository = client.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - _owner = _repository.Owner.Login; + var github = Helper.GetAuthenticatedClient(); + _fixture = github.Repository.DeployKeys; + _context = github.CreateRepositoryContext("public-repo").Result; } [IntegrationTest(Skip = "see https://github.com/octokit/octokit.net/issues/533 for the resolution to this failing test")] @@ -33,7 +30,7 @@ public async Task CanCreateADeployKey() Title = _keyTitle }; - var deployKeyResult = await _fixture.Create(_owner, _repository.Name, deployKey); + var deployKeyResult = await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, deployKey); Assert.NotNull(deployKeyResult); Assert.Equal(_key, deployKeyResult.Key); Assert.Equal(_keyTitle, deployKeyResult.Title); @@ -43,7 +40,7 @@ public async Task CanCreateADeployKey() [IntegrationTest] public async Task CanRetrieveAllDeployKeys() { - var deployKeys = await _fixture.GetAll(_owner, _repository.Name); + var deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName); Assert.Equal(0, deployKeys.Count); var deployKey = new NewDeployKey() @@ -52,9 +49,9 @@ public async Task CanRetrieveAllDeployKeys() Title = _keyTitle }; - await _fixture.Create(_owner, _repository.Name, deployKey); + await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, deployKey); - deployKeys = await _fixture.GetAll(_owner, _repository.Name); + deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName); Assert.Equal(1, deployKeys.Count); Assert.Equal(_key, deployKeys[0].Key); Assert.Equal(_keyTitle, deployKeys[0].Title); @@ -68,9 +65,9 @@ public async Task CanRetrieveADeployKey() Key = _key, Title = _keyTitle }; - var deployKeyResult = await _fixture.Create(_owner, _repository.Name, newDeployKey); + var deployKeyResult = await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, newDeployKey); - var deployKey = await _fixture.Get(_owner, _repository.Name, deployKeyResult.Id); + var deployKey = await _fixture.Get(_context.RepositoryOwner, _context.RepositoryName, deployKeyResult.Id); Assert.NotNull(deployKey); Assert.Equal(deployKeyResult.Id, deployKey.Id); Assert.Equal(_key, deployKey.Key); @@ -86,20 +83,20 @@ public async Task CanRemoveADeployKey() Title = _keyTitle }; - await _fixture.Create(_owner, _repository.Name, newDeployKey); + await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, newDeployKey); - var deployKeys = await _fixture.GetAll(_owner, _repository.Name); + var deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName); Assert.Equal(1, deployKeys.Count); Assert.Equal(_key, deployKeys[0].Key); Assert.Equal(_keyTitle, deployKeys[0].Title); - await _fixture.Delete(_owner, _repository.Name, deployKeys[0].Id); - deployKeys = await _fixture.GetAll(_owner, _repository.Name); + await _fixture.Delete(_context.RepositoryOwner, _context.RepositoryName, deployKeys[0].Id); + deployKeys = await _fixture.GetAll(_context.RepositoryOwner, _context.RepositoryName); Assert.Equal(0, deployKeys.Count); } public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Clients/StatisticsClientTests.cs b/Octokit.Tests.Integration/Clients/StatisticsClientTests.cs index a6b21bc5bf..938a72d638 100644 --- a/Octokit.Tests.Integration/Clients/StatisticsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/StatisticsClientTests.cs @@ -1,4 +1,5 @@ -using System.Linq; +using Octokit.Tests.Integration.Helpers; +using System.Linq; using System.Threading.Tasks; using Xunit; @@ -6,99 +7,106 @@ namespace Octokit.Tests.Integration.Clients { public class StatisticsClientTests { - readonly IGitHubClient _client; - readonly ICommitsClient _fixture; + private readonly IGitHubClient _client; + private readonly ICommitsClient _fixture; public StatisticsClientTests() { _client = Helper.GetAuthenticatedClient(); - _fixture = _client.GitDatabase.Commit; } [IntegrationTest] public async Task CanCreateAndRetrieveContributors() { - var repository = await CreateRepository(); - await CommitToRepository(repository); - var contributors = await _client.Repository.Statistics.GetContributors(repository.Owner, repository.Name); + using (var context = await _client.CreateRepositoryContext("public-repo")) + { + var repository = new RepositorySummary(context); + await CommitToRepository(repository); + var contributors = await _client.Repository.Statistics.GetContributors(repository.Owner, repository.Name); - Assert.NotNull(contributors); - Assert.Equal(1, contributors.Count); + Assert.NotNull(contributors); + Assert.Equal(1, contributors.Count); - var soleContributor = contributors.First(); - Assert.NotNull(soleContributor.Author); - Assert.True(soleContributor.Author.Login == repository.Owner); + var soleContributor = contributors.First(); + Assert.NotNull(soleContributor.Author); + Assert.True(soleContributor.Author.Login == repository.Owner); - Assert.Equal(1, soleContributor.Weeks.Count); - Assert.Equal(1, soleContributor.Total); + Assert.Equal(1, soleContributor.Weeks.Count); + Assert.Equal(1, soleContributor.Total); + } } [IntegrationTest] public async Task CanCreateAndRetrieveEmptyContributors() { - var repository = await CreateRepository(autoInit: false); - var contributors = await _client.Repository.Statistics.GetContributors(repository.Owner, repository.Name); + var newRepository = new NewRepository(Helper.MakeNameWithTimestamp("public-repo")) { AutoInit = false }; + using (var context = await _client.CreateRepositoryContext(newRepository)) + { + var repository = new RepositorySummary(context); + var contributors = await _client.Repository.Statistics.GetContributors(repository.Owner, repository.Name); - Assert.NotNull(contributors); - Assert.Empty(contributors); + Assert.NotNull(contributors); + Assert.Empty(contributors); + } } [IntegrationTest] public async Task CanGetCommitActivityForTheLastYear() { - var repository = await CreateRepository(); - await CommitToRepository(repository); - var commitActivities = await _client.Repository.Statistics.GetCommitActivity(repository.Owner, repository.Name); - Assert.NotNull(commitActivities); - Assert.Equal(52, commitActivities.Activity.Count); - - var thisWeek = commitActivities.Activity.Last(); - Assert.Equal(1, thisWeek.Total); - Assert.NotNull(thisWeek.Days); + using (var context = await _client.CreateRepositoryContext("public-repo")) + { + var repository = new RepositorySummary(context); + await CommitToRepository(repository); + var commitActivities = await _client.Repository.Statistics.GetCommitActivity(repository.Owner, repository.Name); + Assert.NotNull(commitActivities); + Assert.Equal(52, commitActivities.Activity.Count); + + var thisWeek = commitActivities.Activity.Last(); + Assert.Equal(1, thisWeek.Total); + Assert.NotNull(thisWeek.Days); + } } [IntegrationTest] public async Task CanGetAdditionsAndDeletionsPerWeek() { - var repository = await CreateRepository(); - await CommitToRepository(repository); - var commitActivities = await _client.Repository.Statistics.GetCodeFrequency(repository.Owner, repository.Name); - Assert.NotNull(commitActivities); - Assert.True(commitActivities.AdditionsAndDeletionsByWeek.Any()); + using (var context = await _client.CreateRepositoryContext("public-repo")) + { + var repository = new RepositorySummary(context); + await CommitToRepository(repository); + var commitActivities = await _client.Repository.Statistics.GetCodeFrequency(repository.Owner, repository.Name); + Assert.NotNull(commitActivities); + Assert.True(commitActivities.AdditionsAndDeletionsByWeek.Any()); + } } [IntegrationTest] public async Task CanGetParticipationStatistics() { - var repository = await CreateRepository(); - await CommitToRepository(repository); - var weeklyCommitCounts = await _client.Repository.Statistics.GetParticipation(repository.Owner, repository.Name); - Assert.Equal(52, weeklyCommitCounts.All.Count); + using (var context = await _client.CreateRepositoryContext("public-repo")) + { + var repository = new RepositorySummary(context); + await CommitToRepository(repository); + var weeklyCommitCounts = await _client.Repository.Statistics.GetParticipation(repository.Owner, repository.Name); + Assert.Equal(52, weeklyCommitCounts.All.Count); + } } [IntegrationTest] public async Task CanGetPunchCardForRepository() { - var repository = await CreateRepository(); - await CommitToRepository(repository); - var punchCard = await _client.Repository.Statistics.GetPunchCard(repository.Owner, repository.Name); - Assert.NotNull(punchCard); - Assert.NotNull(punchCard.PunchPoints); - } - - async Task CreateRepository(bool autoInit = true) - { - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - var repository = await _client.Repository.Create(new NewRepository(repoName) { AutoInit = autoInit }); - return new RepositorySummary + using (var context = await _client.CreateRepositoryContext("public-repo")) { - Owner = repository.Owner.Login, - Name = repoName - }; + var repository = new RepositorySummary(context); + await CommitToRepository(repository); + var punchCard = await _client.Repository.Statistics.GetPunchCard(repository.Owner, repository.Name); + Assert.NotNull(punchCard); + Assert.NotNull(punchCard.PunchPoints); + } } - async Task CommitToRepository(RepositorySummary repositorySummary) + private async Task CommitToRepository(RepositorySummary repositorySummary) { var owner = repositorySummary.Owner; var repository = repositorySummary.Name; @@ -126,11 +134,17 @@ async Task CommitToRepository(RepositorySummary repositorySummary) return commit; } - class RepositorySummary + private class RepositorySummary { - public string Name { get; set; } + public RepositorySummary(RepositoryContext context) + { + Name = context.Repository.Name; + Owner = context.Repository.Owner.Login; + } + + public string Name { get; private set; } - public string Owner { get; set; } + public string Owner { get; private set; } } } } diff --git a/Octokit.Tests.Integration/Clients/TreeClientTests.cs b/Octokit.Tests.Integration/Clients/TreeClientTests.cs index 94d544b73b..d0f7e2f530 100644 --- a/Octokit.Tests.Integration/Clients/TreeClientTests.cs +++ b/Octokit.Tests.Integration/Clients/TreeClientTests.cs @@ -3,23 +3,21 @@ using Octokit; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class TreeClientTests : IDisposable { - readonly ITreesClient _fixture; - readonly Repository _repository; - readonly string _owner; - readonly IGitHubClient _client; + private readonly IGitHubClient _github; + private readonly ITreesClient _fixture; + private readonly RepositoryContext _context; public TreeClientTests() { - _client = Helper.GetAuthenticatedClient(); + _github = Helper.GetAuthenticatedClient(); - _fixture = _client.GitDatabase.Tree; + _fixture = _github.GitDatabase.Tree; - var repoName = Helper.MakeNameWithTimestamp("public-repo"); - _repository = _client.Repository.Create(new NewRepository(repoName) { AutoInit = true }).Result; - _owner = _repository.Owner.Login; + _context = _github.CreateRepositoryContext("public-repo").Result; } [IntegrationTest] @@ -31,7 +29,7 @@ public async Task CanCreateATree() Encoding = EncodingType.Utf8 }; - var createdBlob = await _client.GitDatabase.Blob.Create(_owner, _repository.Name, blob); + var createdBlob = await _github.GitDatabase.Blob.Create(_context.RepositoryOwner, _context.RepositoryName, blob); var newTree = new NewTree(); newTree.Tree.Add(new NewTreeItem @@ -42,7 +40,7 @@ public async Task CanCreateATree() Mode = FileMode.File }); - var result = await _fixture.Create(_owner, _repository.Name, newTree); + var result = await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, newTree); Assert.NotNull(result); } @@ -65,7 +63,7 @@ public async Task CanGetACreatedTree() Encoding = EncodingType.Utf8 }; - var blobResult = await _client.GitDatabase.Blob.Create(_owner, _repository.Name, blob); + var blobResult = await _github.GitDatabase.Blob.Create(_context.RepositoryOwner, _context.RepositoryName, blob); var newTree = new NewTree(); newTree.Tree.Add(new NewTreeItem @@ -76,9 +74,9 @@ public async Task CanGetACreatedTree() Mode = FileMode.File }); - var tree = await _fixture.Create(_owner, _repository.Name, newTree); + var tree = await _fixture.Create(_context.RepositoryOwner, _context.RepositoryName, newTree); - var result = await _fixture.Get(_owner, _repository.Name, tree.Sha); + var result = await _fixture.Get(_context.RepositoryOwner, _context.RepositoryName, tree.Sha); Assert.NotNull(result); Assert.Equal(1, result.Tree.Count); @@ -86,6 +84,6 @@ public async Task CanGetACreatedTree() public void Dispose() { - Helper.DeleteRepo(_repository); + _context.Dispose(); } } diff --git a/Octokit.Tests.Integration/Helpers/GithubClientExtensions.cs b/Octokit.Tests.Integration/Helpers/GithubClientExtensions.cs new file mode 100644 index 0000000000..58c53d0512 --- /dev/null +++ b/Octokit.Tests.Integration/Helpers/GithubClientExtensions.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Octokit.Tests.Integration.Helpers +{ + internal static class GithubClientExtensions + { + internal async static Task CreateRepositoryContext(this IGitHubClient client, string repositoryName) + { + var repoName = Helper.MakeNameWithTimestamp(repositoryName); + var repo = await client.Repository.Create(new NewRepository(repoName) { AutoInit = true }); + + return new RepositoryContext(repo); + } + + internal async static Task CreateRepositoryContext(this IGitHubClient client, string organizationLogin, NewRepository newRepository) + { + var repo = await client.Repository.Create(organizationLogin, newRepository); + + return new RepositoryContext(repo); + } + + internal async static Task CreateRepositoryContext(this IGitHubClient client, NewRepository newRepository) + { + var repo = await client.Repository.Create(newRepository); + + return new RepositoryContext(repo); + } + } +} \ No newline at end of file diff --git a/Octokit.Tests.Integration/Helpers/RepositoryContext.cs b/Octokit.Tests.Integration/Helpers/RepositoryContext.cs new file mode 100644 index 0000000000..d16afd36de --- /dev/null +++ b/Octokit.Tests.Integration/Helpers/RepositoryContext.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Octokit.Tests.Integration.Helpers +{ + internal sealed class RepositoryContext : IDisposable + { + internal RepositoryContext(Repository repo) + { + Repository = repo; + RepositoryOwner = repo.Owner.Login; + RepositoryName = repo.Name; + } + + internal string RepositoryOwner { get; private set; } + internal string RepositoryName { get; private set; } + + internal Repository Repository { get; private set; } + + public void Dispose() + { + Helper.DeleteRepo(Repository); + } + } +} diff --git a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj index f6154d2829..7398edbe13 100644 --- a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj +++ b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj @@ -102,8 +102,10 @@ + + diff --git a/Octokit.Tests.Integration/Reactive/ObservableIssuesClientTests.cs b/Octokit.Tests.Integration/Reactive/ObservableIssuesClientTests.cs index a1745ee49a..5eb94af756 100644 --- a/Octokit.Tests.Integration/Reactive/ObservableIssuesClientTests.cs +++ b/Octokit.Tests.Integration/Reactive/ObservableIssuesClientTests.cs @@ -5,21 +5,21 @@ using System.Threading.Tasks; using Octokit.Tests.Integration; using Xunit; +using Octokit.Tests.Integration.Helpers; public class ObservableIssuesClientTests : IDisposable { - readonly ObservableIssuesClient _client; - readonly string _repoName; - readonly Repository _createdRepository; + private readonly RepositoryContext _context; + private readonly ObservableIssuesClient _client; public ObservableIssuesClientTests() { var github = Helper.GetAuthenticatedClient(); _client = new ObservableIssuesClient(github); - _repoName = Helper.MakeNameWithTimestamp("public-repo"); - var result = github.Repository.Create(new NewRepository(_repoName)).Result; - _createdRepository = result; + + var repoName = Helper.MakeNameWithTimestamp("public-repo"); + _context = github.CreateRepositoryContext(new NewRepository(repoName)).Result; } [IntegrationTest] @@ -43,8 +43,8 @@ public async Task ReturnsAllIssuesForARepository() [IntegrationTest] public async Task ReturnsAllIssuesForCurrentUser() { - var newIssue = new NewIssue("Integration test issue") { Assignee = _createdRepository.Owner.Login }; - await _client.Create(_createdRepository.Owner.Login, _repoName, newIssue); + var newIssue = new NewIssue("Integration test issue") { Assignee = _context.RepositoryOwner }; + await _client.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); var issues = await _client.GetAllForCurrent().ToList(); @@ -54,8 +54,8 @@ public async Task ReturnsAllIssuesForCurrentUser() [IntegrationTest] public async Task ReturnsAllIssuesForOwnedAndMemberRepositories() { - var newIssue = new NewIssue("Integration test issue") { Assignee = _createdRepository.Owner.Login }; - await _client.Create(_createdRepository.Owner.Login, _repoName, newIssue); + var newIssue = new NewIssue("Integration test issue") { Assignee = _context.RepositoryOwner }; + await _client.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); var result = await _client.GetAllForOwnedAndMemberRepositories().ToList(); Assert.NotEmpty(result); @@ -66,15 +66,15 @@ public async Task CanCreateAndUpdateIssues() { var newIssue = new NewIssue("Integration test issue"); - var createResult = await _client.Create(_createdRepository.Owner.Login, _repoName, newIssue); - var updateResult = await _client.Update(_createdRepository.Owner.Login, _repoName, createResult.Number, new IssueUpdate { Title = "Modified integration test issue" }); + var createResult = await _client.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue); + var updateResult = await _client.Update(_context.RepositoryOwner, _context.RepositoryName, createResult.Number, new IssueUpdate { Title = "Modified integration test issue" }); Assert.Equal("Modified integration test issue", updateResult.Title); } public void Dispose() { - Helper.DeleteRepo(_createdRepository); + _context.Dispose(); } }