Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Alaurant committed Dec 19, 2024
1 parent bdb8131 commit 33ff1ce
Show file tree
Hide file tree
Showing 30 changed files with 1,765 additions and 12 deletions.
21 changes: 18 additions & 3 deletions src/main/java/org/kohsuke/github/GHRepositoryForkBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class GHRepositoryForkBuilder {
private String name;
private Boolean defaultBranchOnly;

static int FORK_RETRY_INTERVAL = 3000;

/**
* Instantiates a new Gh repository fork builder.
*
Expand Down Expand Up @@ -89,7 +91,7 @@ public GHRepository create() throws IOException {
if (r != null) {
return r;
}
sleep(3000);
sleep(FORK_RETRY_INTERVAL);
}
throw new IOException(createTimeoutMessage());
}
Expand All @@ -103,15 +105,28 @@ private GHRepository lookupForkedRepository() throws IOException {
return repo.root().getMyself().getRepository(repoName);
}

private void sleep(int millis) throws IOException {
/**
* Sleep.
*
* @param millis
* the millis
* @throws IOException
* the io exception
*/
protected void sleep(int millis) throws IOException {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw (IOException) new InterruptedIOException().initCause(e);
}
}

Check warning on line 122 in src/main/java/org/kohsuke/github/GHRepositoryForkBuilder.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kohsuke/github/GHRepositoryForkBuilder.java#L118-L122

Added lines #L118 - L122 were not covered by tests

private String createTimeoutMessage() {
/**
* Create timeout message string.
*
* @return the string
*/
String createTimeoutMessage() {
StringBuilder message = new StringBuilder(repo.getFullName());
message.append(" was forked");

Expand Down
87 changes: 78 additions & 9 deletions src/test/java/org/kohsuke/github/GHRepositoryForkBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;

import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;

// TODO: Auto-generated Javadoc

Expand Down Expand Up @@ -45,11 +46,6 @@ public void setUp() throws Exception {

private void verifyBasicForkProperties(GHRepository original, GHRepository forked, String expectedName)
throws IOException, InterruptedException {
assertThat(forked, notNullValue());
assertThat(forked.getName(), equalTo(expectedName));
assertThat(forked.isFork(), is(true));
assertThat(forked.getParent().getFullName(), equalTo(original.getFullName()));

GHRepository updatedFork = forked;
for (int i = 0; i < 10; i++) {
Thread.sleep(3000);
Expand All @@ -58,6 +54,10 @@ private void verifyBasicForkProperties(GHRepository original, GHRepository forke
break;
}
}
assertThat(updatedFork, notNullValue());
assertThat(updatedFork.getName(), equalTo(expectedName));
assertThat(updatedFork.isFork(), is(true));
assertThat(updatedFork.getParent().getFullName(), equalTo(original.getFullName()));
}

private void verifyBranches(GHRepository forked, boolean defaultBranchOnly) throws IOException {
Expand All @@ -81,7 +81,6 @@ public void testFork() throws Exception {
// GHRepository forkedRepo = repo.createFork().create();
GHRepository forkedRepo = repo.fork();

Thread.sleep(3000);
verifyBasicForkProperties(repo, forkedRepo, repo.getName());
verifyBranches(forkedRepo, false);

Expand All @@ -100,7 +99,6 @@ public void testForkToOrg() throws Exception {
// GHRepository forkedRepo = repo.createFork().organization(targetOrg).create();
GHRepository forkedRepo = repo.forkTo(targetOrg);

Thread.sleep(3000);
verifyBasicForkProperties(repo, forkedRepo, repo.getName());
verifyBranches(forkedRepo, false);

Expand All @@ -117,7 +115,6 @@ public void testForkToOrg() throws Exception {
public void testForkDefaultBranchOnly() throws Exception {
GHRepository forkedRepo = repo.createFork().defaultBranchOnly(true).create();

Thread.sleep(3000);
verifyBasicForkProperties(repo, forkedRepo, repo.getName());
verifyBranches(forkedRepo, true);

Expand All @@ -135,12 +132,84 @@ public void testForkChangedName() throws Exception {
String newRepoName = "test-fork-with-new-name";
GHRepository forkedRepo = repo.createFork().name(newRepoName).create();

Thread.sleep(3000);
assertThat(forkedRepo.getName(), equalTo(newRepoName));
verifyBasicForkProperties(repo, forkedRepo, newRepoName);
verifyBranches(forkedRepo, false);

forkedRepo.delete();
}

/**
* Test create timeout message.
*/
@Test
public void testCreateTimeoutMessage() {
GitHub mockGitHub = mock(GitHub.class);
Requester mockRequester = mock(Requester.class);
GHRepository mockRepo = mock(GHRepository.class);

when(mockRepo.getFullName()).thenReturn("test/mock-repo");
when(mockRepo.root()).thenReturn(mockGitHub);
when(mockGitHub.createRequest()).thenReturn(mockRequester);

GHRepositoryForkBuilder builder = new GHRepositoryForkBuilder(mockRepo);

assertThat(builder.createTimeoutMessage(),
equalTo("test/mock-repo was forked but can't find the new repository"));

GHOrganization org = mock(GHOrganization.class);
when(org.getLogin()).thenReturn(TARGET_ORG);
builder.organization(org);

assertThat(builder.createTimeoutMessage(),
equalTo("test/mock-repo was forked into " + TARGET_ORG + " but can't find the new repository"));

String customName = "custom-fork-name";
builder.name(customName);

assertThat(builder.createTimeoutMessage(),
equalTo("test/mock-repo was forked into " + TARGET_ORG + " with name " + customName
+ " but can't find the new repository"));
}

/**
* Test timeout.
*/
@Test
public void testTimeout() {
class TrackingSleepBuilder extends GHRepositoryForkBuilder {
int sleepCount = 0;
int lastSleepMillis = 0;

TrackingSleepBuilder(GHRepository repo) {
super(repo);
}

@Override
protected void sleep(int millis) throws IOException {
sleepCount++;
lastSleepMillis = millis;

}
}

int originalInterval = GHRepositoryForkBuilder.FORK_RETRY_INTERVAL;
try {
GHRepositoryForkBuilder.FORK_RETRY_INTERVAL = 100;

TrackingSleepBuilder builder = new TrackingSleepBuilder(repo);
try {
builder.create();
fail("Expected IOException for timeout");
} catch (IOException e) {
System.out.println("Exception message: " + e.getMessage());
assertThat(builder.sleepCount, equalTo(10));
assertThat(builder.lastSleepMillis, equalTo(100));
assertThat(e.getMessage(), containsString("but can't find the new repository"));
}
} finally {
GHRepositoryForkBuilder.FORK_RETRY_INTERVAL = originalInterval;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"login": "Alaurant",
"id": 41817560,
"node_id": "MDQ6VXNlcjQxODE3NTYw",
"avatar_url": "https://avatars.githubusercontent.com/u/41817560?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Alaurant",
"html_url": "https://github.com/Alaurant",
"followers_url": "https://api.github.com/users/Alaurant/followers",
"following_url": "https://api.github.com/users/Alaurant/following{/other_user}",
"gists_url": "https://api.github.com/users/Alaurant/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Alaurant/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Alaurant/subscriptions",
"organizations_url": "https://api.github.com/users/Alaurant/orgs",
"repos_url": "https://api.github.com/users/Alaurant/repos",
"events_url": "https://api.github.com/users/Alaurant/events{/privacy}",
"received_events_url": "https://api.github.com/users/Alaurant/received_events",
"type": "User",
"user_view_type": "private",
"site_admin": false,
"name": "Danyang Zhao",
"company": null,
"blog": "",
"location": "Brisbane, AUS",
"email": null,
"hireable": null,
"bio": null,
"twitter_username": null,
"notification_email": null,
"public_repos": 11,
"public_gists": 0,
"followers": 5,
"following": 11,
"created_at": "2018-07-28T07:03:48Z",
"updated_at": "2024-12-15T04:04:44Z",
"private_gists": 0,
"total_private_repos": 2,
"owned_private_repos": 2,
"disk_usage": 7314,
"collaborators": 0,
"two_factor_authentication": false,
"plan": {
"name": "pro",
"space": 976562499,
"collaborators": 0,
"private_repos": 9999
}
}
Loading

0 comments on commit 33ff1ce

Please sign in to comment.