Skip to content

Commit

Permalink
ref(result_cache): use pathlib and autofetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Jan Noort committed Mar 21, 2024
1 parent 7f9146b commit e486a91
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 29 deletions.
13 changes: 5 additions & 8 deletions lua/neotest/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, neotest.Result>
---@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

Expand Down
2 changes: 2 additions & 0 deletions lua/neotest/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ define_highlights()

---@class neotest.Config.state
---@field enabled boolean
---@field fetch_cached_results_on_startup boolean

---@class neotest.Config.output_panel
---@field enabled boolean
Expand Down Expand Up @@ -267,6 +268,7 @@ local default_config = {
},
state = {
enabled = true,
fetch_cached_results_on_startup = false
},
watch = {
enabled = true,
Expand Down
76 changes: 55 additions & 21 deletions lua/neotest/consumers/result_cache.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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,
})
Expand Down

0 comments on commit e486a91

Please sign in to comment.