-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathGradleBuildTest.java
More file actions
104 lines (94 loc) · 3.31 KB
/
Copy pathGradleBuildTest.java
File metadata and controls
104 lines (94 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.gradle.tooling.BuildException;
import org.gradle.tooling.BuildLauncher;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.model.GradleProject;
import org.gradle.tooling.model.GradleTask;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import com.google.common.io.ByteStreams;
@RunWith(Parameterized.class)
public class GradleBuildTest {
@Parameters(name="TestBuild{0}")
public static Collection<String> data() {
LinkedHashSet<String> projects = new LinkedHashSet<String>();
try {
Repository repository = new FileRepositoryBuilder()
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
RevCommit head = new Git(repository)
.log().call()
.iterator().next();
TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.addTree(head.getTree());
for (RevCommit p : head.getParents()) {
treeWalk.addTree(p.getTree());
}
treeWalk.setRecursive(false);
treeWalk.setFilter(TreeFilter.ANY_DIFF);
while (treeWalk.next()) {
File f = new File("../" + treeWalk.getPathString());
if (isProject(f)) {
System.err.println("project changed: " + f.getName());
projects.add(f.getName());
}
}
} catch (java.io.IOException e) {
System.err.println("error opening git repository: " + e);
} catch (GitAPIException e) {
System.err.println("error reading git repository log: " + e);
}
for (File p : new File("..").listFiles(new FileFilter() {
public boolean accept(File f) { return isProject(f); }
})) {
projects.add(p.getName());
}
return projects;
}
private static boolean isProject(File f) {
return f.isDirectory() && Arrays.asList(f.list()).containsAll(Arrays.asList("build.gradle", "app"));
}
private File gradleProject;
public GradleBuildTest(String projectDirectory) {
this.gradleProject = new File("../" + projectDirectory);
}
@Test
public void test() {
GradleConnector connector = GradleConnector.newConnector();
connector.forProjectDirectory(gradleProject);
ProjectConnection connection = connector.connect();
BuildLauncher launcher = connection.newBuild();
launcher.setStandardOutput(System.out);
launcher.setStandardError(System.err);
try {
launcher.forTasks("app:lint");
launcher.run();
launcher.forTasks("assembleDebug");
launcher.run();
launcher.forTasks("assembleRelease");
launcher.run();
} catch (BuildException e) {
fail(String.format("BUILD FAILED: %s", e));
} finally {
connection.close();
}
}
}