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

Fix running tests in multi-project builds #100

Closed
JonaLoeffler opened this issue Apr 30, 2024 · 10 comments
Closed

Fix running tests in multi-project builds #100

JonaLoeffler opened this issue Apr 30, 2024 · 10 comments
Labels
bug Something isn't working

Comments

@JonaLoeffler
Copy link

With the latest plugin version, running tests in my multi-project gradle build fails.

The project consists of a number gradle sub-projects like com.company.app.web.server.<component> for about 10 components. Each of these then has src/test/java.

cat: build/neotest-java/classpath.txt: No such file or directory
error: --class-path requires an argument
Usage: javac <options> <source files>
use --help for a list of possible options

The statusline briefly shows src/test/java is not accessible by the current user!, then Error reading file: /tmp/neotest-java/<some kind of id>/TEST-junit-jupiter.xml

This is my Lazy config for neotest:

return {
  {
    'rcasia/neotest-java',
    ft = 'java',
    dependencies = {
      'nvim-neotest/neotest',
      dependencies = {
        'nvim-neotest/nvim-nio',
        'nvim-lua/plenary.nvim',
        'antoinemadec/FixCursorHold.nvim',
        'nvim-treesitter/nvim-treesitter',
      },
    },
    config = function()
      require('neotest').setup {
        adapters = {
          require 'neotest-java' {
            ignore_wrapper = false, -- whether to ignore maven/gradle wrapper
          },
        },
      }

      vim.keymap.set('n', '<leader>tp', function()
        require('neotest').run.run(vim.uv.cwd())
      end, { desc = '[T]est [P]project', noremap = true })

      vim.keymap.set('n', '<leader>tf', function()
        require('neotest').run.run(vim.fn.expand '%')
      end, { desc = '[T]est [F]ile', noremap = true })

      vim.keymap.set('n', '<leader>tr', function()
        require('neotest').run.run()
      end, { desc = '[T]est [R]un', noremap = true })

      vim.keymap.set('n', '<leader>ts', function()
        require('neotest').run.stop()
      end, { desc = '[T]est [S]top', noremap = true })

      vim.keymap.set('n', '<leader>ta', function()
        require('neotest').run.attach()
      end, { desc = '[T]est [A]ttach', noremap = true })

      vim.keymap.set('n', '<leader>to', function()
        require('neotest').output.open()
      end, { desc = '[T]est output [O]pen', noremap = true })
    end,
  },
}

build/neotest-java/classpath.txt does exist in the working directory, but is empty.

Version v.1.10.0 still works as before.

@rcasia rcasia added the bug Something isn't working label May 2, 2024
@rcasia rcasia changed the title Fix running tests in multi-project gradle builds Fix running tests in multi-project builds Jun 22, 2024
@steffsommer
Copy link

This might be the biggest blocker for people to use this plugin in real world settings

@steffsommer
Copy link

I created a related issue with potential solutions, which was moved to discussions. I am linking it here for reference: #149

rcasia added a commit that referenced this issue Sep 22, 2024
Relates to #100 

Key Changes:

* Module & Project Support:

* Introduced Module and Project classes to handle multi-module Java
projects.
* Each module can retrieve its own sources, test sources, and output
directories.
* Managed module dependencies for correct build order and classpath
resolution.
  * Compiler Enhancements:

* Compiler:
* Refactored compile_sources and compile_test_sources to work per
module.

* Gradle & Maven Build Tool Updates:

    * Refactored to handle per-module build and dependency resolution.
* Prepared classpaths for source and test modules, ensuring all
dependencies are included.
* Enhanced detection of source, test source, and generated files for
each module.

* JUnit Command Builder Updates:

* Updated build_junit to use module-specific paths for classpath
arguments and reports directories.

* Makefile Changes:

* Added Groovy parser support for nvim-treesitter in the install target.
@rcasia
Copy link
Owner

rcasia commented Sep 22, 2024

Sorry for the latency, I had no much time for it. I just merged the feature 9e6e8cf.

I'll close this issue but feel completely free to reopen! 🤙

@rcasia rcasia closed this as completed Sep 22, 2024
@LorenzoBettini
Copy link

I seem to understand that multi module projects are supported, but is it only the case for Gradle projects. With a few Maven multi module projects I tried it doesn't work.

@rcasia
Copy link
Owner

rcasia commented Oct 26, 2024

Hi @LorenzoBettini, do you have any examples?

@LorenzoBettini
Copy link

@rcasia
Copy link
Owner

rcasia commented Oct 27, 2024

this a minimal example https://github.com/LorenzoBettini/maven-multimodule-example

This project is using Junit4, which currently is not supported.

@LorenzoBettini
Copy link

Not supported in a multi project? Because in a single project it works: it detects all the tests in a single project using junit4.

@rcasia
Copy link
Owner

rcasia commented Oct 27, 2024

Not supported in a multi project? Because in a single project it works: it detects all the tests in a single project using junit4.

Sorry, you are right.

Junit4 is supported, but not junit4 with the format of the old junit3 which is being implemented here: https://github.com/LorenzoBettini/maven-multimodule-example/blob/master/my-app-parent/my-app-module1/src/test/java/org/my/app/module1/AppTest.

The main incompatibility between neotest-java and junit3 format is that the test methods don't have the Test annotation class, so they will not be detected to be run.

@LorenzoBettini
Copy link

@rcasia sorry, I had forgotten I had created those example projects with an old version of the Maven archetype that generates JUnit 3 tests.

I've update the example LorenzoBettini/maven-multimodule-example@508e6ac

Still I don't understand how it is meant to work.

If I start Neovim from the parent directory my-app-parent, I see that the summary detected the tests in the submodules:

image

However, If I try to run it from there selecting a testclass and press r, I get this error

Error executing vim.schedule lua callback: ...ava/lazy/neotest-java/lua/neotest-java/command/jdtls.lua:18: {
  code = -32001,
  data = {
    message = "my-app-parent does not exist"
  },
  message = "my-app-parent does not exist",
  <metatable> = {
    __tostring = <function 1>
  }
}
stack traceback:
	[C]: in function 'assert'
	...ava/lazy/neotest-java/lua/neotest-java/command/jdtls.lua:18: in function 'handler'
	.../neovim/0.10.0/share/nvim/runtime/lua/vim/lsp/client.lua:685: in function ''
	vim/_editor.lua: in function <vim/_editor.lua:0>

If I try to run all tests with :lua require("neotest").run.run(vim.uv.cwd()), I get

neotest-java: ....local/share/lazyvim-java/lazy/nvim-nio/lua/nio/init.lua:119: The coroutine failed with this message: 
...zy/neotest-java/lua/neotest-java/core/result_builder.lua:71: there was an error while running command
stack traceback:
	[C]: in function 'assert'
	...zy/neotest-java/lua/neotest-java/core/result_builder.lua:71: in function 'results'
	.../lazyvim-java/lazy/neotest/lua/neotest/client/runner.lua:132: in function '_run_spec'
	.../lazyvim-java/lazy/neotest/lua/neotest/client/runner.lua:89: in function <.../lazyvim-java/lazy/neotest/lua/neotest/client/runner.lua:88>
stack traceback:
	...re/lazyvim-java/lazy/neotest/lua/neotest/client/init.lua:89: in function <...re/lazyvim-java/lazy/neotest/lua/neotest/client/init.lua:88>
	[C]: in function 'error'
	....local/share/lazyvim-java/lazy/nvim-nio/lua/nio/init.lua:119: in function 'gather'
	.../lazyvim-java/lazy/neotest/lua/neotest/client/runner.lua:94: in function '_run_tree'
	.../lazyvim-java/lazy/neotest/lua/neotest/client/runner.lua:65: in function <.../lazyvim-java/lazy/neotest/lua/neotest/client/runner.lua:22>
	[C]: in function 'xpcall'
	...re/lazyvim-java/lazy/neotest/lua/neotest/client/init.lua:84: in function 'run_tree'
	.../lazyvim-java/lazy/neotest/lua/neotest/consumers/run.lua:85: in function 'func'
	...local/share/lazyvim-java/lazy/nvim-nio/lua/nio/tasks.lua:173: in function <...local/share/lazyvim-java/lazy/nvim-nio/lua/nio/tasks.lua:172>

It only works if I start Neovim from one of the submodules' directory, e.g., my-app-module1. Of course, I can only run the tests in that project.

Without quitting Neovim, I can use :lcd to switch to another directory, e.g., :lcd ../my-app-module2, and then run :lua require("neotest").run.run(vim.uv.cwd()) to run the tests in the other project (the new current working directory). But then, the summary is not usable anymore: if I try to re-run one of the tests with r it says no Jdtls client attached. And the summary shows it runs without terminating. Sometimes, in this case, it also shows:

neotest-java: ...lazy/neotest-java/lua/neotest-java/core/spec_builder.lua:62: module base_dir not found
stack traceback:
	...re/lazyvim-java/lazy/neotest/lua/neotest/client/init.lua:89: in function <...re/lazyvim-java/lazy/neotest/lua/neotest/client/init.lua:88>
	[C]: in function 'assert'
	...lazy/neotest-java/lua/neotest-java/core/spec_builder.lua:62: in function 'build_spec'
	.../lazyvim-java/lazy/neotest/lua/neotest/client/runner.lua:76: in function '_run_tree'
	.../lazyvim-java/lazy/neotest/lua/neotest/client/runner.lua:65: in function <.../lazyvim-java/lazy/neotest/lua/neotest/client/runner.lua:22>
	[C]: in function 'xpcall'
	...re/lazyvim-java/lazy/neotest/lua/neotest/client/init.lua:84: in function 'run_tree'
	.../lazyvim-java/lazy/neotest/lua/neotest/consumers/run.lua:85: in function 'func'
	...local/share/lazyvim-java/lazy/nvim-nio/lua/nio/tasks.lua:173: in function <...local/share/lazyvim-java/lazy/nvim-nio/lua/nio/tasks.lua:172>

Maybe I don't understand how it is supposed to work...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants