Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional tests #64

Merged
merged 8 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
env:
TRIPTYCH_DIR: .local/share/nvim/site/pack/simonmclean/start/triptych
LUA_LS_LOG_PATH: /home/runner/lua-language-server/logs
LATEST_NVIM_VERSION: "0.10.0"
LATEST_NVIM_VERSION: "0.10.1"

on:
push:
Expand Down Expand Up @@ -65,8 +65,8 @@ jobs:
- name: run unit tests against neovim 0.9.0
run: |
cd $HOME/$TRIPTYCH_DIR
$HOME/nvim-linux64-0.9.0/bin/nvim --headless -c "PlenaryBustedDirectory unit_tests/"
HEADLESS=true $HOME/nvim-linux64-0.9.0/bin/nvim --headless +"so%" tests/run_specs.lua
- name: run unit tests against latest stable neovim
run: |
cd $HOME/$TRIPTYCH_DIR
$HOME/nvim-linux64-$LATEST_NVIM_VERSION/bin/nvim --headless -c "PlenaryBustedDirectory unit_tests/"
HEADLESS=true $HOME/nvim-linux64-$LATEST_NVIM_VERSION/bin/nvim --headless +"so%" tests/run_specs.lua
72 changes: 50 additions & 22 deletions lua/triptych/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local view = require 'triptych.view'
local plenary_path = require 'plenary.path'
local triptych_help = require 'triptych.help'
local autocmds = require 'triptych.autocmds'
local log = require 'triptych.logger'

local Actions = {}

Expand Down Expand Up @@ -44,6 +45,49 @@ local function rename_node_and_publish(from, to)
end
end

---Wraps plenary Path:copy with public events
---Note: It only publishes events for files, not folders. This is probably fine for LSP purposes
---@param target PathDetails
---@param destination string
---@return nil
local function duplicate_node_and_publish(target, destination)
log.debug('duplicate_file_or_dir', { target = target, destination = destination })
if not target.is_dir then
autocmds.publish_will_create_node(destination)
end

local p = plenary_path:new(target.path)
-- Note: Plenary has a bug whereby a copying a directory into itself creates hundreds of nested copies
-- https://github.com/nvim-lua/plenary.nvim/pull/358
local results = p:copy {
destination = destination,
recursive = true,
override = false,
interactive = true,
}

local files_created = {}

local function handle_results(results)
for key, value in pairs(results) do
if type(value) == 'table' then
handle_results(value)
elseif value then
table.insert(files_created, key.filename)
end
end
end

handle_results(results)

-- Sorting to avoid flakey test
table.sort(files_created)

for _, path in ipairs(files_created) do
autocmds.publish_did_create_node(path)
end
end

--- TODO: Return type
---@param State TriptychState
---@param refresh_view fun(): nil
Expand Down Expand Up @@ -188,25 +232,6 @@ function Actions.new(State, refresh_view)
refresh_view()
end

---@param target PathDetails
---@param destination string
---@param callback? fun(boolean) - Callback indicting whether an item an copied
---@return nil
M.duplicate_file_or_dir = function(target, destination, callback)
local p = plenary_path:new(target.path)
local results = p:copy {
destination = destination,
recursive = true,
override = false,
interactive = true,
}
if callback then
for _, v in pairs(results) do
callback(v)
end
end
end

M.bulk_toggle_copy = function()
local targets = view.get_targets_in_selection(State)
local contains_copy_items = false
Expand Down Expand Up @@ -277,9 +302,9 @@ function Actions.new(State, refresh_view)
return get_copy_path(target_path, i + 1)
end

-- TODO: This function is a mess. Maybe break out into handlers for copy and cut?
---@return nil
M.paste = function()
-- TODO: This function is a mess. Maybe break out into handlers for copy and cut?
local cursor_target = view.get_target_under_cursor(State)
local destination_dir = u.eval(function()
if not cursor_target then
Expand All @@ -305,8 +330,11 @@ function Actions.new(State, refresh_view)
-- Handle copy items
for _, item in ipairs(State.copy_list) do
local destination = get_copy_path(u.path_join(destination_dir, item.display_name))
M.duplicate_file_or_dir(item, destination)
autocmds.publish_did_delete_node(destination)
if item.is_dir then
-- Strip trailing slash
destination = string.sub(destination, 1, #destination - 1)
end
duplicate_node_and_publish(item, destination)
end

view.jump_cursor_to(State, destination_dir)
Expand Down
1 change: 0 additions & 1 deletion lua/triptych/types.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---@class TriptychConfig
---@field debug boolean
---@field config boolean
---@field mappings TriptychConfigMappings
---@field extension_mappings { [string]: ExtensionMapping }
---@field options TriptychConfigOptions
Expand Down
20 changes: 11 additions & 9 deletions test_framework/queue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ end
function TestQueue:cleanup()
self.is_running = false
for _, test in ipairs(self.queue) do
test:cleanup()
test:reset()
test.result = nil
end
for _, test in ipairs(self.completed) do
test:cleanup()
test:reset()
test.result = nil
end
self.queue = {}
Expand Down Expand Up @@ -104,13 +104,15 @@ end
---@param fail_message? string
function TestQueue:handle_test_fail(test, fail_message)
test.result = 'failed'
u.print('[FAILED] ' .. test.name)
error(fail_message)
self:cleanup()
if u.is_headless() then
u.exit_status_code 'failed'
else
end
vim.schedule(function()
u.print('[FAILED] ' .. test.name)
error(fail_message)
test:reset()
self:cleanup()
if u.is_headless() then
u.exit_status_code 'failed'
end
end)
end

function TestQueue:run_next()
Expand Down
19 changes: 11 additions & 8 deletions test_framework/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,31 @@ end
---Run an asyncronous test
---@param callback fun(passed: boolean, fail_message?: string)
function Test:run_async(callback)
local success, err = pcall(self.test_body_async, function(test_callback)
local success, err = pcall(self.test_body_async, function(test_finish)
if self.has_run then
error 'Attempted to invoke test completion more than once. Check that any async callbacks in the test are not firing multiple times.'
end

-- Scheduling this allows cleanup to complete before running the next test
local scheduled_callback = vim.schedule_wrap(callback)

self.has_run = true

if test_callback.cleanup then
local cleanup_successful, cleanup_err = pcall(test_callback.cleanup)
if test_finish.cleanup then
local cleanup_successful, cleanup_err = pcall(test_finish.cleanup)
if not cleanup_successful then
error(cleanup_err)
end
end

if self.is_timed_out then
callback(false, 'Timed out')
scheduled_callback(false, 'Timed out')
else
local passed, fail_message = pcall(test_callback.assertions)
local passed, fail_message = pcall(test_finish.assertions)
if passed then
callback(true, nil)
scheduled_callback(true, nil)
else
callback(false, fail_message)
scheduled_callback(false, fail_message)
end
end
end)
Expand Down Expand Up @@ -123,7 +126,7 @@ function Test:only()
return self
end

function Test:cleanup()
function Test:reset()
self.has_run = false
self.result = nil
end
Expand Down
File renamed without changes.
Loading