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

Tests don't work #329

Open
jalvesaq opened this issue Feb 13, 2025 · 25 comments
Open

Tests don't work #329

jalvesaq opened this issue Feb 13, 2025 · 25 comments

Comments

@jalvesaq
Copy link
Member

@she3o has written some tests about a year ago. However, if I do the command make test in R.nvim directory, I get:

Adding plenary.nvim: https://github.com/nvim-lua/plenary.nvim -> tests/xdg/local/share/nvim/site/pack/testing/start/plenary.nvim
fatal: 'tests/xdg/local/share/nvim/site/pack/testing/start/plenary.nvim' already exists in the index
Adding nvim-treesitter: https://github.com/nvim-treesitter/nvim-treesitter -> tests/xdg/local/share/nvim/site/pack/testing/start/nvim-treesitter
fatal: 'tests/xdg/local/share/nvim/site/pack/testing/start/nvim-treesitter' already exists in the index
Testing dependencies are now available.
●◼●◼◼◌◌◌◌◌◌◌◌◌●●●●
6 successes / 3 failures / 0 errors / 9 pending : 0.218343 seconds

Pending → ./tests/r/rmd_spec.lua @ 64
rmd module get_lang() Unit Tests returns true if inside code chunk (filetype-agnostic)
./tests/r/rmd_spec.lua:64: test filetype-agnostic code chunk detection

Pending → ./tests/r/rmd_spec.lua @ 68
rmd module get_lang() Unit Tests returns false if outside code chunk (filetype-agnostic)
./tests/r/rmd_spec.lua:68: test filetype-agnostic code chunk detection

Pending → ./tests/r/rmd_spec.lua @ 72
rmd module get_lang() Unit Tests returns false if it cannot match ```
./tests/r/rmd_spec.lua:72: test behaviour if chunk start/end not found

Pending → ./tests/r/rmd_spec.lua @ 78
rmd module write_chunk() Unit Tests Inserts an R code chunk in an empty line for RMarkdown
./tests/r/rmd_spec.lua:78: Not implemented

Pending → ./tests/r/rmd_spec.lua @ 82
rmd module write_chunk() Unit Tests Inserts an R code chunk in an empty line for Quarto
./tests/r/rmd_spec.lua:82: Not implemented

Pending → ./tests/r/rmd_spec.lua @ 86
rmd module write_chunk() Unit Tests Does not insert an R code chunk in a non-empty line in RMarkdown
./tests/r/rmd_spec.lua:86: Not implemented

Pending → ./tests/r/rmd_spec.lua @ 90
rmd module write_chunk() Unit Tests Does not insert an R code chunk in a non-empty line in Quarto
./tests/r/rmd_spec.lua:90: Not implemented

Pending → ./tests/r/rmd_spec.lua @ 94
rmd module write_chunk() Unit Tests Does not insert an R code chunk in a code chunk line in RMarkdown
./tests/r/rmd_spec.lua:94: Not implemented

Pending → ./tests/r/rmd_spec.lua @ 98
rmd module write_chunk() Unit Tests Does not insert an R code chunk in a code chunk in Quarto
./tests/r/rmd_spec.lua:98: Not implemented

Failure → ./tests/r/rmd_spec.lua @ 38
rmd module get_lang() Unit Tests returns the correct language at cursor position
./tests/r/rmd_spec.lua:42: Expected objects to be the same.
Passed in:
(boolean) false
Expected:
(boolean) true

Failure → ./tests/r/rmd_spec.lua @ 50
rmd module get_lang() Unit Tests returns the correct language at cursor position
./tests/r/rmd_spec.lua:54: Expected objects to be the same.
Passed in:
(boolean) false
Expected:
(boolean) true

Failure → ./tests/r/rmd_spec.lua @ 56
rmd module get_lang() Unit Tests returns the correct language at cursor position
./tests/r/rmd_spec.lua:60: Expected objects to be the same.
Passed in:
(boolean) false
Expected:
(boolean) true
E5113: Error while calling lua chunk: [NULL]
Error creating directory: Vim:E739: Cannot create directory /create_me: permission denied
Tests failed. Please review the errors above to diagnose issues.

Problems that I see:

  • There is an initial error message about plenary.nvim and nvim-treesitter already being installed.
  • The output doesn't inform what are the 6 successful tests.
  • The get_lang() tests that I tried to add in July 2024 don't work.
@PMassicotte
Copy link
Collaborator

For me, make test is not even working:

❯ make test

/bin/sh: 1: busted: not found

Tests failed. Please review the errors above to diagnose issues.

That would be very nice to have that working and even run on the CI.

@PMassicotte
Copy link
Collaborator

For the first 5 test, I am getting this error:

Diagnostics:
1. Cursor position outside buffer
   
   stack traceback:
   	/media/LaCie16TB/work/dev/nvim/R.nvim/tests/r/rmd_spec.lua:57: in function </media/LaCie16TB/work/dev/nvim/R.nvim/tests/r/rmd_spec.lua:56>
   
2. Cursor position outside buffer
   
   stack traceback:
   	/media/LaCie16TB/work/dev/nvim/R.nvim/tests/r/rmd_spec.lua:57: in function </media/LaCie16TB/work/dev/nvim/R.nvim/tests/r/rmd_spec.lua:56>
   
3. Cursor position outside buffer
   
   stack traceback:
   	/media/LaCie16TB/work/dev/nvim/R.nvim/tests/r/rmd_spec.lua:57: in function </media/LaCie16TB/work/dev/nvim/R.nvim/tests/r/rmd_spec.lua:56>
   
4. Cursor position outside buffer
   
   stack traceback:
   	/media/LaCie16TB/work/dev/nvim/R.nvim/tests/r/rmd_spec.lua:57: in function </media/LaCie16TB/work/dev/nvim/R.nvim/tests/r/rmd_spec.lua:56>
   
5. Cursor position outside buffer
   
   stack traceback:
   	/media/LaCie16TB/work/dev/nvim/R.nvim/tests/r/rmd_spec.lua:57: in function </media/LaCie16TB/work/dev/nvim/R.nvim/tests/r/rmd_spec.lua:56>

Looks like this is something with cursor placement. I suspect it is related to nvim_win_set_cursor.

@jalvesaq
Copy link
Member Author

I don't plan to fix the tests. I may try to add some more health checks on the weekend, though. I'll see if we can get the result of get_lang() from :checkhealth...

@PMassicotte
Copy link
Collaborator

I have found that local lang = send_lua_via_rpc([[return require('r.utils').get_lang()]]) always return "markdown" despite changing the cursor position. This is why tests are failing.

@PMassicotte
Copy link
Collaborator

I have modifed one test to see what is going on:

 it("returns the correct language at cursor position", function()
            -- Set the cursor position
            rpcrequest(nvim, "nvim_win_set_cursor", 0, { 13, 0 })

            -- Verify the cursor position
            local cursor_pos = rpcrequest(nvim, "nvim_win_get_cursor", 0)
            print("Cursor position:", cursor_pos[1], cursor_pos[2])

            local filetype = rpcrequest(nvim, "nvim_buf_get_option", 0, "filetype")
            print("Filetype:", filetype)

            -- Get the content at the cursor position
            local line_content = rpcrequest(nvim, "nvim_get_current_line")
            print("Content at cursor position:", line_content)

            -- Get the language at the cursor position
            local lang = send_lua_via_rpc([[return require('r.utils').get_lang()]])
            print("Language at cursor position:", lang)

            -- Check if the language is "r"
            local result = lang == "r"
            print("Language at cursor position:", result and "r" or "not r\n")

            -- Assert the result
            assert.is_true(result)
        end)

I am getting this output

●Cursor position: 13 0
Filetype: rmd
Content at cursor position: library(reticulate)
Language at cursor position: markdown
Language at cursor position: not r

@jalvesaq
Copy link
Member Author

Maybe it will be easier to make it work within :checkhealth r because, then, it will be running within a real nvim instance.

@PMassicotte
Copy link
Collaborator

How this could be used to test R.nvim functionalities?

@jalvesaq
Copy link
Member Author

I don't know yet. But I will have time to try to find a way on the weekend.

@jalvesaq
Copy link
Member Author

I couldn't make it work. I put this at the end of our health.check():

    local lines = {
        "---",
        "title: Test",
        "author: R.nvim",
        "---",
        "",
        "Normal text.",
        "",
        "```{r}",
        "x <- 1",
        "```",
        "",
        "Normal text again."
    }

    local b = vim.api.nvim_create_buf(false, false)
    vim.api.nvim_set_option_value("filetype", "quarto", { buf = b })
    vim.api.nvim_buf_set_lines(b, 0, -1, false, lines)
    local w = vim.api.nvim_open_win(b, true, { relative = "win", row = 1, col = 1, width = 80, height = 24 })
    vim.api.nvim_win_set_cursor(w, { 1, 1 })
    local lang = require("r.utils").get_lang()
    vim.api.nvim_win_close(w, true)
    if lang == "yaml" then
        vim.health.ok("YAML header correctly identified.")
    else
        vim.health.error("YAML header not identified: " .. lang)
    end

The last line of the output of :checkhealth r is:

- ERROR YAML header not identified: markdown

@PMassicotte
Copy link
Collaborator

So it always returns "markdown" wherever the cursor is? This is the same issue we have with the actual tests. Could it be that TS is not loaded or properly initialized?

@jalvesaq
Copy link
Member Author

Could it be that TS is not loaded or properly initialized?

This seems to be the problem, but how could we initialize it?

@jalvesaq
Copy link
Member Author

I get the correct language with this:

local finish_check_lang = function()
    local lang = require("r.utils").get_lang()
    vim.cmd("quit!")
    if lang == "r" then
        vim.notify("Languge correctly detected: " .. lang)
    else
        vim.notify("Wrong languge detected: " .. lang)
    end
end

local check_lang = function()
    local lines = {
        "---",
        "title: Test",
        "author: R.nvim",
        "---",
        "",
        "Normal text.",
        "",
        "```{r}",
        "x <- 1",
        "y <- 2",
        "z <- 3",
        "```",
        "",
        "Normal text again."
    }

    local b = vim.api.nvim_create_buf(false, false)
    vim.api.nvim_set_option_value("filetype", "quarto", { buf = b })
    vim.api.nvim_buf_set_lines(b, 0, -1, false, lines)
    vim.api.nvim_open_win(b, true, { relative = "win", row = 1, col = 1, width = 80, height = 24 })
    vim.api.nvim_win_set_cursor(0, { 10, 1 })
    vim.schedule(finish_check_lang)
end

And then,

    check_lang()

as the last line of health.check(). However, the checkhealth window disappears. So, it seems that we can write a Lua script to test the get_lang() function, but not within health.check().

@PMassicotte
Copy link
Collaborator

That is great, can you explain a bit what was the issue? I see that you are using vim.schedule(), but not sure how this change something compared if you just used:

local check_lang = function()
    local lines = {
        "---",
        "title: Test",
        "author: R.nvim",
        "---",
        "",
        "Normal text.",
        "",
        "```{r}",
        "x <- 1",
        "y <- 2",
        "z <- 3",
        "```",
        "",
        "Normal text again.",
    }

    local b = vim.api.nvim_create_buf(false, false)
    vim.api.nvim_set_option_value("filetype", "quarto", { buf = b })
    vim.api.nvim_buf_set_lines(b, 0, -1, false, lines)
    vim.api.nvim_open_win(b, true, { relative = "win", row = 1, col = 1, width = 80, height = 24 })
    vim.api.nvim_win_set_cursor(0, { 10, 1 })

    -- Add the code here, not using vim.schedule()
    local lang = require("r.utils").get_lang()
    vim.cmd("quit!")
    if lang == "r" then
        vim.notify("Languge correctly detected: " .. lang)
    else
        vim.notify("Wrong languge detected: " .. lang)
    end
end

@jalvesaq
Copy link
Member Author

I understand the command vim.schedule() as meaning:

  • Skip this command for now and continue the execution of everything else. When idle, execute the scheduled command.

It seems that tree-sitter is also scheduled to be updated only when Neovim is idle, that is, in this case, after the :checkhealth command has finished.

@jalvesaq
Copy link
Member Author

It's working now:

local check_lang = function()
    local lines = {
        "---",
        "title: Test",
        "---",
        "",
        "Normal text.",
        "",
        "```{r}",
        "x <- 1",
        "```",
        "",
        "Normal text again.",
    }

    local b = vim.api.nvim_create_buf(false, false)
    vim.api.nvim_set_option_value("bufhidden", "wipe", { scope = "local" })
    vim.api.nvim_set_option_value("number", false, { scope = "local" })
    vim.api.nvim_set_option_value("swapfile", false, { scope = "local" })
    vim.api.nvim_set_option_value("buftype", "nofile", { scope = "local" })
    vim.api.nvim_set_option_value("filetype", "quarto", { buf = b })
    vim.api.nvim_buf_set_lines(b, 0, -1, false, lines)
    local w = vim.api.nvim_open_win(
        b,
        true,
        { relative = "win", row = 1, col = 1, width = 80, height = 24 }
    )
    vim.cmd("redraw")

    local langs = {
        { "yaml", 2, 0, "" },
        { "markdown", 4, 0, "" },
        { "markdown_inline", 5, 0, "" },
        { "chunk_header", 7, 0, "" },
        { "r", 8, 0, "" },
        { "chunk_end", 9, 0, "" },
    }

    for k, v in pairs(langs) do
        vim.api.nvim_win_set_cursor(w, { v[2], v[3] })
        langs[k][4] = require("r.utils").get_lang()
    end
    vim.api.nvim_win_close(w, true)
    vim.api.nvim_buf_delete(b, { force = true })
    vim.health.start("Checking language detection in a Quarto document:")
    for _, v in pairs(langs) do
        if v[1] == v[4] then
            vim.health.ok("Correctly detected: `" .. v[1] .. "`")
        else
            vim.health.error(
                "Wrongly detected: `" .. v[1] .. "` vs `" .. v[4]("`")
            )
        end
    end
end

@jalvesaq
Copy link
Member Author

The command vim.cmd("redraw") forces the update of the user interface, including tree-sitter parsing.

@jalvesaq
Copy link
Member Author

The code is on the branch "health". To try it: :checkhealth r

Problem: a float window appears briefly while :checkhealth is being executed.

@jalvesaq
Copy link
Member Author

I added the option "hide" and the float window no longer pops up.

@PMassicotte
Copy link
Collaborator

Very nice. Minor suggestions, maybe refactor as:

R.nvim/lua/r/health.lua

Lines 97 to 114 in 078d6bc

vim.health.start("Checking language detection in a Quarto document:")
for _, v in pairs(qlangs) do
if v[1] == v[4] then
vim.health.ok("Correctly detected: `" .. v[1] .. "`")
else
vim.health.error("Wrongly detected: `" .. v[1] .. "` vs `" .. v[4] .. "`")
end
end
vim.health.start("Checking language detection in an Rnoweb document:")
for _, v in pairs(nlangs) do
if v[1] == v[4] then
vim.health.ok("Correctly detected: `" .. v[1] .. "`")
else
vim.health.error("Wrongly detected: `" .. v[1] .. "` vs `" .. v[4] .. "`")
end
end
end

local function check_language_detection(doc_type, langs)
    vim.health.start("Checking language detection in a " .. doc_type .. " document:")
    for _, v in pairs(langs) do
        if v[1] == v[4] then
            vim.health.ok("Correctly detected: `" .. v[1] .. "`")
        else
            vim.health.error("Wrongly detected: `" .. v[1] .. "` vs `" .. v[4] .. "`")
        end
    end
end

local check_lang = function()
    -- Fill the buffer with a Quarto document and check language detection
    local qlines = {
        "---",
        "title: Test",
        "---",
        "",
        "Normal text.",
        "",
        "```{r}",
        "x <- 1",
        "```",
        "",
        "Normal text again.",
    }
    local qlangs = {
        { "yaml", 2, 0, "" },
        { "markdown", 4, 0, "" },
        { "markdown_inline", 5, 0, "" },
        { "chunk_header", 7, 0, "" },
        { "r", 8, 0, "" },
        { "chunk_end", 9, 0, "" },
    }
    qlangs = check_buffer("quarto", qlines, qlangs)

    local nlines = {
        "\\documentclass{article}",
        "\\begin{document}",
        "",
        "Normal text.",
        "",
        "<<example>>=",
        "x <- 1",
        "@",
        "",
        "Normal text again.",
        "",
        "\\end{document}",
    }
    local nlangs = {
        { "latex", 4, 0, "" },
        { "chunk_header", 6, 0, "" },
        { "r", 7, 0, "" },
        { "chunk_end", 8, 0, "" },
    }
    nlangs = check_buffer("rnoweb", nlines, nlangs)

    check_language_detection("Quarto", qlangs)
    check_language_detection("Rnoweb", nlangs)
end

@PMassicotte
Copy link
Collaborator

If you agree, I will push it.

@jalvesaq
Copy link
Member Author

Thanks! I've just made a minor change to avoid code repetition, and I think the branch is ready to be merged now.

@PMassicotte
Copy link
Collaborator

LGTM.

@PMassicotte
Copy link
Collaborator

PMassicotte commented Feb 16, 2025

I notice we no longer need to use schedule()?

Never mind, I guess this is what you meant here: #329 (comment)

@jalvesaq
Copy link
Member Author

Yes, I replaced vim.schedule() with vim.cmd("redraw") because if we schedule the execution of check_lang() it will be executed only after :checkhealth is finished.

@jalvesaq
Copy link
Member Author

It would be better if there were a native Lua command to replace the VimScript command redraw, but I couldn't find one.

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

2 participants