Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 committed Sep 28, 2023
1 parent fd31fdc commit c8ba716
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 41 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,30 @@ jobs:
uses: lunarmodules/luacheck@v1
with:
args: lua --config .luacheckrc
unit_test:
name: Unit Test
strategy:
matrix:
nvim_version: [stable, nightly, v0.6.0]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup neovim
uses: rhysd/action-setup-vim@v1
id: vim
with:
neovim: true
version: ${{ matrix.nvim_version }}
- name: Install luajit
uses: leafo/gh-actions-lua@v10
with:
luaVersion: "luajit-2.1.0-beta3"
- name: Install luarocks
uses: leafo/gh-actions-luarocks@v4
- name: Run test cases
shell: bash
run: |
luarocks install luacheck
luarocks install vusted
vusted --shuffle ./tests
6 changes: 2 additions & 4 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"diagnostics.globals": [
"vim"
],
"workspace.checkThirdParty": false
"diagnostics.globals": ["vim", "describe", "before_each", "it"],
"workspace.checkThirdParty": false
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ For more details, please see [Design & Technics](https://github.com/linrongbin16

## Requirement

- Neovim version ≥ 0.5.
- Neovim version ≥ 0.6.0.
- [Nerd fonts](https://www.nerdfonts.com/) for icons.

## Install
Expand Down
2 changes: 1 addition & 1 deletion lua/lsp-progress.lua
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ local function setup(option)

-- setup logger
logger.setup(
Configs.debug,
Configs.debug and "DEBUG" or "INFO",
Configs.console_log,
Configs.file_log,
Configs.file_log_name
Expand Down
43 changes: 22 additions & 21 deletions lua/lsp-progress/event.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
--- @type table<string, any>
local logger = require("lsp-progress.logger")

--- @type string|nil
local EventName = nil
--- @type integer?
local EventUpdateTimeLimit = nil
--- @type boolean
local EventEmit = false
--- @type integer?
local InternalRegularUpdateTime = nil
--- @type Configs
local EventConfigs = {
name = nil,
update_time_limit = nil,
regular_update_time = nil,
emit = false,
}

--- @class DisableEventOpt
--- @field mode string?
Expand Down Expand Up @@ -79,29 +77,32 @@ local GlobalDisabledEventOptsManager = nil

--- @return nil
local function reset()
EventEmit = false
EventConfigs.emit = false
end

--- @return nil
local function emit()
if not EventEmit then
if not EventConfigs.emit then
if
GlobalDisabledEventOptsManager == nil
or not GlobalDisabledEventOptsManager:match()
then
vim.cmd("doautocmd User " .. EventName)
EventEmit = true
logger.debug("Emit user event:%s", EventName)
vim.cmd("doautocmd User " .. EventConfigs.name)
EventConfigs.emit = true
logger.debug("Emit user event:%s", EventConfigs.name)
else
logger.debug("Disabled emit user event:%s", EventName)
logger.debug("Disabled emit user event:%s", EventConfigs.name)
end
vim.defer_fn(reset, EventUpdateTimeLimit --[[@as integer]])
vim.defer_fn(reset, EventConfigs.update_time_limit --[[@as integer]])
end
end

local function regular_update()
emit()
vim.defer_fn(regular_update, InternalRegularUpdateTime --[[@as integer]])
vim.defer_fn(
regular_update,
EventConfigs.regular_update_time --[[@as integer]]
)
end

--- @param event_name string
Expand All @@ -115,9 +116,9 @@ local function setup(
internal_regular_update_time,
disable_events_opts
)
EventName = event_name
EventUpdateTimeLimit = event_update_time_limit
InternalRegularUpdateTime = internal_regular_update_time
EventConfigs.name = event_name
EventConfigs.update_time_limit = event_update_time_limit
EventConfigs.regular_update_time = internal_regular_update_time
GlobalDisabledEventOptsManager =
DisableEventOptsManager:new(disable_events_opts)
reset()
Expand All @@ -130,4 +131,4 @@ local M = {
emit = emit,
}

return M
return M
28 changes: 14 additions & 14 deletions lua/lsp-progress/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ local LogHighlights = {
}

--- @type Configs
local Configs = {
local LogConfigs = {
level = LogLevels.INFO,
use_console = true,
use_file = false,
file_name = "lsp-progress.log",
console_log = true,
file_log = false,
file_name = nil,
}

--- @param debug boolean
--- @param level string
--- @param console_log boolean
--- @param file_log boolean
--- @param file_log_name string
--- @return nil
local function setup(debug, console_log, file_log, file_log_name)
Configs.level = debug and LogLevels.DEBUG or LogLevels.INFO
Configs.use_console = console_log
Configs.use_file = file_log
local function setup(level, console_log, file_log, file_log_name)
LogConfigs.level = LogLevels[level]
LogConfigs.console_log = console_log
LogConfigs.file_log = file_log
-- For Windows: $env:USERPROFILE\AppData\Local\nvim-data\lsp-progress.log
-- For *NIX: ~/.local/share/nvim/lsp-progress.log
Configs.file_name = string.format(
LogConfigs.file_name = string.format(
"%s%s%s",
vim.fn.stdpath("data"),
PATH_SEPARATOR,
Expand All @@ -48,12 +48,12 @@ end
--- @param level integer
--- @param msg string
local function log(level, msg)
if level < Configs.level then
if level < LogConfigs.level then
return
end

local msg_lines = vim.split(msg, "\n", { plain = true })
if Configs.use_console and level >= LogLevels.INFO then
if LogConfigs.console_log and level >= LogLevels.INFO then
local msg_chunks = {}
for _, line in ipairs(msg_lines) do
table.insert(msg_chunks, {
Expand All @@ -63,8 +63,8 @@ local function log(level, msg)
end
vim.api.nvim_echo(msg_chunks, false, {})
end
if Configs.use_file then
local fp = io.open(Configs.file_name, "a")
if LogConfigs.file_log then
local fp = io.open(LogConfigs.file_name, "a")
if fp then
for _, line in ipairs(msg_lines) do
fp:write(
Expand Down
48 changes: 48 additions & 0 deletions tests/logger_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
local cwd = vim.fn.getcwd()

describe("logger", function()
local assert_eq = assert.is_equal
local assert_true = assert.is_true
local assert_false = assert.is_false

before_each(function()
vim.api.nvim_command("cd " .. cwd)
end)

local logger = require("lsp-progress.log")
logger.setup({
level = "DEBUG",
console_log = true,
file_log = true,
})
describe("[log]", function()
it("debug", function()
logger.debug("debug without parameters")
logger.debug("debug with 1 parameters: %s", "a")
logger.debug("debug with 2 parameters: %s, %d", "a", 1)
logger.debug("debug with 3 parameters: %s, %d, %f", "a", 1, 3.12)
assert_true(true)
end)
it("info", function()
logger.info("info without parameters")
logger.info("info with 1 parameters: %s", "a")
logger.info("info with 2 parameters: %s, %d", "a", 1)
logger.info("info with 3 parameters: %s, %d, %f", "a", 1, 3.12)
assert_true(true)
end)
it("warn", function()
logger.warn("warn without parameters")
logger.warn("warn with 1 parameters: %s", "a")
logger.warn("warn with 2 parameters: %s, %d", "a", 1)
logger.warn("warn with 3 parameters: %s, %d, %f", "a", 1, 3.12)
assert_true(true)
end)
it("err", function()
logger.err("err without parameters")
logger.err("err with 1 parameters: %s", "a")
logger.err("err with 2 parameters: %s, %d", "a", 1)
logger.err("err with 3 parameters: %s, %d, %f", "a", 1, 3.12)
assert_true(true)
end)
end)
end)

0 comments on commit c8ba716

Please sign in to comment.