From afa6b4b6195483d73cb6a91bc757652301275ed6 Mon Sep 17 00:00:00 2001 From: Christian Segundo Date: Sun, 8 Oct 2023 11:52:40 +0200 Subject: Add test cases --- .gitignore | 1 + Jenkinsfile | 51 +++++++++++++++++++++++++++++ Makefile | 11 +++++++ neovim.toml | 49 ++++++++++++++++++++++++++++ neovim.yaml | 29 ----------------- plugin/promql.lua | 50 ++++++++++++++++++++++++++--- test/init.lua | 37 +++++++++++++++++++++ test/plugin_spec.lua | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 286 insertions(+), 33 deletions(-) create mode 100644 Jenkinsfile create mode 100644 Makefile create mode 100644 neovim.toml delete mode 100644 neovim.yaml create mode 100644 test/init.lua create mode 100644 test/plugin_spec.lua diff --git a/.gitignore b/.gitignore index 9bbbeea..f9d23ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .luarc.json +.test diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..ad6a568 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,51 @@ +String cron_string = BRANCH_NAME == "master" ? "@daily" : "" +pipeline { + agent none + options { parallelsAlwaysFailFast() } + triggers { cron(cron_string) } + stages { + stage('Lint') { + agent { + docker { + image "chn2guevara/nvim:stable" + alwaysPull true + } + } + steps { sh 'make lint' } + } + stage('Test') { + matrix { + agent { + docker { + image "${DOCKER_IMAGE}" + alwaysPull true + customWorkspace "workspace/${JOB_NAME}/IMAGE/${DOCKER_IMAGE}/" + } + } + axes { + axis { + name 'DOCKER_IMAGE' + values 'chn2guevara/nvim:stable', 'chn2guevara/nvim:nightly' + } + } + stages { + stage('Test') { + steps { + sh ''' + apk update && \ + apk add go && \ + mkdir tmp && \ + cd tmp && \ + git clone https://github.com/prometheus/prometheus.git . && \ + GO111MODULE=on go install github.com/prometheus/prometheus/cmd/promtool && \ + export PATH="/root/go/bin/:${PATH}" && \ + cd .. && \ + make test + ''' + } + } + } + } + } + } +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3cae985 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +.PHONY: lint +lint: + stylua -c . + +.PHONY: test +test: + nvim --headless -u test/init.lua -c "PlenaryBustedDirectory test/ {minimal_init = 'test/init.lua', sequential = true}" + +.PHONY: clean +clean: + rm -rf .test diff --git a/neovim.toml b/neovim.toml new file mode 100644 index 0000000..352bd0b --- /dev/null +++ b/neovim.toml @@ -0,0 +1,49 @@ +[selene] +base = "lua51" +name = "neovim" + +[vim] +any = true + +[jit] +any = true + +[[describe.args]] +type = "string" +[[describe.args]] +type = "function" + +[[it.args]] +type = "string" +[[it.args]] +type = "function" + +[[before_each.args]] +type = "function" +[[after_each.args]] +type = "function" + +[assert.is_not] +any = true + +[[assert.equals.args]] +type = "any" +[[assert.equals.args]] +type = "any" +[[assert.equals.args]] +type = "any" +required = false + +[[assert.same.args]] +type = "any" +[[assert.same.args]] +type = "any" + +[[assert.truthy.args]] +type = "any" + +[[assert.spy.args]] +type = "any" + +[[assert.stub.args]] +type = "any" diff --git a/neovim.yaml b/neovim.yaml deleted file mode 100644 index 1288950..0000000 --- a/neovim.yaml +++ /dev/null @@ -1,29 +0,0 @@ ---- -base: lua51 - -globals: - vim: - any: true - assert: - args: - - type: bool - - type: string - required: false - assert.are.same: - args: - - type: any - - type: any - after_each: - args: - - type: function - before_each: - args: - - type: function - describe: - args: - - type: string - - type: function - it: - args: - - type: string - - type: function diff --git a/plugin/promql.lua b/plugin/promql.lua index e44d1c3..e3f1bb0 100644 --- a/plugin/promql.lua +++ b/plugin/promql.lua @@ -6,11 +6,14 @@ --) -- How it's going -if vim.g.promqlfmt == 1 then +if vim.g.loaded_promqlfmt == 1 then return end -vim.g.promqlfmt = 1 +vim.g.loaded_promqlfmt = 1 + +-- remove once 0.10 is released +local vim_is_old = vim.version().minor < 10 and vim.version().major == 0 --- Format a PromQL query using promtool --- @param query string @query to format @@ -22,6 +25,18 @@ local promql_format = function(query, padding, padchar) padchar = padchar or " " local success, result = pcall(function() + if vim_is_old then + return { + code = 0, + stdout = vim.fn.system({ + "promtool", + "promql", + "format", + "--experimental", + query, + }), + } + end return vim .system( { "promtool", "promql", "format", "--experimental", query }, @@ -65,7 +80,25 @@ local buf_get_selected_text = function(bufnr) end_line = nil, end_row = nil, } - local region = vim.region(bufnr or 0, "'<", "'>", vim.fn.visualmode(), true) + + local pos1, pos2 = "'<", "'>" + if vim_is_old then + local pos = vim.fn.getpos("'<") + pos1 = { pos[2] - 1, pos[3] - 1 + pos[4] } + pos = vim.fn.getpos("'>") + pos2 = { pos[2] - 1, pos[3] - 1 + pos[4] } + + if pos1[1] > pos2[1] or (pos1[1] == pos2[1] and pos1[2] > pos2[2]) then + pos1, pos2 = pos2, pos1 + end + + -- getpos() may return {0,0,0,0} + if pos1[1] < 0 or pos1[2] < 0 then + return {} + end + end + + local region = vim.region(bufnr or 0, pos1, pos2, vim.fn.visualmode(), true) local maxcol = vim.v.maxcol for line, cols in vim.spairs(region) do if r.start_line == nil then @@ -82,7 +115,16 @@ local buf_get_selected_text = function(bufnr) r.end_row = endcol end - r.end_row = r.end_row > last_col_line(0, r.end_line) and -1 or r.end_row + -- 0.9 does not support negative end_row + if vim_is_old then + local last_row = last_col_line(0, r.end_line) + if r.end_row == -1 or r.end_row > last_row then + r.end_row = last_row + end + else + r.end_row = r.end_row > last_col_line(0, r.end_line) and -1 or r.end_row + end + return r end diff --git a/test/init.lua b/test/init.lua new file mode 100644 index 0000000..f05408e --- /dev/null +++ b/test/init.lua @@ -0,0 +1,37 @@ +local M = {} + +function M.root(root) + local f = debug.getinfo(1, "S").source:sub(2) + return vim.fn.fnamemodify(f, ":p:h:h") .. "/" .. (root or "") +end + +---@param plugin string +function M.load(plugin) + local name = plugin:match(".*/(.*)") + local package_root = M.root(".test/site/pack/deps/start/") + if not vim.loop.fs_stat(package_root .. name) then + print("Installing " .. plugin) + vim.fn.mkdir(package_root, "p") + vim.fn.system({ + "git", + "clone", + "--depth=1", + "https://github.com/" .. plugin .. ".git", + package_root .. "/" .. name, + }) + end +end + +function M.setup() + vim.cmd([[set runtimepath=$VIMRUNTIME]]) + vim.opt.runtimepath:append(M.root()) + vim.opt.packpath = { M.root(".test/site") } + M.load("nvim-lua/plenary.nvim") + vim.env.XDG_CONFIG_HOME = M.root(".test/config") + vim.env.XDG_DATA_HOME = M.root(".test/data") + vim.env.XDG_STATE_HOME = M.root(".test/state") + vim.env.XDG_CACHE_HOME = M.root(".test/cache") + vim.api.nvim_command([[source plugin/promql.lua]]) +end + +M.setup() diff --git a/test/plugin_spec.lua b/test/plugin_spec.lua new file mode 100644 index 0000000..b24c8da --- /dev/null +++ b/test/plugin_spec.lua @@ -0,0 +1,91 @@ +local assert = require("luassert") + +local function buf(input) + local b = vim.api.nvim_create_buf(false, false) + vim.api.nvim_command("buffer " .. b) + vim.api.nvim_buf_set_lines(b, 0, -1, true, vim.split(input, "\n")) + return b +end + +describe("plugin spec", function() + it("loads", function() + assert.is_true(vim.g.loaded_promqlfmt == 1) + assert.is_true(vim.fn.exists(":Promqlfmt") == 2) + end) + + local tests = { + { + name = "formats whole buffer", + input = 'sum(rate(foo{bar="baz"}[5m])) by (bar)', + expected = 'sum by (bar) (rate(foo{bar="baz"}[5m]))', + fmtfn = function() + vim.api.nvim_command(":Promqlfmt") + end, + }, + { + name = "formats visual selection of whole lines", + input = 'sum(rate(foo{bar="baz"}[5m])) by (bar)\nsum(rate(foo{bar="baz"}[5m])) by (bar)', + expected = 'sum(rate(foo{bar="baz"}[5m])) by (bar)\nsum by (bar) (rate(foo{bar="baz"}[5m]))', + fmtfn = function() + vim.api.nvim_command(":2") + vim.api.nvim_exec('execute "normal V\\"', false) + vim.api.nvim_command(":'<,'>Promqlfmt") + end, + }, + { + name = "formats visual selection of whole lines with padding", + input = [[ +--- +foo: + bar: | + ((foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m-foo:bar:baz:by_action:rate5m{foo="bar"})/foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m)]], + expected = [[ +--- +foo: + bar: | + ( + (foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m - foo:bar:baz:by_action:rate5m{foo="bar"}) + / + foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m + )]], + fmtfn = function() + vim.api.nvim_command(":4") + vim.api.nvim_exec('execute "normal V\\"', false) + vim.api.nvim_command(":'<,'>Promqlfmt") + end, + }, + { + name = "formats visual selection of multine whole lines with padding", + input = [[ +--- +foo: + bar: | + ((foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m - foo:bar:baz:by_action:rate5m{foo="bar"}) + / + foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m)]], + expected = [[ +--- +foo: + bar: | + ( + (foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m - foo:bar:baz:by_action:rate5m{foo="bar"}) + / + foo:bar:baz:by_action:rate5m{foo="bar"} offset 5m + )]], + fmtfn = function() + vim.api.nvim_command(":4") + vim.api.nvim_exec('execute "normal V2j\\"', false) + vim.api.nvim_command(":'<,'>Promqlfmt") + end, + }, + } + + for _, test in ipairs(tests) do + it(test.name, function() + buf(test.input) + test.fmtfn() + local result = vim.api.nvim_buf_get_lines(0, 0, -1, true) + assert.are.same(test.expected, table.concat(result, "\n")) + end) + end +end) -- cgit v1.2.3