Skip to content

Commit

Permalink
refactor: remove all uses of plenary.nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Oct 29, 2023
1 parent dbb5017 commit e0d94ce
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 72 deletions.
16 changes: 16 additions & 0 deletions lua/neotest-haskell/compat.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---@diagnostic disable: deprecated, duplicate-doc-field
---@mod neotest-haskell.compat Functions for backward compatibility with older Neovim versions
---@brief [[

---WARNING: This is not part of the public API.
---Breaking changes to this module will not be reflected in the semantic versioning of this plugin.

---@brief ]]

local compat = {}

compat.joinpath = vim.fs.joinpath or function(...)
return (table.concat({ ... }, '/'):gsub('//+', '/'))
end

return compat
8 changes: 0 additions & 8 deletions lua/neotest-haskell/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ local lua_dependencies = {
url = '[nvim-neotest/neotest](https://github.com/nvim-neotest/neotest)',
info = '',
},
{
module = 'plenary',
optional = function()
return false
end,
url = '[nvim-lua/plenary.nvim](https://github.com/nvim-lua/plenary.nvim)',
info = '',
},
}

---@class ExternalDependency
Expand Down
12 changes: 6 additions & 6 deletions lua/neotest-haskell/runner.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
local compat = require('neotest-haskell.compat')
local runner = {}

---Check if the given directory contains a file matching a list of patterns.
---@param directory string The directory to check for.
---@param patterns string[] The patterns to check for.
---@return boolean
local function directory_contains_file_matching(directory, patterns)
local Path = require('plenary.path')
for _, pattern in ipairs(patterns) do
for _, file in ipairs(vim.fn.glob(Path:new(directory, pattern).filename, true, true)) do
if Path:new(file):exists() then
for _, file in ipairs(vim.fn.glob(compat.joinpath(directory, pattern), true, true)) do
if vim.fn.filereadable(file) == 1 then
return true
end
end
Expand All @@ -26,9 +26,9 @@ local function get_package_name(package_root)
if not ok then
nio = require('neotest.async')
end
local Path = require('plenary.path')
---@diagnostic disable-next-line -- nio.fn is private?
for _, package_file_path in ipairs(nio.fn.glob(Path:new(package_root, '*.cabal').filename, true, true)) do
---@diagnostic disable-next-line private -- nio.fn is private?
local glob = nio.fn.glob
for _, package_file_path in ipairs(glob(compat.joinpath(package_root, '*.cabal'), true, true)) do
local package_file_name = package_file_path and vim.fn.fnamemodify(package_file_path, ':t')
local package_name = package_file_name and package_file_name:gsub('.cabal', '')
if package_name then
Expand Down
31 changes: 17 additions & 14 deletions tests/hspec_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local Path = require('plenary.path')

local compat = require('neotest-haskell.compat')
local hspec = require('neotest-haskell.hspec')
local async = require('nio').tests

Expand All @@ -26,8 +25,8 @@ local parse_positions = hspec.parse_positions
describe('hspec', function()
describe('parse positions', function()
async.it('unqualified imports 0', function()
local test_file = Path:new(test_cwd .. '/fixtures/hspec/cabal/simple/test/FirstSpec.hs')
local filename = test_file.filename
local test_file = compat.joinpath(test_cwd, 'fixtures/hspec/cabal/simple/test/FirstSpec.hs')
local filename = test_file
local result = parse_positions(filename)
local file_pos_id = filename
assert_has_position(result, file_pos_id)
Expand All @@ -41,8 +40,9 @@ describe('hspec', function()
end)
end)
async.it('unqualified imports 1', function()
local test_file = Path:new(test_cwd .. '/fixtures/hspec/cabal/multi-package/subpackage1/test/Fix1/FixtureSpec.hs')
local filename = test_file.filename
local test_file =
compat.joinpath(test_cwd, 'fixtures/hspec/cabal/multi-package/subpackage1/test/Fix1/FixtureSpec.hs')
local filename = test_file
local result = parse_positions(filename)
local filename_pos_id = filename
assert_has_position(result, filename_pos_id)
Expand All @@ -59,8 +59,9 @@ describe('hspec', function()
assert_has_position(result, ns_3_pos_id .. '::"implicitly skipped it"')
end)
async.it('unqualified imports 2', function()
local test_file = Path:new(test_cwd .. '/fixtures/hspec/stack/multi-package/subpackage1/test/Fix1/FixtureSpec.hs')
local filename = test_file.filename
local test_file =
compat.joinpath(test_cwd, 'fixtures/hspec/stack/multi-package/subpackage1/test/Fix1/FixtureSpec.hs')
local filename = test_file
local result = parse_positions(filename)
local filename_pos_id = filename
assert_has_position(result, filename_pos_id)
Expand All @@ -78,8 +79,9 @@ describe('hspec', function()
assert_has_position(result, ns_4_pos_id .. '::"Returns the empty list"')
end)
async.it('qualified imports', function()
local test_file = Path:new(test_cwd .. '/fixtures/hspec/cabal/multi-package/subpackage2/test/Fix2/FixtureSpec.hs')
local filename = test_file.filename
local test_file =
compat.joinpath(test_cwd, 'fixtures/hspec/cabal/multi-package/subpackage2/test/Fix2/FixtureSpec.hs')
local filename = test_file
local result = parse_positions(filename)
local filename_pos_id = filename
assert_has_position(result, filename_pos_id)
Expand All @@ -98,11 +100,12 @@ describe('hspec', function()

describe('parse results', function()
async.it('test failure', function()
local test_file = Path:new(test_cwd .. '/fixtures/hspec/stack/multi-package/subpackage1/test/Fix1/FixtureSpec.hs')
local filename = test_file.filename
local test_file =
compat.joinpath(test_cwd, 'fixtures/hspec/stack/multi-package/subpackage1/test/Fix1/FixtureSpec.hs')
local filename = test_file
local tree = parse_positions(filename)
local test_result_file = Path:new(test_cwd .. '/fixtures/results/hspec_test_file_fail.txt')
local result_filename = test_result_file.filename
local test_result_file = compat.joinpath(test_cwd, 'fixtures/results/hspec_test_file_fail.txt')
local result_filename = test_result_file
local context = {
file = filename,
pos_id = filename,
Expand Down
55 changes: 24 additions & 31 deletions tests/runner_spec.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
local runner = require('neotest-haskell.runner')
local Path = require('plenary.path')
local async = require('nio').tests

local simple_cabal_hspec_test_file = Path:new('tests/fixtures/hspec/cabal/simple/test/FirstSpec.hs')
local simple_cabal_hspec_test_file = 'tests/fixtures/hspec/cabal/simple/test/FirstSpec.hs'
local multi_package_cabal_hspec_test_file =
Path:new('tests/fixtures/hspec/cabal/multi-package/subpackage1/test/Fix1/FixtureSpec.hs')
local simple_stack_hspec_test_file = Path:new('tests/fixtures/hspec/stack/simple/test/FirstSpec.hs')
'tests/fixtures/hspec/cabal/multi-package/subpackage1/test/Fix1/FixtureSpec.hs'
local simple_stack_hspec_test_file = 'tests/fixtures/hspec/stack/simple/test/FirstSpec.hs'
local simple_stack_hspec_test_file_only_package_yaml =
Path:new('tests/fixtures/hspec/stack/simple-package-yaml/test/FirstSpec.hs')
'tests/fixtures/hspec/stack/simple-package-yaml/test/FirstSpec.hs'
local multi_package_stack_hspec_test_file =
Path:new('tests/fixtures/hspec/stack/multi-package/subpackage1/test/Fix1/FixtureSpec.hs')
local multi_package_cabal_tasty_test_file = Path:new('tests/fixtures/tasty/cabal/multi-package/tasty-pkg/test/Spec.hs')
local simple_cabal_sydtest_test_file = Path:new('tests/fixtures/sydtest/cabal/simple/test/SydtestFixtureSpec.hs')
'tests/fixtures/hspec/stack/multi-package/subpackage1/test/Fix1/FixtureSpec.hs'
local multi_package_cabal_tasty_test_file = 'tests/fixtures/tasty/cabal/multi-package/tasty-pkg/test/Spec.hs'
local simple_cabal_sydtest_test_file = 'tests/fixtures/sydtest/cabal/simple/test/SydtestFixtureSpec.hs'

local hspec = require('neotest-haskell.hspec')
local tasty = require('neotest-haskell.tasty')
Expand All @@ -22,31 +21,28 @@ describe('runner', function()
async.it('selects hspec for hspec file', function()
assert.equals(
hspec,
runner.select_framework(multi_package_cabal_hspec_test_file.filename, { 'sydtest', 'tasty', 'hspec' })
runner.select_framework(multi_package_cabal_hspec_test_file, { 'sydtest', 'tasty', 'hspec' })
)
end)
async.it('selects tasty for tasty file if tasty is specified before hspec', function()
assert.equals(
tasty,
runner.select_framework(multi_package_cabal_tasty_test_file.filename, { 'sydtest', 'tasty', 'hspec' })
runner.select_framework(multi_package_cabal_tasty_test_file, { 'sydtest', 'tasty', 'hspec' })
)
end)
async.it('selects sydtest for sydtest file if sydtest is specified before hspec', function()
assert.equals(
sydtest,
runner.select_framework(simple_cabal_sydtest_test_file.filename, { 'sydtest', 'tasty', 'hspec' })
)
assert.equals(sydtest, runner.select_framework(simple_cabal_sydtest_test_file, { 'sydtest', 'tasty', 'hspec' }))
end)
async.it('errors for hspec file if hspec is not specified', function()
assert.errors(function()
runner.select_framework(multi_package_cabal_hspec_test_file.filename, { 'tasty' })
runner.select_framework(multi_package_cabal_hspec_test_file, { 'tasty' })
end)
end)
async.it('can detect framework by qualified module name', function()
assert.equals(
hspec,
runner.select_framework(
multi_package_cabal_hspec_test_file.filename,
multi_package_cabal_hspec_test_file,
{ { framework = 'hspec', modules = { 'Fix1.FixtureSpec' } } }
)
)
Expand All @@ -56,50 +52,49 @@ describe('runner', function()
describe('select_build_tool', function()
describe('simple project without stack.yaml', function()
it('uses cabal if it is in the list of build tools', function()
local mk_command = runner.select_build_tool(hspec, simple_cabal_hspec_test_file.filename, { 'stack', 'cabal' })
local mk_command = runner.select_build_tool(hspec, simple_cabal_hspec_test_file, { 'stack', 'cabal' })
local command = mk_command()
assert.equals(command[1], 'cabal')
end)
it('throws if only stack is specified', function()
assert.errors(function()
runner.select_build_tool(hspec, simple_cabal_hspec_test_file.filename, { 'stack' })
runner.select_build_tool(hspec, simple_cabal_hspec_test_file, { 'stack' })
end)
end)
it('throws if no build tool is specified', function()
assert.errors(function()
runner.select_build_tool(hspec, simple_cabal_hspec_test_file.filename, {})
runner.select_build_tool(hspec, simple_cabal_hspec_test_file, {})
end)
end)
end)

describe('multi-package project without stack.yaml', function()
it('uses cabal if it is in the list of build tools', function()
local mk_command =
runner.select_build_tool(hspec, multi_package_cabal_hspec_test_file.filename, { 'stack', 'cabal' })
local mk_command = runner.select_build_tool(hspec, multi_package_cabal_hspec_test_file, { 'stack', 'cabal' })
local command = mk_command()
assert.equals(command[1], 'cabal')
assert.equals(command[3], 'subpackage1')
end)
it('throws if only stack is specified', function()
assert.errors(function()
runner.select_build_tool(hspec, multi_package_cabal_hspec_test_file.filename, { 'stack' })
runner.select_build_tool(hspec, multi_package_cabal_hspec_test_file, { 'stack' })
end)
end)
it('throws if no build tool is specified', function()
assert.errors(function()
runner.select_build_tool(hspec, multi_package_cabal_hspec_test_file.filename, {})
runner.select_build_tool(hspec, multi_package_cabal_hspec_test_file, {})
end)
end)
end)

describe('simple project with stack.yaml', function()
it('uses stack if it is in the list of build tools before cabal', function()
local mk_command = runner.select_build_tool(hspec, simple_stack_hspec_test_file.filename, { 'stack', 'cabal' })
local mk_command = runner.select_build_tool(hspec, simple_stack_hspec_test_file, { 'stack', 'cabal' })
local command = mk_command()
assert.equals(command[1], 'stack')
end)
it('uses stack if it is the only build tool', function()
local mk_command = runner.select_build_tool(hspec, simple_stack_hspec_test_file.filename, { 'stack' })
local mk_command = runner.select_build_tool(hspec, simple_stack_hspec_test_file, { 'stack' })
local command = mk_command()
assert.equals(command[1], 'stack')
end)
Expand All @@ -113,13 +108,12 @@ describe('runner', function()
describe('simple project with stack.yaml, package.yaml and no *.cabal', function()
it('uses stack if it is in the list of build tools before cabal', function()
local mk_command =
runner.select_build_tool(hspec, simple_stack_hspec_test_file_only_package_yaml.filename, { 'stack', 'cabal' })
runner.select_build_tool(hspec, simple_stack_hspec_test_file_only_package_yaml, { 'stack', 'cabal' })
local command = mk_command()
assert.equals(command[1], 'stack')
end)
it('uses stack if it is the only build tool', function()
local mk_command =
runner.select_build_tool(hspec, simple_stack_hspec_test_file_only_package_yaml.filename, { 'stack' })
local mk_command = runner.select_build_tool(hspec, simple_stack_hspec_test_file_only_package_yaml, { 'stack' })
local command = mk_command()
assert.equals(command[1], 'stack')
end)
Expand All @@ -132,14 +126,13 @@ describe('runner', function()

describe('multi-package project with stack.yaml', function()
it('uses stack if it is in the list of build tools before cabal', function()
local mk_command =
runner.select_build_tool(hspec, multi_package_stack_hspec_test_file.filename, { 'stack', 'cabal' })
local mk_command = runner.select_build_tool(hspec, multi_package_stack_hspec_test_file, { 'stack', 'cabal' })
local command = mk_command()
assert.equals(command[1], 'stack')
assert.equals(command[3], 'subpackage1')
end)
it('uses stack if it is the only build tool', function()
local mk_command = runner.select_build_tool(hspec, multi_package_stack_hspec_test_file.filename, { 'stack' })
local mk_command = runner.select_build_tool(hspec, multi_package_stack_hspec_test_file, { 'stack' })
local command = mk_command()
assert.equals(command[1], 'stack')
end)
Expand Down
13 changes: 6 additions & 7 deletions tests/sydtest_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local Path = require('plenary.path')

local sydtest = require('neotest-haskell.sydtest')
local compat = require('neotest-haskell.compat')
local async = require('nio').tests

local test_cwd = os.getenv('TEST_CWD')
Expand All @@ -24,9 +23,9 @@ end
local parse_positions = sydtest.parse_positions

describe('sydtest', function()
local test_file = Path:new(test_cwd .. '/fixtures/sydtest/cabal/simple/test/SydtestFixtureSpec.hs')
local test_file = compat.joinpath(test_cwd, 'fixtures/sydtest/cabal/simple/test/SydtestFixtureSpec.hs')
async.it('parse positions', function()
local filename = test_file.filename
local filename = test_file
local result = parse_positions(filename)
local file_pos_id = filename
assert_has_position(result, file_pos_id)
Expand All @@ -45,10 +44,10 @@ describe('sydtest', function()
end)
describe('parse results', function()
async.it('test failure', function()
local filename = test_file.filename
local filename = test_file
local tree = parse_positions(filename)
local test_result_file = Path:new(test_cwd .. '/fixtures/sydtest/results/failure.txt')
local result_filename = test_result_file.filename
local test_result_file = compat.joinpath(test_cwd, 'fixtures/sydtest/results/failure.txt')
local result_filename = test_result_file
local context = {
file = filename,
pos_id = filename,
Expand Down
11 changes: 5 additions & 6 deletions tests/tasty_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local Path = require('plenary.path')

local tasty = require('neotest-haskell.tasty')
local compat = require('neotest-haskell.compat')
local has_position = require('neotest-haskell.position').has_position
local async = require('nio').tests

Expand All @@ -12,8 +11,8 @@ end

local parse_positions = tasty.parse_positions

local test_file = Path:new(test_cwd .. '/fixtures/tasty/cabal/multi-package/tasty-pkg/test/Spec.hs')
local test_filename = test_file.filename
local test_file = compat.joinpath(test_cwd, 'fixtures/tasty/cabal/multi-package/tasty-pkg/test/Spec.hs')
local test_filename = test_file

describe('tasty', function()
async.it('parse positions', function()
Expand Down Expand Up @@ -67,8 +66,8 @@ describe('tasty', function()
describe('parse results', function()
async.it('test failure', function()
local tree = parse_positions(test_filename)
local test_result_file = Path:new(test_cwd .. '/fixtures/results/tasty_test_file_fail.txt')
local result_filename = test_result_file.filename
local test_result_file = compat.joinpath(test_cwd, 'fixtures/results/tasty_test_file_fail.txt')
local result_filename = test_result_file
local context = {
file = test_filename,
pos_id = test_filename,
Expand Down

0 comments on commit e0d94ce

Please sign in to comment.