From c8ba716b2cc4ae4ef4b79141f2d005cb274ed841 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Thu, 28 Sep 2023 12:07:59 +0800 Subject: [PATCH] test --- .github/workflows/ci.yml | 27 +++++++++++++++++++++ .luarc.json | 6 ++--- README.md | 2 +- lua/lsp-progress.lua | 2 +- lua/lsp-progress/event.lua | 43 +++++++++++++++++---------------- lua/lsp-progress/logger.lua | 28 +++++++++++----------- tests/logger_spec.lua | 48 +++++++++++++++++++++++++++++++++++++ 7 files changed, 115 insertions(+), 41 deletions(-) create mode 100644 tests/logger_spec.lua diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a8ce33..0e3d355 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.luarc.json b/.luarc.json index 47a3ccd..681241e 100644 --- a/.luarc.json +++ b/.luarc.json @@ -1,6 +1,4 @@ { - "diagnostics.globals": [ - "vim" - ], - "workspace.checkThirdParty": false + "diagnostics.globals": ["vim", "describe", "before_each", "it"], + "workspace.checkThirdParty": false } diff --git a/README.md b/README.md index 2cd8c26..d7aafa3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/lsp-progress.lua b/lua/lsp-progress.lua index 6f2ebba..46b584f 100644 --- a/lua/lsp-progress.lua +++ b/lua/lsp-progress.lua @@ -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 diff --git a/lua/lsp-progress/event.lua b/lua/lsp-progress/event.lua index 9411ad8..83632b7 100644 --- a/lua/lsp-progress/event.lua +++ b/lua/lsp-progress/event.lua @@ -1,14 +1,12 @@ ---- @type table 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? @@ -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 @@ -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() @@ -130,4 +131,4 @@ local M = { emit = emit, } -return M \ No newline at end of file +return M diff --git a/lua/lsp-progress/logger.lua b/lua/lsp-progress/logger.lua index 701d889..c5843ac 100644 --- a/lua/lsp-progress/logger.lua +++ b/lua/lsp-progress/logger.lua @@ -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, @@ -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, { @@ -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( diff --git a/tests/logger_spec.lua b/tests/logger_spec.lua new file mode 100644 index 0000000..f15ee74 --- /dev/null +++ b/tests/logger_spec.lua @@ -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)