Skip to content

Commit

Permalink
WIP: work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Jul 7, 2024
1 parent 820a83a commit 3230519
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 8 deletions.
1 change: 0 additions & 1 deletion integration-tests/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,5 @@ afterEach(() => {
})

beforeEach(() => {
// cy.task("showYaziLog")
cy.task("removeYaziLog")
})
29 changes: 22 additions & 7 deletions lua/yazi/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,37 @@ return {

if vim.fn.executable('ya') ~= 1 then
vim.health.warn('ya (yazi command line interface) not found on PATH')

if require('yazi').config.use_ya_for_events_reading == true then
vim.health.error(
'You have opted in to use `ya` for events reading, but `ya` is not found on PATH. Please install `ya` or disable `use_ya_for_events_reading` in your config.'
)
end

vim.health.warn(
'In future versions of yazi.nvim, `ya` will be required. Please install `ya` to avoid future issues.'
)
end

-- example data:
-- Ya 0.2.5 (f5a7ace 2024-06-23)
local raw_ya_version = vim.fn.system('ya --version')
local raw_ya_version = vim.fn.system('ya --version') or ''
local ya_semver = raw_ya_version:match('[Yy]a (%w+%.%w+%.%w+)')
if ya_semver == nil then
vim.health.warn('ya --version looks unexpected, saw ' .. raw_ya_version)
end

if not checker.gt(ya_semver, '0.2.4') then
vim.health.warn(
'The `ya` executable version (yazi command line interface) is too old. Please upgrade to the newest version.'
string.format(
'`ya --version` looks unexpected, saw `%s` 🤔',
raw_ya_version
)
)
else
vim.health.info(('Found `ya` version `%s` 👍'):format(ya_semver))
if not checker.gt(ya_semver, '0.2.4') then
vim.health.warn(
'The `ya` executable version (yazi command line interface) is too old. Please upgrade to the newest version.'
)
else
vim.health.info(('Found `ya` version `%s` 👍'):format(ya_semver))
end
end

local logfile_location = require('yazi.log'):get_logfile_path()
Expand Down
127 changes: 127 additions & 0 deletions spec/yazi/health_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
local stub = require('luassert.stub')
local assert = require('luassert')

local function assert_buffer_contains_text(needle)
local buffer_text = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local text = table.concat(buffer_text, '\n')
local message = string.format(
"Expected the main string to contain the substring.\nMain string: '%s'\nSubstring: '%s'",
text,
needle
)

local found = string.find(text, needle, 1, true) ~= nil
assert(found, message)
end

-- make nvim find the health check file so that it can be executed by :checkhealth
-- without this, the health check will not be found
vim.opt.rtp:append('.')

local mock_app_versions = {}

describe('the happy path', function()
local snapshot

before_each(function()
snapshot = assert:snapshot()
mock_app_versions = {
['yazi'] = 'yazi 0.2.5 (f5a7ace 2024-06-23)',
['ya'] = 'Ya 0.2.5 (f5a7ace 2024-06-23)',
['nvim-0.10.0'] = true,
}

stub(vim.fn, 'has', function(needle)
if mock_app_versions[needle] then
return 1
else
return 0
end
end)

stub(vim.fn, 'executable', function(command)
return mock_app_versions[command] and 1 or 0
end)

stub(vim.fn, 'system', function(command)
if command == 'yazi --version' then
return mock_app_versions['yazi']
elseif command == 'ya --version' then
return mock_app_versions['ya']
else
error('unexpected command: ' .. command)
end
end)
end)

after_each(function()
snapshot:revert()
end)

it('reports everything is ok', function()
vim.cmd('checkhealth yazi')

assert_buffer_contains_text('Found `yazi` version `0.2.5`')
assert_buffer_contains_text('Found `ya` version `0.2.5`')
assert_buffer_contains_text('OK yazi')
end)

it('warns if the yazi version is too old', function()
mock_app_versions['yazi'] = 'yazi 0.2.4 (f5a7ace 2024-06-23)'
vim.cmd('checkhealth yazi')

assert_buffer_contains_text(
'yazi version is too old, please upgrade to 0.2.5 or newer'
)
end)

it('warns if the ya version is too old', function()
mock_app_versions['ya'] = 'Ya 0.2.4 (f5a7ace 2024-06-23)'

vim.cmd('checkhealth yazi')

assert_buffer_contains_text(
'WARNING The `ya` executable version (yazi command line interface) is too old.'
)
end)

it('warns when yazi is not found', function()
mock_app_versions['yazi'] = 'command not found'
end)

it('warns when ya is not found', function()
mock_app_versions['ya'] = 'command not found'

vim.cmd('checkhealth yazi')

assert_buffer_contains_text(
'WARNING `ya --version` looks unexpected, saw `command not found`'
)
end)

it(
'warns when `ya` cannot be found but is set as the event_reader',
function()
stub(vim.fn, 'executable', function(command)
if command == 'ya' then
return 0
else
return 1
end
end)

require('yazi').setup(
---@type YaziConfig
{
use_ya_for_events_reading = true,
}
)

vim.cmd('checkhealth yazi')

assert_buffer_contains_text(
'ERROR You have opted in to use `ya` for events reading, but `ya` is not found on PATH.'
)
end
)
end)

0 comments on commit 3230519

Please sign in to comment.