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

Set experimental fields dynamically #98

Open
drewbailey opened this issue Jan 8, 2025 · 0 comments
Open

Set experimental fields dynamically #98

drewbailey opened this issue Jan 8, 2025 · 0 comments

Comments

@drewbailey
Copy link

Is it possible to set experimental fields dynamically? I would like to make a keymap to run with test_table = true, but keep it set to false by default.

Unfortunately I've been unable to configrue neotest/neotest-go to work properly with this setup.

return {
  "nvim-neotest/neotest",
  dependencies = {
    "nvim-neotest/nvim-nio",
    "nvim-neotest/neotest-go",
    "nvim-lua/plenary.nvim",
    "antoinemadec/FixCursorHold.nvim",
    "nvim-treesitter/nvim-treesitter",
  },
  opts = {},
  config = function(_, opts)
    local neotest = require("neotest")
    local neotest_go = require("neotest-go")

    -- Default adapter configuration
    local adapter = neotest_go({
      experimental = {
        test_table = false, -- Default behavior
      },
      args = { "-count=1", "-timeout=30s" },
    })

    opts.adapters = { adapter }
    neotest.setup(opts)

    -- Function to run with `test_table` enabled
    local function run_with_test_table()
      -- Print debug messages
      print("Running with `test_table` enabled")

      -- Update the adapter dynamically
      local temp_adapter = neotest_go({
        experimental = {
          test_table = true,
        },
        args = { "-count=1", "-timeout=30s" },
      })

      -- Reconfigure Neotest with the temporary adapter
      neotest.setup({
        adapters = { temp_adapter },
      })

      -- Run tests
      neotest.run.run(vim.fn.expand("%")) -- Run tests in the current file

      -- Restore the default adapter
      neotest.setup({
        adapters = { adapter },
      })
    end

    -- Keymaps
    vim.keymap.set("n", "<leader>tf", function()
      require("neotest").run.run(vim.fn.expand("%"))
    end, { desc = "[t]est [f]ile" })

    vim.keymap.set("n", "<leader>T", function()
      require("neotest").run.run()
    end, { desc = "[t]est current function" })

    vim.keymap.set("n", "<leader>tS", function()
      require("neotest").run.stop()
    end, { desc = "[S]top" })

    vim.keymap.set("n", "<leader>tw", function()
      require("neotest").watch.toggle(vim.fn.expand("%"))
    end, { desc = "[t]oggle [w]atch" })

    vim.keymap.set("n", "<leader>tts", function()
      require("neotest").summary.toggle()
    end, { desc = "[t]oggle [t]est [s]ummary" })

    vim.keymap.set("n", "<leader>tto", function()
      require("neotest").output.open({ enter = true, auto_close = true })
    end, { desc = "[t]oggle [t]est [o]utput" })

    vim.keymap.set("n", "<leader>ttO", function()
      require("neotest").output_panel.toggle()
    end, { desc = "[t]oggle [t]est [O]utput panel" })

    -- Custom keymap for running with `test_table`
    vim.keymap.set("n", "<leader>ts", function()
      run_with_test_table()
    end, { desc = "Run [t]ests with [s]ingle table test" })
  end,
}
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

No branches or pull requests

1 participant