Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #358 from MarkEWaite/master-JENKINS-30073-timestam…
…p-parsing [Fix JENKINS-30073] API timestamp return correct value
- Loading branch information
Showing
8 changed files
with
326 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
src/test/java/hudson/plugins/git/GitChangeSetPluginHistoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package hudson.plugins.git; | ||
|
||
import org.jenkinsci.plugins.gitclient.Git; | ||
import org.jenkinsci.plugins.gitclient.GitClient; | ||
|
||
import org.eclipse.jgit.lib.ObjectId; | ||
|
||
import hudson.EnvVars; | ||
import hudson.FilePath; | ||
import hudson.model.TaskListener; | ||
import hudson.util.StreamTaskListener; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.StringWriter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.Matchers.*; | ||
import static org.junit.Assert.*; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
@RunWith(Parameterized.class) | ||
public class GitChangeSetPluginHistoryTest { | ||
|
||
private static final long FIRST_COMMIT_TIMESTAMP = 1198029565000L; | ||
private static final long NOW = System.currentTimeMillis(); | ||
|
||
private final GitClient git; | ||
private final boolean authorOrCommitter; | ||
private final ObjectId sha1; | ||
|
||
private final GitChangeSet changeSet; | ||
|
||
private static String gitVersion = "Unknown"; | ||
|
||
/* git 1.7.1 on CentOS 6.7 "whatchanged" generates no output for | ||
* the SHA1 hashes (from this repository) in this list. Rather | ||
* than skip testing on that old git version, this exclusion list | ||
* allows most tests to run. | ||
*/ | ||
private static final String[] git171exceptions = { | ||
"750b6806", | ||
"7eeb070b", | ||
"87988f4d", | ||
"a571899e", | ||
"bc71cd2d" | ||
}; | ||
|
||
public GitChangeSetPluginHistoryTest(GitClient git, boolean authorOrCommitter, String sha1String) throws IOException, InterruptedException { | ||
this.git = git; | ||
this.authorOrCommitter = authorOrCommitter; | ||
this.sha1 = ObjectId.fromString(sha1String); | ||
StringWriter stringWriter = new StringWriter(); | ||
git.changelog().includes(sha1).max(1).to(stringWriter).execute(); | ||
List<String> changeLogStrings = new ArrayList<String>(Arrays.asList(stringWriter.toString().split("\n"))); | ||
changeSet = new GitChangeSet(changeLogStrings, authorOrCommitter); | ||
} | ||
|
||
private static String getGitVersion() throws IOException { | ||
Process process = new ProcessBuilder("git", "--version").start(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
String version = "unknown"; | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
version = line.trim(); | ||
} | ||
reader.close(); | ||
process.destroy(); | ||
return version; | ||
} | ||
|
||
/** | ||
* Merge changes won't compute their date in GitChangeSet, | ||
* apparently as an intentional design choice. Return all changes | ||
* for this repository which are not merges. | ||
* | ||
* @return ObjectId list for all changes which aren't merges | ||
*/ | ||
private static List<ObjectId> getNonMergeChanges(boolean honorExclusions) throws IOException { | ||
List<ObjectId> nonMergeChanges = new ArrayList<ObjectId>(); | ||
Process process = new ProcessBuilder("git", "rev-list", "--no-merges", "HEAD").start(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
if (honorExclusions) { | ||
boolean ignore = false; | ||
for (String exclusion : git171exceptions) { | ||
if (line.startsWith(exclusion)) { | ||
ignore = true; | ||
break; | ||
} | ||
} | ||
if (!ignore) { | ||
nonMergeChanges.add(ObjectId.fromString(line)); | ||
} | ||
} else { | ||
nonMergeChanges.add(ObjectId.fromString(line)); | ||
} | ||
} | ||
reader.close(); | ||
process.destroy(); | ||
Collections.shuffle(nonMergeChanges); | ||
return nonMergeChanges; | ||
} | ||
|
||
@Parameterized.Parameters(name = "{2}-{1}") | ||
public static Collection<Object[]> generateData() throws IOException, InterruptedException { | ||
gitVersion = getGitVersion(); | ||
|
||
List<Object[]> args = new ArrayList<Object[]>(); | ||
String[] implementations = new String[]{"git", "jgit"}; | ||
boolean[] choices = {true, false}; | ||
|
||
for (final String implementation : implementations) { | ||
EnvVars envVars = new EnvVars(); | ||
TaskListener listener = StreamTaskListener.fromStdout(); | ||
GitClient git = Git.with(listener, envVars).in(new FilePath(new File("."))).using(implementation).getClient(); | ||
List<ObjectId> allNonMergeChanges = getNonMergeChanges(gitVersion.equals("git version 1.7.1") && implementation.equals("git")); | ||
int count = allNonMergeChanges.size() / 10; /* 10% of all changes */ | ||
for (boolean authorOrCommitter : choices) { | ||
for (int index = 0; index < count; index++) { | ||
ObjectId sha1 = allNonMergeChanges.get(index); | ||
Object[] argList = {git, authorOrCommitter, sha1.getName()}; | ||
args.add(argList); | ||
} | ||
} | ||
} | ||
return args; | ||
} | ||
|
||
@Test | ||
public void timestampInRange() { | ||
long timestamp = changeSet.getTimestamp(); | ||
assertThat(timestamp, is(greaterThanOrEqualTo(FIRST_COMMIT_TIMESTAMP))); | ||
assertThat(timestamp, is(lessThan(NOW))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/hudson/plugins/git/GitChangeSetTimestampTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package hudson.plugins.git; | ||
|
||
import java.util.ArrayList; | ||
import static org.junit.Assert.*; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
|
||
/** | ||
* JENKINS-30073 reports that the timestamp returns -1 for the typical timestamp | ||
* reported by the +%ci format to git log and git whatchanged. This test | ||
* duplicates the bug. | ||
* | ||
* @author Mark Waite | ||
*/ | ||
public class GitChangeSetTimestampTest { | ||
|
||
private GitChangeSet changeSet = null; | ||
|
||
@Before | ||
public void createChangeSet() { | ||
changeSet = genChangeSetForJenkins30073(true); | ||
} | ||
|
||
@Test | ||
public void testChangeSetDate() { | ||
assertEquals("2015-10-06 19:29:47 +0300", changeSet.getDate()); | ||
} | ||
|
||
@Test | ||
@Issue("JENKINS-30073") | ||
public void testChangeSetTimeStamp() { | ||
assertEquals(1444148987000L, changeSet.getTimestamp()); | ||
} | ||
|
||
private GitChangeSet genChangeSetForJenkins30073(boolean authorOrCommitter) { | ||
ArrayList<String> lines = new ArrayList<String>(); | ||
lines.add("commit 302548f75c3eb6fa1db83634e4061d0ded416e5a"); | ||
lines.add("tree e1bd430d3f45b7aae54a3061b7895ee1858ec1f8"); | ||
lines.add("parent c74f084d8f9bc9e52f0b3fe9175ad27c39947a73"); | ||
lines.add("author Viacheslav Kopchenin <vkopchenin@odin.com> 2015-10-06 19:29:47 +0300"); | ||
lines.add("committer Viacheslav Kopchenin <vkopchenin@odin.com> 2015-10-06 19:29:47 +0300"); | ||
lines.add(""); | ||
lines.add(" pom.xml"); | ||
lines.add(" "); | ||
lines.add(" :100644 100644 bb32d78c69a7bf79849217bc02b1ba2c870a5a66 343a844ad90466d8e829896c1827ca7511d0d1ef M modules/platform/pom.xml"); | ||
lines.add(""); | ||
return new GitChangeSet(lines, authorOrCommitter); | ||
} | ||
} |
Oops, something went wrong.