Skip to content

Commit

Permalink
Create more exhaustive flag tests
Browse files Browse the repository at this point in the history
Finding that one flag does work made me nervous.
  • Loading branch information
bitwiseman committed Apr 1, 2020
1 parent 279df00 commit 02fd57d
Show file tree
Hide file tree
Showing 155 changed files with 10,562 additions and 561 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

Expand Down Expand Up @@ -217,7 +218,7 @@ protected void cleanupRepository(String fullName) throws IOException {
if (repository != null) {
repository.delete();
}
} catch (GHFileNotFoundException e) {
} catch (FileNotFoundException e) {
// Repo already deleted
}

Expand Down
159 changes: 126 additions & 33 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.regex.Pattern;

import static org.hamcrest.CoreMatchers.*;
Expand All @@ -29,8 +31,130 @@
public class AppTest extends AbstractGitHubWireMockTest {
static final String GITHUB_API_TEST_REPO = "github-api-test";

boolean workaroundFlagUpdateIssue = false;

@FunctionalInterface
public interface BiConsumerIOException<T, U> {

/**
* Performs this operation on the given arguments.
*
* @param t
* the first input argument
* @param u
* the second input argument
*
* @throws IOException
* if there is a problem
*/
void accept(T t, U u) throws IOException;
}

// This method tests that a flag can be initialized and changed as expected.
public void repoFlagAll(boolean defaultValue,
BiConsumer<GHCreateRepositoryBuilder, Boolean> builderAction,
Function<GHRepository, Boolean> readAction,
BiConsumerIOException<GHRepository, Boolean> updateAction) throws Exception {

// Test the default value when the initializer is not used
repoFlagValue(defaultValue, null, readAction, updateAction);

// Test that value can be initialized to the same as default
repoFlagValue(defaultValue, builderAction, readAction, updateAction);

// Test that value can be initialized to opposite of default
repoFlagValue(!defaultValue, builderAction, readAction, updateAction);
}

public void repoFlagValue(boolean startingValue,
BiConsumer<GHCreateRepositoryBuilder, Boolean> builderAction,
Function<GHRepository, Boolean> readAction,
BiConsumerIOException<GHRepository, Boolean> updateAction) throws Exception {
String name = "flag-test-" + this.mockGitHub.getMethodName();
cleanupUserRepository(name);
try {
// Build a repository
GHCreateRepositoryBuilder builder = gitHub.createRepository(name)
.description("a test repository for a flag")
.homepage("http://github-api.kohsuke.org/");

// If a builder action is supplied call it
if (builderAction != null) {
builderAction.accept(builder, startingValue);
}

// Create the repository
GHRepository r = builder.create();
assertThat("Verify starting value", readAction.apply(r), equalTo(startingValue));

// Flip the flag
updateAction.accept(r, !startingValue);
assertThat("Verify initial instance still has starting value", readAction.apply(r), equalTo(startingValue));

// Get updated version
r = gitHub.getRepository(r.getFullName());

// Issue #765: has_projects does not update correctly, but we still want to test everything else about it.
if (workaroundFlagUpdateIssue) {
startingValue = !startingValue;
}

assertThat("Verify value has been updated", readAction.apply(r), equalTo(!startingValue));
} finally {
cleanupUserRepository(name);
}
}

@Test
public void testRepoFlagHasIssues() throws Exception {
repoFlagAll(true, GHCreateRepositoryBuilder::issues, GHRepository::hasIssues, GHRepository::enableIssueTracker);
}

@Test
public void testRepoFlagHasDownloads() throws Exception {
repoFlagAll(true,
GHCreateRepositoryBuilder::downloads,
GHRepository::hasDownloads,
GHRepository::enableDownloads);
}

@Test
public void testRepoFlagHasWiki() throws Exception {
repoFlagAll(true, GHCreateRepositoryBuilder::wiki, GHRepository::hasWiki, GHRepository::enableWiki);
}

@Test
public void testRepoFlagAllowMergeCommit() throws Exception {
repoFlagAll(true,
GHCreateRepositoryBuilder::allowMergeCommit,
GHRepository::isAllowMergeCommit,
GHRepository::allowMergeCommit);
}

@Test
public void testRepoFlagAllowRebaseMerge() throws Exception {
repoFlagAll(true,
GHCreateRepositoryBuilder::allowRebaseMerge,
GHRepository::isAllowRebaseMerge,
GHRepository::allowRebaseMerge);
}

@Test
public void testRepoFlagAllowSquashMerge() throws Exception {
repoFlagAll(true,
GHCreateRepositoryBuilder::allowSquashMerge,
GHRepository::isAllowSquashMerge,
GHRepository::allowSquashMerge);
}

@Test
public void testRepoFlagHasProjects() throws Exception {
workaroundFlagUpdateIssue = true;
repoFlagAll(true, GHCreateRepositoryBuilder::projects, GHRepository::hasProjects, GHRepository::enableProjects);
}

@Test
public void testRepoCRUD() throws Exception {
public void testRepoRename() throws Exception {
String targetName = "github-api-test-rename2";

cleanupUserRepository("github-api-test-rename");
Expand All @@ -40,40 +164,9 @@ public void testRepoCRUD() throws Exception {
"a test repository",
"http://github-api.kohsuke.org/",
true);

assertThat(r.hasIssues(), is(true));
assertThat(r.hasWiki(), is(true));
assertThat(r.hasDownloads(), is(true));
assertThat(r.hasProjects(), is(true));

r.enableIssueTracker(false);
r.enableDownloads(false);
r.enableWiki(false);
r.enableProjects(false);

r.renameTo(targetName);

// local instance remains unchanged
assertThat(r.getName(), equalTo("github-api-test-rename"));
assertThat(r.hasIssues(), is(true));
assertThat(r.hasWiki(), is(true));
assertThat(r.hasDownloads(), is(true));
assertThat(r.hasProjects(), is(true));

r = gitHub.getMyself().getRepository(targetName);

// values are updated
assertThat(r.hasIssues(), is(false));
assertThat(r.hasWiki(), is(false));
assertThat(r.hasDownloads(), is(false));
assertThat(r.getName(), equalTo(targetName));

// ISSUE: #765
// From what I can tell this is a bug in GithHub.
// updating `has_projects` doesn't seem to do anything
assertThat(r.hasProjects(), is(true));

r.delete();
getUser().getRepository(targetName).delete();
}

@Test
Expand Down

This file was deleted.

Loading

0 comments on commit 02fd57d

Please sign in to comment.