Skip to content

Commit 384fa16

Browse files
committed
Add RemoteGitImplTest for a few methods
1 parent 711fcfd commit 384fa16

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2020 Mark Waite.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package org.jenkinsci.plugins.gitclient;
25+
26+
import com.cloudbees.plugins.credentials.CredentialsScope;
27+
import com.cloudbees.plugins.credentials.common.StandardCredentials;
28+
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
29+
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
30+
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
31+
import hudson.EnvVars;
32+
import hudson.FilePath;
33+
import hudson.ProxyConfiguration;
34+
import hudson.model.TaskListener;
35+
import hudson.plugins.git.Branch;
36+
import hudson.plugins.git.GitException;
37+
import hudson.plugins.git.GitObject;
38+
import hudson.plugins.git.IndexEntry;
39+
import hudson.plugins.git.Revision;
40+
import hudson.plugins.git.Tag;
41+
import java.io.File;
42+
import java.io.IOException;
43+
import java.io.OutputStream;
44+
import java.io.Writer;
45+
import java.nio.charset.StandardCharsets;
46+
import java.nio.file.Files;
47+
import java.nio.file.Path;
48+
import java.util.List;
49+
import java.util.Map;
50+
import java.util.Set;
51+
import org.eclipse.jgit.lib.ObjectId;
52+
import org.eclipse.jgit.lib.PersonIdent;
53+
import org.eclipse.jgit.lib.Repository;
54+
import org.eclipse.jgit.transport.RefSpec;
55+
import org.eclipse.jgit.transport.RemoteConfig;
56+
import org.eclipse.jgit.transport.URIish;
57+
import org.junit.After;
58+
import org.junit.AfterClass;
59+
import org.junit.Before;
60+
import org.junit.BeforeClass;
61+
import org.junit.Rule;
62+
import org.junit.Test;
63+
import org.junit.rules.TemporaryFolder;
64+
65+
import static org.hamcrest.MatcherAssert.*;
66+
import static org.hamcrest.Matchers.*;
67+
68+
import static org.junit.Assert.assertFalse;
69+
import static org.junit.Assert.assertThrows;
70+
import static org.junit.Assert.assertTrue;
71+
72+
public class RemoteGitImplTest {
73+
74+
public RemoteGitImplTest() {
75+
}
76+
77+
@Rule
78+
public TemporaryFolder temporaryFolderRule = new TemporaryFolder();
79+
80+
private File localFolder;
81+
private GitClient defaultClient;
82+
private RemoteGitImpl remoteGit;
83+
private String gitImplName;
84+
85+
private java.util.Random random = new java.util.Random();
86+
87+
@Before
88+
public void setUp() throws IOException, InterruptedException {
89+
/* Use a randomly selected git implementation in hopes of detecting more issues with fewer tests */
90+
String[] gitImplAlternatives = { "git", "jgit", "jgitapache" };
91+
gitImplName = gitImplAlternatives[random.nextInt(gitImplAlternatives.length)];
92+
localFolder = temporaryFolderRule.newFolder();
93+
defaultClient = Git.with(TaskListener.NULL, new EnvVars()).in(localFolder).using(gitImplName).getClient();
94+
remoteGit = new RemoteGitImpl(defaultClient);
95+
}
96+
97+
@Test
98+
public void testGetRepository() throws IOException, InterruptedException {
99+
assertThrows(UnsupportedOperationException.class, () -> {
100+
remoteGit.getRepository();
101+
});
102+
}
103+
104+
@Test
105+
public void testClearCredentials() throws IOException, InterruptedException {
106+
remoteGit.clearCredentials();
107+
}
108+
109+
@Test
110+
public void testAddCredentials() {
111+
CredentialsScope scope = CredentialsScope.GLOBAL;
112+
String password = "password";
113+
String url = "https://github.com/jenkinsci/git-client-plugin";
114+
String username = "user";
115+
String id = "username-" + username + "-password-" + password + "-" + random.nextInt();
116+
StandardCredentials credentials = new UsernamePasswordCredentialsImpl(scope, username, password, id, "Credential description");
117+
remoteGit.addCredentials(url, credentials);
118+
}
119+
120+
@Test
121+
public void testSetCredentials() {
122+
CredentialsScope scope = CredentialsScope.GLOBAL;
123+
String password = "password";
124+
String url = "https://github.com/jenkinsci/git-client-plugin";
125+
String username = "user";
126+
String id = "username-" + username + "-password-" + password + "-" + random.nextInt();
127+
StandardUsernameCredentials credentials = new UsernamePasswordCredentialsImpl(scope, username, password, id, "Credential description");
128+
remoteGit.setCredentials(credentials);
129+
}
130+
131+
@Test
132+
public void testAddDefaultCredentials() {
133+
CredentialsScope scope = CredentialsScope.GLOBAL;
134+
String password = "password";
135+
String url = "https://github.com/jenkinsci/git-client-plugin";
136+
String username = "user";
137+
String id = "username-" + username + "-password-" + password + "-" + random.nextInt();
138+
StandardCredentials credentials = new UsernamePasswordCredentialsImpl(scope, username, password, id, "Credential description");
139+
remoteGit.addDefaultCredentials(credentials);
140+
}
141+
142+
@Test
143+
public void testSetAuthor_String_String() {
144+
String name = "charlie";
145+
String email = "charlie@example.com";
146+
remoteGit.setAuthor(name, email);
147+
}
148+
149+
@Test
150+
public void testSetAuthor_PersonIdent() {
151+
String name = "charlie";
152+
String email = "charlie@example.com";
153+
PersonIdent p = new PersonIdent(name, email);
154+
remoteGit.setAuthor(p);
155+
}
156+
157+
@Test
158+
public void testSetCommitter_String_String() {
159+
String name = "charlie";
160+
String email = "charlie@example.com";
161+
remoteGit.setCommitter(name, email);
162+
}
163+
164+
@Test
165+
public void testSetCommitter_PersonIdent() {
166+
String name = "charlie";
167+
String email = "charlie@example.com";
168+
PersonIdent p = new PersonIdent(name, email);
169+
remoteGit.setCommitter(p);
170+
}
171+
172+
@Test
173+
public void testGetWorkTree() {
174+
FilePath folderPath = new FilePath(localFolder);
175+
FilePath remoteFolderPath = remoteGit.getWorkTree();
176+
assertThat(remoteFolderPath, is(folderPath));
177+
}
178+
179+
@Test
180+
public void testInit() throws Exception {
181+
assertFalse("defaultClient has repo before init", defaultClient.hasGitRepo());
182+
remoteGit.init();
183+
assertTrue("defaultClient missing repo after init", defaultClient.hasGitRepo());
184+
}
185+
186+
private void firstAdd(String fileName) throws Exception {
187+
File localFile = new File(localFolder, fileName);
188+
byte [] content = ("File " + fileName).getBytes();
189+
Files.write(localFile.toPath(), content);
190+
remoteGit.init();
191+
remoteGit.add(fileName);
192+
}
193+
194+
private ObjectId firstCommit(String fileName) throws Exception {
195+
firstAdd(fileName);
196+
remoteGit.commit("Adding the " + fileName + " file");
197+
return remoteGit.revParse("HEAD");
198+
}
199+
200+
@Test
201+
public void testAddAndCommit() throws Exception {
202+
assertThat(firstCommit("testAddAndCommit"), is(not(nullValue())));
203+
}
204+
205+
@Test
206+
public void testCommit_3args() throws Exception {
207+
firstAdd("testCommit_3args_abc");
208+
String message = "Committing with authorName and commiterName";
209+
PersonIdent author = new PersonIdent("authorName", "authorEmail@example.com");
210+
PersonIdent committer = new PersonIdent("committerName", "committerEmail@example.com");
211+
remoteGit.commit(message, author, committer);
212+
ObjectId commit = remoteGit.revParse("HEAD");
213+
assertTrue("Commit not in repo", remoteGit.isCommitInRepo(commit));
214+
}
215+
216+
@Test
217+
public void testHasGitRepo() throws Exception {
218+
assertFalse("remoteGit has repo before init", remoteGit.hasGitRepo());
219+
remoteGit.init();
220+
assertTrue("remoteGit missing repo after init", remoteGit.hasGitRepo());
221+
}
222+
223+
@Test
224+
public void testIsCommitInRepo() throws Exception {
225+
ObjectId commit = firstCommit("testIsCommitInRepo-abc");
226+
assertTrue("Commit not in repo", remoteGit.isCommitInRepo(commit));
227+
ObjectId missingCommit = ObjectId.fromString("deededbeadedcededaddedbedded5ea6b842da60");
228+
assertFalse("Missing commit found in repo", remoteGit.isCommitInRepo(missingCommit));
229+
}
230+
231+
@Test
232+
public void testGetRemoteUrl_String() throws Exception {
233+
String name = "originName";
234+
String url = "https://github.com/jenkinsci/git-client-plugin";
235+
remoteGit.init();
236+
if (gitImplName.equals("git")) { // JGit does not throw an exception for undefined remote
237+
assertThrows(GitException.class, () -> {
238+
remoteGit.getRemoteUrl(name);
239+
});
240+
}
241+
remoteGit.setRemoteUrl(name, url);
242+
assertThat(remoteGit.getRemoteUrl(name), is(url));
243+
remoteGit.addRemoteUrl(name + "2", url + "-2");
244+
assertThat(remoteGit.getRemoteUrl(name + "2"), is(url + "-2"));
245+
}
246+
}

0 commit comments

Comments
 (0)