Skip to content

Commit

Permalink
Merge pull request #789 from MarkEWaite/adapt-to-git-main-branch
Browse files Browse the repository at this point in the history
Adapt tests to new default branch name
  • Loading branch information
MarkEWaite committed Dec 24, 2021
2 parents 4d3512a + b5bc115 commit a1f50ff
Show file tree
Hide file tree
Showing 15 changed files with 22 additions and 55 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -6,6 +6,8 @@
target/
work/
.@tmp/
.vs/
.vscode/

.project
.classpath
Expand All @@ -25,4 +27,4 @@ tags
/nbproject/

# Mac OSX
.DS_Store
.DS_Store
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/git/GitTool.java
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import static hudson.init.InitMilestone.EXTENSIONS_AUGMENTED;
Expand Down Expand Up @@ -124,6 +125,7 @@ public static void onLoaded() {
GitTool[] installations = getInstallations(descriptor);

if (installations != null && installations.length > 0) {
LOGGER.log(Level.FINEST, "Already initialized GitTool, no need to initialize again");
//No need to initialize if there's already something
return;
}
Expand Down
Expand Up @@ -65,7 +65,7 @@ public static void createTestRepo() throws IOException, InterruptedException {
public static void computeDefaultBranchName() throws Exception {
File configDir = Files.createTempDirectory("readGitConfig").toFile();
CliGitCommand getDefaultBranchNameCmd = new CliGitCommand(Git.with(TaskListener.NULL, new hudson.EnvVars()).in(configDir).using("git").getClient());
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--global", "--get", "init.defaultBranch");
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--get", "init.defaultBranch");
for (String s : output) {
String result = s.trim();
if (result != null && !result.isEmpty()) {
Expand Down
Expand Up @@ -136,7 +136,7 @@ public static void loadLocalMirror() throws Exception {
public static void computeDefaultBranchName() throws Exception {
File configDir = Files.createTempDirectory("readGitConfig").toFile();
CliGitCommand getDefaultBranchNameCmd = new CliGitCommand(Git.with(TaskListener.NULL, new hudson.EnvVars()).in(configDir).using("git").getClient());
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--global", "--get", "init.defaultBranch");
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--get", "init.defaultBranch");
for (String s : output) {
String result = s.trim();
if (result != null && !result.isEmpty()) {
Expand Down
Expand Up @@ -408,7 +408,7 @@ private String getDefaultBranchName() throws Exception {
String defaultBranchValue = "mast" + "er"; // Intentionally split to note this will remain
File configDir = Files.createTempDirectory("readGitConfig").toFile();
CliGitCommand getDefaultBranchNameCmd = new CliGitCommand(Git.with(TaskListener.NULL, env).in(configDir).using("git").getClient());
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--global", "--get", "init.defaultBranch");
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--get", "init.defaultBranch");
for (String s : output) {
String result = s.trim();
if (result != null && !result.isEmpty()) {
Expand Down
Expand Up @@ -91,7 +91,7 @@ public GitClientFetchTest(final String gitImplName) {
public static void computeDefaultBranchName() throws Exception {
File configDir = Files.createTempDirectory("readGitConfig").toFile();
CliGitCommand getDefaultBranchNameCmd = new CliGitCommand(Git.with(TaskListener.NULL, new hudson.EnvVars()).in(configDir).using("git").getClient());
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--global", "--get", "init.defaultBranch");
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--get", "init.defaultBranch");
for (String s : output) {
String result = s.trim();
if (result != null && !result.isEmpty()) {
Expand Down
Expand Up @@ -224,7 +224,7 @@ public static void mirrorUpstreamRepositoryLocally() throws Exception {
public static void computeDefaultBranchName() throws Exception {
File configDir = Files.createTempDirectory("readGitConfig").toFile();
CliGitCommand getDefaultBranchNameCmd = new CliGitCommand(Git.with(TaskListener.NULL, new EnvVars()).in(configDir).using("git").getClient());
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--global", "--get", "init.defaultBranch");
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--get", "init.defaultBranch");
for (String s : output) {
String result = s.trim();
if (result != null && !result.isEmpty()) {
Expand Down
@@ -1,13 +1,11 @@
package org.jenkinsci.plugins.gitclient;

import hudson.Launcher;
import hudson.model.TaskListener;
import hudson.plugins.git.GitException;
import hudson.plugins.git.IndexEntry;
import hudson.plugins.git.Revision;
import hudson.plugins.git.Tag;
import hudson.util.StreamTaskListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
Expand All @@ -16,12 +14,10 @@
import java.util.List;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -60,7 +56,7 @@ public LegacyCompatibleGitAPIImplTest() {
public static void computeDefaultBranchName() throws Exception {
File configDir = Files.createTempDirectory("readGitConfig").toFile();
CliGitCommand getDefaultBranchNameCmd = new CliGitCommand(Git.with(TaskListener.NULL, new hudson.EnvVars()).in(configDir).using("git").getClient());
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--global", "--get", "init.defaultBranch");
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--get", "init.defaultBranch");
for (String s : output) {
String result = s.trim();
if (result != null && !result.isEmpty()) {
Expand Down Expand Up @@ -101,33 +97,6 @@ private File touch(String path, String content) throws IOException {
return f;
}

private String cmd(boolean ignoreError, String... args) throws IOException, InterruptedException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int st = new Launcher.LocalLauncher(listener).launch().pwd(repo).cmds(args).
envs(env).stdout(out).join();
String s = out.toString();
if (!ignoreError) {
if (s == null || s.isEmpty()) {
s = StringUtils.join(args, ' ');
}
assertEquals(s, 0, st); /* Reports full output of failing commands */

}
return s;
}

private String log() throws IOException, InterruptedException {
return cmd(false, "git", "log", "-n", "3");
}

private String log(ObjectId rev) throws IOException, InterruptedException {
return cmd(false, "git", "log", "-n", "3", rev.name());
}

private String contentOf(File file) throws IOException {
return FileUtils.readFileToString(file, "UTF-8");
}

@Test
@Deprecated
public void testCloneRemoteConfig() throws URISyntaxException, InterruptedException, IOException, ConfigInvalidException {
Expand All @@ -143,7 +112,6 @@ public void testCloneRemoteConfig() throws URISyntaxException, InterruptedExcept
+ "fetch = +refs/heads/*:refs/remotes/" + remoteName + "/*\n";
config.fromText(configText);
RemoteConfig remoteConfig = new RemoteConfig(config, remoteName);
List<URIish> list = remoteConfig.getURIs();
git.clone(remoteConfig);
File[] files = git.workspace.listFiles();
assertEquals(files.length + "files in " + Arrays.toString(files), 1, files.length);
Expand Down Expand Up @@ -176,14 +144,14 @@ private File commitTrackedFile() throws IOException, GitException, InterruptedEx
@Test
@Deprecated
public void testShowRevisionThrowsGitException() throws Exception {
File trackedFile = commitTrackedFile();
commitTrackedFile();
assertThrows(GitException.class, () -> git.showRevision(new Revision(gitClientCommit)));
}

@Test
@Deprecated
public void testShowRevisionTrackedFile() throws Exception {
File trackedFile = commitTrackedFile();
commitTrackedFile();
ObjectId head = git.getHeadRev(repo.getPath(), defaultBranchName);
List<String> revisions = git.showRevision(new Revision(head));
assertEquals("commit " + head.name(), revisions.get(0));
Expand All @@ -199,7 +167,7 @@ public void testGetTagsOnCommit_empty() throws Exception {
@Test
@Deprecated
public void testGetTagsOnCommit_non_empty() throws Exception {
File trackedFile = commitTrackedFile();
commitTrackedFile();
List<Tag> result = git.getTagsOnCommit(taggedCommit.name());
assertTrue("Tag list not empty: " + result, result.isEmpty());
}
Expand All @@ -216,7 +184,7 @@ public void testGetTagsOnCommit_SHA1() throws Exception {
@Deprecated
public void testGetTagsOnCommit() throws Exception {
LegacyCompatibleGitAPIImpl myGit = (LegacyCompatibleGitAPIImpl) Git.with(listener, env).in(repo).using(gitImpl).getClient();
File trackedFile = commitTrackedFile();
commitTrackedFile();
final String uniqueTagName = "testGetTagsOnCommit-" + UUID.randomUUID();
final String tagMessage = "Tagged with " + uniqueTagName;
myGit.tag(uniqueTagName, tagMessage);
Expand Down Expand Up @@ -244,7 +212,7 @@ public void testLsTreeThrows() {

@Test
public void testLsTreeOneCommit() throws Exception {
File trackedFile = commitTrackedFile();
commitTrackedFile();
List<IndexEntry> lsTree = git.lsTree("HEAD");
assertEquals("lsTree wrong size - " + lsTree, 1, lsTree.size());
assertEquals("tracked-file", lsTree.get(0).getFile());
Expand Down
Expand Up @@ -67,7 +67,7 @@ public MergeCommandTest(String implementation) {
public static void computeDefaultBranchName() throws Exception {
File configDir = java.nio.file.Files.createTempDirectory("readGitConfig").toFile();
CliGitCommand getDefaultBranchNameCmd = new CliGitCommand(Git.with(TaskListener.NULL, new hudson.EnvVars()).in(configDir).using("git").getClient());
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--global", "--get", "init.defaultBranch");
String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--get", "init.defaultBranch");
for (String s : output) {
String result = s.trim();
if (result != null && !result.isEmpty()) {
Expand Down Expand Up @@ -180,7 +180,7 @@ private void createConflictingCommit() throws Exception {
}

@Parameterized.Parameters(name = "{0}")
public static Collection gitImplementations() {
public static Collection<Object[]> gitImplementations() {
List<Object[]> args = new ArrayList<>();
String[] implementations = new String[]{"git", "jgit"};
for (String implementation : implementations) {
Expand Down
Expand Up @@ -34,7 +34,7 @@ public void pushBadURIThrows() throws IOException, GitException, InterruptedExce
}

@Parameterized.Parameters(name = "{0} with {1}")
public static Collection pushParameters() {
public static Collection<Object[]> pushParameters() {
List<Object[]> parameters = new ArrayList<>();
Object[] gitParameter = {"git", "master", "master", null};
parameters.add(gitParameter);
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/org/jenkinsci/plugins/gitclient/PushTest.java
Expand Up @@ -17,7 +17,6 @@
import org.apache.commons.io.FileUtils;

import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.URIish;

import org.junit.After;
Expand Down Expand Up @@ -110,7 +109,7 @@ public void pushNonFastForwardForce() throws IOException, GitException, Interrup
}

@Parameterized.Parameters(name = "{0} with {1} refspec {2}")
public static Collection pushParameters() {
public static Collection<Object[]> pushParameters() {
List<Object[]> parameters = new ArrayList<>();
final String[] implementations = {"git", "jgit"};
final String[] goodRefSpecs = {
Expand Down Expand Up @@ -154,7 +153,6 @@ public static Collection pushParameters() {
public void createWorkingRepository() throws IOException, InterruptedException {
hudson.EnvVars env = new hudson.EnvVars();
TaskListener listener = StreamTaskListener.fromStderr();
List<RefSpec> refSpecs = new ArrayList<>();
workingRepo = temporaryFolder.newFolder();
workingGitClient = Git.with(listener, env).in(workingRepo).using(gitImpl).getClient();
workingGitClient.clone_()
Expand Down
Expand Up @@ -98,7 +98,6 @@ public void testAddCredentials() {
public void testSetCredentials() {
CredentialsScope scope = CredentialsScope.GLOBAL;
String password = "password";
String url = "https://github.com/jenkinsci/git-client-plugin";
String username = "user";
String id = "username-" + username + "-password-" + password + "-" + random.nextInt();
StandardUsernameCredentials credentials = new UsernamePasswordCredentialsImpl(scope, username, password, id, "Credential description");
Expand All @@ -109,7 +108,6 @@ public void testSetCredentials() {
public void testAddDefaultCredentials() {
CredentialsScope scope = CredentialsScope.GLOBAL;
String password = "password";
String url = "https://github.com/jenkinsci/git-client-plugin";
String username = "user";
String id = "username-" + username + "-password-" + password + "-" + random.nextInt();
StandardCredentials credentials = new UsernamePasswordCredentialsImpl(scope, username, password, id, "Credential description");
Expand Down
Expand Up @@ -36,7 +36,7 @@ public SubmodulePatternStringTest(String repoUrl, String remoteName)
* Tests file, ssh (both forms), git, and https.
*/
@Parameterized.Parameters(name = "{0}-{1}")
public static Collection repoAndRemote() {
public static Collection<Object[]> repoAndRemote() {
List<Object[]> arguments = new ArrayList<>();
String[] repoUrls = {
"file://gitroot/thirdparty.url.repo",
Expand Down
Expand Up @@ -73,7 +73,7 @@ public void checkLogger() {
}

@Parameterized.Parameters(name = "{0}")
public static Collection envVarsToCheck() {
public static Collection<Object[]> envVarsToCheck() {
List<Object[]> envVarNames = new ArrayList<>();
Object[] tmp = {"TMP"};
envVarNames.add(tmp);
Expand Down
Expand Up @@ -95,7 +95,6 @@ public void testThrowsUnsupportedOperationException() {

@Test
public void testSupportsDisallowed() {
Secret secret = Secret.fromString(SECRET_VALUE);
listener = StreamTaskListener.fromStdout();
StandardUsernameCredentials badCred = new MyUsernameCredentialsImpl(USER_NAME);
CredentialsProviderImpl badProvider = new CredentialsProviderImpl(listener, badCred);
Expand Down

0 comments on commit a1f50ff

Please sign in to comment.