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

Running test:watch only on current method or current file #12

Open
dorkster100 opened this issue Apr 21, 2023 · 6 comments
Open

Running test:watch only on current method or current file #12

dorkster100 opened this issue Apr 21, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@dorkster100
Copy link
Contributor

dorkster100 commented Apr 21, 2023

Would be nice to add watch only for one method or one file as it may be too much to run whole test suite every time. Probably a simple treesitter parse.

I can implement this, it seems to be simple considering the watch runner that is in the project.

@adalessa
Copy link
Owner

It's an interesting Idea, But I don't know if should be in the core or in a plugin.
But how to achieve it can be with telescope list the test directory, get all the files relative to tests.
With that you can just use the selected filename (relative path) and with that call

require("laravel.artisan").run({ "test", "<file>" }, "watch")

this same can be use but selecting the current file name and calling the function.
Because at the same time is simple to implement in the configuration I think is better suited as an snippet to share in the repo.
Or to have in extras namespace and have it there and the user can just assign the keybinding.

@adalessa adalessa added the enhancement New feature or request label Apr 21, 2023
@dorkster100
Copy link
Contributor Author

dorkster100 commented Apr 22, 2023

I had something like this in mind, you could just have your cursor in vim in the test method and run that command

  ["test_file:watch"] = function()
    local currentFile = string.gsub(vim.api.nvim_buf_get_name(0), vim.loop.cwd() .. "/", "")
    return require("laravel.artisan").run({ "test", currentFile }, "watch")
  end,

  ["test_method:watch"] = function()
    local currentFile = string.gsub(vim.api.nvim_buf_get_name(0), vim.loop.cwd() .. "/", "")
    local node = vim.treesitter.get_node()
    local testMethod = function(node)
      if node:type() == "method_declaration" then
        return true
      end
      return false
    end

    if not testMethod(node) then
      while node do
        if not testMethod(node) then
          node = node:parent()
        else
          break
        end
      end
    end

    if node then
      local method_name = vim.inspect(vim.treesitter.get_node_text(node:field("name")[1], 0))
      if string.sub(method_name, 2, 5) == "test" then
        return require("laravel.artisan").run({ "test", currentFile, "--filter", method_name }, "watch")
      end
    end

    utils.notify("artisan.run", { msg = "cursor is not on the testable method", level = "ERROR" })
  end,

It supports only phpunit style tests though.

@dorkster100
Copy link
Contributor Author

dorkster100 commented May 2, 2023

@adalessa let me know if you find this useful, i can make the PR, otherwise we can just close the issue

@adalessa
Copy link
Owner

adalessa commented May 2, 2023

@dorkster100 yes I have started to work on this I will have a PR soon.

@adalessa
Copy link
Owner

@dorkster100 been thinking about this, I think trying to test the near test intead of write if from scratch we can integrate the https://github.com/vim-test/vim-test I currently use it, and it works really well.
I think a good solution is to configure the plugin to run with docker or sail base on the configuration of the plugin

@dorkster100
Copy link
Contributor Author

I will checkout the vim-test solution, do you think of integrating it or just adding some documentation of how to set it up?

In general the solution i posted in this issue is sufficient for most of my needs, the only missing thing is PEST variant, since I have not used it yet, i do not have enough information to implement it. What do you think is missing from the code I wrote?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants