Skip to content

Commit

Permalink
feat(tests): better logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
kylo252 committed Apr 12, 2022
1 parent aae5dd8 commit f45cf70
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
6 changes: 2 additions & 4 deletions lua/lvim/core/log.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
local Log = {}

local logfile = string.format("%s/%s.log", get_cache_dir(), "lvim")

Log.levels = {
TRACE = 1,
DEBUG = 2,
Expand Down Expand Up @@ -39,7 +37,7 @@ function Log:init()
{ level = structlog.formatters.FormatColorizer.color_level() }
),
}),
structlog.sinks.File(log_level, logfile, {
structlog.sinks.File(log_level, self:get_path(), {
processors = {
structlog.processors.Namer(),
structlog.processors.StackWriter({ "line", "file" }, { max_parents = 3, stack_level = 2 }),
Expand Down Expand Up @@ -155,7 +153,7 @@ end
---Retrieves the path of the logfile
---@return string path of the logfile
function Log:get_path()
return logfile
return string.format("%s/%s.log", get_cache_dir(), "lvim")
end

---Add a log entry at TRACE level
Expand Down
23 changes: 16 additions & 7 deletions tests/specs/lsp_spec.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
local a = require "plenary.async_lib.tests"
local utils = require "lvim.utils"
local helpers = require "tests.lvim.helpers"
local temp_dir = vim.loop.os_getenv "TEMP" or "/tmp"
lvim.lsp.templates_dir = join_paths(temp_dir, "lvim", "tests", "artifacts")

a.describe("lsp workflow", function()
local Log = require "lvim.core.log"
local logfile = Log:get_path()
local temp_dir = vim.loop.os_getenv "TEMP" or "/tmp"
lvim.lsp.templates_dir = join_paths(temp_dir, "lvim", "tests", "artifacts")
local lsp_log = join_paths(temp_dir, "lvim", "tests", "lsp.log")

pcall(function()
lvim.log.level = "debug"
package.loaded["packer.log"] = nil
package.loaded["lvim.core.log"] = nil
local Log = require "lvim.core.log"
Log.get_path = function()
return lsp_log
end
end)

a.it("should be able to delete ftplugin templates", function()
if utils.is_directory(lvim.lsp.templates_dir) then
Expand All @@ -33,16 +42,16 @@ a.describe("lsp workflow", function()
local plugins = require "lvim.plugins"
require("lvim.plugin-loader").load { plugins, lvim.plugins }

if utils.is_file(logfile) then
assert.equal(vim.fn.delete(logfile), 0)
if utils.is_file(lsp_log) then
assert.equal(vim.fn.delete(lsp_log), 0)
end

assert.True(utils.is_directory(lvim.lsp.templates_dir))
require("lvim.lsp").setup()

-- we need to delay this check until the log gets populated
vim.schedule(function()
assert.False(helpers.log_contains "templates")
assert.False(helpers.file_contains(lsp_log, "Templates installation in progress"))
end)
end)

Expand Down
6 changes: 6 additions & 0 deletions tests/specs/plugins_load_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ a.describe("plugin-loader", function()
local plugins = require "lvim.plugins"
local loader = require "lvim.plugin-loader"

pcall(function()
lvim.log.level = "debug"
package.loaded["packer.log"] = nil
package.loaded["lvim.core.log"] = nil
end)

a.it("should be able to load default packages without errors", function()
loader.load { plugins, lvim.plugins }

Expand Down

0 comments on commit f45cf70

Please sign in to comment.