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

Add filter for assert statement #613

Closed
wants to merge 2 commits into from
Closed

Conversation

bjkail
Copy link
Contributor

@bjkail bjkail commented Oct 15, 2017

Add a filter for assert statements (JAVAC.ASSERT):

  • Ignore the initialization of the $assertionsDisabled static field in the <clinit> method.
  • Ignore the assert by looking for the GETSTATIC+IFNE of $assertionsDisabled.

@bjkail
Copy link
Contributor Author

bjkail commented Oct 15, 2017

On second thought, rather than removing all assert-related code, perhaps we should only remove the "assertions disabled" and "assertions failed" code. That is, assume -ea and that assertions pass. Thoughts?

@marchof
Copy link
Member

marchof commented Oct 16, 2017

@bjkail First of all thanks a lot for your contribution and helping us with the filters!

In the first round for the built-in filters we're focusing on synthetic compiler artifacts which have no direct correspondence to the source code. For Java assertions I would see the following artifacts as synthetic:

  • Initialization of $assertionsDisabled
  • Check for $assertionsDisabled

The assertion code itself is present in source code and should therefore be treated like regular code to be executed and tested. From my point of view also for both cases (pass and fail).

@bjkail
Copy link
Contributor Author

bjkail commented Oct 16, 2017

Ok, I'll see if I can rework along those lines, thanks.

It's a little unfortunate to leave the failed assertion code in place, since assert is almost always used to test internal invariants, which might be difficult/impossible/undesirable to force negative. For the "second round", do you intend to add options to allow users to ignore user-written code?

@marchof
Copy link
Member

marchof commented Oct 16, 2017

@bjkail I agree. In general Java assertions are questionable and probably not widely used in practice. As far as I remember we received only a single question about it.

In our filter candidates there is a section "Test Execution is Questionable or Impossible by Design".

@bjkail
Copy link
Contributor Author

bjkail commented Oct 16, 2017

@marchof I guess you can consider this to be a second question about them :). I see that we implemented "Private empty constructors", so would it be ok for this PR to also ignore the 'throw new AssertionError' + "message" portion of assert? I understand if you'd rather not by default, but it would be nice to have some kind of plan to address that in the future.

@bjkail bjkail force-pushed the filter-assert branch 2 times, most recently from f6e483c to 2c80983 Compare October 18, 2017 01:58
@bjkail
Copy link
Contributor Author

bjkail commented Oct 18, 2017

Rebased to master, and updated to only filter compiler-generated code not present in the source code (initialization and load+jump for $assertionsDisabled).

@marchof
Copy link
Member

marchof commented Oct 19, 2017

@bjkail Regarding ignore AssertionError path: As I personally don't like the usage of Java assertions at all I'm ok with ignoring the throws AssertionError(...) path. So we will only see whether the assertion was executed (only the case if run with -ea) and there will be no branches visible.

@bjkail
Copy link
Contributor Author

bjkail commented Oct 19, 2017

@marchof Thanks for the feedback. I found it's non-trivial to accurately find the boundary between the assert expression (especially those containing ?:) and the throw statement, so I removed it from this PR. My first attempt was to walk backward from the assertion branch target label and to look for the first NEW AssertionError, but that approach fails for assert x : new AssertionError() and other complicated expressions involving ?: and AssertionError in the message. Perhaps those edge cases aren't worth worrying about, or perhaps we could walk the instructions following branches until the stack size drops to 0. Perhaps I'll get motivated to attempt something in another PR :-).

@marchof
Copy link
Member

marchof commented Oct 19, 2017

@bjkail ok! Let's do baby steps. This his how we try to come to a first 0.8.0 release.

@bjkail
Copy link
Contributor Author

bjkail commented Nov 1, 2017

@Godin Did my latest commit address your concerns?

@MarkSinke
Copy link

Would be helpful if this got merged - we are probably one of the few teams actually liking asserts, and we sprinkle them through our code where preconditions should be met. Our coverage numbers are now artificially low because of that, and it's harder to keep a baseline.

@marchof
Copy link
Member

marchof commented Sep 24, 2018

@MarkSinke Have you actually tested this PR with your code base? Does it provide the expected results for you?

@MarkSinke
Copy link

MarkSinke commented Sep 24, 2018 via email

@marchof
Copy link
Member

marchof commented Sep 24, 2018

@MarkSinke I just rebased this PR on master. In case you want to it with Ant or the CLI you can download a build of this PR here:

https://ci.appveyor.com/project/JaCoCo/jacoco/build/1.0.2195/artifacts

@Godin Godin added this to Implementation in Current work items via automation Oct 21, 2018
@Godin Godin moved this from Implementation to Review in Current work items Oct 21, 2018
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
Copy link
Member

Choose a reason for hiding this comment

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

@bjkail I started working on getting this done - wanted to mention your contribution in our changelog and realized that you didn't wrote name in headers. So wondering if this was on purpose or just was forgotten? is there any IP/legal issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't recall, I probably copied from somewhere, cleared it out, and then just didn't bother to fill in my name :-). There are no IP/legal issues.

@Godin Godin self-assigned this Dec 29, 2018
@marcust
Copy link

marcust commented May 8, 2019

@Godin What's the state of this? Any chance of getting this merged and released soon? (My use-case being ignoring Lombok generated null check assertions that are possible since 1.18.8)

@lblasa
Copy link

lblasa commented Aug 2, 2019

@Godin any update on this? Is this going to be merged and released soon?

@Godin Godin moved this from Review to Candidates in Current work items Aug 30, 2019
@kno10
Copy link

kno10 commented Feb 3, 2020

I would appreciate to see this in the next jacoco release.

@mirabilos
Copy link

mirabilos commented Jun 25, 2020

I’ve just stumbled on this and would also like to see something fixing it merged.

I managed to test 3 out of 4 branches (by specifically writing tests that trigger the assertions) and am fine with requiring that at first, but the fourth is virtually impossible (unload the class, then reload it with assertions disabled, with a custom classloader, ugh… no, thanks).

So, could we please exclude the “untestable” branch (Edit: and the static initialiser, see #1063) first, get that into a release I can use with Maven, and perhaps consider ignoring assertions later/elsewhere?

Thanks in advance!

@mirabilos
Copy link

What’s holding this up?

@TomerFi
Copy link

TomerFi commented Feb 15, 2021

This pr getting merged will be much appreciated.
:-)

@Godin
Copy link
Member

Godin commented Jun 13, 2021

#1196 was merged instead

@Godin Godin closed this Jun 13, 2021
Current work items automation moved this from Candidates to Done Jun 13, 2021
@Godin Godin moved this from In Progress to Done in Filtering Jun 13, 2021
@TomerFi
Copy link

TomerFi commented Aug 5, 2021

@Godin do you perhaps know of a timeline for a possible release containing the modifications in #1196 ?

@mirabilos
Copy link

This bug is unfortunately still open with jacoco-maven-plugin 0.8.8 (I still get the partial coverage at the beginning of a class using assert and on the assert lines).

mirabilos added a commit to qvest-digital/maven-parent that referenced this pull request Apr 19, 2022
jacoco/jacoco#1063 (which
  jacoco/jacoco#613 and/or
  jacoco/jacoco#1196 were supposed to fix)
  is still open, so the code coverage is still off
• projectlombok/lombok.patcher#10
  latest lombok source not published
• maven-release-plugin 3.0.0 still not available, need to bump the
  date manually or force oneself to not care about reproducible builds
• antrun plugin, which we don’t use, uses insecure versions of ant
  by default; unsure if continuously bumping it will be worth the
  hassle? does anyone use any even?
• maven-release-plugin 2.5.3 also uses insecure jdom (3.x doesn’t
  but is still in early beta)
@marchof
Copy link
Member

marchof commented Apr 20, 2022

@mirabilos What exactly is "still open" for you? Note that in #1196 we added a filter to hide the internal initialization code for asserts. But still each assert gives two branches: Good case and failure -- which is how assert works.

@mirabilos
Copy link

mirabilos commented Apr 20, 2022 via email

@marchof
Copy link
Member

marchof commented Apr 20, 2022

@mirabilos This filter is implemented since our latest release 0.8.8. Here is the test case:

https://github.com/jacoco/jacoco/blob/master/org.jacoco.core.test.validation.java5/src/org/jacoco/core/test/validation/java5/targets/AssertTarget.java

Please open a new bug with an executable reproducer which demonstrates the problem - preferably a Git repository with simple Maven project. Thx!

@Godin
Copy link
Member

Godin commented Apr 20, 2022

Furthermore in addition to what @marchof said there is example in #1196 demonstrating behavior before and after.

@mirabilos
Copy link

Testcase is still https://github.com/tarent/rfc822/ (git master this time).

Screenshot_20220420_204054
Screenshot_20220420_204035

@marchof
Copy link
Member

marchof commented Apr 20, 2022

@mirabilos We cannot provide support for Sonarqube. If you think the problem is with JaCoCo please provide a executable build which includes a JaCoCo report goal and shows the problem.

Otherwise please report the problem to Sponarqube.

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

Successfully merging this pull request may close these issues.

None yet

9 participants