Skip to content

Commit

Permalink
test: add fully functional testing suite and basic core.dirman tests (
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro authored Jun 13, 2024
1 parent b10d681 commit ebf75f5
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 30 deletions.
1 change: 1 addition & 0 deletions .busted
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ return {
coverage = false,
lpath = "lua/?.lua;lua/?/init.lua",
pattern = "tests%.lua$",
lua = "nlua",
ROOT = { "lua/" },
},
default = {
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/luarocks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ jobs:
LUAROCKS_API_KEY: ${{ secrets.LUAROCKS_API_KEY }}
with:
version: ${{ env.LUAROCKS_VERSION }}
test_interpreters: |
neovim-stable
test_interpreters: ""
dependencies: |
nvim-nio ~> 1.7
lua-utils.nvim == 1.0.2
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Run tests
on:
pull_request: ~
push:
branches:
- main

jobs:
build:
name: Run tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Run tests
uses: nvim-neorocks/nvim-busted-action@v1
with:
nvim_version: stable
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ shell:

integration-test:
nix run ".#integration-test"

test:
LUA_PATH="$(shell luarocks path --lr-path --lua-version 5.1 --local)" \
LUA_CPATH="$(shell luarocks path --lr-cpath --lua-version 5.1 --local)" \
luarocks test --local --lua-version 5.1
2 changes: 1 addition & 1 deletion lua/neorg/modules/core/dirman/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ module.public = {
return module.private.current_workspace
end,
--- Sets the workspace to the one specified (if it exists) and broadcasts the workspace_changed event
---@return boolean True if the workspace is set correctly, false otherwise
---@param ws_name string #The name of a valid namespace we want to switch to
---@return boolean #True if the workspace is set correctly, false otherwise
set_workspace = function(ws_name)
-- Grab the workspace location
local workspace = module.config.public.workspaces[ws_name]
Expand Down
41 changes: 41 additions & 0 deletions lua/neorg/modules/core/dirman/tests.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local tests = require("neorg.tests")
local Path = require("pathlib")

describe("core.dirman tests", function()
local dirman = tests
.neorg_with("core.dirman", {
workspaces = {
test = "./test-workspace",
},
}).modules
.get_module("core.dirman")

describe("workspace-related functions", function()
it("properly expands workspace paths", function()
assert.same(dirman.get_workspaces(), {
default = Path.cwd(),
test = Path.cwd() / "test-workspace",
})
end)

it("properly sets and retrieves workspaces", function()
assert.is_true(dirman.set_workspace("test"))

assert.equal(dirman.get_current_workspace()[1], "test")
end)

it("properly creates and writes files", function()
local ws_path = (Path.cwd() / "test-workspace")

dirman.create_file("example-file", "test", {
no_open = true,
})

finally(function()
vim.fn.delete(ws_path:tostring(), "rf")
end)

assert.equal(vim.fn.filereadable((ws_path / "example-file.norg"):tostring()), 1)
end)
end)
end)
10 changes: 0 additions & 10 deletions lua/neorg/modules/core/integrations/treesitter/tests.lua

This file was deleted.

43 changes: 43 additions & 0 deletions lua/neorg/tests/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--[[
This file contains a variety of utility functions for writing Neorg tests.
The functions used here should be generalized and moved out into a separate rock on luarocks.org in due time.
Neorg sets up `busted` using `neolua` (a wrapper around neovim) as its test interpreter. This allows access to all
Neovim-related APIs natively. The entire process occurs in the flake.nix file.
--]]

local tests = {}

--- Sets up Neorg with a given module.
---@param module_name string The name of the module to load.
---@param configuration table? The configuration for the module.
---@return table #The main Neorg table with the setup() function called.
function tests.neorg_with(module_name, configuration)
local neorg = require("neorg")

neorg.setup({
load = {
["core.defaults"] = {},
[module_name] = { config = configuration },
},
})

return neorg
end

--- Runs a callback in the context of a given file.
---@param filename string The name of the file (used to determine filetype)
---@param content string The content of the buffer.
---@param callback fun(bufnr: number) The function to execute with the buffer number provided.
function tests.in_file(filename, content, callback)
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(buf, filename)
vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(content, "\n"))

callback(buf)

vim.api.nvim_buf_delete(buf, { force = true })
end

return tests
8 changes: 8 additions & 0 deletions neorg-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ dependencies = {
-- "norgopolis-server.lua >= 1.3.1",
"lua-utils.nvim",
"pathlib.nvim ~> 2.2",
"tree-sitter-norg == 0.2.4",
"tree-sitter-norg-meta == 0.1.0",
}

source = {
Expand All @@ -29,6 +31,12 @@ if MODREV == "scm" then
}
end

test_dependencies = {
"nlua",
-- Placed here as we plan on removing nvim-treesitter as a dependency soon, but it's still required for various tests.
"nvim-treesitter == 0.9.2",
}

build = {
type = "builtin",
copy_directories = {
Expand Down
16 changes: 0 additions & 16 deletions nix/checks/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
self,
git-hooks,
pkgs,
lib,
...
}: {
Expand Down Expand Up @@ -31,21 +30,6 @@
# stylua.enable = true;
};
};

neorocks-test =
(pkgs.neorocksTest {
src = "${self}";
name = "neorg";
version = "scm-1";
neovim = pkgs.neovim-unwrapped;
})
.overrideAttrs (oa:
lib.recursiveUpdate oa {
luarocksConfig = {
rocks_servers = ["${pkgs.installed-dependencies}/luarocks"];
rocks_trees = oa.luarocksConfig.rocks_trees ++ ["${pkgs.installed-dependencies}/luarocks" "."];
};
});
};
};
}
3 changes: 2 additions & 1 deletion nix/overlays/installed-dependencies.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
wget,
self,
}: let
dependencies = builtins.fromJSON (builtins.readFile "${self}/res/deps.json");
# Temporarily remove the parsers due to installation problems on Nix. Causes the integration test to fail for now.
dependencies = builtins.removeAttrs (builtins.fromJSON (builtins.readFile "${self}/res/deps.json")) ["tree-sitter-norg" "tree-sitter-norg-meta"];
in
runCommand "install-neorg-dependencies" {
nativeBuildInputs = [lua51Packages.luarocks wget];
Expand Down

0 comments on commit ebf75f5

Please sign in to comment.