Skip to content

Support reading feature paths from the rerun formatter file #726

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 4 commits into from
Jun 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/cucumber/runtime/FeatureBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private String checksum(String gherkin) {
return new BigInteger(1, md5.digest(gherkin.getBytes(UTF8))).toString(16);
}

private String read(Resource resource) {
public String read(Resource resource) {
try {
String source = FixJava.readReader(new InputStreamReader(resource.getInputStream(), "UTF-8"));
String encoding = new Encoding().encoding(source);
Expand Down
11 changes: 2 additions & 9 deletions core/src/main/java/cucumber/runtime/RuntimeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class RuntimeOptions {

private final List<String> glue = new ArrayList<String>();
private final List<Object> filters = new ArrayList<Object>();
private final List<Object> lineFilters = new ArrayList<Object>();
private final List<Formatter> formatters = new ArrayList<Formatter>();
private final List<String> featurePaths = new ArrayList<String>();
private final List<String> formatterNames = new ArrayList<String>();
Expand Down Expand Up @@ -80,7 +79,6 @@ public RuntimeOptions(Env env, FormatterFactory formatterFactory, List<String> a
if (cucumberOptionsFromEnv != null) {
parse(Shellwords.parse(cucumberOptionsFromEnv));
}
filters.addAll(lineFilters);

if (formatterNames.isEmpty()) {
formatterNames.add("progress");
Expand All @@ -89,7 +87,6 @@ public RuntimeOptions(Env env, FormatterFactory formatterFactory, List<String> a

private void parse(List<String> args) {
List<Object> parsedFilters = new ArrayList<Object>();
List<Object> parsedLineFilters = new ArrayList<Object>();
List<String> parsedFeaturePaths = new ArrayList<String>();
List<String> parsedGlue = new ArrayList<String>();

Expand Down Expand Up @@ -129,9 +126,7 @@ private void parse(List<String> args) {
printUsage();
throw new CucumberException("Unknown option: " + arg);
} else {
PathWithLines pathWithLines = new PathWithLines(arg);
parsedFeaturePaths.add(pathWithLines.path);
parsedLineFilters.addAll(pathWithLines.lines);
parsedFeaturePaths.add(arg);
}
}
if (!parsedFilters.isEmpty()) {
Expand All @@ -140,9 +135,7 @@ private void parse(List<String> args) {
}
if (!parsedFeaturePaths.isEmpty()) {
featurePaths.clear();
lineFilters.clear();
featurePaths.addAll(parsedFeaturePaths);
lineFilters.addAll(parsedLineFilters);
}
if (!parsedGlue.isEmpty()) {
glue.clear();
Expand All @@ -157,7 +150,7 @@ private void printUsage() {
public List<CucumberFeature> cucumberFeatures(ResourceLoader resourceLoader) {
return load(resourceLoader, featurePaths, filters, System.out);
}

List<Formatter> getFormatters() {
if(!formattersCreated) {
for (String formatterName : formatterNames){
Expand Down
44 changes: 41 additions & 3 deletions core/src/main/java/cucumber/runtime/model/CucumberFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cucumber.runtime.FeatureBuilder;
import cucumber.runtime.Runtime;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.Resource;
import cucumber.runtime.io.ResourceLoader;
import gherkin.I18n;
Expand Down Expand Up @@ -47,15 +48,52 @@ public static List<CucumberFeature> load(ResourceLoader resourceLoader, List<Str
final List<CucumberFeature> cucumberFeatures = new ArrayList<CucumberFeature>();
final FeatureBuilder builder = new FeatureBuilder(cucumberFeatures);
for (String featurePath : featurePaths) {
Iterable<Resource> resources = resourceLoader.resources(featurePath, ".feature");
for (Resource resource : resources) {
builder.parse(resource, filters);
if (featurePath.startsWith("@")) {
loadFromRerunFile(builder, resourceLoader, featurePath.substring(1), filters);
} else {
loadFromFeaturePath(builder, resourceLoader, featurePath, filters);
}
}
Collections.sort(cucumberFeatures, new CucumberFeatureUriComparator());
return cucumberFeatures;
}

private static void loadFromRerunFile(FeatureBuilder builder, ResourceLoader resourceLoader, String rerunPath, final List<Object> filters) {
Iterable<Resource> resources = resourceLoader.resources(rerunPath, null);
for (Resource resource : resources) {
String source = builder.read(resource);
for (String featurePath : source.split(" ")) {
loadFromFileSystemOrClasspath(builder, resourceLoader, featurePath, filters);
}
}
}

private static void loadFromFileSystemOrClasspath(FeatureBuilder builder, ResourceLoader resourceLoader, String featurePath, final List<Object> filters) {
try {
loadFromFeaturePath(builder, resourceLoader, featurePath, filters);
} catch (IllegalArgumentException originalException) {
if (!featurePath.startsWith(MultiLoader.CLASSPATH_SCHEME)) {
try {
loadFromFeaturePath(builder, resourceLoader, MultiLoader.CLASSPATH_SCHEME + featurePath, filters);
} catch (IllegalArgumentException secondException) {
throw originalException;
}
} else {
throw originalException;
}
}
}

private static void loadFromFeaturePath(FeatureBuilder builder, ResourceLoader resourceLoader, String featurePath, final List<Object> filters) {
PathWithLines pathWithLines = new PathWithLines(featurePath);
ArrayList<Object> filtersForPath = new ArrayList<Object>(filters);
filtersForPath.addAll(pathWithLines.lines);
Iterable<Resource> resources = resourceLoader.resources(pathWithLines.path, ".feature");
for (Resource resource : resources) {
builder.parse(resource, filtersForPath);
}
}

public CucumberFeature(Feature feature, String path) {
this.feature = feature;
this.path = path;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cucumber.runtime;
package cucumber.runtime.model;

import java.util.ArrayList;
import java.util.List;
Expand Down
15 changes: 12 additions & 3 deletions core/src/main/resources/cucumber/api/cli/USAGE.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Usage: java cucumber.api.cli.Main [options] [ [FILE|DIR][:LINE[:LINE]*] ]+
Usage: java cucumber.api.cli.Main [options] [[[FILE|DIR][:LINE[:LINE]*] ]+ | @FILE ]

Options:

-g, --glue PATH Where glue code (step definitions and hooks) is loaded from.
-f, --format FORMAT[:PATH_OR_URL] How to format results. Goes to STDOUT unless PATH_OR_URL is specified.
Built-in FORMAT types: junit, html, pretty, progress, json.
FORMAT can also be a fully qualified class name.
Built-in FORMAT types: junit, html, pretty, progress, json, usage,
rerun. FORMAT can also be a fully qualified class name.
-t, --tags TAG_EXPRESSION Only run scenarios tagged with tags matching TAG_EXPRESSION.
-n, --name REGEXP Only run scenarios whose names match REGEXP.
-d, --[no-]-dry-run Skip execution of glue code.
Expand All @@ -16,3 +16,12 @@ Options:
path or a URL.
-v, --version Print version.
-h, --help You're looking at it.

Feature path examples:
<path> Load the files with the extension ".feature" for the directory <path>
and its sub directories.
<path>/<name>.feature Load the feature file <path>/<name>.feature from the file system.
classpath:<path>/<name>.feature Load the feature file <path>/<name>.feature from the classpath.
<path>/<name>.feature:3:9 Load the scenarios on line 3 and line 9 in the file
<path>/<name>.feature.
@<path>/<file> Parse <path>/<file> for feature paths generated by the rerun formatter.
59 changes: 53 additions & 6 deletions core/src/test/java/cucumber/runtime/RuntimeOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import cucumber.runtime.formatter.ColorAware;
import cucumber.runtime.formatter.FormatterFactory;
import cucumber.runtime.formatter.StrictAware;
import cucumber.runtime.io.Resource;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.model.CucumberFeature;
import gherkin.formatter.Formatter;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -42,17 +49,17 @@ public void assigns_feature_paths() {
}

@Test
public void assigns_line_filters_from_feature_paths() {
public void keep_line_filters_on_feature_paths() {
RuntimeOptions options = new RuntimeOptions("--glue somewhere somewhere_else:3");
assertEquals(asList("somewhere_else"), options.getFeaturePaths());
assertEquals(asList(3L), options.getFilters());
assertEquals(asList("somewhere_else:3"), options.getFeaturePaths());
assertEquals(Collections.<Object>emptyList(), options.getFilters());
}

@Test
public void assigns_filters_and_line_filters_from_feature_paths() {
public void assigns_filters_from_tags() {
RuntimeOptions options = new RuntimeOptions("--tags @keep_this somewhere_else:3");
assertEquals(asList("somewhere_else"), options.getFeaturePaths());
assertEquals(Arrays.<Object>asList("@keep_this", 3L), options.getFilters());
assertEquals(asList("somewhere_else:3"), options.getFeaturePaths());
assertEquals(Arrays.<Object>asList("@keep_this"), options.getFilters());
}

@Test
Expand Down Expand Up @@ -251,4 +258,44 @@ public void set_snippet_type() {
assertEquals(SnippetType.CAMELCASE, runtimeOptions.getSnippetType());
}

@Test
public void applies_line_filters_only_to_own_feature() throws Exception {
String featurePath1 = "path/bar.feature";
String feature1 = "" +
"Feature: bar\n" +
" Scenario: scenario_1_1\n" +
" * step\n" +
" Scenario: scenario_1_2\n" +
" * step\n";
String featurePath2 = "path/foo.feature";
String feature2 = "" +
"Feature: foo\n" +
" Scenario: scenario_2_1\n" +
" * step\n" +
" Scenario: scenario_2_2\n" +
" * step\n";
ResourceLoader resourceLoader = mock(ResourceLoader.class);
mockResource(resourceLoader, featurePath1, feature1);
mockResource(resourceLoader, featurePath2, feature2);
RuntimeOptions options = new RuntimeOptions(featurePath1 + ":2 " + featurePath2 + ":4");

List<CucumberFeature> features = options.cucumberFeatures(resourceLoader);

assertEquals(2, features.size());
assertOnlyScenarioName(features.get(0), "scenario_1_1");
assertOnlyScenarioName(features.get(1), "scenario_2_2");
}

private void assertOnlyScenarioName(CucumberFeature feature, String scenarioName) {
assertEquals("Wrong number of scenarios loaded for feature", 1, feature.getFeatureElements().size());
assertEquals("Scenario: " + scenarioName, feature.getFeatureElements().get(0).getVisualName());
}

private void mockResource(ResourceLoader resourceLoader, String featurePath, String feature)
throws IOException, UnsupportedEncodingException {
Resource resource1 = mock(Resource.class);
when(resource1.getPath()).thenReturn(featurePath);
when(resource1.getInputStream()).thenReturn(new ByteArrayInputStream(feature.getBytes("UTF-8")));
when(resourceLoader.resources(featurePath, ".feature")).thenReturn(asList(resource1));
}
}
115 changes: 110 additions & 5 deletions core/src/test/java/cucumber/runtime/model/CucumberFeatureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -39,11 +42,7 @@ public void logs_message_if_no_features_are_found() {
@Test
public void logs_message_if_features_are_found_but_filters_are_too_strict() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ResourceLoader resourceLoader = mock(ResourceLoader.class);
Resource resource = mock(Resource.class);
when(resource.getPath()).thenReturn("foo.feature");
when(resource.getInputStream()).thenReturn(new ByteArrayInputStream("Feature: foo".getBytes("UTF-8")));
when(resourceLoader.resources("features", ".feature")).thenReturn(asList(resource));
ResourceLoader resourceLoader = mockFeatureFileResource("features", "Feature: foo");

CucumberFeature.load(resourceLoader, asList("features"), asList((Object) "@nowhere"), new PrintStream(baos));

Expand All @@ -60,4 +59,110 @@ public void logs_message_if_no_feature_paths_are_given() {
assertEquals(String.format("Got no path to feature directory or feature file%n"), baos.toString());
}

@Test
public void applies_line_filters_when_loading_a_feature() throws Exception {
String featurePath = "path/foo.feature";
String feature = "" +
"Feature: foo\n" +
" Scenario: scenario 1\n" +
" * step\n" +
" Scenario: scenario 2\n" +
" * step\n";
ResourceLoader resourceLoader = mockFeatureFileResource(featurePath, feature);

List<CucumberFeature> features = CucumberFeature.load(
resourceLoader,
asList(featurePath + ":2"),
new ArrayList<Object>(),
new PrintStream(new ByteArrayOutputStream()));

assertEquals(1, features.size());
assertEquals(1, features.get(0).getFeatureElements().size());
assertEquals("Scenario: scenario 1", features.get(0).getFeatureElements().get(0).getVisualName());
}

@Test
public void loads_features_specified_in_rerun_file() throws Exception {
String featurePath1 = "path/bar.feature";
String feature1 = "" +
"Feature: bar\n" +
" Scenario: scenario bar\n" +
" * step\n";
String featurePath2 = "path/foo.feature";
String feature2 = "" +
"Feature: foo\n" +
" Scenario: scenario 1\n" +
" * step\n" +
" Scenario: scenario 2\n" +
" * step\n";
String rerunPath = "path/rerun.txt";
String rerunFile = featurePath1 + ":2 " + featurePath2 + ":4";
ResourceLoader resourceLoader = mockFeatureFileResource(featurePath1, feature1);
mockFeatureFileResource(resourceLoader, featurePath2, feature2);
mockFileResource(resourceLoader, rerunPath, null, rerunFile);

List<CucumberFeature> features = CucumberFeature.load(
resourceLoader,
asList("@" + rerunPath),
new ArrayList<Object>(),
new PrintStream(new ByteArrayOutputStream()));

assertEquals(2, features.size());
assertEquals(1, features.get(0).getFeatureElements().size());
assertEquals("Scenario: scenario bar", features.get(0).getFeatureElements().get(0).getVisualName());
assertEquals(1, features.get(1).getFeatureElements().size());
assertEquals("Scenario: scenario 2", features.get(1).getFeatureElements().get(0).getVisualName());
}

@Test
public void loads_features_specified_in_rerun_file_from_classpath_when_not_in_file_system() throws Exception {
String featurePath = "path/bar.feature";
String feature = "" +
"Feature: bar\n" +
" Scenario: scenario bar\n" +
" * step\n";
String rerunPath = "path/rerun.txt";
String rerunFile = featurePath + ":2";
ResourceLoader resourceLoader = mockFeatureFileResource("classpath:" + featurePath, feature);
mockFeaturePathToNotExist(resourceLoader, featurePath);
mockFileResource(resourceLoader, rerunPath, suffix(null), rerunFile);

List<CucumberFeature> features = CucumberFeature.load(
resourceLoader,
asList("@" + rerunPath),
new ArrayList<Object>(),
new PrintStream(new ByteArrayOutputStream()));

assertEquals(1, features.size());
assertEquals(1, features.get(0).getFeatureElements().size());
assertEquals("Scenario: scenario bar", features.get(0).getFeatureElements().get(0).getVisualName());
}

private ResourceLoader mockFeatureFileResource(String featurePath, String feature)
throws IOException, UnsupportedEncodingException {
ResourceLoader resourceLoader = mock(ResourceLoader.class);
mockFeatureFileResource(resourceLoader, featurePath, feature);
return resourceLoader;
}

private void mockFeatureFileResource(ResourceLoader resourceLoader, String featurePath, String feature)
throws IOException, UnsupportedEncodingException {
mockFileResource(resourceLoader, featurePath, ".feature", feature);
}

private void mockFileResource(ResourceLoader resourceLoader, String featurePath, String extension, String feature)
throws IOException, UnsupportedEncodingException {
Resource resource = mock(Resource.class);
when(resource.getPath()).thenReturn(featurePath);
when(resource.getInputStream()).thenReturn(new ByteArrayInputStream(feature.getBytes("UTF-8")));
when(resourceLoader.resources(featurePath, extension)).thenReturn(asList(resource));
}

private void mockFeaturePathToNotExist(ResourceLoader resourceLoader, String featurePath) {
when(resourceLoader.resources(featurePath, ".feature")).thenThrow(new IllegalArgumentException("Not a file or directory"));
}

private String suffix(String suffix) {
return suffix;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cucumber.runtime;
package cucumber.runtime.model;

import org.junit.Test;

Expand Down