Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Use uri instead of path in CucumberFeature #562

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 12 additions & 1 deletion core/src/main/java/cucumber/runtime/FeatureBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import gherkin.parser.Parser;
import gherkin.util.FixJava;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
Expand All @@ -28,13 +29,19 @@
public class FeatureBuilder implements Formatter {
private static final Charset UTF8 = Charset.forName("UTF-8");
private final List<CucumberFeature> cucumberFeatures;
private final char fileSeparatorChar;
private final MessageDigest md5;
private final Map<String, String> pathsByChecksum = new HashMap<String, String>();
private CucumberFeature currentCucumberFeature;
private String uri;

public FeatureBuilder(List<CucumberFeature> cucumberFeatures) {
this(cucumberFeatures, File.separatorChar);
}

FeatureBuilder(List<CucumberFeature> cucumberFeatures, char fileSeparatorChar) {
this.cucumberFeatures = cucumberFeatures;
this.fileSeparatorChar = fileSeparatorChar;
try {
this.md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
Expand Down Expand Up @@ -110,7 +117,7 @@ public void parse(Resource resource, List<Object> filters) {
}
Parser parser = new Parser(formatter);

parser.parse(gherkin, resource.getPath(), 0);
parser.parse(gherkin, convertPathToUri(resource.getPath()), 0);
I18n i18n = parser.getI18nLanguage();
if (currentCucumberFeature != null) {
// The current feature may be null if we used a very restrictive filter, say a tag that isn't used.
Expand All @@ -119,6 +126,10 @@ public void parse(Resource resource, List<Object> filters) {
}
}

private String convertPathToUri(String path) {
return path.replace(fileSeparatorChar, '/');
}

private String checksum(String gherkin) {
return new BigInteger(1, md5.digest(gherkin.getBytes(UTF8))).toString(16);
}
Expand Down
46 changes: 41 additions & 5 deletions core/src/test/java/cucumber/runtime/FeatureBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,50 @@ public class FeatureBuilderTest {
public void ignores_duplicate_features() throws IOException {
List<CucumberFeature> features = new ArrayList<CucumberFeature>();
FeatureBuilder builder = new FeatureBuilder(features);
Resource resource = mock(Resource.class);
when(resource.getPath()).thenReturn("foo.feature");
ByteArrayInputStream firstFeature = new ByteArrayInputStream("Feature: foo".getBytes("UTF-8"));
ByteArrayInputStream secondFeature = new ByteArrayInputStream("Feature: foo".getBytes("UTF-8"));
when(resource.getInputStream()).thenReturn(firstFeature, secondFeature);
String featurePath = "foo.feature";
Resource resource1 = createResourceMock(featurePath);
Resource resource2 = createResourceMock(featurePath);

builder.parse(resource1, NO_FILTERS);
builder.parse(resource2, NO_FILTERS);

assertEquals(1, features.size());
}

@Test
public void works_when_path_and_uri_are_the_same() throws IOException {
char fileSeparatorChar = '/';
String featurePath = "path" + fileSeparatorChar + "foo.feature";
Resource resource = createResourceMock(featurePath);
List<CucumberFeature> features = new ArrayList<CucumberFeature>();
FeatureBuilder builder = new FeatureBuilder(features, fileSeparatorChar);

builder.parse(resource, NO_FILTERS);

assertEquals(1, features.size());
assertEquals(featurePath, features.get(0).getUri());
}

@Test
public void converts_windows_path_to_uri() throws IOException {
char fileSeparatorChar = '\\';
String featurePath = "path" + fileSeparatorChar + "foo.feature";
Resource resource = createResourceMock(featurePath);
List<CucumberFeature> features = new ArrayList<CucumberFeature>();
FeatureBuilder builder = new FeatureBuilder(features, fileSeparatorChar);

builder.parse(resource, NO_FILTERS);

assertEquals(1, features.size());
assertEquals("path/foo.feature", features.get(0).getUri());
}

private Resource createResourceMock(String featurePath) throws IOException {
Resource resource = mock(Resource.class);
when(resource.getPath()).thenReturn(featurePath);
ByteArrayInputStream feature = new ByteArrayInputStream("Feature: foo".getBytes("UTF-8"));
when(resource.getInputStream()).thenReturn(feature);
return resource;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class JSONPrettyFormatterTest {
public void featureWithOutlineTest() throws Exception {
File report = runFeaturesWithJSONPrettyFormatter(asList("cucumber/runtime/formatter/JSONPrettyFormatterTest.feature"));
String expected = new Scanner(getClass().getResourceAsStream("JSONPrettyFormatterTest.json"), "UTF-8").useDelimiter("\\A").next();
expected = expected.replace("cucumber/runtime/formatter/JSONPrettyFormatterTest.feature", "cucumber" + File.separator + "runtime"
+ File.separator + "formatter" + File.separator + "JSONPrettyFormatterTest.feature");
String actual = new Scanner(report, "UTF-8").useDelimiter("\\A").next();
assertEquals(expected, actual);
}
Expand Down