Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
Refactoring, extract classes, javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
mstachniuk committed May 14, 2017
1 parent 0309e5e commit 4920672
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 155 deletions.
74 changes: 4 additions & 70 deletions e2eTests/build.gradle
Original file line number Diff line number Diff line change
@@ -1,79 +1,13 @@
apply plugin: 'base' // for clean task
apply plugin: 'org.mockito.mockito-release-tools.e2e-test'

group = 'gradle.plugin.org.mockito'
description = 'for running e2e tests'

// org.gradle.language.base.plugins.LifecycleBasePlugin

//def e2eProjects = ["mockito-release-tools-example": "https://github.com/mockito/mockito-release-tools-example"]

//subprojects {
// tasks.create("e2eTest", E2ETestingPlugin.E2ETest) {
// create("https://github.com/mockito/mockito-release-tools-example")
// }
//}

//task e2eTest {
// doFirst {
// e2eProjects.each { entry ->
// println " E2E tests for $entry.key :)"
// def projectName = entry.key
// task e2eTest$projectName {
// deleteProjectDir(projectName)
// gitClone(projectName, e2eProjects[projectName])
// bumpProjectVersion(projectName)
// performRelease(projectName)
// }
// }
// }
//}
//
//def deleteProjectDir(String projectName) {
// tasks.create("deleteDir$projectName", Delete) {
// delete "$projectDir/$projectName"
// }.execute()
//}
//
//def gitClone(String projectName, String repoUrl) {
// tasks.create("gitClone$projectName", Exec) {
// commandLine 'git', 'clone', "$repoUrl", "$projectDir/$projectName"
// }.execute()
//}
//
//def bumpProjectVersion(String projectName) {
// ant.replaceregexp(
// match: "gradle\\.plugin\\.org\\.mockito:mockito-release-tools:(\\d+)?\\.(\\d+)?\\.(\\d+)?",
// replace: "gradle.plugin.org.mockito:mockito-release-tools:$project.version") {
// fileset(dir: "$projectDir/$projectName", includes: 'build.gradle gradle.properties')
// }
//}
//
//def performRelease(String projectName) {
// println " Testing the Gradle build..."
// exec {
// workingDir "$projectDir/$projectName"
// commandLine './gradlew', 'publishToMavenLocal', 'testRelease', '-x', 'gitPush', '-x', 'bintrayUpload'
// }
//}

//task check {
// dependsOn e2eTest
//}
//
//task build {
// dependsOn check
//}
// It needs to be commented until first version of release-tools with E2E plugin is released.
// If not, Gradle is unable to configure whole project, because E2E plugin isn't published in any version yet.
// TODO uncomment in next PR
// apply plugin: 'org.mockito.mockito-release-tools.e2e-test'

/*
I loooove the direction :) I was even able to run it locally when I did some tweaks to the code (pushed to your branch)
Before we can push the code, I'd like to clean it up a little bit (TODO)
1. Clone into build directory of the project, no need to tweak .idea
This also means that the cloned content will be automatically 'excluded' from indexing by IDEA
2. Use imperative exec instead of task.execute()
3. Task descriptions
4. In order for Gradle to neatly paralelize,
let's have the mockito and mockito-release-tools-example be separate subprojects,
just like I have set it up in PR #100.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.mockito.release.internal.gradle;

import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import org.mockito.release.exec.ProcessRunner;

import java.io.File;

/**
* This task clone git project from repository to target dir.
* It support clone from remote server and from local filesystem.
*/
public class CloneGitRepositoryTask extends DefaultTask {

private static final Logger LOG = Logging.getLogger(CloneGitRepositoryTask.class);

private String repository;
private File targetDir;

@TaskAction
public void cloneRepository() {
LOG.lifecycle(" Clone repository");
getProject().getBuildDir().mkdirs(); // build dir can be not created yet
ProcessRunner processRunner = org.mockito.release.exec.Exec.getProcessRunner(getProject().getBuildDir());
processRunner.run("git", "clone", repository, targetDir.getAbsolutePath());
}

@Input
public void setRepository(String repository) {
this.repository = repository;
}

@OutputDirectory
public void setTargetDir(File targetDir) {
this.targetDir = targetDir;
}

public File getTargetDir() {
return targetDir;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package org.mockito.release.internal.gradle;

import org.gradle.api.DefaultTask;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.*;
import org.mockito.release.exec.*;

import java.io.File;
import java.util.List;

import static java.util.Arrays.asList;
import static org.mockito.release.internal.gradle.util.StringUtil.capitalize;

/**
* This plugin tests your library end-to-end (e2e) using client projects.
* TODO MS doc
* Plugin clones client projects to '$buildDir/project-name-pristine' first, next clone project from 'pristine' to
* '$buildDir/project-name-work' and execute 'testRelease' task using the newest mockito-release-tools version
*
* Adds tasks:
* <ul>
* <li>cloneProjectFromGitHub$projectName - {@link CloneGitRepositoryTask}</li>
* <li>cloneProjectToWorkDir$projectName - {@link CloneGitRepositoryTask}</li>
* <li>runTest$projectName - {@link RunTestReleaseTask}</li>
* </ul>
*/
public class E2ETestingPlugin implements Plugin<Project> {

private static final Logger LOG = Logging.getLogger(ContinuousDeliveryPlugin.class);

public void apply(final Project project) {
E2ETest e2eTest = project.getExtensions().create("e2eTest", E2ETest.class, project);
// TODO hardcoded for now
Expand All @@ -37,103 +38,52 @@ public E2ETest(Project project) {

void create(String gitHubRepoUrl) {
String repoName = extractRepoName(gitHubRepoUrl);
CloneGitHubRepositoryTask clone = project.getTasks().create("cloneProjectFromGitHub" + repoName, CloneGitHubRepositoryTask.class);
// TODO add depth clone configuration for shallow clone
CloneGitRepositoryTask clone = project.getTasks().create(
"cloneProjectFromGitHub" + capitalize(repoName),
CloneGitRepositoryTask.class);
clone.setRepository(gitHubRepoUrl);
clone.setTargetDir(new File(project.getBuildDir(), repoName + "-pristine"));
// For now for easier testing
clone.dependsOn("clean");

// Clone from *-pristine to *-work. Copy task will not work because of ignoring git specific files:
// https://discuss.gradle.org/t/copy-git-specific-files/11970
// Furthermore we can verify push to pristine origin
File workDir = new File(project.getBuildDir(), repoName + "-work");
CloneGitHubRepositoryTask copy = project.getTasks().create("cloneProjectToWorkDir" + repoName, CloneGitHubRepositoryTask.class);
CloneGitRepositoryTask copy = project.getTasks().create(
"cloneProjectToWorkDir" + capitalize(repoName),
CloneGitRepositoryTask.class);
copy.dependsOn(clone);
copy.setRepository(clone.getTargetDir().getAbsolutePath());
copy.setTargetDir(workDir);

RunTestTask run = project.getTasks().create("runTest" + repoName, RunTestTask.class);
RunTestReleaseTask run = project.getTasks().create(
"runTest" + capitalize(repoName),
RunTestReleaseTask.class);
run.dependsOn(copy);
run.setWorkDir(workDir);
run.setRepoName(repoName);

//Using Gradle's composite builds ("--include-build") so that we're picking up current version of tools
// Using Gradle's composite builds ("--include-build") so that we're picking up current version of tools
run.setCommand(asList("./gradlew", "publishToMavenLocal", "testRelease",
"-x", "gitPush", "-x", "bintrayUpload",
"--include-build", project.getRootDir().getAbsolutePath(), "-s"));

//we should put the build log in separate file instead of including it in the console of the parent build
//otherwise the output will be really messy
// Build log in separate file instead of including it in the console of the parent build
// Otherwise the output will be really messy
run.setBuildOutputFile(new File(project.getBuildDir(), repoName + "-build.log"));
}

// TODO MS Unit testing
private String extractRepoName(String gitHubRepo) {
return gitHubRepo.substring(gitHubRepo.lastIndexOf('/') + 1, gitHubRepo.length());
String text = gitHubRepo.trim();
if(text.lastIndexOf('/') == text.length() - 1) {
// cut last slash
text = text.substring(0, text.length() - 1);
}
return text.substring(text.lastIndexOf('/') + 1, text.length());
}
}

public static class CloneGitHubRepositoryTask extends DefaultTask {

private String repository;
private File targetDir;

@TaskAction
public void cloneRepository() {
LOG.lifecycle(" Clone repository");
getProject().getBuildDir().mkdirs(); // build dir can be not created yet
ProcessRunner processRunner = org.mockito.release.exec.Exec.getProcessRunner(getProject().getBuildDir());
processRunner.run("git", "clone", repository, targetDir.getAbsolutePath());
}

@Input
public void setRepository(String repository) {
this.repository = repository;
}

@OutputDirectory
public void setTargetDir(File targetDir) {
this.targetDir = targetDir;
}

public File getTargetDir() {
return targetDir;
}
}


public static class RunTestTask extends DefaultTask {

private List<String> command;
private File buildOutput;
private File workDir;
private String repoName;

@TaskAction
public void runTest() {
LOG.lifecycle(" Run test of {}. The output will be save in {}", repoName, buildOutput.getAbsoluteFile());
ProcessRunner processRunner = org.mockito.release.exec.Exec.getProcessRunner(workDir, buildOutput);
processRunner.run(command);
}

@Input
public void setWorkDir(File workDir) {
this.workDir = workDir;
}

@Input
public void setCommand(List<String> command) {
this.command = command;
}

@OutputFile
public void setBuildOutputFile(File file) {
buildOutput = file;
}

@Input
public void setRepoName(String repoName) {
this.repoName = repoName;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.mockito.release.internal.gradle;

import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.mockito.release.exec.ProcessRunner;

import java.io.File;
import java.util.List;

/**
* This task run external process and additionally store output of external process to file.
*/
public class RunTestReleaseTask extends DefaultTask {

private static final Logger LOG = Logging.getLogger(RunTestReleaseTask.class);

private List<String> command;
private File buildOutput;
private File workDir;
private String repoName;

@TaskAction
public void runTest() {
LOG.lifecycle(" Run test of {}. The output will be save in {}", repoName, buildOutput.getAbsoluteFile());
ProcessRunner processRunner = org.mockito.release.exec.Exec.getProcessRunner(workDir, buildOutput);
processRunner.run(command);
}

@Input
public void setWorkDir(File workDir) {
this.workDir = workDir;
}

@Input
public void setCommand(List<String> command) {
this.command = command;
}

@Input
public void setRepoName(String repoName) {
this.repoName = repoName;
}

@OutputFile
public void setBuildOutputFile(File file) {
buildOutput = file;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,35 @@ class E2ETestingPluginTest extends Specification {

def project = new ProjectBuilder().build()

def "e2e"() {
def "e2e with example project by default"() {
project.plugins.apply("org.mockito.mockito-release-tools.e2e-test")
println project.tasks

// when:
// project.e2eTest.create("https://github.com/mockito/mockito-release-tools-example")

expect:
project.tasks.'runTestmockito-release-tools-example'
project.tasks.'runTestMockito-release-tools-example'
}

def "should extract project name correctly"() {
E2ETestingPlugin.E2ETest sut = new E2ETestingPlugin.E2ETest(project)

when:
sut.create("https://github.com/mockito/mockito")

then:
project.tasks.runTestMockito
project.tasks.cloneProjectFromGitHubMockito
project.tasks.cloneProjectToWorkDirMockito
}

def "should extract project name correctly when slash is the last char in url"() {
E2ETestingPlugin.E2ETest sut = new E2ETestingPlugin.E2ETest(project)

when:
sut.create("https://github.com/xx/yyy/")

then:
project.tasks.runTestYyy
project.tasks.cloneProjectFromGitHubYyy
project.tasks.cloneProjectToWorkDirYyy
}
}

0 comments on commit 4920672

Please sign in to comment.