[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang

run tests independent of user nvim setup (#39)

* chore(lua_ls): now lua_ls knows about testing functions

* spec: change way how tests srtuctured

* test(config): refactor tests

* test: utils

* refactor(utils): remove not used function

* chore(ci): add test runner

* chore(ci): remove taskfile from deps

* fix: now it works

authored by olexsmir.xyz and committed by GitHub 5f8466d0 b5327cd2

+24
.github/workflows/tests.yml
··· 1 + name: tests 2 + on: [push, pull_request] 3 + 4 + jobs: 5 + tests: 6 + strategy: 7 + matrix: 8 + os: [ubuntu-latest] 9 + runs-on: ${{ matrix.os }} 10 + steps: 11 + - uses: actions/checkout@v3 12 + - name: Install Neovim 13 + shell: bash 14 + run: | 15 + mkdir -p /tmp/nvim 16 + wget -q https://github.com/neovim/neovim/releases/download/nightly/nvim.appimage -O /tmp/nvim/nvim.appimage 17 + cd /tmp/nvim 18 + chmod a+x ./nvim.appimage 19 + ./nvim.appimage --appimage-extract 20 + echo "/tmp/nvim/squashfs-root/usr/bin/" >> $GITHUB_PATH 21 + - name: Run Tests 22 + run: | 23 + nvim --version 24 + nvim --headless -u ./spec/minimal_init.lua -c "PlenaryBustedDirectory spec {minimal_init='./spec/minimal_init.lua', sequential=true}"
+10
.luarc.json
··· 1 + { 2 + "diagnostics.globals": [ 3 + "describe", 4 + "it", 5 + "before_each", 6 + "after_each", 7 + "before_all", 8 + "after_all" 9 + ] 10 + }
-11
lua/gopher/_utils/init.lua
··· 9 9 return next(t) == nil 10 10 end 11 11 12 - ---@param s string 13 - ---@return string 14 - function utils.rtrim(s) 15 - local n = #s 16 - while n > 0 and s:find("^%s", n) do 17 - n = n - 1 18 - end 19 - 20 - return s:sub(1, n) 21 - end 22 - 23 12 ---@param msg string 24 13 ---@param lvl any 25 14 function utils.deferred_notify(msg, lvl)
-41
spec/gopher_config_spec.lua
··· 1 - describe("gopher.config", function() 2 - it("can be required", function() 3 - require "gopher.config" 4 - end) 5 - 6 - it(".setup() when gets empty table not edit config", function() 7 - local c = require "gopher.config" 8 - c.setup {} 9 - 10 - assert.are.same(c.config.commands.go, "go") 11 - assert.are.same(c.config.commands.gomodifytags, "gomodifytags") 12 - assert.are.same(c.config.commands.gotests, "gotests") 13 - assert.are.same(c.config.commands.impl, "impl") 14 - end) 15 - 16 - it(".setup() when get one custom value sets that", function() 17 - local c = require "gopher.config" 18 - c.setup { commands = { 19 - go = "custom_go", 20 - } } 21 - 22 - assert.are.same(c.config.commands.go, "custom_go") 23 - end) 24 - 25 - it(".setup() when get all custom values sets it", function() 26 - local c = require "gopher.config" 27 - c.setup { 28 - commands = { 29 - go = "go1.18", 30 - gomodifytags = "user-gomodifytags", 31 - gotests = "gotests", 32 - impl = "goimpl", 33 - }, 34 - } 35 - 36 - assert.are.same(c.config.commands.go, "go1.18") 37 - assert.are.same(c.config.commands.gomodifytags, "user-gomodifytags") 38 - assert.are.same(c.config.commands.gotests, "gotests") 39 - assert.are.same(c.config.commands.impl, "goimpl") 40 - end) 41 - end)
-5
spec/gopher_spec.lua
··· 1 - describe("gopher", function() 2 - it("can be required", function() 3 - require "gopher" 4 - end) 5 - end)
-49
spec/gopher_struct_tags_spec.lua
··· 1 - local cur_dir = vim.fn.expand "%:p:h" 2 - 3 - describe("gopher.struct_tags", function() 4 - it("can be required", function() 5 - require "gopher.struct_tags" 6 - end) 7 - 8 - it("can add json tag to struct", function() 9 - local tag = require "gopher.struct_tags" 10 - local temp_file = vim.fn.tempname() .. ".go" 11 - local input_file = vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/add_input.go") 12 - local output_file = 13 - vim.fn.join(vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/add_output.go"), "\n") 14 - 15 - vim.fn.writefile(input_file, temp_file) 16 - vim.cmd("silent exe 'e " .. temp_file .. "'") 17 - vim.bo.filetype = "go" 18 - 19 - local bufn = vim.fn.bufnr(0) 20 - vim.fn.setpos(".", { bufn, 3, 6, 0 }) 21 - tag.add() 22 - 23 - vim.wait(100) 24 - assert.are.same(output_file, vim.fn.join(vim.fn.readfile(temp_file), "\n")) 25 - 26 - vim.cmd("bd! " .. temp_file) 27 - end) 28 - 29 - it("can remove json tag from struct", function() 30 - local tag = require "gopher.struct_tags" 31 - local temp_file = vim.fn.tempname() .. ".go" 32 - local input_file = vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/remove_input.go") 33 - local output_file = 34 - vim.fn.join(vim.fn.readfile(cur_dir .. "/spec/fixtures/tags/remove_output.go"), "\n") 35 - 36 - vim.fn.writefile(input_file, temp_file) 37 - vim.cmd("silent exe 'e " .. temp_file .. "'") 38 - vim.bo.filetype = "go" 39 - 40 - local bufn = vim.fn.bufnr() 41 - vim.fn.setpos(".", { bufn, 3, 6, 0 }) 42 - tag.remove() 43 - 44 - vim.wait(100) 45 - assert.are.same(output_file, vim.fn.join(vim.fn.readfile(temp_file), "\n")) 46 - 47 - vim.cmd("bd! " .. temp_file) 48 - end) 49 - end)
-19
spec/gopher_utils_spec.lua
··· 1 - describe("gopher._utils", function() 2 - it("can be requried", function() 3 - require "gopher._utils" 4 - end) 5 - 6 - it(".empty() with non-empty talbe", function() 7 - local empty = require("gopher._utils").empty 8 - local res = empty { first = "1", second = 2 } 9 - 10 - assert.are.same(res, false) 11 - end) 12 - 13 - it(".empty() with empty talbe", function() 14 - local empty = require("gopher._utils").empty 15 - local res = empty {} 16 - 17 - assert.are.same(res, true) 18 - end) 19 - end)
+29
spec/units/config_spec.lua
··· 1 + describe("gopher.config", function() 2 + it(".setup() should provide default when .setup() is not called", function() 3 + local c = require "gopher.config" 4 + 5 + assert.are.same(c.commands.go, "go") 6 + assert.are.same(c.commands.gomodifytags, "gomodifytags") 7 + assert.are.same(c.commands.gotests, "gotests") 8 + assert.are.same(c.commands.impl, "impl") 9 + assert.are.same(c.commands.iferr, "iferr") 10 + assert.are.same(c.commands.dlv, "dlv") 11 + end) 12 + 13 + it(".setup() should change options on users config", function() 14 + local c = require "gopher.config" 15 + c.setup { 16 + commands = { 17 + go = "go1.420", 18 + gomodifytags = "iDontUseRustBtw", 19 + }, 20 + } 21 + 22 + assert.are.same(c.commands.go, "go1.420") 23 + assert.are.same(c.commands.gomodifytags, "iDontUseRustBtw") 24 + assert.are.same(c.commands.gotests, "gotests") 25 + assert.are.same(c.commands.impl, "impl") 26 + assert.are.same(c.commands.iferr, "iferr") 27 + assert.are.same(c.commands.dlv, "dlv") 28 + end) 29 + end)
+25
spec/units/utils_spec.lua
··· 1 + describe("gopher._utils", function() 2 + local u = require "gopher._utils" 3 + 4 + describe(".is_tbl_empty()", function() 5 + it("it is empty", function() 6 + assert.are.same(true, u.is_tbl_empty {}) 7 + end) 8 + 9 + it("it is not empty", function() 10 + assert.are.same(false, u.is_tbl_empty { first = "1", second = 2 }) 11 + end) 12 + end) 13 + 14 + describe(".sreq()", function() 15 + it("can require existing module", function() 16 + assert.are.same(require "gopher", u.sreq "gopher") 17 + end) 18 + 19 + it("cannot require non-existing module", function() 20 + assert.has.errors(function() 21 + u.sreq "iDontExistBtw" 22 + end) 23 + end) 24 + end) 25 + end)