Skip to content

Commit a659718

Browse files
authored
test: Improve minimal init, test launching (#616)
1 parent 2b2f7a3 commit a659718

File tree

7 files changed

+127
-40
lines changed

7 files changed

+127
-40
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ jobs:
2626
token: ${{ secrets.GITHUB_TOKEN }}
2727
# CLI arguments
2828
args: --check lua/ tests/
29-
- name: Install Plenary
30-
uses: actions/checkout@v2
31-
with:
32-
repository: nvim-lua/plenary.nvim
33-
path: plenary.nvim
34-
- name: Install Treesitter
35-
uses: actions/checkout@v2
36-
with:
37-
repository: nvim-treesitter/nvim-treesitter
38-
path: nvim-treesitter
3929
- name: Install Neovim
4030
uses: rhysd/action-setup-vim@v1
4131
id: neovim

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,5 @@ luac.out
3939
*.x86_64
4040
*.hex
4141

42-
plenary.nvim/
43-
nvim-treesitter/
44-
mini.nvim/
42+
# Directory containing testing dependencies downloaded during test run
43+
.deps/

CONTRIBUTING.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ Please document any new code you add with [emmylua annotations](https://emmylua.
2525

2626
### Tests
2727

28-
To run tests, [plenary.nvim](https://github.com/nvim-lua/plenary.nvim) and [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) must be present in the nvim-orgmode directory:
28+
To run tests run `make test` in the nvim-orgmode directory:
2929
```
30-
git clone https://github.com/nvim-treesitter/nvim-treesitter
31-
git clone https://github.com/nvim-lua/plenary.nvim
3230
make test
3331
```
3432

33+
To run a specific test you can set a `FILE` environment variable to a specific
34+
spec you want to test. Example:
35+
```
36+
make test FILE=./tests/plenary/api/api_spec.lua
37+
```
38+
3539
### Formatting
3640

3741
Formatting is done via [StyLua](https://github.com/JohnnyMorganz/StyLua). To format everything run:

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
clean:
2+
nvim --headless --clean -n -c "lua vim.fn.delete('./tests/.deps', 'rf')" +q
13
test:
2-
nvim --headless --clean -u tests/minimal_init.vim -c "TSUpdateSync org" -c "PlenaryBustedDirectory tests/plenary/ {minimal_init = 'tests/minimal_init.vim'}"
3-
testfile:
4-
nvim --headless --clean -u tests/minimal_init.vim -c "TSUpdateSync org" -c "PlenaryBustedDirectory $(FILE) {minimal_init = 'tests/minimal_init.vim'}"
4+
nvim --headless --clean -u tests/test.lua "$(FILE)"
55
docs:
66
md2vim -desc "*orgmode* *orgmode.nvim*\n* NOTE: This file is autogenerated from DOCS.md file" DOCS.md doc/orgmode.txt
77
api_docs:

tests/minimal_init.lua

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
local M = {}
2+
---
3+
---@class MinPlugin A plugin to download and register on the package path
4+
---@alias PluginName string The plugin name, will be used as part of the git clone destination
5+
---@alias PluginUrl string The git url at which a plugin is located, can be a path. See https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols for details
6+
---@alias MinPlugins table<PluginName, PluginUrl>
7+
8+
-- Gets the current directory of this file
9+
local base_root_path = vim.fn.fnamemodify(debug.getinfo(1, 'S').source:sub(2), ':p:h')
10+
---Gets the root directory of the minimal init and if path is specified appends the given path to the root allowing for
11+
---subdirectories within the current cwd
12+
---@param path string? The additional path to append to the root, not required
13+
---@return string root The root path suffixed with the path provided or an empty suffix if none was given
14+
function M.root(path)
15+
return base_root_path .. '/.deps/' .. (path or '')
16+
end
17+
18+
---Downloads a plugin from a given url and registers it on the 'runtimepath'
19+
---@param plugin_name PluginName
20+
---@param plugin_url PluginUrl
21+
function M.load_plugin(plugin_name, plugin_url)
22+
local package_root = M.root('plugins/')
23+
local install_destination = package_root .. plugin_name
24+
vim.opt.runtimepath:append(install_destination)
25+
26+
if not vim.loop.fs_stat(package_root) then
27+
vim.fn.mkdir(package_root, 'p')
28+
end
29+
30+
-- If the plugin install path already exists, we don't need to clone it again.
31+
if not vim.loop.fs_stat(install_destination) then
32+
print(string.format('>> Downloading plugin "%s" to "%s"', plugin_name, install_destination))
33+
vim.fn.system({
34+
'git',
35+
'clone',
36+
'--depth=1',
37+
plugin_url,
38+
install_destination,
39+
})
40+
if vim.v.shell_error > 0 then
41+
error(
42+
string.format('>> Failed to clone plugin: "%s" to "%s"!', plugin_name, install_destination),
43+
vim.log.levels.ERROR
44+
)
45+
end
46+
end
47+
end
48+
49+
---Do the initial setup. Downloads plugins, ensures the minimal init does not pollute the filesystem by keeping
50+
---everything self contained to the CWD of the minimal init file. Run prior to running tests, reproducing issues, etc.
51+
---@param plugins? MinPlugins
52+
function M.setup(plugins)
53+
vim.opt.packpath = {} -- Empty the package path so we use only the plugins specified
54+
vim.opt.runtimepath:append(M.root('.min')) -- Ensure the runtime detects the root min dir
55+
56+
-- Install required plugins
57+
if plugins ~= nil then
58+
for plugin_name, plugin_url in pairs(plugins) do
59+
M.load_plugin(plugin_name, plugin_url)
60+
end
61+
end
62+
63+
vim.env.XDG_CONFIG_HOME = M.root('xdg/config')
64+
vim.env.XDG_DATA_HOME = M.root('xdg/data')
65+
vim.env.XDG_STATE_HOME = M.root('xdg/state')
66+
vim.env.XDG_CACHE_HOME = M.root('xdg/cache')
67+
68+
-- NOTE: Cleanup the xdg cache on exit so new runs of the minimal init doesn't share any previous state, e.g. shada
69+
vim.api.nvim_create_autocmd('VimLeave', {
70+
callback = function()
71+
vim.fn.delete(M.root('xdg'), 'rf')
72+
end,
73+
})
74+
end
75+
76+
M.setup({
77+
plenary = 'https://github.com/nvim-lua/plenary.nvim.git',
78+
treesitter = 'https://github.com/nvim-treesitter/nvim-treesitter',
79+
})
80+
-- WARN: Do all plugin setup, test runs, reproductions, etc. AFTER calling setup with a list of plugins!
81+
-- Basically, do all that stuff AFTER this line.
82+
83+
--## Set proper settings ##
84+
-- Register Orgmode on the runtimepath, base_root_path is the directory where this file exists
85+
vim.opt.runtimepath:prepend(vim.fn.fnamemodify(base_root_path, ':h'))
86+
vim.opt.termguicolors = true
87+
vim.opt.swapfile = false
88+
vim.cmd.language('en_US.utf-8')
89+
vim.env.TZ = 'Europe/London'
90+
vim.g.mapleader = ','
91+
92+
require('orgmode').setup_ts_grammar()
93+
require('nvim-treesitter.configs').setup({
94+
ensure_installed = { 'org' },
95+
sync_install = true,
96+
})
97+
98+
require('orgmode').setup({
99+
org_agenda_files = { base_root_path .. '/plenary/fixtures/*' },
100+
org_default_notes_file = base_root_path .. '/plenary/fixtures/refile.org',
101+
})

tests/minimal_init.vim

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/test.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require('tests.minimal_init')
2+
---@type string
3+
local test_file = vim.v.argv[#vim.v.argv]
4+
if test_file == '' or not test_file:find('tests/plenary/', nil, true) then
5+
test_file = 'tests/plenary'
6+
else
7+
print('Individual Test File/Directory provided: ' .. test_file)
8+
print('Running all tests at ' .. test_file)
9+
end
10+
11+
require('plenary.test_harness').test_directory(test_file, {
12+
minimal_init = 'tests/minimal_init.lua',
13+
sequential = true,
14+
})

0 commit comments

Comments
 (0)