Skip to content

Commit

Permalink
feat: introduce a testing framework
Browse files Browse the repository at this point in the history
Enter `nix develop ./contrib` to get all the necessary dependencies.
Run 'make test' to run tests.

Tests could drive the API towards a more programmatic interface.
This is just a bootstrap to future work.
  • Loading branch information
teto committed Dec 14, 2022
1 parent 0f26e2a commit d34e1b8
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 20 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: default

on: [push, pull_request]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v18
with:
nix_path: nixpkgs=channel:nixos-unstable
- run: |
nix develop ./contrib -c make test
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Use lua52 so we will no receive errors regarding to goto statements
std = 'lua52'
std = 'lua52+busted'

-- Rerun tests only if their modification time changed
cache = true
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ lint:

format:
stylua .

test:
# possible args to test_directory: sequential=true,keep_going=false
# minimal.vim is generated when entering the flake, aka `nix develop ./contrib`
nvim --headless -u minimal.vim -c "lua require('plenary.test_harness').test_directory('.', {minimal_init='minimal.vim'})"

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ request method (e.g. `GET`) and run `rest.nvim`.
4. Push to the branch (<kbd>git push origin my-new-feature</kbd>)
5. Create a new Pull Request

To run the tests, enter a nix shell with `nix develop ./contrib`, then run `make
test`.

## Related software

Expand Down
75 changes: 61 additions & 14 deletions contrib/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,83 @@
let
pkgs = nixpkgs.legacyPackages.${system};

mkPackage = luaVersion:
pkgs."lua${luaVersion}Packages".luarocks;

mkDevShell = luaVersion:
let
luaPkgs = pkgs."lua${luaVersion}".pkgs;
# luaPkgs = pkgs."lua${luaVersion}".pkgs;
luaEnv = pkgs."lua${luaVersion}".withPackages (lp: with lp; [
luacheck
luarocks
(pkgs.lib.hiPrio plenary-nvim)
]);
neovimConfig = pkgs.neovimUtils.makeNeovimConfig {
plugins = with pkgs.vimPlugins; [
{
plugin = packer-nvim;
type = "lua";
config = ''
require('packer').init({
luarocks = {
python_cmd = 'python' -- Set the python command to use for running hererocks
},
})
-- require my own manual config
require('init-manual')
'';
}
{ plugin = (nvim-treesitter.withPlugins (
plugins: with plugins; [
tree-sitter-lua
tree-sitter-http
tree-sitter-json
]
));
}
{ plugin = plenary-nvim; }
];
customRC = "";
wrapRc = false;
};
myNeovim = pkgs.wrapNeovimUnstable pkgs.neovim-unwrapped neovimConfig;
in
luaPkgs.luarocks.overrideAttrs (oa: {
name = "luarocks-dev";
buildInputs = oa.buildInputs ++ [
pkgs.mkShell {
name = "rest-nvim";
buildInputs = [
pkgs.sumneko-lua-language-server
luaEnv
pkgs.stylua
myNeovim
# pkgs.neovim # assume user has one already installed
];
});

shellHook = let
myVimPackage = with pkgs.vimPlugins; {
start = [ plenary-nvim (nvim-treesitter.withPlugins (
plugins: with plugins; [
tree-sitter-lua
tree-sitter-http
tree-sitter-json
]
))];
# opt = map (x: x.plugin) pluginsPartitioned.right;
};
# };
packDirArgs.myNeovimPackages = myVimPackage;
in
''
cat <<-EOF > minimal.vim
set rtp+=.
set packpath^=${pkgs.vimUtils.packDir packDirArgs}
EOF
'';
};

in
{

packages = {
default = self.packages.${system}.luarocks-51;
luarocks-51 = mkPackage "5_1";
luarocks-52 = mkPackage "52";
};
# packages = {
# default = self.packages.${system}.luarocks-51;
# luarocks-51 = mkPackage "5_1";
# luarocks-52 = mkPackage "5_2";
# };

devShells = {
default = self.devShells.${system}.luajit;
Expand Down
4 changes: 3 additions & 1 deletion lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ end

-- curl_cmd runs curl with the passed options, gets or creates a new buffer
-- and then the results are printed to the recently obtained/created buffer
-- @param opts curl arguments
-- @param opts (table) curl arguments:
-- - yank_dry_run (boolean): displays the command
-- - arguments are forwarded to plenary
M.curl_cmd = function(opts)
if opts.dry_run then
local res = curl[opts.method](opts)
Expand Down
8 changes: 8 additions & 0 deletions lua/rest-nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ rest.run = function(verbose)
return
end

return rest.run_request(result, verbose)
end

rest.run_request = function(req, verbose)

local result = req
Opts = {
method = result.method:lower(),
url = result.url,
Expand Down Expand Up @@ -74,4 +80,6 @@ rest.last = function()
end
end

rest.request = request

return rest
8 changes: 6 additions & 2 deletions lua/rest-nvim/request/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,12 @@ end

local M = {}
M.get_current_request = function()
local curpos = vim.fn.getcurpos()
local bufnr = vim.api.nvim_win_get_buf(0)
return M.buf_get_request(vim.api.nvim_win_get_buf(0), vim.fn.getcurpos())
end

M.buf_get_request = function(bufnr, curpos)
curpos = curpos or vim.fn.getcurpos()
bufnr = bufnr or vim.api.nvim_win_get_buf(0)

local start_line = start_request()
if start_line == 0 then
Expand Down
4 changes: 2 additions & 2 deletions rest.nvim-scm-1.rockspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local MAJOR, REV = "scm", "-1"
local MAJOR, REV = "0.1", "-2"
rockspec_format = "3.0"
package = "rest.nvim"
version = MAJOR .. REV
Expand All @@ -19,7 +19,7 @@ dependencies = {
}

source = {
url = "http://github.com/rest-nvim/rest.nvim/archive/v" .. MAJOR .. ".zip",
url = "http://github.com/rest-nvim/rest.nvim/archive/" .. MAJOR .. ".zip",
dir = "rest.nvim-" .. MAJOR,
}

Expand Down
13 changes: 13 additions & 0 deletions tests/main_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local rest = require('rest-nvim')
local v = vim

describe("rest testing framework", function()

it('test create users', function()

v.api.nvim_cmd({cmd='edit', args = {'tests/post_create_user.http'}}, {})

-- first argument is for verbosity
rest.run(false)
end)
end)

0 comments on commit d34e1b8

Please sign in to comment.