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

[Core] Add location to tag expression expcetion #1979

Merged
merged 19 commits into from
Jun 11, 2020
Merged

Conversation

cyocum
Copy link
Contributor

@cyocum cyocum commented May 25, 2020

Summary

This integrates changes to the way that TagExpressionParser works in Cucumber-JVM to fix #1976.

Details

Updated the TagPredicate and the CoreHookDefinition to catch TagExpressionException and add information from the relevant classes to the exception message which is then wrapped in a RuntimeException and rethrown. The rethrow as a RuntimeException is due to the fact that the constructor for TagExpressionParser is package scope only and is not available in other packages.

Motivation and Context

This is to fix issue #1976.

How Has This Been Tested?

Added tests to both TagPredicateTest and CoreHookDefinition test to see if the proper exception was rethrown and if the information expected was included in the expected exception.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

Checklist:

  • I've added tests for my code.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

cyocum added 4 commits May 25, 2020 12:34
* Updated TagExpressionParser to use static parse function
* WIP on fixing #1976
* Added TagExpressionException interception to CoreHookDefinition to
  add the hook that caused the exception to be added to the exception
  information at runtime.
* Added a test for this in HookTest
* Updated TagPredicate to intercept TagExpressionException to add
  information about where the exceptin comes form.
* Added a test to ensure that the information is added
* Updated various parsers to use TagExpressionParser so that the parse
  is closer to the beginning of the run.

* Updated tests to pass.  Additionally, as there is no equals method
  override on Expression instances, I had to call toString() on them so
  that matching could occur.  This could be improved by overriding
  equals so that Hamcrest matchers could match directly but overriding
  equals is tricky to get right so used the already overridden toString
  instead.
cyocum and others added 6 commits May 27, 2020 10:20
* Updated tag expression parsing on CommandLineOptionParser and
  CucumberOptionsAnnotationParser
* Updated everything that uses the tag expression to use and
  Expression object rather than a string
* Updated tests to reflect the new method of parsing
* Updated everything that uses the tag expression to use and
  Expression object rather than a string
* Updated tests to reflect the new method of parsing (d91208c)
try {
this.tagExpression = TagExpressionParser.parse(delegate.getTagExpression());
} catch (TagExpressionException tee) {
throw new IllegalArgumentException(String.format("%s at '%s'", tee.getMessage(), delegate.getLocation()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You originally pushed this down into the different hook implementations. I think because I suggested something about being as close to the source as possible. However this means that we'll have to implement the correct location reporting in all different hook implementations. Using the getLocation method we can get the location of the hook. So this is close enough.

try {
parsedOptions.addTagFilter(TagExpressionParser.parse(removeArgFor(arg, args)));
} catch (TagExpressionException tee) {
throw new IllegalArgumentException(tee);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll have to look at this. Currently the problem is that it doesn't report back where the problem came from. But this is true for all other CLI arguments as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be easier if we have a shared session to discuss the options.

@mpkorstanje
Copy link
Contributor

mpkorstanje commented Jun 10, 2020

I've reverted the changes to cucumber-java and cucumber-java8. I also cleaned up some non-idiomatic java stuff. You can use Optional to avoid null checks but only when a method already returns an optional type. Using Optional.offNullable(thing).ifPresent(...) doesn't help.

This usually means that a bit more code has to change. Something like:

        @Override
        public Optional<CucumberOptionsAnnotationParser.CucumberOptions> getOptions(Class<?> clazz) {
            final CucumberOptions annotation = clazz.getAnnotation(CucumberOptions.class);
            if (annotation == null) {
                return Optional.empty();
            }
            return Optional.of(new CoreCucumberOptions(annotation));
        }

    }
            requireNonNull(optionsProvider).getOptions(classWithOptions)
                    .ifPresent(options -> {
                        addDryRun(options, args);
                        addMonochrome(options, args);
                        addTags(clazz, options, args);
                        addPlugins(options, args);
                        addStrict(options, args);
                        addName(options, args);
                        addSnippets(options, args);
                        addGlue(options, args);
                        addFeatures(options, args);
                        addObjectFactory(options, args);
                    });
        }

@cyocum
Copy link
Contributor Author

cyocum commented Jun 10, 2020

Thanks for the changes. I have not had a chance to look over them yet. I know you had suggested to revert the change that I made to HookDefinition. I have done that locally but I am having serious difficulties with a test that I cannot see why it is failing so I am a bit stuck at the moment.

@mpkorstanje mpkorstanje changed the title Tag exception loc [Core] Add location to tag expression expcetion Jun 11, 2020
@mpkorstanje mpkorstanje merged commit e65f3fc into master Jun 11, 2020
@mpkorstanje mpkorstanje deleted the tag_exception_loc branch June 11, 2020 13:00
@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 85.995% when pulling b03016f on tag_exception_loc into afa3a99 on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TagExpressionException doesn't include location
3 participants