Skip to content

Commit

Permalink
Use same Gradle executor for embedded samples as we do for external ones
Browse files Browse the repository at this point in the history
  • Loading branch information
eriwen committed Aug 9, 2018
1 parent e529e0a commit bd4de10
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 62 deletions.
Expand Up @@ -15,7 +15,6 @@
*/
package org.gradle.samples.test.runner;

import org.gradle.internal.impldep.com.google.common.collect.Lists;
import org.gradle.samples.loader.SamplesDiscovery;
import org.gradle.samples.model.Sample;
import org.junit.runners.model.InitializationError;
Expand All @@ -37,30 +36,11 @@ public EmbeddedSamplesRunner(Class<?> testClass) throws InitializationError {

@Override
protected List<Sample> getChildren() {
List<Sample> samplesFromDirectory = getEmbeddedSamplesFromAsciidocSources();
List<Sample> result = Lists.newArrayList();
result.addAll(samplesFromDirectory);
return result;
}

private List<Sample> getEmbeddedSamplesFromAsciidocSources() {
AsciidocSourcesRoot asciidocSourcesRoot = getTestClass().getAnnotation(AsciidocSourcesRoot.class);
File asciidocSourcesRootDir;
File samplesRootDir = getSamplesRootDir();
try {
if (asciidocSourcesRoot != null) {
asciidocSourcesRootDir = new File(asciidocSourcesRoot.value());
} else {
throw new InitializationError("Asciidoctor sources root is not declared. Please annotate your test class with @AsciidocSourcesRoot(\"path/to/docs/\")");
}

if (!asciidocSourcesRootDir.exists()) {
throw new InitializationError("Directory " + asciidocSourcesRoot.value() + " does not exist. NOTE: it is relative to the Gradle (sub)project rootDir.");
}
return SamplesDiscovery.embeddedSamples(asciidocSourcesRootDir);
} catch (InitializationError e) {
throw new RuntimeException("Could not initialize SamplesRunner", e);
return SamplesDiscovery.embeddedSamples(samplesRootDir);
} catch (IOException e) {
throw new RuntimeException("Could not extract samples from " + asciidocSourcesRoot.value(), e);
throw new RuntimeException("Could not extract samples from " + samplesRootDir, e);
}
}
}
@@ -0,0 +1,44 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.samples.test.runner;

import org.gradle.samples.loader.SamplesDiscovery;
import org.gradle.samples.model.Sample;
import org.junit.runners.model.InitializationError;

import java.io.IOException;
import java.util.List;

/**
* A custom implementation of {@link SamplesRunner} that uses the Gradle Tooling API to execute sample builds.
*/
public class GradleEmbeddedSamplesRunner extends GradleSamplesRunner {
/**
* {@inheritDoc}
*/
public GradleEmbeddedSamplesRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}

@Override
protected List<Sample> getChildren() {
try {
return SamplesDiscovery.embeddedSamples(getSamplesRootDir());
} catch (IOException e) {
throw new RuntimeException("Could not extract embedded samples", e);
}
}
}
Expand Up @@ -20,7 +20,6 @@
import org.gradle.samples.executor.CommandExecutionResult;
import org.gradle.samples.executor.CommandExecutor;
import org.gradle.samples.executor.ExecutionMetadata;
import org.gradle.samples.loader.SamplesDiscovery;
import org.gradle.samples.model.Command;
import org.gradle.samples.model.Sample;
import org.gradle.testkit.runner.BuildResult;
Expand All @@ -45,9 +44,7 @@ public class GradleSamplesRunner extends SamplesRunner {
private File customGradleInstallation = null;

/**
* Constructs a new {@code ParentRunner} that will run {@code @TestClass}
*
* @param testClass reference to test class being run
* {@inheritDoc}
*/
public GradleSamplesRunner(Class<?> testClass) throws InitializationError {
super(testClass);
Expand All @@ -62,27 +59,27 @@ protected boolean isIgnored(Sample child) {
}

@Override
protected List<Sample> getChildren() {
// Allow Gradle installation and samples root dir to be set from a system property
// This is to allow Gradle to test Gradle installations during integration testing
final String gradleHomeDirProperty = System.getProperty("integTest.gradleHomeDir");
if (gradleHomeDirProperty != null) {
File customGradleInstallationDir = new File(gradleHomeDirProperty);
if (customGradleInstallationDir.exists()) {
this.customGradleInstallation = customGradleInstallationDir;
} else {
throw new RuntimeException(String.format("Custom Gradle installation dir at %s was not found", gradleHomeDirProperty));
}
public CommandExecutionResult execute(final File tempSampleOutputDir, final Command command) {
File workingDir = tempSampleOutputDir;
if (command.getExecutionSubdirectory() != null) {
workingDir = new File(tempSampleOutputDir, command.getExecutionSubdirectory());
}

File samplesRootDir;
boolean expectFailure = command.isExpectFailure();
ExecutionMetadata executionMetadata = getExecutionMetadata(tempSampleOutputDir);
return new GradleRunnerCommandExecutor(workingDir, customGradleInstallation, expectFailure).execute(command, executionMetadata);
}

@Override
protected File getSamplesRootDir() {
final String gradleHomeDir = getCustomGradleInstallationFromSystemProperty();
SamplesRoot samplesRoot = getTestClass().getAnnotation(SamplesRoot.class);
File samplesRootDir;
try {

if (samplesRoot != null) {
samplesRootDir = new File(samplesRoot.value());
} else if (System.getProperty("integTest.samplesdir") != null) {
String samplesRootProperty = System.getProperty("integTest.samplesdir", gradleHomeDirProperty + "/samples");
String samplesRootProperty = System.getProperty("integTest.samplesdir", gradleHomeDir + "/samples");
samplesRootDir = new File(samplesRootProperty);
} else if (customGradleInstallation != null) {
samplesRootDir = new File(customGradleInstallation, "samples");
Expand All @@ -93,22 +90,26 @@ protected List<Sample> getChildren() {
if (!samplesRootDir.exists()) {
throw new InitializationError("Samples root directory " + samplesRootDir.getAbsolutePath() + " does not exist");
}
return SamplesDiscovery.independentSamples(samplesRootDir);
} catch (InitializationError e) {
throw new RuntimeException("Could not initialize GradleSamplesRunner", e);
}

return samplesRootDir;
}

@Override
public CommandExecutionResult execute(final File tempSampleOutputDir, final Command command) {
File workingDir = tempSampleOutputDir;
if (command.getExecutionSubdirectory() != null) {
workingDir = new File(tempSampleOutputDir, command.getExecutionSubdirectory());
private String getCustomGradleInstallationFromSystemProperty() {
// Allow Gradle installation and samples root dir to be set from a system property
// This is to allow Gradle to test Gradle installations during integration testing
final String gradleHomeDirProperty = System.getProperty("integTest.gradleHomeDir");
if (gradleHomeDirProperty != null) {
File customGradleInstallationDir = new File(gradleHomeDirProperty);
if (customGradleInstallationDir.exists()) {
this.customGradleInstallation = customGradleInstallationDir;
} else {
throw new RuntimeException(String.format("Custom Gradle installation dir at %s was not found", gradleHomeDirProperty));
}
}

boolean expectFailure = command.isExpectFailure();
ExecutionMetadata executionMetadata = getExecutionMetadata(tempSampleOutputDir);
return new GradleRunnerCommandExecutor(workingDir, customGradleInstallation, expectFailure).execute(command, executionMetadata);
return gradleHomeDirProperty;
}

private static class GradleRunnerCommandExecutor extends CommandExecutor {
Expand Down
Expand Up @@ -75,6 +75,10 @@ public SamplesRunner(Class<?> testClass) throws InitializationError {

@Override
protected List<Sample> getChildren() {
return SamplesDiscovery.externalSamples(getSamplesRootDir());
}

protected File getSamplesRootDir() {
SamplesRoot samplesRoot = getTestClass().getAnnotation(SamplesRoot.class);
File samplesRootDir;
try {
Expand All @@ -87,10 +91,10 @@ protected List<Sample> getChildren() {
if (!samplesRootDir.exists()) {
throw new InitializationError("Samples root directory " + samplesRootDir.getAbsolutePath() + " does not exist");
}
return SamplesDiscovery.independentSamples(samplesRootDir);
} catch (InitializationError e) {
throw new RuntimeException("Could not initialize SamplesRunner", e);
}
return samplesRootDir;
}

@Override
Expand Down Expand Up @@ -155,6 +159,7 @@ private void verifyOutput(final Command command, final CommandExecutionResult ex
}

public CommandExecutionResult execute(final File tempSampleOutputDir, final Command command) throws IOException {
// TODO: get executor
return new CliCommandExecutor(tempSampleOutputDir).execute(command, getExecutionMetadata(tempSampleOutputDir));
}

Expand Down
Expand Up @@ -18,6 +18,6 @@
import org.junit.runner.RunWith;

@RunWith(EmbeddedSamplesRunner.class)
@AsciidocSourcesRoot("src/test/docs")
@SamplesRoot("src/test/docs")
public class EmbeddedSamplesRunnerIntegrationTest {
}
Expand Up @@ -28,12 +28,12 @@
import java.util.List;

public class SamplesDiscovery {
public static List<Sample> independentSamples(File rootSamplesDir) {
public static List<Sample> externalSamples(File rootSamplesDir) {
// The .sample.conf suffix makes it clear that this is a HOCON file specifically for samples
return filteredIndependentSamples(rootSamplesDir, new String[]{"sample.conf"}, true);
return filteredExternalSamples(rootSamplesDir, new String[]{"sample.conf"}, true);
}

public static List<Sample> filteredIndependentSamples(File rootSamplesDir, String[] fileExtensions, boolean recursive) {
public static List<Sample> filteredExternalSamples(File rootSamplesDir, String[] fileExtensions, boolean recursive) {
Collection<File> sampleConfigFiles = FileUtils.listFiles(rootSamplesDir, fileExtensions, recursive);

List<Sample> samples = new ArrayList<>();
Expand Down
Expand Up @@ -33,7 +33,7 @@ class SamplesDiscoveryTest extends Specification {
// tmpDir.newFile("advanced-sample/nested/crazy.sample.conf") << "commands: [{executable: build}, {executable: cleanup}]"

when:
Collection<Sample> samples = SamplesDiscovery.independentSamples(tmpDir.root)
Collection<Sample> samples = SamplesDiscovery.externalSamples(tmpDir.root)

then:
samples.size() == 2
Expand All @@ -47,7 +47,7 @@ class SamplesDiscoveryTest extends Specification {
tmpDir.newFile("src/play/bogus.conf") << "I'm not a sample file"

when:
Collection<Sample> samples = SamplesDiscovery.filteredIndependentSamples(tmpDir.root, ["sample"].toArray() as String[], true)
Collection<Sample> samples = SamplesDiscovery.filteredExternalSamples(tmpDir.root, ["sample"].toArray() as String[], true)

then:
samples.size() == 1
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/gradle/exemplar/ReadmeTest.java
Expand Up @@ -15,11 +15,11 @@
*/
package org.gradle.exemplar;

import org.gradle.samples.test.runner.AsciidocSourcesRoot;
import org.gradle.samples.test.runner.EmbeddedSamplesRunner;
import org.gradle.samples.test.runner.GradleEmbeddedSamplesRunner;
import org.gradle.samples.test.runner.SamplesRoot;
import org.junit.runner.RunWith;

@RunWith(EmbeddedSamplesRunner.class)
@AsciidocSourcesRoot("docs")
@RunWith(GradleEmbeddedSamplesRunner.class)
@SamplesRoot("docs")
public class ReadmeTest {
}

0 comments on commit bd4de10

Please sign in to comment.