Skip to content

SGauvin/ctest-telescope.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

ctest-telescope

ctest-telescope is a simple plugin that integrates with CTest to allows you to fuzzy find your tests with telescope and debug them via nvim-dap.

Motivation

If you are looking for a simple solution to run C++ tests with nvim-dap through ctest without having to manually specify an executable path, arguments, and the working directory, this plugin might be for you.

Installation

Install ctest-telescope with your preferred package manager:

Lazy:

{
    "SGauvin/ctest-telescope.nvim",
    opts = {
        -- Your config
    }
}

You also must have ctest installed on your local machine. If ctest isn't available in your PATH, you can specify a path in the configuration.

Configuration

ctest-telescope comes with the following defaults:

{
  -- Path to the ctest executable
  ctest_path = "ctest",

  -- Extra arguments to pass to ctest
  -- For example, to add '-C Debug', you should set this variable to {"-C", "Debug"}
  extra_ctest_args = {},

  -- Folder where your compiled executables will be found
  build_folder = "build",

  -- Configuration you would pass to require("dap").configurations.cpp
  dap_config = {
    type = "cppdbg",
    request = "launch",
  },
}

You can set the dap_config field to specify parameters you want pass to dap. If you specify program, cwd, or args, they will be overridden (since the goal of this plugin is to select those automatically).

You can learn more about what fields are available for cppdbg here.

It should be possible to override cppdbg with codelldb or any other debug adapter that works with C++, but this hasn't been tested yet:

dap_config = {
  type = "codelldb",
},

Example configuration to enable pretty-printing and stopAtEntry

Note: If you are using lazy.nvim, you should pass these options to opts (example) instead of explicitly passing them to setup.

-- Note: if you are using Lazy.nvim, pass these
-- arguments to `opts` instead of manually calling `setup`
require("ctest-telescope").setup({
  dap_config = {
    stopAtEntry = true,
    setupCommands = {
      {
        text = "-enable-pretty-printing",
        description = "Enable pretty printing",
        ignoreFailures = false,
      },
    },
  },
})

Usage

ctest-telescope exports one useful function: pick_test_and_debug.

Here is an example on how you could integrate this in your own config:

vim.keymap.set("n", "<F5>", function()
  local dap = require("dap")
  if dap.session() == nil and (vim.bo.filetype == "cpp" or vim.bo.filetype == "c") then
    -- Only call this on C++ and C files
    require("ctest-telescope").pick_test_and_debug()
  else
    dap.continue()
  end
end, { desc = "Debug: Start/Continue" })

Troubleshooting

If this plugin isn't working for you, try running this in your terminal: ctest --test-dir <build_folder> --show-only=json-v1.

Make sure your CMakeLists.txt is set up properly with automatic tests registration. For gtest, gtest_discover_tests has to be used. For catch2, catch_discover_tests has to be used.

If this plugin still isn't working, feel free to make an issue or a pull request.

Alternatives

If you use only gtest, don't mind having to manually specify an executable, and don't mind a bit more configuration, you might want to try neotest-gtest, as it integrates with neotest.

If you're looking for a plugin that fully integrates CMake in Neovim, you might want to check out cmake-tools.nvim.

About

Fuzzy find and debug tests inside Neovim

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages