From 78513205a28056b4ec155298d6723ae9dbfd49fa Mon Sep 17 00:00:00 2001 From: Willem Jan Noort Date: Thu, 21 Mar 2024 16:34:04 +0100 Subject: [PATCH] ref(result_cache): use pathlib and autofetch --- lua/neotest/client/init.lua | 13 ++--- lua/neotest/config/init.lua | 2 + lua/neotest/consumers/result_cache.lua | 76 +++++++++++++++++++------- 3 files changed, 62 insertions(+), 29 deletions(-) diff --git a/lua/neotest/client/init.lua b/lua/neotest/client/init.lua index 534fa45..2d6153b 100644 --- a/lua/neotest/client/init.lua +++ b/lua/neotest/client/init.lua @@ -228,16 +228,13 @@ function neotest.Client:is_running(position_id, args) return #self:_get_running_adapters(position_id) > 0 end ----Loads results from cache into state +---Loads results for all adapters into state ---@async ----@param cached_results_file_path string +---@param all_results table ---@return nil -function neotest.Client:load_cached_results(cached_results_file_path) - if lib.files.exists(cached_results_file_path) then - local cached_results = vim.json.decode(lib.files.read(cached_results_file_path)) - for adapter_id, results in pairs(cached_results) do - self._state:update_results(adapter_id, results) - end +function neotest.Client:load_results(all_results) + for adapter_id, adapter_results in pairs(all_results) do + self._state:update_results(adapter_id, adapter_results) end end diff --git a/lua/neotest/config/init.lua b/lua/neotest/config/init.lua index 56a0b6a..298e54a 100644 --- a/lua/neotest/config/init.lua +++ b/lua/neotest/config/init.lua @@ -121,6 +121,7 @@ local js_watch_query = [[ ---@class neotest.Config.state ---@field enabled boolean +---@field fetch_cached_results_on_startup boolean ---@class neotest.Config.output_panel ---@field enabled boolean @@ -281,6 +282,7 @@ local default_config = { }, state = { enabled = true, + fetch_cached_results_on_startup = false }, watch = { enabled = true, diff --git a/lua/neotest/consumers/result_cache.lua b/lua/neotest/consumers/result_cache.lua index cdc0c0b..b51bd2e 100644 --- a/lua/neotest/consumers/result_cache.lua +++ b/lua/neotest/consumers/result_cache.lua @@ -1,5 +1,7 @@ local nio = require("nio") local lib = require("neotest.lib") +local Path = require("plenary.path") +local config = require("neotest.config") ---@private ---@type neotest.Client @@ -11,9 +13,8 @@ local plugin = "neotest" local consumer = "result_cache" local result_cache_file_name = "results.json" -local plugin_cache = vim.fn.stdpath("cache") .. "/" .. plugin -local consumer_cache = plugin_cache .. "/" .. consumer -local result_cache_file_path = consumer_cache .. "/" .. result_cache_file_name +local plugin_cache = Path:new(vim.fn.stdpath("cache")):joinpath(plugin) +local consumer_cache = plugin_cache:joinpath(consumer) ---@toc_entry Result Cache Consumer ---@text @@ -22,45 +23,65 @@ local result_cache_file_path = consumer_cache .. "/" .. result_cache_file_name ---@class neotest.consumers.result_cache neotest.result_cache = {} +---@private +---@async +---@return nil +local function _cache_output_tmp_file(output_file_tmp_path, output_file_cache_path) + if not lib.files.exists(output_file_cache_path) then + nio.uv.fs_copyfile(output_file_tmp_path, output_file_cache_path) + end +end + +---@private +---@async +---@param adapter_id string neotest adapter_id +---@return Path adapter_cache_path safe adapter cache path +local function _adapter_cache_path(adapter_id) + local adapter_cache_name + if vim.base64 then + adapter_cache_name = vim.base64.encode(adapter_id) + else + adapter_cache_name = adapter_id:gsub("[:/]", "") + end + return consumer_cache:joinpath(adapter_cache_name) +end --- Cache test results to cache file ---@async ---@return nil function neotest.result_cache:cache() - nio.uv.fs_mkdir(plugin_cache, 504) - nio.uv.fs_mkdir(consumer_cache, 504) + plugin_cache:mkdir() + consumer_cache:mkdir() local results_to_cache = {} for _, adapter_id in pairs(client:get_adapters()) do + local adapter_cache_path = _adapter_cache_path(adapter_id) + adapter_cache_path:mkdir() local adapter_results = client:get_results(adapter_id) for key, result in pairs(adapter_results) do if result.output then - local output_file_cache_path = consumer_cache .. "/" .. vim.split(result.output, "/")[5] - neotest.result_cache._cache_output_tmp_file(result.output, output_file_cache_path) - adapter_results[key].output = output_file_cache_path + local result_output = Path:new(result.output) + local result_filename = + vim.split(tostring(result_output), tostring(result_output:parent()))[2] + local output_file_cache_path = adapter_cache_path:joinpath(result_filename:sub(2)) + _cache_output_tmp_file(result.output, tostring(output_file_cache_path)) + adapter_results[key].output = tostring(output_file_cache_path) end end results_to_cache[adapter_id] = adapter_results - end - lib.files.write(result_cache_file_path, vim.json.encode(results_to_cache)) + local result_cache_file_path = adapter_cache_path:joinpath(result_cache_file_name) + lib.files.write(tostring(result_cache_file_path), vim.json.encode(results_to_cache)) + vim.notify("Test results cached.") + end end neotest.result_cache.cache = nio.create(neotest.result_cache.cache, 1) ----@private ----@async ----@return nil -function neotest.result_cache._cache_output_tmp_file(output_file_tmp_path, output_file_cache_path) - if not lib.files.exists(output_file_cache_path) then - nio.uv.fs_copyfile(output_file_tmp_path, output_file_cache_path) - end -end - --- Clear cached test results ---@async ---@return nil function neotest.result_cache:clear() - local cache_files = lib.files.find(consumer_cache) + local cache_files = lib.files.find(tostring(consumer_cache)) for _, file in pairs(cache_files) do os.remove(file) end @@ -72,7 +93,14 @@ neotest.result_cache.clear = nio.create(neotest.result_cache.clear, 1) ---@async ---@return nil function neotest.result_cache:fetch() - client:load_cached_results(result_cache_file_path) + for _, adapter_id in pairs(client:get_adapters()) do + local result_cache_file_path = _adapter_cache_path(adapter_id):joinpath(result_cache_file_name) + + if result_cache_file_path:exists() then + local cached_results = vim.json.decode(lib.files.read(tostring(result_cache_file_path))) + client:load_results(cached_results) + end + end end neotest.result_cache.fetch = nio.create(neotest.result_cache.fetch, 1) @@ -81,6 +109,12 @@ neotest.result_cache = setmetatable(neotest.result_cache, { ---@param client_ neotest.Client __call = function(_, client_) client = client_ + + if config.state.fetch_results_on_startup then + client.listeners.started = function() + neotest.result_cache:fetch() + end + end return neotest.result_cache end, })