Skip to content

Commit

Permalink
feat: add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lopi-py committed Jul 31, 2024
1 parent 669a634 commit 494694f
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 3 deletions.
13 changes: 13 additions & 0 deletions .busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
return {
_all = {
coverage = false,
lpath = "lua/?.lua;lua/?/init.lua",
lua = "~/.luarocks/bin/nlua",
},
default = {
verbose = true,
},
tests = {
verbose = true,
},
}
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Tests
on:
pull_request: ~
push:
branches:
- main

jobs:
build:
name: Run tests
runs-on: ubuntu-latest
strategy:
matrix:
neovim_version: ["nightly", "stable", "v0.9.0"]

steps:
- uses: actions/checkout@v4

- name: Install luau-lsp
run: |
wget https://github.com/JohnnyMorganz/luau-lsp/releases/download/1.32.1/luau-lsp-linux-arm64.zip
unzip luau-lsp*.zip
sudo mv luau-lsp /usr/local/bin/luau-lsp
- name: Run tests
uses: nvim-neorocks/nvim-busted-action@v1
with:
nvim_version: ${{ matrix.neovim_version }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
docs/tags
/luarocks
/lua_modules
/.luarocks
5 changes: 5 additions & 0 deletions lua/luau-lsp/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ M.uv = vim.loop or vim.uv
---@diagnostic disable-next-line: deprecated
M.get_clients = vim.lsp.get_clients or vim.lsp.get_active_clients

---@param client vim.lsp.Client
function M.get_client_settings(client)
return client.settings or client.config.settings
end

---@param t table
---@param value any
---@return boolean
Expand Down
4 changes: 2 additions & 2 deletions lua/luau-lsp/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ end
function M.stop()
local client = util.get_client()
if client then
client.stop(true)
client.stop()
end
end

Expand All @@ -213,7 +213,7 @@ function M.restart()
return
end

client.stop(true)
client.stop()

local buffers = vim.lsp.get_buffers_by_client_id(client.id)

Expand Down
2 changes: 1 addition & 1 deletion lua/luau-lsp/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ end
---@return string
function M.storage_file(key)
local storage = Path:new(vim.fn.stdpath "data") / "luau-lsp"
storage:mkdir()
storage:mkdir { parents = true }

return tostring(storage / key)
end
Expand Down
15 changes: 15 additions & 0 deletions luau-lsp.nvim-scm-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
rockspec_format = "3.0"
package = "luau-lsp.nvim"
version = "scm-1"

source = {
url = "git+https://github.com/lopi-py/luau-lsp.nvim.git",
}

dependencies = {
"plenary.nvim",
}

test_dependencies = {
"nlua",
}
10 changes: 10 additions & 0 deletions neovim.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ globals:
any: true
jit:
any: true
describe:
any: true
it:
any: true
assert:
any: true
after_each:
any: true
stub:
any: true
21 changes: 21 additions & 0 deletions spec/command_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local command = require "luau-lsp.command"
local compat = require "luau-lsp.compat"

describe("command executor", function()
it("should provide completion for regenerate_sourcemap", function()
local cwd = compat.uv.cwd()
assert(cwd)

compat.uv.chdir(cwd .. "/spec/roblox-project")

local items1 = command.complete("reg", "LuauLsp reg")
local items2 = command.complete("", "LuauLsp regenerate_sourcemap ")
local items3 = command.complete("serv", "LuauLsp regenerate_sourcemap serv")

compat.uv.chdir(cwd)

assert.same({ "regenerate_sourcemap" }, items1)
assert.same({ "dev.project.json", "serve.project.json" }, items2)
assert.same({ "serve.project.json" }, items3)
end)
end)
6 changes: 6 additions & 0 deletions spec/roblox-project/dev.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dev",
"tree": {
"$className": "DataModel"
}
}
6 changes: 6 additions & 0 deletions spec/roblox-project/serve.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "server",
"tree": {
"$className": "DataModel"
}
}
138 changes: 138 additions & 0 deletions spec/server_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
local compat = require "luau-lsp.compat"
local config = require "luau-lsp.config"
local server = require "luau-lsp.server"

local buffers = {}

local function create_luau_buffer()
local bufnr = vim.api.nvim_create_buf(true, false)
vim.bo[bufnr].filetype = "luau"

server.start(bufnr)
table.insert(buffers, bufnr)

vim.api.nvim_set_current_buf(bufnr)

return bufnr
end

local function wait_for_client()
vim.wait(5000, function()
local clients = compat.get_clients { name = "luau-lsp" }
return clients[1] ~= nil
end)

local clients = compat.get_clients { name = "luau-lsp" }
assert.same(1, #clients)

return clients[1]
end

describe("luau-lsp server", function()
---@diagnostic disable-next-line: duplicate-set-field
vim.notify = function() end

after_each(function()
local client = wait_for_client()
client.stop(true)
vim.wait(100)

for _, bufnr in ipairs(buffers) do
if vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_buf_delete(bufnr, {})
end
end
end)

it("should attach a client to a luau buffer", function()
local bufnr = create_luau_buffer()
local client = wait_for_client()
assert.same({ bufnr }, vim.lsp.get_buffers_by_client_id(client.id))
end)

it("should resue the same client for multiple buffers", function()
local first = create_luau_buffer()
local second = create_luau_buffer()
local third = create_luau_buffer()

-- try to start the server for the same buffer many times
server.start(first)
server.start(first)
server.start(third)

local client = wait_for_client()
assert.same({ first, second, third }, vim.lsp.get_buffers_by_client_id(client.id))
end)

it("should handle roblox configuration", function()
config.config {
platform = {
type = "standard",
},
sourcemap = {
enabled = false,
},
}

local notify = stub(vim, "notify")
local bufnr = create_luau_buffer()
local client = wait_for_client()

assert.stub(notify).called(0)
assert.same({ "luau-lsp", "lsp", "--no-flags-enabled" }, client.config.cmd)

client.stop(true)
vim.wait(100)

config.config {
platform = {
type = "roblox",
},
sourcemap = {
enabled = true,
},
}

server.start(bufnr)
client = wait_for_client()

assert.stub(notify).was.called_with(
"[luau-lsp.nvim]: Unable to find project file `default.project.json`",
vim.log.levels.ERROR,
{ title = "luau-lsp.nvim" }
)
assert.match("globalTypes.PluginSecurity.d.luau", client.config.cmd[4])
assert.match("api%-docs.json", client.config.cmd[5])
end)

it("should respect user settings", function()
config.config {
server = {
settings = {
["luau-lsp"] = {
testSetting = {
testField = "testing",
},
},
},
},
}

create_luau_buffer()
local client = wait_for_client()

assert.same({
["luau-lsp"] = {
platform = {
type = "roblox",
},
sourcemap = {
enabled = true,
},
testSetting = {
testField = "testing",
},
},
}, compat.get_client_settings(client))
end)
end)

0 comments on commit 494694f

Please sign in to comment.