Skip to content

Commit

Permalink
test: add tests and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Jan 16, 2024
1 parent fd8b830 commit d20e23d
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Continuous Integration

on:
push:
branches:
- main
pull_request: ~

concurrency:
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
cancel-in-progress: true

jobs:
tests:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-20.04
url: https://github.com/neovim/neovim/releases/download/nightly/nvim-linux64.tar.gz
- os: ubuntu-20.04
url: https://github.com/neovim/neovim/releases/download/v0.9.2/nvim-linux64.tar.gz

steps:
- uses: actions/checkout@v2
- run: date +%F > todays-date
- name: Restore from todays cache
uses: actions/cache@v2
with:
path: _neovim
key: ${{ runner.os }}-${{ matrix.url }}-${{ hashFiles('todays-date') }}

- name: Prepare
run: |
test -d _neovim || {
mkdir -p _neovim
curl -sL ${{ matrix.url }} | tar xzf - --strip-components=1 -C "${PWD}/_neovim"
}
mkdir -p ~/.local/share/nvim/site/pack/vendor/start
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
- name: Run tests
run: |
export PATH="${PWD}/_neovim/bin:${PATH}"
export VIM="${PWD}/_neovim/share/nvim/runtime"
nvim --version
make test
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
PANVIMDOC_DIR = misc/panvimdoc
PANVIMDOC_URL = https://github.com/kdheepak/panvimdoc
PLENARY_DIR = misc/plenary
PLENARY_URL = https://github.com/nvim-lua/plenary.nvim
TREESITTER_DIR = misc/treesitter
TREESITTER_URL = https://github.com/nvim-treesitter/nvim-treesitter

all: format test docs

docs: $(PANVIMDOC_DIR)
@echo "===> Docs:" && \
cd $(PANVIMDOC_DIR) && \
pandoc \
--metadata="project:codecompanion" \
--metadata="description:Use the OpenAI APIs directly in Neovim" \
--metadata="toc:true" \
--metadata="incrementheadinglevelby:0" \
--metadata="treesitter:true" \
--lua-filter scripts/skip-blocks.lua \
--lua-filter scripts/include-files.lua \
--lua-filter scripts/remove-emojis.lua \
-t scripts/panvimdoc.lua \
../../README.md \
-o ../../doc/codecompanion.txt

$(PANVIMDOC_DIR):
git clone --depth=1 --no-single-branch $(PANVIMDOC_URL) $(PANVIMDOC_DIR)
@rm -rf doc/panvimdoc/.git

format:
@echo "===> Formatting:"
@stylua lua/ -f ./stylua.toml

test: $(PLENARY_DIR) $(TREESITTER_DIR)
@echo "===> Testing:"
nvim --headless --clean \
-u scripts/minimal.vim \
-c "PlenaryBustedDirectory lua/spec/codecompanion { minimal_init = 'scripts/minimal.vim' }"

$(PLENARY_DIR):
git clone --depth=1 --branch v0.1.4 $(PLENARY_URL) $(PLENARY_DIR)
@rm -rf $(PLENARY_DIR)/.git

$(TREESITTER_DIR):
git clone --depth=1 --branch v0.9.1 $(TREESITTER_URL) $(TREESITTER_DIR)
@rm -rf $(TREESITTER_DIR)/.git
53 changes: 53 additions & 0 deletions lua/spec/codecompanion/client_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local mock = require("luassert.mock")
local stub = require("luassert.stub")
local match = require("luassert.match")
local spy = require("luassert.spy")

local Client = require("codecompanion.client")

local function setup(opts)
require("codecompanion").setup(opts)
end

describe("Client", function()
it("should call API correctly when chat is invoked", function()
local fn_mock = mock(vim.fn, true)
local log_mock = mock(require("codecompanion.utils.log"), true)
local autocmds_spy = spy.on(vim.api, "nvim_exec_autocmds")

local jobstart_stub = stub(fn_mock, "jobstart", function(_, opts)
local stdout_response = { vim.json.encode("SOME JSON RESPONSE") }

if opts.on_stdout then
opts.on_stdout(nil, stdout_response)
end

local exit_code = 0
if opts.on_exit then
opts.on_exit(nil, exit_code)
end

return 1
end)

setup({
base_url = "https://api.example.com",
})

local client = Client.new({ secret_key = "TEST_SECRET_KEY" })
local cb_stub = stub.new()

client:chat({ messages = { { role = "user", content = "hello" } } }, cb_stub)

assert.stub(jobstart_stub).was_called(1)
assert.stub(jobstart_stub).was_called_with(match.is_table(), match.is_table())

-- It's only called once as the jobstart_stub is stubbed to not fire an event
assert.spy(autocmds_spy).was_called(1)

autocmds_spy:revert()
jobstart_stub:revert()
mock.revert(fn_mock)
mock.revert(log_mock)
end)
end)
23 changes: 23 additions & 0 deletions scripts/minimal.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
set rtp+=.
set rtp+=./misc/plenary
set rtp+=./misc/treesitter

set noswapfile

runtime! plugin/plenary.vim
runtime! plugin/nvim-treesitter.lua

lua <<EOF
local required_parsers = { 'lua', 'markdown', 'markdown_inline' }
local installed_parsers = require'nvim-treesitter.info'.installed_parsers()
local to_install = vim.tbl_filter(function(parser)
return not vim.tbl_contains(installed_parsers, parser)
end, required_parsers)
if #to_install > 0 then
-- fixes 'pos_delta >= 0' error - https://github.com/nvim-lua/plenary.nvim/issues/52
vim.cmd('set display=lastline')
-- make "TSInstall*" available
vim.cmd 'runtime! plugin/nvim-treesitter.vim'
vim.cmd('TSInstallSync ' .. table.concat(to_install, ' '))
end
EOF

0 comments on commit d20e23d

Please sign in to comment.