Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add tests and ci action #4

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 9 additions & 0 deletions lua/codecompanion/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ end
---@return nil|any
local function parse_response(code, stdout)
if code ~= 0 then
log:error("Error: %s", stdout)
return string.format("Error: %s", stdout)
end

local ok, data = pcall(vim.json.decode, stdout, { luanil = { object = true } })
if not ok then
log:error("Error malformed json: %s", data)
return string.format("Error malformed json: %s", data)
end

if data.error then
log:error("API Error: %s", data.error.message)
return string.format("API Error: %s", data.error.message)
end

return nil, data
end

Expand Down Expand Up @@ -174,6 +180,7 @@ function Client:stream_call(url, payload, bufnr, cb)
local ok, data = pcall(vim.json.decode, chunk, { luanil = { object = true } })
if not ok then
done = true
log:error("Error malformed json: %s", data)
return cb(string.format("Error malformed json: %s", data))
end

Expand Down Expand Up @@ -202,6 +209,7 @@ function Client:stream_call(url, payload, bufnr, cb)
end
end,
})

if jid == 0 then
cb("Passed invalid arguments to curl")
elseif jid == -1 then
Expand All @@ -213,6 +221,7 @@ function Client:stream_call(url, payload, bufnr, cb)
strategy = "chat",
}
end

return jid
end

Expand Down
Loading
Loading