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 adapter for C assuming Meson build system and Criterion unit testing framework #1

Merged
merged 38 commits into from
Jun 26, 2024

Conversation

oyvindaakre
Copy link
Contributor

@oyvindaakre oyvindaakre commented Jun 20, 2024

Add an adapter for unit tests written in C using the Criterion unit test framework and assuming the meson build system.

Note: Build directory assumptions:

  • Defaults to build unless set by the user when configuring the adapter
  • build directory has been set up and configured by a previous call to meson setup build (the adapter will automatically build the project before each test).

Dependencies

  • meson installed on the system
  • ninja installed on the system

Supported features

  • Run test closest to the cursor
  • Run all tests in the file

@quolpr
Copy link
Owner

quolpr commented Jun 20, 2024

@oyvindaakre looks great! Ready for merge?

@quolpr
Copy link
Owner

quolpr commented Jun 20, 2024

@oyvindaakre also, could add please example to test dir, like I did for go - test/gotest. The implementation should be very simple, just text for like 1 + 1 == 2, or as you want

@oyvindaakre
Copy link
Contributor Author

oyvindaakre commented Jun 21, 2024

Hi @quolpr, thanks for checking in :) I'm still fixing bugs and adding functionality, so the PR is not ready just yet :) I'll change the status back to a draft to reflect that. I will also clean up the implementation and add documentation before trying to merge this :)

I have to say, kudos to you! This is the first plugin I work on and as a lua-noob I found this as a good starting point and I think it was easy to get going on my own adapter.

@oyvindaakre oyvindaakre marked this pull request as draft June 21, 2024 07:11
@oyvindaakre oyvindaakre force-pushed the add-meson-plugin branch 3 times, most recently from 0e3a69e to f559b31 Compare June 21, 2024 10:47
@quolpr
Copy link
Owner

quolpr commented Jun 21, 2024

Btw, I reload plugin while developing with this command, could be helpful for you:

vim.api.nvim_create_user_command("C", function()
  require("lazy.core.loader").reload("quicktest.nvim")
end)

So, what I usually do:

  1. Add sample project to test folder
  2. Edit adapter
  3. Run :C command
  4. Rerun test

@quolpr
Copy link
Owner

quolpr commented Jun 21, 2024

@oyvindaakre

I made some breaking changes in API. Changes are not so much, please check Readme 🙂

@oyvindaakre oyvindaakre force-pushed the add-meson-plugin branch 2 times, most recently from b5731e2 to fc8a1c9 Compare June 21, 2024 18:51
@oyvindaakre
Copy link
Contributor Author

@quolpr Should this PR update the doc too?

@quolpr
Copy link
Owner

quolpr commented Jun 22, 2024

@oyvindaakre the doc will be generated from Readme once PR is merged to main. So, please, mention your adapter in Readme 🙂

@oyvindaakre
Copy link
Contributor Author

@quolpr Finally, I think the adapter is ready. I have two questions though:

  1. The adapter needs to know the path to the build directory. Currently I hardcode build here: https://github.com/oyvindaakre/quicktest.nvim/blob/ea9396b6660116e220f575a5aa33743fe8a1a13a/lua/quicktest/adapters/criterion/init.lua#L12. Is it possible for users to override this and set a custom path if they want?

  2. I added a sample project to tests/support/criterion. I also added a readme in an attempt to explain how to set up the project (+ some notes on the adapter itself).
    I'm not sure how to run it with the makefile. When I run make test it only seems to run the plugin_name_spec.lua test. Is that expected?

$ make test
Starting...	
Scheduling: tests/plugin_name/plugin_name_spec.lua

========================================	
Testing: 	/home/oyvind/.local/share/nvim/lazy/quicktest.nvim/tests/plugin_name/plugin_name_spec.lua	
Success	||	setup works with default	
	
Success: 	1	
Failed : 	0	
Errors : 	0	
========================================	

@oyvindaakre oyvindaakre marked this pull request as ready for review June 23, 2024 20:36
@quolpr
Copy link
Owner

quolpr commented Jun 24, 2024

@oyvindaakre as for

The adapter needs to know the path to the build directory. Currently I hardcode build here: https://github.com/oyvindaakre/quicktest.nvim/blob/ea9396b6660116e220f575a5aa33743fe8a1a13a/lua/quicktest/adapters/criterion/init.lua#L12. Is it possible for users to override this and set a custom path if they want?

Yep, I think the best way will be to make your plugin to work like this:

qt.setup({
  adapters = {
    require("quicktest.adapters.golang")({
       exec_path = function(params)
       end
    }),
  }
})

You can do this with meta table, check example here
https://github.com/fredrikaverpil/neotest-golang/blob/main/lua/neotest-golang/init.lua#L179

I will be adding the same API to other adapters too soon. Also, notice that function is supplied for param, that's how user will be able to change behaviour per project. Based on what vim.fn.bufname(params.bufnr)(returns buffer path) contains, they will be able to dynamically configure exec_path.

Or you can support both functions and strings, like here https://github.com/olimorris/neotest-rspec?tab=readme-ov-file#setting-the-root-directory

Let me know what you think

I'm not sure how to run it with the makefile. When I run make test it only seems to run the plugin_name_spec.lua test. Is that expected?

Ha-ha, I didn't make tests for quicktet 😄 So only manual tests for now

lua/quicktest.lua Outdated Show resolved Hide resolved
lua/quicktest/adapters/criterion/init.lua Outdated Show resolved Hide resolved
lua/quicktest/adapters/criterion/init.lua Outdated Show resolved Hide resolved
@oyvindaakre
Copy link
Contributor Author

oyvindaakre commented Jun 25, 2024

Let me know what you think

It sounds neat to support both function and string, but I don't have enough Lua-experience under my belt to know the implications of supporting both :) Metatable is a new concept for me, so I have to read up on it, but I'm happy to take suggestions on how to implement it for this adapter.

@quolpr
Copy link
Owner

quolpr commented Jun 25, 2024

@oyvindaakre I made config options support for golang, could you do the same way? a3bb0b6

…data

Uses 'meson introspect --targets' to find the test executable that
contains the source file in the currently open buffer.

This fixes an issue with the existing implementation that assumed the
name of the test executable from the name of the buffer.

However this is not bullet proof. It is possible that the source file is
used in multiple test executables, for instance to run with different
parameters and flags.
Try to keep meson specific interaction in its own module for reusing
later
Use empty parameter table to signal that the test can't be run
Update API for capturing JSOn
Store result in M
Locate line number with error and update diagnostics UI
…d of through `meson test`

Should have done this all along. This adapter is all about criterion,
not meson.

We still use meson introspect to find the link between the source file
and the test exectuable. The primary difference is that we now call the
test executable directly instead of through `meson test -C build
test_exe`.

This allows for some simplifications:
- Depend only on how the test executable behaves

- Test output can be read directly instead of being captured in the
output stream of meson (lots of text + some json + lots of text)

- Simpler command line since test arguments don't have to be wrapped
with meson's --test-args=""

- Perhaps slighly easier to modify adapter later if we need to support a
different build system

delete test_parser
Let util parse the results and return the error messages.
Update diagnostics namespace
Update adapter path
Update adatper name
Replace the external json5 decoder with the built-in vim.json module.

During development I had been testing vim.fn.json_decode whichs turns
out to be a vimscript function that cannot be used in lua loop
callbacks. I did not know there was also a module called vim.json which
I'm using now...
The criterion and util modules return an empty string instead of nil
value when a test exe was not found or if a test was not found in the
buffer
Realized that M.can_run is not called by quicktest, so need to call
internally to make sure error handling still works
The comment was valid when running the test through `meson test` which
would automatically compile. Now we run the test executable directly and
need to do compilation ourselves (which we do anyway).
@oyvindaakre
Copy link
Contributor Author

@oyvindaakre I made config options support for golang, could you do the same way? a3bb0b6

Looks good! Copied your steps. Does it look okay?

@quolpr
Copy link
Owner

quolpr commented Jun 26, 2024

@oyvindaakre LGTM! Thank you for the contribution, merging it now :)

@quolpr quolpr merged commit c3aab39 into quolpr:main Jun 26, 2024
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.

None yet

2 participants