Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some GitAPITestCase tests were replaced in GitClientTest #504

Merged
merged 9 commits into from
Feb 14, 2020
123 changes: 0 additions & 123 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -865,15 +865,6 @@ public void test_setRemoteURL_local_clone() throws Exception {
assertTrue("remote URL has not been updated", remotes.contains(originURL));
}

public void test_addRemoteUrl_local_clone() throws Exception {
w = clone(localMirror());
assertEquals("Wrong origin URL before add", localMirror(), w.git.getRemoteUrl("origin"));
String upstreamURL = "https://github.com/jenkinsci/git-client-plugin.git";
w.git.addRemoteUrl("upstream", upstreamURL);
assertEquals("Wrong upstream URL", upstreamURL, w.git.getRemoteUrl("upstream"));
assertEquals("Wrong origin URL after add", localMirror(), w.git.getRemoteUrl("origin"));
}

public void test_clean_with_parameter() throws Exception {
w.init();
w.commitEmpty("init");
Expand Down Expand Up @@ -2435,29 +2426,6 @@ public void assertFixSubmoduleUrlsThrows() throws InterruptedException {
}
}

public void test_addSubmodule() throws Exception {
String sub1 = "sub1-" + java.util.UUID.randomUUID().toString();
String readme1 = sub1 + File.separator + "README.adoc";
w.init();
assertFalse("submodule1 dir found too soon", w.file(sub1).exists());
assertFalse("submodule1 file found too soon", w.file(readme1).exists());

w.git.addSubmodule(localMirror(), sub1);
assertTrue("submodule1 dir not found after add", w.file(sub1).exists());
assertTrue("submodule1 file not found after add", w.file(readme1).exists());

w.igit().submoduleUpdate().recursive(false).execute();
assertTrue("submodule1 dir not found after add", w.file(sub1).exists());
assertTrue("submodule1 file not found after add", w.file(readme1).exists());

w.igit().submoduleUpdate().recursive(true).execute();
assertTrue("submodule1 dir not found after recursive update", w.file(sub1).exists());
assertTrue("submodule1 file found after recursive update", w.file(readme1).exists());

w.igit().submoduleSync();
assertFixSubmoduleUrlsThrows();
}

private File createTempDirectoryWithoutSpaces() throws IOException {
// JENKINS-56175 notes that the plugin does not support submodule URL's
// which contain a space character. Parent pom 3.36 and later use a
Expand Down Expand Up @@ -3846,27 +3814,6 @@ public void test_getHeadRev_returns_accurate_SHA1_values() throws Exception {
check_headRev(w.repoPath(), getMirrorHead());
}

private void check_changelog_sha1(final String sha1, final String branchName) throws InterruptedException
{
ChangelogCommand changelogCommand = w.git.changelog();
changelogCommand.max(1);
StringWriter writer = new StringWriter();
changelogCommand.to(writer);
changelogCommand.execute();
String splitLog[] = writer.toString().split("[\\n\\r]", 3); // Extract first line of changelog
assertEquals("Wrong changelog line 1 on branch " + branchName, "commit " + sha1, splitLog[0]);
}

public void test_changelog() throws Exception {
w = clone(localMirror());
String sha1Prev = w.git.revParse("HEAD").name();
w.touch("changelog-file", "changelog-file-content-" + sha1Prev);
w.git.add("changelog-file");
w.git.commit("changelog-commit-message");
String sha1 = w.git.revParse("HEAD").name();
check_changelog_sha1(sha1, "master");
}

public void test_show_revision_for_merge() throws Exception {
w = clone(localMirror());
ObjectId from = ObjectId.fromString("45e76942914664ee19f31d90e6f2edbfe0d13a46");
Expand Down Expand Up @@ -4117,76 +4064,6 @@ public void test_checkout_interrupted_with_existing_lock() throws Exception {
assertTrue("lock file '" + lockFile.getCanonicalPath() + " removed by cleanup", lockFile.exists());
}

@Issue("JENKINS-19108")
public void test_checkoutBranch() throws Exception {
w.init();
w.commitEmpty("c1");
w.tag("t1");
w.commitEmpty("c2");

w.git.checkoutBranch("foo", "t1");

assertEquals(w.head(),w.git.revParse("t1"));
assertEquals(w.head(),w.git.revParse("foo"));

Ref head = w.repo().exactRef("HEAD");
assertTrue(head.isSymbolic());
assertEquals("refs/heads/foo",head.getTarget().getName());
}

/**
* Test case for auto local branch creation behviour.
* This is essentially a stripped down version of {@link #test_branchContainingRemote()}
* @throws Exception on exceptions occur
*/
public void test_checkout_remote_autocreates_local() throws Exception {
final WorkingArea r = new WorkingArea();
r.init();
r.commitEmpty("c1");

w.git.clone_().url("file://" + r.repoPath()).execute();
final URIish remote = new URIish(Constants.DEFAULT_REMOTE_NAME);
final List<RefSpec> refspecs = Collections.singletonList(new RefSpec(
"refs/heads/*:refs/remotes/origin/*"));
w.git.fetch_().from(remote, refspecs).execute();
w.git.checkout().ref(Constants.MASTER).execute();

Set<String> refNames = w.git.getRefNames("refs/heads/");
assertThat(refNames, contains("refs/heads/master"));
}

public void test_autocreate_fails_on_multiple_matching_origins() throws Exception {
final WorkingArea r = new WorkingArea();
r.init();
r.commitEmpty("c1");

w.git.clone_().url("file://" + r.repoPath()).execute();
final URIish remote = new URIish(Constants.DEFAULT_REMOTE_NAME);

try ( // add second remote
FileRepository repo = w.repo()) {
StoredConfig config = repo.getConfig();
config.setString("remote", "upstream", "url", "file://" + r.repoPath());
config.setString("remote", "upstream", "fetch", "+refs/heads/*:refs/remotes/upstream/*");
config.save();
}

// fill both remote branches
List<RefSpec> refspecs = Collections.singletonList(new RefSpec(
"refs/heads/*:refs/remotes/origin/*"));
w.git.fetch_().from(remote, refspecs).execute();
refspecs = Collections.singletonList(new RefSpec(
"refs/heads/*:refs/remotes/upstream/*"));
w.git.fetch_().from(remote, refspecs).execute();

try {
w.git.checkout().ref(Constants.MASTER).execute();
fail("GitException expected");
} catch (GitException e) {
// expected
}
}

public void test_revList_remote_branch() throws Exception {
w = clone(localMirror());
List<ObjectId> revList = w.git.revList("origin/1.4.x");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@
import org.apache.commons.io.FileUtils;

import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.lib.Constants;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import org.junit.AfterClass;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -597,6 +600,70 @@ public void testAddRemoteUrl() throws Exception {
assertEquals(upstreamRepoURL, gitClient.getRemoteUrl("upstream"));
}

@Test
public void testAutocreateFailsOnMultipleMatchingOrigins() throws Exception {
File repoRootTemp = tempFolder.newFolder();
GitClient gitClientTemp = Git.with(TaskListener.NULL, new EnvVars()).in(repoRootTemp).using(gitImplName).getClient();
gitClientTemp.init();
loghijiaha marked this conversation as resolved.
Show resolved Hide resolved
FilePath gitClientFilePath = gitClientTemp.getWorkTree();
FilePath gitClientTempFile = gitClientFilePath.createTextTempFile("aPre", ".txt", "file contents");
gitClientTemp.add(".");
gitClientTemp.commit("Added " + gitClientTempFile.toURI().toString());
gitClient.clone_().url("file://" + repoRootTemp.getPath()).execute();
final URIish remote = new URIish(Constants.DEFAULT_REMOTE_NAME);

try ( // add second remote
FileRepository repo = new FileRepository(new File(repoRoot, ".git"))) {
StoredConfig config = repo.getConfig();
config.setString("remote", "upstream", "url", "file://" + repoRootTemp.getPath());
config.setString("remote", "upstream", "fetch", "+refs/heads/*:refs/remotes/upstream/*");
config.save();
}

// fill both remote branches
List<RefSpec> refspecs = Collections.singletonList(new RefSpec(
"refs/heads/*:refs/remotes/origin/*"));
gitClient.fetch_().from(remote, refspecs).execute();
refspecs = Collections.singletonList(new RefSpec(
"refs/heads/*:refs/remotes/upstream/*"));
gitClient.fetch_().from(remote, refspecs).execute();

// checkout will fail
try {
gitClient.checkout().ref(Constants.MASTER).execute();
} catch (GitException e) {
// expected
MarkEWaite marked this conversation as resolved.
Show resolved Hide resolved
Set<String> refNames = gitClient.getRefNames("refs/heads/");
assertFalse("RefNames will not contain master", refNames.contains("refs/heads/master"));
}

}

/**
* Test case for auto local branch creation behviour.
* This is essentially a stripped down version of {@link GitAPITestCase#test_branchContainingRemote()}
* @throws Exception on exceptions occur
*/
@Test
public void testCheckoutRemoteAutocreatesLocal() throws Exception {
File repoRootTemp = tempFolder.newFolder();
GitClient gitClientTemp = Git.with(TaskListener.NULL, new EnvVars()).in(repoRootTemp).using(gitImplName).getClient();
gitClientTemp.init();
FilePath gitClientFilePath = gitClientTemp.getWorkTree();
FilePath gitClientTempFile = gitClientFilePath.createTextTempFile("aPre", ".txt", "file contents");
gitClientTemp.add(".");
gitClientTemp.commit("Added " + gitClientTempFile.toURI().toString());
gitClient.clone_().url("file://" + repoRootTemp.getPath()).execute();
final URIish remote = new URIish(Constants.DEFAULT_REMOTE_NAME);
final List<RefSpec> refspecs = Collections.singletonList(new RefSpec(
"refs/heads/*:refs/remotes/origin/*"));
gitClient.fetch_().from(remote, refspecs).execute();
gitClient.checkout().ref(Constants.MASTER).execute();
MarkEWaite marked this conversation as resolved.
Show resolved Hide resolved

Set<String> refNames = gitClient.getRefNames("refs/heads/");
assertThat(refNames, contains("refs/heads/master"));
}

private void assertFileInWorkingDir(GitClient client, String fileName) {
File fileInRepo = new File(repoRoot, fileName);
assertTrue(fileInRepo.getAbsolutePath() + " not found", fileInRepo.isFile());
Expand Down