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

Fix cucumberjvm package label generation #406

Merged
merged 2 commits into from
Dec 2, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,23 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static io.qameta.allure.util.ResultsUtils.createFeatureLabel;
import static io.qameta.allure.util.ResultsUtils.createFrameworkLabel;
import static io.qameta.allure.util.ResultsUtils.createHostLabel;
import static io.qameta.allure.util.ResultsUtils.createLabel;
import static io.qameta.allure.util.ResultsUtils.createLanguageLabel;
import static io.qameta.allure.util.ResultsUtils.createPackageLabel;
import static io.qameta.allure.util.ResultsUtils.createStoryLabel;
import static io.qameta.allure.util.ResultsUtils.createSuiteLabel;
import static io.qameta.allure.util.ResultsUtils.createTestClassLabel;
Expand Down Expand Up @@ -122,13 +120,16 @@ class LabelBuilder {
createThreadLabel(),
createFeatureLabel(featureName),
createStoryLabel(scenario.getName()),
createPackageLabel(featurePackage(uri, featureName)),
createSuiteLabel(featureName),
createTestClassLabel(scenario.getName()),
createFrameworkLabel("cucumber4jvm"),
createLanguageLabel("java"),
createLabel("gherkin_uri", uri)
));

featurePackage(uri, featureName)
.map(ResultsUtils::createPackageLabel)
.ifPresent(getScenarioLabels()::add);
}

public List<Label> getScenarioLabels() {
Expand Down Expand Up @@ -162,15 +163,32 @@ private void tryHandleNamedLink(final String tagString) {
}
}

private String featurePackage(final String uri, final String featureName) {
final Path parent = Paths.get(URI.create(uri).getSchemeSpecificPart()).getParent();
if (Objects.nonNull(parent)) {
final Stream<String> folders = StreamSupport.stream(parent.spliterator(), false)
.map(Path::toString);
final Stream<String> name = Stream.of(featureName);
return Stream.concat(folders, name).collect(Collectors.joining("."));
private Optional<String> featurePackage(final String uriString, final String featureName) {
final Optional<URI> maybeUri = safeUri(uriString);
if (!maybeUri.isPresent()) {
return Optional.empty();
}
URI uri = maybeUri.get();

if (!uri.isOpaque()) {
final URI work = new File("").toURI();
uri = work.relativize(uri);
}
return featureName;
final String schemeSpecificPart = uri.normalize().getSchemeSpecificPart();
final Stream<String> folders = Stream.of(schemeSpecificPart.replaceAll("\\.", "_").split("/"));
final Stream<String> name = Stream.of(featureName);
return Optional.of(Stream.concat(folders, name)
.filter(Objects::nonNull)
.filter(s -> !s.isEmpty())
.collect(Collectors.joining(".")));
}

private static Optional<URI> safeUri(final String uri) {
try {
return Optional.of(URI.create(uri));
} catch (Exception e) {
LOGGER.debug("could not parse feature uri {}", uri, e);
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ void shouldAddBddLabels() {

@AllureFeatures.Timeline
@Test
void shouldThreadHostLabels() {
void shouldAddThreadHostLabels() {
final AllureResultsWriterStub writer = new AllureResultsWriterStub();
runFeature(writer, "features/tags.feature");

Expand All @@ -452,7 +452,7 @@ void shouldThreadHostLabels() {

@AllureFeatures.MarkerAnnotations
@Test
void shouldCommonLabels() {
void shouldAddCommonLabels() {
final AllureResultsWriterStub writer = new AllureResultsWriterStub();
runFeature(writer, "features/tags.feature");

Expand All @@ -462,7 +462,7 @@ void shouldCommonLabels() {
.flatExtracting(TestResult::getLabels)
.extracting(Label::getName, Label::getValue)
.contains(
tuple(PACKAGE_LABEL_NAME, "features.Test Simple Scenarios"),
tuple(PACKAGE_LABEL_NAME, "features.tags_feature.Test Simple Scenarios"),
tuple(SUITE_LABEL_NAME, "Test Simple Scenarios"),
tuple(TEST_CLASS_LABEL_NAME, "Add a to b")
);
Expand Down