andy-bell101 / neotest-java

Java plugin for Neotest
MIT License
11 stars 4 forks source link

Issue with passed tests indicators #5

Closed antonpetrovmain closed 1 year ago

antonpetrovmain commented 1 year ago

There is some issue with the results interpreted by neotest from the neotest-java adapter. Whenever the test has a non-default package, e.g. com.example.demo.service.TransferServiceTest the results contain that fqcn (fully qualified class name) (which looks very logical), but the neotest adds new entries to the result (and fails them), which contain only the classname + method:

results from neotest-java:

{
  ["com.example.demo.service.TransferServiceTest.moneyTransferHappyFlow"] = {
    output = "/var/folders/3y/fyv5szf13b39s5jbsk_zkq980000gn/T/nvim.anton/ckWC41/0",
    status = "passed"
  }

results after neotest processes the above table:

{
  ["TransferServiceTest.moneyTransferHappyFlow"] = {
    errors = {},
    output = "/var/folders/3y/fyv5szf13b39s5jbsk_zkq980000gn/T/nvim.anton/ckWC41/0",
    status = "failed"
  },
  ["com.example.demo.service.TransferServiceTest.moneyTransferHappyFlow"] = {
    output = "/var/folders/3y/fyv5szf13b39s5jbsk_zkq980000gn/T/nvim.anton/ckWC41/0",
    status = "passed"
  }
}

It seems that neotest is looking at some paths and does not respect packages. The end result is that the test is marked as failed, even though it has passed.

As a workaround, I have modified neotest-java to use the simple class name + method name format. The only drawback I see from this is if I have the same TransferServiceTest test in 2 different packages, the test results will be wrong (something I can live with, since I rarely (if ever) need to have the absolute same test+methods in two different packages). e.g., if I copy com.example.demo.service.TransferServiceTest to com.example.demo.model.TransferServiceTest (and fail the test method in that new test class) the results will be wrong.

Here is the modification I have made locally with which things are working fine: in buf 8: "~/.local/share/nvim/lazy/neotest-java/lua/neotest-java/init.lua" 328 lines --82%--

        local simple_name = testcase._attr.classname:match("%.([^%.]+)$")
        if testcase.failure then
          results[simple_name .. "." .. method_name] = {
            status = "failed",
            short = testcase.failure[1],
          }
        else
          results[simple_name .. "." .. method_name] = {
            status = "passed",
          }
        end
andy-bell101 commented 1 year ago

I've found the cause of this. Treesitter parses the package name as an identifier when there's only one level of nesting e.g. mypackage but as a scoped_identifier when there's more than that e.g. mypackage.mysubpackage. I never tested with nested packages so I never found the problem. Just working on test cases and a fix now

andy-bell101 commented 1 year ago

Hopefully this should be addressed in the latest commit

antonpetrovmain commented 1 year ago

Works great, thank you so much for the quick fix!