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

Introduce RefasterRuleTestExtractor for documentation generation #1317

Merged
merged 5 commits into from
Sep 14, 2024

Conversation

Stephan202
Copy link
Member

@Stephan202 Stephan202 commented Aug 31, 2024

Suggested commit message:

Introduce `RefasterRuleTestExtractor` for documentation generation (#1317)

This new `Extractor` implementation collects Refaster example input and 
output code from rule collection tests.

This change also introduces explicit compilation steps for the test
code. As a side-effect this produces faster feedback in case of invalid
input or output code.

After this PR, approximately one more PR remains to port the remainder of the logic on the website branch to master. After that we can configure the build such that any change on master is nearly immediately reflected on https://error-prone.picnic.tech.

(The build will fail because of this issue; will try to address that separately.)

@Stephan202 Stephan202 added the documentation A documentation update label Aug 31, 2024
@Stephan202 Stephan202 added this to the 0.19.0 milestone Aug 31, 2024
@Stephan202 Stephan202 force-pushed the sschroevers/refaster-test-code-doc-generation branch from a999cc6 to c8c5107 Compare August 31, 2024 16:33
Copy link

Copy link

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 35
class surviving killed
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor 2 25
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCase 1 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCollector 0 5
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCases 0 1
🎉tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCases 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCase 0 1

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
Copy link

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 35
class surviving killed
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor 2 25
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCase 1 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCollector 0 5
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCases 0 1
🎉tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCases 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCase 0 1

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member Author

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Added some context. I also ported these changes to the website branch, where one can see how this all ties together.

@@ -40,7 +40,7 @@
@Immutable
@AutoService(Extractor.class)
@SuppressWarnings("rawtypes" /* See https://github.com/google/auto/issues/870. */)
public final class BugPatternTestExtractor implements Extractor<TestCases> {
public final class BugPatternTestExtractor implements Extractor<BugPatternTestCases> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Renaming for disambiguation.

private static String getFormattedSource(MethodTree method, VisitorState state) {
String source = SourceCode.treeToString(method, state);
int finalNewline = source.lastIndexOf(LINE_SEPARATOR);
if (finalNewline < 0) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Pitest flags that this case isn't covered. There is actually an empty method test, but I suppose that source code does have a trailing newline (didn't check).

Copy link
Member

Choose a reason for hiding this comment

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

Usually with checks like this -1 is returned if it is not found, it might be slightly more inuitive is we say == -1 instead of < 0, although it's the same, WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I generally prefer < 0, because any negative value isn't a valid index. We do this in a bunch of other places too:

$ git grep -i 'index.*< 0' | wc -l
8

Copy link
Member Author

Choose a reason for hiding this comment

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

Rebased and reverted this change as discussed offline.

}

int indentation = source.substring(finalNewline).lastIndexOf(' ');
String prefixToStrip = " ".repeat(indentation);
Copy link
Member Author

Choose a reason for hiding this comment

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

Pitest flags that the .repeat(indentation) logic isn't covered. This is because the test code is properly formatted; not an issue I suppose. (I did manually check that unformatted code doesn't break the extractor.)

@JsonDeserialize(as = AutoValue_RefasterRuleCollectionTestExtractor_RefasterTestCase.class)
abstract static class RefasterTestCase {
static RefasterTestCase create(String name, String content) {
return new AutoValue_RefasterRuleCollectionTestExtractor_RefasterTestCase(name, content);
Copy link
Member Author

Choose a reason for hiding this comment

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

Pitest flags that these parameters can be swapped without failing a test. That's because we only validate that the type can be (de)serialized, without asserting the exact serialization format. That's a conscious choice. The final PR will improve coverage in this respect.

@@ -26,6 +26,7 @@ ImmutableMultiset<ImmutableMultiset<Integer>> testEmptyImmutableMultiset() {
Stream.<Integer>empty().collect(toImmutableMultiset()));
}

@SuppressWarnings("unchecked")
Copy link
Member Author

Choose a reason for hiding this comment

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

Here and below: these annotations are now required, as the files are now compiled.

@rickie rickie force-pushed the sschroevers/refaster-test-code-doc-generation branch from c8c5107 to 2ee006d Compare September 3, 2024 13:23
Copy link

github-actions bot commented Sep 3, 2024

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 35
class surviving killed
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor 2 25
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCase 1 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCollector 0 5
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCases 0 1
🎉tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCases 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCase 0 1

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202 Stephan202 force-pushed the sschroevers/refaster-test-code-doc-generation branch from 2ee006d to 3102a99 Compare September 3, 2024 20:41
@Stephan202
Copy link
Member Author

Rebased and added a commit to resolve a SonarCloud violation. Filed WIP PR #1320 related to this.

Copy link

github-actions bot commented Sep 3, 2024

  • Surviving mutants in this change: 4
  • Killed mutants in this change: 34
class surviving killed
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor 3 24
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCase 1 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCollector 0 5
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCases 0 1
🎉tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCases 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCase 0 1

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

Only tiny suggestions.

Code looks ✨ !

private static String getFormattedSource(MethodTree method, VisitorState state) {
String source = SourceCode.treeToString(method, state);
int finalNewline = source.lastIndexOf(LINE_SEPARATOR);
if (finalNewline < 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Usually with checks like this -1 is returned if it is not found, it might be slightly more inuitive is we say == -1 instead of < 0, although it's the same, WDYT?

String className = tree.getSimpleName().toString();

// XXX: Instead of throwing an error here, it'd be nicer to have a bug checker validate key
// aspects of `RefasterRuleCollectionRefasterTestCase` subtypes.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// aspects of `RefasterRuleCollectionRefasterTestCase` subtypes.
// aspects of `RefasterRuleCollectionTestCase` subtypes.

Small typo?

Copy link
Member Author

Choose a reason for hiding this comment

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

I renamed things a few times, and used "global search and replace". Likely a result of that 😅

Copy link

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 35
class surviving killed
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor 2 25
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCase 1 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCollector 0 5
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCases 0 1
🎉tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCases 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCase 0 1

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202
Copy link
Member Author

Tnx for the review @rickie! 🚀

@mohamedsamehsalah mohamedsamehsalah force-pushed the sschroevers/refaster-test-code-doc-generation branch from b09f3da to 661f1a7 Compare September 11, 2024 14:08
Copy link

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 35
class surviving killed
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor 2 25
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCase 1 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCollector 0 5
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCases 0 1
🎉tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCases 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCase 0 1

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Contributor

@mohamedsamehsalah mohamedsamehsalah left a comment

Choose a reason for hiding this comment

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

🧠 🧠 🧠

private static String getRuleCollectionName(ClassTree tree) {
String className = tree.getSimpleName().toString();

// XXX: Instead of throwing an error here, it'd be nicer to have a bug checker validate key
Copy link
Contributor

Choose a reason for hiding this comment

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

Unless I am missing something, you can also argue that throwing an error provides faster feedback when developing / running tests locally.

Copy link
Member Author

Choose a reason for hiding this comment

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

The bug checker would also run at compile time, but would provide a (much) better error message.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense 👍

Stephan202 and others added 5 commits September 14, 2024 11:11
This new `Extractor` implementation collects Refaster example input and
output code from rule collection tests.

This change also introduces explicit compilation steps for the test
code. As a side-effect this produces faster feedback in case of invalid
input or output code.
@Stephan202 Stephan202 force-pushed the sschroevers/refaster-test-code-doc-generation branch from 661f1a7 to 7a5ac15 Compare September 14, 2024 09:12
Copy link

  • Surviving mutants in this change: 4
  • Killed mutants in this change: 34
class surviving killed
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor 3 24
🧟tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCase 1 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCollector 0 5
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCases 0 1
🎉tech.picnic.errorprone.documentation.RefasterRuleCollectionTestExtractor$RefasterTestCases 0 1
🎉tech.picnic.errorprone.documentation.BugPatternTestExtractor$BugPatternTestCase 0 1

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link

@Stephan202 Stephan202 merged commit b3ca01a into master Sep 14, 2024
16 checks passed
@Stephan202 Stephan202 deleted the sschroevers/refaster-test-code-doc-generation branch September 14, 2024 10:02
@Stephan202
Copy link
Member Author

Synced the website branch. Not too much logic now remains:

$ git diff origin/master --stat | tail -n 1
 19 files changed, 835 insertions(+), 77 deletions(-)

Largest remaining effort will be to write proper tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation A documentation update
Development

Successfully merging this pull request may close these issues.

3 participants