Skip to content

Add implied features... - #1598

Merged
leonard84 merged 6 commits into
spockframework:masterfrom
Vampire:issue-1593
Mar 20, 2023
Merged

leonard84 merged 6 commits into
spockframework:masterfrom
Vampire:issue-1593

Conversation

@Vampire

@Vampire Vampire commented Mar 10, 2023

Copy link
Copy Markdown
Member

Fixes #1593

With these changes, the dependency features are executed
and any extension could also define such dependencies.

It works perfectly fine for Maven and IntelliJ.
For Gradle it also works, as far as running is concerned,
the dependency tests are just not reported if they were successful
and reported awkwardly when they have a failure like being part of
UnknownClass. But that is most probably more a Gradle problem.

@codecov

codecov Bot commented Mar 10, 2023 •

Copy link
Copy Markdown

Codecov Report

Patch coverage: 91.54% and project coverage change: +0.09 🎉

Comparison is base (3d7aa63) 78.18% compared to head (6171c1c) 78.27%.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1598      +/-   ##
============================================
+ Coverage     78.18%   78.27%   +0.09%     
- Complexity     3992     4023      +31     
============================================
  Files           422      423       +1     
  Lines         12922    12991      +69     
  Branches       1675     1679       +4     
============================================
+ Hits          10103    10169      +66     
- Misses         2195     2196       +1     
- Partials        624      626       +2     
Impacted Files Coverage Δ
.../org/spockframework/runtime/model/FeatureInfo.java 92.77% <69.23%> (-4.38%) ⬇️
...spockframework/runtime/ImplicationFeatureNode.java 95.55% <95.55%> (ø)
...n/java/org/spockframework/runtime/FeatureNode.java 87.50% <100.00%> (+1.78%) ⬆️
...ork/runtime/SpockEngineDiscoveryPostProcessor.java 94.28% <100.00%> (+0.53%) ⬆️
...ain/java/org/spockframework/runtime/SpockNode.java 80.39% <100.00%> (+1.66%) ⬆️
...k/runtime/extension/builtin/StepwiseExtension.java 97.43% <100.00%> (+2.84%) ⬆️

... and 1 file with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Vampire
Vampire marked this pull request as draft March 10, 2023 17:57
@Vampire

Vampire commented Mar 10, 2023

Copy link
Copy Markdown
Member Author

Hm, if the dependencies are data-driven, the iterations do get reported even if successful, but also as part of UnknownClass, such for now changed to draft to see whether this can be improved.

@leonard84 leonard84 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

An interesting idea, but currently it has too many issues.

import static java.util.stream.Collectors.toSet;

public abstract class FeatureNode extends SpockNode<FeatureInfo> {
private boolean alreadyPrepared = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This will probably cause issues when parallel execution is enabled. I don't know if it will suffice to make it into an AtomicBoolean/volatile.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I thought about that when writing it.
But I think any sane usage of such dependencies need to use SAME_THREAD anyway which the @Stepwise also already implies.
So I can add a volatile but I don't think that would change much.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Then, shouldn't adding dependencies imply the SAME_THREAD execution mode on all? If we would add this new feature then we should minimize the potential for problems.
(No need to fix this at the moment until the other issues are solved, just wanted to point it out.)


private final List<FeatureInfo> dependencies = new ArrayList<>();

private FeatureNode node;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This introduces a cyclic dependency between these classes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

And would that be a problem?

@leonard84 leonard84 Mar 16, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

They are often considered a code smell.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok, I replaced the explicit dependency causing the cycle with a search through the node-tree to find the according node.

for (int i = features.size() - 1; i > 0; i--) {
FeatureInfo feature = features.get(i);
for (int j = i - 1; j >= 0; j--) {
feature.addDependency(features.get(j));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wouldn't it be enough to just depend on the previous feature?

@Vampire Vampire Mar 11, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I had that first, but if step2 is ignored, step1 is still a dependency but would then not be executed.

}

@Override
public SpockExecutionContext execute(SpockExecutionContext context, DynamicTestExecutor dynamicTestExecutor) throws Exception {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Running other things in execute is awkward as it will have already initialized the current test instance and executed its setup. This can lead to all kind of weird problems, e.g. @Shared int counter would show weird behavior if incremented in the setup.

Furthermore, I think this will cause issues with @ResourceLock, trying it threw this error.

org.junit.platform.commons.JUnitException: Dynamic test descriptors must not declare exclusive resources: [ExclusiveResource [key = 'foo', lockMode = READ_WRITE]]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm, bad about the resource lock indeed, while usage wise the resource lock could of course be acquired on the spec as it has to be SAME_THREAD anyway as mentioned above.

Modifying shared fields in setup is indeed a bit unlucky.
Unfortunately, the DynamicTestExecutor (which was also Marcs first idea) is only available in the execute method. :-/
But due to the resource lock problem it might not be the best idea anyway.

@Vampire
Vampire force-pushed the issue-1593 branch 2 times, most recently from 6528e9a to 4f1528f Compare March 13, 2023 03:08
@Vampire

Vampire commented Mar 13, 2023

Copy link
Copy Markdown
Member Author

Ok, a different approach, that hopefully works better and is a bit less dirty. :-)
It works fine in Gradle, Maven, and IntelliJ this time.

@Vampire
Vampire marked this pull request as ready for review March 13, 2023 03:09
@Vampire
Vampire requested a review from leonard84 March 13, 2023 03:09
return dependencies;
}

public void addDependency(FeatureInfo featureInfo) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One thing that is confusing, is that this naming suggest that the dependencies would be executed before this feature. However, that is not the case, the execution order is dependent on the executionOrder field and the ExecutionMode of the Spec/Feature.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's indeed correct.

I would indeed not impose any implicit ordering due to the dependency configuration.

This could easily conflict with explicitly defined order from another extension and actually the way it is right now it can also be used for other use-cases like "if this feature is executed, this other feature also always needs to be executed".

This could for example also be used for something like having a feature that does a cleanup for another feature. Then the cleanup feature would be the dependency but configured to run after the dependee.
Or for similar use-cases, whatever a user might come up with. :-D

The meaning is just that if the dependee is run, the dependency will also run, no matter whether before, after, or concurrently, and I think it would be best to keep it like that.

So what do you suggest?
Adding JavaDoc that explains that it does not impose order, just that it is run too?
Or a different name?
If the latter, any idea for good replacements for dependee and dependency?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This could for example also be used for something like having a feature that does a cleanup for another feature.

That sounds like a terrible idea 😅.

I think it needs a JavaDoc in any case, explaining what it does and doesn't do. As for a name, maybe addRequiredExecution or addImpliedExecution? Not sure if those are better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should also make sure that you can only depend on features in the same Spec hierarchy, it don't think cross spec relationships is something we should support.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why not, as long as it does not impose order?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think coupling between specs is a bad idea.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Tbh, meanings about what is a bad idea and what isn't greatly vary from person to person.
I'm not too much of a fan of forbidding something that would work perfectly fine without any reason besides personal opinion.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I renamed them to implied and implying features and added JavaDoc and @Beta

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And it would also prevent the need to traverse the whole test hierarchy for dependents, instead it would be contained to one SpecNode.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm, yeah, with the explicit info->node dependency this was not a problem at all.
Now with the node-searching it would be bad, especially when always searching the whole tree from root if there are many tests.
Even if I would just search siblings, then parent and so on this would not help much as at some point the features might already be absent from the tree and it would then still search the whole tree.

So I changed it now, restricting to inner-spec implications and just looking for siblings.

@Vampire
Vampire requested a review from leonard84 March 16, 2023 17:45
}

if (!getParent().equals(feature.getParent())) {
throw new IllegalArgumentException("Features can only imply features within the same specification");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you need to be a bit more flexible here, I'm pretty sure that features will have the Spec they are declared on as parent, with this it would only accept other features on the same hierarchy level. This is fine for @Stepwise as it doesn't support inheritance, but it might be overly limiting.
Could you add a test that checks that impliedFeature works from Parent -> Child and vice-versa.
This should only be an issue on the *Info and not the *Node level.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep, good point.
Changed.

@Vampire
Vampire requested a review from leonard84 March 19, 2023 23:00
@leonard84 leonard84 changed the title Run dependency features even if they were filtered out by post discovery filters (#1593) Add implied features... Mar 20, 2023
@leonard84
leonard84 merged commit 4c82f75 into spockframework:master Mar 20, 2023
@leonard84

Copy link
Copy Markdown
Member

Thanks @Vampire, good to see you back.

@Vampire
Vampire deleted the issue-1593 branch March 22, 2023 14:13
@leonard84 leonard84 added this to the 2.4-M2 milestone Feb 22, 2024
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.

@Stepwise does not automatically trigger previous features anymore with Spock 2.x as well as with Gradle 2.12+

2 participants