Skip to content

Commit

Permalink
ci: added deps to .gitignore, fixed Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Jan 25, 2025
1 parent ba6eee1 commit e68d21b
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 112 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
doc/tags
deps/
1 change: 1 addition & 0 deletions lua/fzf-lua/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ M.fzf = function(contents, opts)
{
fzf_bin = opts.fzf_bin,
cwd = opts.cwd,
pipe_cmd = opts.pipe_cmd,
silent_fail = opts.silent_fail,
is_fzf_tmux = opts._is_fzf_tmux,
debug = opts.debug_cmd or opts.debug and not (opts.debug_cmd == false),
Expand Down
8 changes: 7 additions & 1 deletion lua/fzf-lua/fzf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,13 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
-- MSYS2 comes with "/usr/bin/cmd" that precedes "cmd.exe" (#1396)
and { "cmd.exe", "/d", "/e:off", "/f:off", "/v:off", "/c" }
or { "sh", "-c" }
if utils.__IS_WINDOWS then
if opts.pipe_cmd then
if FZF_DEFAULT_COMMAND then
table.insert(cmd, 1, string.format("(%s) | ", FZF_DEFAULT_COMMAND))
FZF_DEFAULT_COMMAND = nil
end
table.insert(shell_cmd, table.concat(cmd, " "))
elseif utils.__IS_WINDOWS then
utils.tbl_join(shell_cmd, cmd)
else
table.insert(shell_cmd, table.concat(cmd, " "))
Expand Down
63 changes: 43 additions & 20 deletions lua/fzf-lua/test/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local __FILE__ = debug.getinfo(1, "S").source:gsub("^@", "")
vim.cmd.lcd(vim.fn.fnamemodify(__FILE__, ":p:h:h:h:h"))

local MiniTest = require("mini.test")
local screenshot = require("fzf-lua.test.screenshot")

local M = {}

-- Busted like expectations
Expand All @@ -26,29 +28,18 @@ M.NVIM_VERSION = function()
return M._NVIM_VERSION
end

M.NVIM_IS_STABLE = function()
if M._IS_STABLE == nil then
M._IS_STABLE = M.NVIM_VERSION() == "0.10.3"
end
return M._IS_STABLE
end

M.SKIP_IF_NOT_STABLE = function()
if not M.NVIM_IS_STABLE() then
MiniTest.skip("Screenshots are only tested on latest stable release.")
end
end

local os_detect = {
WIN = {
name = "Windows",
fn = function() return vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1 end
},
MAC = { name = "MacOS", fn = function() return vim.fn.has("mac") == 1 end },
LINUX = { name = "Linux", fn = function() return vim.fn.has("linux") == 1 end },
STABLE = { name = "Neovim stable", fn = function() return M.NVIM_VERSION() == "0.10.3" end },
NIGHTLY = { name = "Neovim nightly", fn = function() return vim.fn.has("nvim-0.11") == 1 end },
}

-- Creates M.IS_WIN(), M.SKIP_IF_WIN(), etc
-- Creates M.IS_WIN(), M.IS_NOT_WIN(), M.SKIP_IF_WIN(), etc
for k, v in pairs(os_detect) do
M["IS_" .. k] = function()
local var = "_IS_" .. k
Expand All @@ -57,9 +48,17 @@ for k, v in pairs(os_detect) do
end
return M[var]
end
M["IS_NOT_" .. k] = function()
return not M["IS_" .. k]
end
M["SKIP_IF_" .. k] = function(msg)
if M["IS_" .. k]() then
MiniTest.skip(msg or string.format("Does not test properly on %s", v.name))
MiniTest.skip(msg or string.format("Skip test on %s", v.name))
end
end
M["SKIP_IF_NOT_" .. k] = function(msg)
if not M["IS_" .. k]() then
MiniTest.skip(msg or string.format("Skip test: not %s", v.name))
end
end
end
Expand Down Expand Up @@ -129,10 +128,9 @@ M.new_child_neovim = function()
--- Setup fzf-lua
---@param config? table, config table
child.setup = function(config)
local lua_cmd = [[
local lua_cmd = ([[
require("fzf-lua").setup(vim.tbl_deep_extend("keep", ..., {
files = { cwd_prompt = false, cmd = "rg --files --sort=path" },
fzf_opts = { ["--no-scrollbar"] = true },
%s
winopts = {
on_create = function() _G._fzf_lua_on_create = true end,
on_close = function() _G._fzf_lua_on_create = nil end,
Expand All @@ -142,7 +140,10 @@ M.new_child_neovim = function()
load = function() _G._fzf_load_called = true end,
} }
}))
]]
]])
-- using "FZF_DEFAULT_OPTS" hangs the command on the
-- child process and the loading indicator never stops
:format(M.IS_WIN() and "defaults = { pipe_cmd = true }," or "")
child.lua(lua_cmd, { config or {} })
end

Expand Down Expand Up @@ -230,6 +231,27 @@ M.new_child_neovim = function()
MiniTest.expect.reference_screenshot(child.get_screenshot(screenshot_opts), path, opts)
end

child.get_screen_lines = function()
return screenshot.fromChildScreen(child)
end

-- Expect screenshot without the "attrs" (highlights)
child.expect_screen_lines = function(opts, path)
opts = opts or {}
opts.force = not not vim.env["update_screenshots"]
MiniTest.expect.reference_screenshot(child.get_screen_lines(), path, opts)
end

child.get_buf_lines = function(buf)
return screenshot.fromChildBufLines(child, buf)
end

child.expect_buflines = function(opts, path)
opts = opts or {}
opts.force = not not vim.env["update_screenshots"]
MiniTest.expect.reference_screenshot(child.get_buf_lines(), path, opts)
end

--- waits until condition fn evals to true, checking every interval ms
--- times out at timeout ms
---@param condition fun(): boolean
Expand All @@ -246,7 +268,8 @@ M.new_child_neovim = function()
end
end

error(string.format("Timed out waiting for condition after %d ms\n\n%s", max,
error(string.format("Timed out waiting for condition after %d ms\n\n%s\n\n%s", max,
tostring(child.cmd_capture("messages")),
tostring(child.get_screenshot())
))
end
Expand Down
84 changes: 84 additions & 0 deletions lua/fzf-lua/test/screenshot.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
-- Borrowed from grug-far.nvim
-- Used to compare screenshots without "attrs" (highlights)
local M = {}

---@class MiniTestScreenshot

--- copied over from mini.test
---@param t { text: string[], attr: string[] }
---@return MiniTestScreenshot
local function screenshot_new(t)
local process_screen = function(arr_2d)
local n_lines, n_cols = #arr_2d, #arr_2d[1]

-- Prepend lines with line number of the form `01|`
local n_digits = math.floor(math.log10(n_lines)) + 1
local format = string.format("%%0%dd|%%s", n_digits)
local lines = {}
for i = 1, n_lines do
table.insert(lines, string.format(format, i, table.concat(arr_2d[i])))
end

-- Make ruler
local prefix = string.rep("-", n_digits) .. "|"
local ruler = prefix .. ("---------|"):rep(math.ceil(0.1 * n_cols)):sub(1, n_cols)

return string.format("%s\n%s", ruler, table.concat(lines, "\n"))
end

return setmetatable(t, {
__tostring = function(x)
return string.format("%s\n\n%s", process_screen(x.text), process_screen(x.attr))
end,
})
end

---@param s string
---@return string[]
local function string_to_chars(s)
-- Can't use `vim.split(s, '')` because of multibyte characters
local res = {}
for i = 1, vim.fn.strchars(s) do
table.insert(res, vim.fn.strcharpart(s, i - 1, 1))
end
return res
end

--- gets a screenshot from given text lines and attrs
--- note that length of text lines and length of attrs must match
---@param text_lines string[]
---@param attr_lines? string[]
---@return MiniTestScreenshot
function M.from_lines(text_lines, attr_lines)
local attr_linez = attr_lines or {}
for _ = 1, #text_lines do
table.insert(attr_linez, " ")
end

local f = function(x)
return string_to_chars(x)
end
return screenshot_new({ text = vim.tbl_map(f, text_lines), attr = vim.tbl_map(f, attr_linez) })
end

function M.fromChildBufLines(child, buf)
local lines = child.api.nvim_buf_get_lines(buf or 0, 0, -1, true)
return M.from_lines(lines)
end

function M.fromChildScreen(child)
local lines = child.lua([[
local lines = {}
for i = 1, vim.o.lines do
local line_text = {}
for j = 1, vim.o.columns do
table.insert(line_text, vim.fn.screenstring(i, j))
end
table.insert(lines, table.concat(line_text))
end
return lines
]])
return M.from_lines(lines)
end

return M
38 changes: 31 additions & 7 deletions tests/file/ui_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,46 @@ T["files()"] = new_set()
T["files()"]["start and abort <esc>"] = new_set({
parametrize = { { "<esc>" }, { "<c-c>" } } }, {
function(key)
helpers.SKIP_IF_WIN()
-- Global vars
child.lua([[FzfLua.files()]])
-- sort output and remove cwd in prompt as will be different on CI
child.lua([[FzfLua.files({
previewer = false,
cwd_prompt = false,
cmd = "rg --files --sort=path",
})]])
eq(child.lua_get([[_G._fzf_lua_on_create]]), true)
child.wait_until(function()
return child.lua_get([[_G._fzf_load_called]]) == true
end)
helpers.SKIP_IF_NOT_STABLE()
-- Ignore the prompt path and info line since the CI machine
-- path will be different as well as the file count
child.expect_screenshot({ ignore_lines = { 4 } })
-- Ignore last "-- TERMINAL --" line and paths on Windows (separator is "\")
local ignore_lines = { 28 }
if helpers.IS_WIN() then
for i = 12, 21 do table.insert(ignore_lines, i) end
end
-- NOTE: we compare screen lines without "attrs", this way
-- we can test on stable, nightly and windows
-- child.expect_screenshot({ ignore_lines = ignore_lines })
child.expect_screen_lines({ ignore_lines = ignore_lines })
child.type_keys(key)
child.wait_until(function()
return child.lua_get([[_G._fzf_lua_on_create]]) == vim.NIL
end)
end,
})

T["files()"]["defaults with icons"] = function()
helpers.SKIP_IF_WIN()
helpers.SKIP_IF_NOT_STABLE()
-- sort output and remove cwd in prompt as will be different on CI
child.lua([[FzfLua.files({ cwd_prompt = false, cmd = "rg --files --sort=path" })]])
eq(child.lua_get([[_G._fzf_lua_on_create]]), true)
child.wait_until(function()
return child.lua_get([[_G._fzf_load_called]]) == true
end)
child.expect_screenshot({ ignore_lines = { 28 } })
child.type_keys("<c-c>")
child.wait_until(function()
return child.lua_get([[_G._fzf_lua_on_create]]) == vim.NIL
end)
end

return T
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--|---------|---------|---------|---------|---------|---------|----
01|
02|~
03|~ ╭───────────────────── Files ─────────────────────╮
04|~ │> 88/88 (0) │
05|~ │──────────────────────────────────────────────── │
06|~ │ :: <ctrl-g> to Disable .gitignore │
07|~ │▌ LICENSE ││
08|~ │ Makefile │
09|~ │ OPTIONS.md │
10|~ │ README-Win.md │
11|~ │ README.md │
12|~ │ autoload/fzf_lua.vim │
13|~ ╰─────────────────────────────────────────────────╯
14|~ ╭──────────────────── LICENSE ────────────────────╮
15|~ │ 1 MIT License
16|~ │ 2
17|~ │ 3 Copyright (c) 2021 iBhagwan
18|~ │ 4 │
19|~ │ 5 Permission is hereby granted, free of charge,│
20|~ │ 6 of this software and associated documentation│
21|~ │ 7 in the Software without restriction, includin│
22|~ ╰─────────────────────────────────────────────────╯
23|~
24|[No Name] 0,0-1 All
25|
26|
27|
28|-- TERMINAL -- 1,3 All

--|---------|---------|---------|---------|---------|---------|----
01|0000000000000000000000000000000000000000000000000000000000000000
02|1111111111111111111111111111111111111111111111111111111111111111
03|1111111222222222222222222222222222222222222222222222222222111111
04|1111111234522222222222222222222222222222222222266666666622111111
05|1111111277777777777777777777777777777777777777777777777722111111
06|1111111222888888888888888888888888888888888222222222222222111111
07|111111129:;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;72111111
08|11111112:2222222222222222222222222222222222222222222222222111111
09|11111112:2222222222222222222222222222222222222222222222222111111
10|11111112:2222222222222222222222222222222222222222222222222111111
11|11111112:2222222222222222222222222222222222222222222222222111111
12|11111112:2222222222222222222222222222222222222222222222222111111
13|1111111222222222222222222222222222222222222222222222222222111111
14|1111111222222222222222222222222222222222222222222222222222111111
15|11111112<<<<222222222222222222222222222222222222222222222=111111
16|11111112<<<<222222222222222222222222222222222222222222222=111111
17|11111112<<<<222222222222222222222222222222222222222222222=111111
18|11111112<<<<2222222222222222222222222222222222222222222222111111
19|11111112<<<<2222222222222222222222222222222222222222222222111111
20|11111112<<<<2222222222222222222222222222222222222222222222111111
21|11111112<<<<2222222222222222222222222222222222222222222222111111
22|1111111222222222222222222222222222222222222222222222222222111111
23|1111111111111111111111111111111111111111111111111111111111111111
24|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
25|????????????????????????????????????????????????????????????????
26|????????????????????????????????????????????????????????????????
27|????????????????????????????????????????????????????????????????
28|@@@@@@@@@@@@@@??????????????????????????????????????????????????
Loading

0 comments on commit e68d21b

Please sign in to comment.