diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5caaeb84..ae053e54 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -62,3 +62,62 @@ jobs: run: | nvim --version nvim -l scripts/test.lua + e2e_tests: + name: e2e tests + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu-22.04 + rev: nightly/nvim-linux64.tar.gz + - os: ubuntu-22.04 + rev: v0.9.0/nvim-linux64.tar.gz + - os: macos-latest + rev: nightly/nvim-macos.tar.gz + steps: + - uses: actions/checkout@v3 + - run: date +%F > todays-date + - name: Restore from todays cache + uses: actions/cache@v3 + with: + path: _neovim + key: ${{ runner.os }}-${{ matrix.rev }}-${{ hashFiles('todays-date') }} + + - name: Cache rust items + uses: actions/cache@v3 + with: + path: | + ~/.rustup/ + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + + - name: Prepare + run: | + # ${{ matrix.manager }} update + # ${{ matrix.manager }} install ${{ matrix.packages }} + test -d _neovim || { + mkdir -p _neovim + curl -sL "https://github.com/neovim/neovim/releases/download/${{ matrix.rev }}" | 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 + echo "${PWD}/_neovim/bin:${PATH}" >> $GITHUB_PATH + echo VIM="${PWD}/_neovim/share/nvim/runtime" >> $GITHUB_ENV + + - name: Run build scripts + run: | + nvim -u scripts/init.lua -l build/init.lua + + - name: Run tests + run: | + nvim --version + nvim -l scripts/e2e_test.lua + env: + SRC_ACCESS_TOKEN: ${{ secrets.SRC_ACCESS_TOKEN }} + SG_NVIM_E2E: true diff --git a/lua/e2e_tests/e2e_spec.lua b/lua/e2e_tests/e2e_spec.lua new file mode 100644 index 00000000..6208c780 --- /dev/null +++ b/lua/e2e_tests/e2e_spec.lua @@ -0,0 +1,56 @@ +-- Force loading plugin files +vim.cmd [[runtime! after/plugin/cody.lua]] +vim.cmd [[runtime! after/plugin/cody-agent.lua]] + +require("sg.cody.rpc").start() + +require("plenary.async").tests.add_to_env() + +local cody_commands = require "sg.cody.commands" +local rpc = assert(require "sg.cody.rpc", "able to load cody rpc") + +-- Create a temp directory for the stable testing data used by these tests.. +local tmp_dir = vim.loop.fs_mkdtemp(string.format("%s/cody-nvim-e2e-XXXXXXX", vim.loop.os_tmpdir())) +os.execute(string.format("git clone https://github.com/sourcegraph/e2e-sg.nvim %s", tmp_dir)) + +local find_initialized = function() + return vim.tbl_filter(function(msg) + return msg.type == "notify" and msg.method == "initialized" + end, rpc.messages)[1] +end + +describe("cody e2e", function() + before_each(function() + if string.sub(vim.env.SRC_ACCESS_TOKEN, 1, 4) ~= "sgp_" then + error "Need a real token to run e2e test suite" + end + + vim.cmd.cd(tmp_dir) + end) + + a.it("should ask through chat what file we are in", function() + vim.wait(5000, find_initialized) + + vim.cmd.edit "pool/pool.go" + + vim.cmd.CodyChat() + cody_commands.focus_prompt() + local prompt_bufnr = vim.api.nvim_get_current_buf() + + vim.api.nvim_buf_set_lines(prompt_bufnr, 0, -1, false, { "What file am I looking at" }) + vim.cmd.CodySubmit() + + cody_commands.focus_history() + local history_bufnr = vim.api.nvim_get_current_buf() + + vim.wait(20000, function() + return vim.api.nvim_buf_line_count(history_bufnr) > 5 + end) + + local lines = table.concat(vim.api.nvim_buf_get_lines(history_bufnr, 0, -1, false), "\n") + assert( + string.find(lines, "/pool/pool.go"), + string.format("Cody told us the path to the current file:\n\n %s", lines) + ) + end) +end) diff --git a/scripts/e2e_test.lua b/scripts/e2e_test.lua new file mode 100644 index 00000000..5b271622 --- /dev/null +++ b/scripts/e2e_test.lua @@ -0,0 +1,3 @@ +(loadfile "./scripts/init.lua")() + +require("plenary.test_harness").test_directory("lua/e2e_tests/", { minimal = "./scripts/init.lua" }) diff --git a/scripts/init.lua b/scripts/init.lua index be66b3cc..72026aa3 100644 --- a/scripts/init.lua +++ b/scripts/init.lua @@ -1,7 +1,11 @@ vim.env.SG_NVIM_TESTING = "true" -vim.env.SRC_ENDPOINT = "https://sourcegraph.com" -vim.env.SRC_ACCESS_TOKEN = "testing-token-doesnt-work" +vim.env.SRC_ENDPOINT = "https://sourcegraph.sourcegraph.com" + +-- when outside of github actions set the token to a fake token +if vim.env.SG_NVIM_E2E ~= "true" then + vim.env.SRC_ACCESS_TOKEN = "testing-token-doesnt-work" +end vim.opt.rtp:append { ".", "../plenary.nvim", "../tree-sitter-lua" }