Skip to content

Commit

Permalink
Issue-1527: implement changes from jjohannes
Browse files Browse the repository at this point in the history
these have been detailed https://github.com/jjohannes/JGiven/commit/cc3f67a7b4b39f9f78599c2cea18c42c95870c73
and authority to use these has been explicitly granted #1527 (comment)

Signed-off-by: l-1squared <30831153+l-1squared@users.noreply.github.com>
  • Loading branch information
l-1squared committed Feb 23, 2024
1 parent 6ffb5ef commit da07bf2
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
import com.tngtech.jgiven.impl.Config;
import com.tngtech.jgiven.impl.util.WordUtil;
import org.gradle.api.*;
import org.gradle.api.internal.ConventionMapping;
import org.gradle.api.internal.IConventionAware;
import org.gradle.api.file.Directory;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.ReportingBasePlugin;
import org.gradle.api.provider.Provider;
import org.gradle.api.reporting.Report;
import org.gradle.api.reporting.ReportingExtension;
import org.gradle.api.tasks.testing.Test;

import java.io.File;
import javax.inject.Inject;
import java.util.Objects;
import java.util.concurrent.Callable;

@SuppressWarnings("unused")
@NonNullApi
public class JGivenPlugin implements Plugin<Project> {
public abstract class JGivenPlugin implements Plugin<Project> {

@Inject
protected abstract ObjectFactory getObjects();

@Override
public void apply(final Project project) {
project.getPluginManager().apply(ReportingBasePlugin.class);
Expand All @@ -32,39 +37,43 @@ private void addTaskExtension(Project project) {

private void applyTo(Test test) {
final String testName = test.getName();
final JGivenTaskExtension extension = test.getExtensions().create("jgiven", JGivenTaskExtension.class);
final Project project = test.getProject();
((IConventionAware) extension).getConventionMapping().map("resultsDir",
(Callable<File>) () -> project.file(project.getBuildDir() + "/jgiven-results/" + testName));

File resultsDir = extension.getResultsDir().get();
test.getOutputs().dir(resultsDir).withPropertyName("jgiven.resultsDir");
final JGivenTaskExtension extension = getObjects().newInstance(JGivenTaskExtension.class);
//having this brakes the "test is cacheable" test while not checking whether the extension is configured breaks the "no test no output" test.
//also the groovy tests might be ill-configured, because it is not exactly clear to me, why these get away with not ordering a jgiven report.
test.getExtensions().add("jgiven", extension);
var isConfigured = extension.getResultsDir().isPresent();
extension.getResultsDir().convention(project.getLayout().getBuildDirectory().dir("jgiven-results/" + testName));
Provider<Directory> resultsDir = extension.getResultsDir();
if (isConfigured) {
test.getOutputs().dir(resultsDir).withPropertyName("jgiven.resultsDir");
}

/* Java lambda classes are created at runtime with a non-deterministic classname.
* Therefore, the class name does not identify the implementation of the lambda,
* and changes between different Gradle runs.
* See: https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:how_does_it_work
*/
//noinspection Convert2Lambda
test.prependParallelSafeAction(new Action<>() {
test.doFirst(new Action<>() {
@Override
public void execute(Task task) {
((Test) task).systemProperty(Config.JGIVEN_REPORT_DIR, extension.getResultsDir().get().getAbsolutePath());
((Test) task).systemProperty(Config.JGIVEN_REPORT_DIR, extension.getResultsDir().get().getAsFile().getAbsolutePath());
}
});
}

private void configureJGivenReportDefaults(Project project) {
project.getTasks()
.withType(JGivenReportTask.class).forEach(reportTask ->
reportTask.getReports().all((Action<Report>) report ->
report.getRequired().convention(report.getName().equals(JGivenHtmlReportImpl.NAME))
));
.withType(JGivenReportTask.class).forEach(reportTask ->
reportTask.getReports().all((Action<Report>) report ->
report.getRequired().convention(report.getName().equals(JGivenHtmlReportImpl.NAME))
));
}

private void addDefaultReports(final Project project) {
final ReportingExtension reportingExtension = Objects.requireNonNull(
project.getExtensions().findByType(ReportingExtension.class));
project.getExtensions().findByType(ReportingExtension.class));

project.getTasks().withType(Test.class).forEach(test -> project.getTasks()
.register("jgiven" + WordUtil.capitalize(test.getName()) + "Report", JGivenReportTask.class)
Expand All @@ -76,18 +85,15 @@ private void configureDefaultReportTask(final Test test, JGivenReportTask report
final ReportingExtension reportingExtension) {
reportTask.mustRunAfter(test);

ConventionMapping mapping = ((IConventionAware) reportTask).getConventionMapping();
Callable<File> getResultsDirectory = () -> test.getExtensions()
.getByType(JGivenTaskExtension.class)
.getResultsDir().get();
mapping.map("results", getResultsDirectory);

Objects.requireNonNull(mapping.getConventionValue(reportTask.getReports(), "reports", false))
.all(report -> {
ConventionMapping reportMapping = ((IConventionAware) report).getConventionMapping();
String relativeFilePath = "jgiven" + "/" + test.getName() + "/" + report.getName();
Callable<File> getDestination = () -> reportingExtension.file(relativeFilePath);
reportMapping.map("destination", getDestination);
});
Provider<Directory> getResultsDirectory = test.getExtensions()
.getByType(JGivenTaskExtension.class)
.getResultsDir();

reportTask.getResults().set(getResultsDirectory);

reportTask.getReports().configureEach(report -> {
String relativeFilePath = "jgiven" + "/" + test.getName() + "/" + report.getName();
report.getOutputLocation().set(reportingExtension.getBaseDirectory().dir(relativeFilePath));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.NonNullApi;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.reporting.Reporting;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.InputDirectory;
Expand All @@ -20,29 +22,22 @@

@CacheableTask
@NonNullApi
public class JGivenReportTask extends DefaultTask implements Reporting<JGivenReportsContainer> {
public abstract class JGivenReportTask extends DefaultTask implements Reporting<JGivenReportsContainer> {
private final JGivenReportsContainer reports;
private File results;

public JGivenReportTask() {
reports = getInstantiator().newInstance(JGivenReportsContainerImpl.class, this);
reports = getObjects().newInstance(JGivenReportsContainerImpl.class, this);
}

@Inject
protected Instantiator getInstantiator() {
protected ObjectFactory getObjects() {
throw new UnsupportedOperationException();
}

@InputDirectory
@SkipWhenEmpty
@PathSensitive(PathSensitivity.NONE)
public File getResults() {
return results;
}

public void setResults(File results) {
this.results = results;
}
public abstract DirectoryProperty getResults();

@TaskAction
public void generate() {
Expand All @@ -53,7 +48,7 @@ public void generate() {

private void generateReport(JGivenReport report) {
AbstractReportGenerator generator = report.createGenerator();
generator.config.setSourceDir(getResults());
generator.config.setSourceDir(getResults().get().getAsFile());
generator.loadReportModel();
try {
generator.generate();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.tngtech.jgiven.gradle;

import org.gradle.api.provider.Property;

import java.io.File;
import org.gradle.api.file.DirectoryProperty;

public interface JGivenTaskExtension {
Property<File> getResultsDir();
DirectoryProperty getResultsDir();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import org.gradle.api.Task;
import org.gradle.api.reporting.internal.TaskReportContainer;

import javax.inject.Inject;

import static org.gradle.api.internal.CollectionCallbackActionDecorator.NOOP;

public class JGivenReportsContainerImpl extends TaskReportContainer<JGivenReport> implements JGivenReportsContainer {

@Inject
public JGivenReportsContainerImpl( Task task ) {
super( JGivenReport.class, task, NOOP);
add(JGivenHtmlReportImpl.class, task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JGivenPluginShould extends Specification {
when:
def result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments("--configuration-cache", "test", "jgivenTestReport", "-S", "--info", "--no-daemon", "-Dorg.gradle.debug=true" )
.withArguments("--configuration-cache", "test", "jgivenTestReport", "-S", "--info"/*, "-Dorg.gradle.debug=true"*/ )
.withPluginClasspath()
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,11 @@ When a_build() {
}

When is_successful() {
var arguments = new ArrayList<>(tasks);
//arguments.add("-Dorg.gradle.debug=true");
result = GradleRunner.create()
.withProjectDir(testProjectDir.get())
.withArguments(tasks)
.withArguments(arguments)
.withPluginClasspath()
.build();
return self();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void generate() {
copyCustomFile( specializedConfig.getCustomCss(), new File( specializedConfig.getTargetDir(), "css" ), "custom.css" );
copyCustomFile( specializedConfig.getCustomJs(), new File( specializedConfig.getTargetDir(), "js" ), "custom.js" );
} catch( IOException e ) {
throw Throwables.propagate( e );
throw new RuntimeException(e);
}
}

Expand Down

0 comments on commit da07bf2

Please sign in to comment.