Skip to content

test: provide test case for Jakarta quick fixes - #793

Closed
dessina-devasia wants to merge 15 commits into
redhat-developer:mainfrom
dessina-devasia:code-action-cancel-test-approach-2
Closed

dessina-devasia wants to merge 15 commits into
redhat-developer:mainfrom
dessina-devasia:code-action-cancel-test-approach-2

Conversation

@dessina-devasia

@dessina-devasia dessina-devasia commented Feb 3, 2025 •

Copy link
Copy Markdown
  • PR to test the Jakarta quick fix
  • Test corresponding to WebFilterQuickFix

@dessina-devasia dessina-devasia changed the title test: test: provide test case for Jakarta quick fixes Feb 3, 2025
@dessina-devasia
dessina-devasia marked this pull request as draft February 3, 2025 08:34
@angelozerr

Copy link
Copy Markdown
Contributor

@dessina-devasia thanks so much , your test looks very good!

Please tell me when you will think your PR will be ready (since the PR is in draft)

@dessina-devasia

Copy link
Copy Markdown
Author

sure @angelozerr. Testing is currently in progress, and I'm addressing encountered failures. I will update you once the PR is ready. Thank you!

@angelozerr

Copy link
Copy Markdown
Contributor

What is the status of this PR @dessina-devasia ?

@dessina-devasia

Copy link
Copy Markdown
Author

Hi @angelozerr,

I'm encountering an issue at this specific line (https://github.com/redhat-developer/lsp4ij/pull/793/files#diff-08cdc1aaff36adf7f735e4752b512541a3be8e725cfb3d572e6b1446854d19e8R85). I'm unable to retrieve a value from provider.getCodeActionAt(offset) since it expects certain predefined class variable values. I'm currently debugging this, and it seems to be the only remaining issue.

@angelozerr

Copy link
Copy Markdown
Contributor

Hi @angelozerr,

I'm encountering an issue at this specific line (https://github.com/redhat-developer/lsp4ij/pull/793/files#diff-08cdc1aaff36adf7f735e4752b512541a3be8e725cfb3d572e6b1446854d19e8R85). I'm unable to retrieve a value from provider.getCodeActionAt(offset) since it expects certain predefined class variable values. I'm currently debugging this, and it seems to be the only remaining issue.

You must never creating instance of provider, you need to get the real IJ QuickFixes. I will try to consume super.getAvailableIntentions()

@angelozerr

Copy link
Copy Markdown
Contributor

I will try calling CodeInsightTestFixtureImpl.getAvailableIntentions(editor, file); to get list of IntentAction and compare thme with the input CodeAction

var file = myFixture.getFile();
int offset = editor.getCaretModel().getOffset();

LSPLazyCodeActionProvider provider = new LSPIntentionCodeActionSupport(file);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't use LSP internal class, you need to test the real IJ Quickfix. I suggest that you call

CodeInsightTestFixtureImpl.getAvailableIntentions(editor, file);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

image

As you can see the corresponding value also returns nothing.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@angelozerr I've modified as you suggested, still debugging and looking into the potential issue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@angelozerr I've modified as you suggested, still debugging and looking into the potential issue

I think it is normal that yiu have empty intention actions, because your test is for quick fixes which are generated on validation step.

So you will need to emulate also a publishDiagnostics with the proper diagnostic. It will trigger the IJ annotator which will call your code actions.

After that I think (hope) the list will not empty.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated the code by setting diagnostics, still issue persists. I've tried another approach as well by using myFixture.doHighlighting(), that's also failing. You can find the code as commented

@dessina-devasia dessina-devasia Feb 13, 2025 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@angelozerr The reported issue is now resolved, and code actions are loading properly. I retrieved them using the myFixture.doHighlighting() API.

However, there is still an underlying issue - myFixture.launchAction() is not working. The input is correct, but the code action is not being applied, and the editor value is not changing. Is there any alternative for this myFixture.LaunchAction

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you check if the extra file that you add is updated?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Its also not updated

@@ -0,0 +1,11 @@
package io.openliberty.sample.jakarta.servlet;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why you need this file?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Earlier the corresponding line looks like this: 0ec0391#diff-831aa0e00f3317cf6a4848241cf4c7692456654d49e0eb867a621bb321b60844R52

If you want to revert the code, I can do accordingly

@angelozerr angelozerr Feb 13, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok I understand more why you did that, but your very nice assertCodeAction could be used to write another tests and if we use this strategy we will need to have 2 Java file contents (one with String in the test and one with a file). It would be very nice if your test could be done without adding this extra file.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sure @angelozerr . I'll change it accordingly

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Modifications done as discussed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@angelozerr

Copy link
Copy Markdown
Contributor

It doesn't apply your code action because edit is null:

image

I think your real usecase uses resolve code action, so you will need to set this resolve code action to the language server

@dessina-devasia

Copy link
Copy Markdown
Author

Thank you @angelozerr. Let me try that as well

@angelozerr

Copy link
Copy Markdown
Contributor

Thank you @angelozerr. Let me try that as well

Thanks. Your asset method must have a new parameter with resolved code actions

@dessina-devasia

Copy link
Copy Markdown
Author

@angelozerr Resolved all issues related to this PR. Thank you for your support!
Please have a look!

@dessina-devasia
dessina-devasia marked this pull request as ready for review February 14, 2025 20:16
@angelozerr

Copy link
Copy Markdown
Contributor

,hy you need to update LSP4IJ utils? You should not update it.

I dont see resolve codeaction uding in your test to update edit.

@dessina-devasia

dessina-devasia commented Feb 17, 2025 •

Copy link
Copy Markdown
Author

,hy you need to update LSP4IJ utils? You should not update it.

Once the code action is applied by calling myFixture.launchAction(), the new text is only updated in the document but not in the editor. To resolve this, I passed the editor value instead of null (previously, null was passed here) and ensured that the text from the document is properly set in the editor here.

The replacement with the new value happens exactly here. It updates only the document, but the text in the editor is not updated.

The only change that I've done in LSP4IJUtil is, passed the editor in applyWorkspaceEdit as here and set back the document text value to editor editor.getDocument().setText(document.getText());

@dessina-devasia

Copy link
Copy Markdown
Author

I dont see resolve codeaction uding in your test to update edit.

I've used myFixture.launchAction() to perform code action resolution. I couldn't find any APIs similar to resolveCodeAction. Could you please clarify which API you are referring to?

@angelozerr

Copy link
Copy Markdown
Contributor

First you must not update LSPIJUtils. Tests must never impact this utility class.

I couldn't find any APIs similar to resolveCodeAction.

MockTextDocumentService is a copy/paste from LSP4E and I think there are no test with resolve code action.

You will need to update MockTextDocumentService like this:

public void setResolveCodeAction(CodeAction resolvedCodeAction) {
        this.mockResolvedCodeAction = resolvedCodeAction;
    }
    
    @Override
    public CompletableFuture<CodeAction> resolveCodeAction(CodeAction unresolved) {
        return CompletableFuture.completedFuture(mockResolvedCodeAction);
    }

But let me some times to give you more explanation.

@angelozerr

Copy link
Copy Markdown
Contributor

Ok the first thing which is not good is that your assertApplyCodeAction redo configureByText, it should never done that.

The main idea is that your assertCodeActions initialize the editor with configureByText, but assertApplyCodeAction should not updated again the editor. It should just apply the resolved code action.

I suggest that you see completion test

public void assertApplyCompletionItem(int selectedItemIndex,

As you can see the apply completion doesn't reinitialize the editor, it uses the previous completion list which as loaded with assertCompletion. See the test

@angelozerr

Copy link
Copy Markdown
Contributor

When your code actions are loaded by assertCodeActions, your "edit" is null. It is because you don't load this edit to have fast performance.

When quick fix is applied, the LSP request codeAction/resolve is called to update this edit. You need to emulate that by calling your assertApplyCodeAction with the resolved code action (with the edit).

@angelozerr

Copy link
Copy Markdown
Contributor

When your code actions are loaded by assertCodeActions, your "edit" is null. It is because you don't load this edit to have fast performance.

When quick fix is applied, the LSP request codeAction/resolve is called to update this edit. You need to emulate that by calling your assertApplyCodeAction with the resolved code action (with the edit).

@dessina-devasia do you understand what I mean?

@dessina-devasia

Copy link
Copy Markdown
Author

Yes @angelozerr. I'm currently working on it. I'll let you know with the update soon.

@angelozerr

Copy link
Copy Markdown
Contributor

Ok great. It is not a trivial issue. The main important thing is to understand how lsp codeaction, resolve codeaction are working and your test highlight this mechanism.

@dessina-devasia

Copy link
Copy Markdown
Author

Sure @angelozerr. Understood. Thank you for the detailed explanation. Let me check into the resolveCodeAction working as well.

@dessina-devasia

Copy link
Copy Markdown
Author

Hi @angelozerr, I've updated the code as per the suggestions. Code is working fine now. Could you please review?

@angelozerr

Copy link
Copy Markdown
Contributor

I am sorry again @dessina-devasia but you dont test the QuickFix apply with IJ since you have recoded the apply.

You need to apply quick fix like you did before with launchActions.

As soon as I will finish my work with DAP support, I will try to help you more with sample code.

"message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027."
}
],
"edit": {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just one question, this JSON content is a copy/paste from the result of codeAction/result response?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@dessina-devasia

dessina-devasia commented Feb 18, 2025 •

Copy link
Copy Markdown
Author

I am sorry again @dessina-devasia but you dont test the QuickFix apply with IJ since you have recoded the apply.

You need to apply quick fix like you did before with launchActions.

As soon as I will finish my work with DAP support, I will try to help you more with sample code.

No Problem @angelozerr

Regarding launchActions, if we proceed with it, there seems to be an issue. The edited value is not properly updated in the editor document. While the document itself is replaced with new text, the document in the editor remains unchanged. As a result, the quick fix application does not reflect in the editor. Additionally, calling myFixture.getEditor().getDocument().getText() returns the old text.

This is why I previously made some modifications in LSP4IJUtils. You can find the commit history here.

Could you please let me know if there is any alternative approach for this scenario while applying launchActions()

@angelozerr

Copy link
Copy Markdown
Contributor

To be honnest with you, I need to debug IJ to understand why it doesnt update the editor.

Your tentatives will help me a lot. I need to find time to investigate your pr.

But for now I am super busy to finish dap support and review existing prs because I would like to create a release asap.

@dessina-devasia

Copy link
Copy Markdown
Author

okay @angelozerr

@angelozerr

Copy link
Copy Markdown
Contributor

@dessina-devasia as there are so many changes to do in your code, I decided to create a new PR #862

It is working on my local labtop but not in CI build. I don't know why?

If you have any questions, don't hesitate.

I close your PR and thanks so much to have investigated the write of this test. Your attempt was very helpfull for me.

Closed for #862

@angelozerr angelozerr closed this Feb 21, 2025
@dessina-devasia

Copy link
Copy Markdown
Author

Thank you @angelozerr for taking it forward! Your cooperation also helped me a lot in developing this test case. Let me know if you need any help.

@angelozerr

Copy link
Copy Markdown
Contributor

You are welcome!

As soon as I will find time I will try to investigate why there is a problem on ci build.

Any idea are welcome !

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.

2 participants