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

Fixtures: Unit tests updates #394

Merged
merged 34 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
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
17 changes: 17 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"Lua.diagnostics.globals": [
"vim",
"describe",
"it",
"before_each",
"after_each",
"teardown",
"pending",
"lfs"
],
"Lua.workspace.checkThirdParty": false,
"Lua.workspace.library": [
"${3rd}/lfs/library"
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ require('go').setup({
-- settings with {}
go='go', -- go command, can be go[default] or go1.18beta1
goimport='gopls', -- goimport command, can be gopls[default] or goimport
fillstruct = 'gopls', -- can be nil (use fillstruct, slower) and gopls
fillstruct = 'gopls', -- default, can also use fillstruct
gofmt = 'gofumpt', --gofmt cmd,
max_line_len = 128, -- max line length in golines format, Target maximum line length for golines
tag_transform = false, -- can be transform option("snakecase", "camelcase", etc) check gomodifytags for details and more options
Expand Down
38 changes: 26 additions & 12 deletions lua/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ _GO_NVIM_CFG = {
-- it can be a nulls source name e.g. `golines` or a nulls query table
lsp_keymaps = true, -- true: use default keymaps defined in go/lsp.lua
lsp_codelens = true,
diagnostic = { -- set diagnostic to false to disable diagnostic
diagnostic = { -- set diagnostic to false to disable diagnostic
hdlr = false, -- hook diagnostic handler and send error to quickfix
underline = true,
-- virtual text setup
Expand Down Expand Up @@ -125,7 +125,7 @@ _GO_NVIM_CFG = {
posititon = 'auto', -- one of {`top`, `bottom`, `left`, `right`, `center`, `auto`}
width = 0.45, -- width of float window if not auto
height = 0.98, -- height of float window if not auto
title_colors = 'nord', -- table of colors for title, 'rainbow' for system default of rainbow colors
title_colors = 'nord', -- table of colors for title, or a color scheme name
},
trouble = false, -- true: use trouble to open quickfix
test_efm = false, -- errorfomat for quickfix, default mix mode, set to true will be efm only
Expand All @@ -134,11 +134,19 @@ _GO_NVIM_CFG = {
username = '',
useremail = '',
disable_per_project_cfg = false, -- set to true to disable load script from .gonvim/init.lua
on_jobstart = function(cmd) _=cmd end, -- callback for stdout
on_stdout = function(err, data) _, _ = err, data end, -- callback when job started
on_stderr = function(err, data) _, _ = err, data end, -- callback for stderr
on_exit = function(code, signal, output) _, _, _ = code, signal, output end, -- callback for jobexit, output : string
iferr_vertical_shift = 4 -- defines where the cursor will end up vertically from the begining of if err statement after GoIfErr command
on_jobstart = function(cmd)
_ = cmd
end, -- callback for stdout
on_stdout = function(err, data)
_, _ = err, data
end, -- callback when job started
on_stderr = function(err, data)
_, _ = err, data
end, -- callback for stderr
on_exit = function(code, signal, output)
_, _, _ = code, signal, output
end, -- callback for jobexit, output : string
iferr_vertical_shift = 4, -- defines where the cursor will end up vertically from the begining of if err statement after GoIfErr command
}

-- TODO: nvim_{add,del}_user_command https://github.com/neovim/neovim/pull/16752
Expand All @@ -155,10 +163,16 @@ function go.setup(cfg)
vim.notify('go.nvim lsp_diag_hdlr deprecated, use diagnostic.hdlr', vim.log.levels.WARN)
end
if cfg.lsp_diag_underline ~= nil then
vim.notify('go.nvim lsp_diag_underline deprecated, use diagnostic.underline', vim.log.levels.WARN)
vim.notify(
'go.nvim lsp_diag_underline deprecated, use diagnostic.underline',
vim.log.levels.WARN
)
end
if cfg.lsp_diag_virtual_text ~= nil then
vim.notify('go.nvim lsp_diag_virtual_text deprecated, use diagnostic.virtual_text', vim.log.levels.WARN)
vim.notify(
'go.nvim lsp_diag_virtual_text deprecated, use diagnostic.virtual_text',
vim.log.levels.WARN
)
end
if cfg.lsp_diag_signs ~= nil then
vim.notify('go.nvim lsp_diag_signs deprecated, use diagnostic.signs', vim.log.levels.WARN)
Expand Down Expand Up @@ -192,9 +206,9 @@ function go.setup(cfg)
end

if _GO_NVIM_CFG.diagnostic then
local cfg = vim.tbl_extend('force', {}, _GO_NVIM_CFG.diagnostic)
cfg.hdlr = nil
vim.diagnostic.config(cfg)
local dcfg = vim.tbl_extend('force', {}, _GO_NVIM_CFG.diagnostic)
dcfg.hdlr = nil
vim.diagnostic.config(dcfg)

require('go.lsp_diag').setup()
end
Expand Down
4 changes: 2 additions & 2 deletions lua/go/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ M.gofmt = function(...)
end

M.org_imports = function()
local r = require('go.lsp').codeaction('', 'source.organizeImports', function()
require('go.lsp').codeaction('', 'source.organizeImports', function()
vim.lsp.buf.format({
async = _GO_NVIM_CFG.lsp_fmt_async,
bufnr = vim.api.nvim_get_current_buf(),
Expand All @@ -172,7 +172,7 @@ M.org_imports = function()
end

M.goimport = function(...)
local goimport = _GO_NVIM_CFG.goimport or 'goimports'
local goimport = _GO_NVIM_CFG.goimport or 'gopls'
local args = { ... }
log(args, goimport)
if goimport == 'gopls' then
Expand Down
9 changes: 5 additions & 4 deletions lua/go/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ local on_attach = function(client, bufnr)
end
local keymaps
if _GO_NVIM_CFG.lsp_keymaps == true then
log('go.nvim lsp_keymaps', client, bufnr)
log('go.nvim lsp_keymaps', bufnr)
keymaps = {
{ key = 'gd', func = vim.lsp.buf.definition, desc = 'goto definition' },
{ key = 'K', func = vim.lsp.buf.hover, desc = 'hover' },
Expand Down Expand Up @@ -232,8 +232,8 @@ function M.setup()

local vim_version = vim.version().major * 100 + vim.version().minor * 10 + vim.version().patch

if vim_version < 61 then
vim.notify('LSP: go.nvim requires neovim 0.6.1 or later', vim.log.levels.WARN)
if vim_version < 81 then
vim.notify('LSP: go.nvim requires neovim 0.8.1 or later', vim.log.levels.WARN)
end
log(goplscfg)
lspconfig.gopls.setup(goplscfg)
Expand All @@ -253,11 +253,12 @@ write", "source", "source.organizeImports" }
-- only this action 'refactor.rewrite' source.organizeImports
M.codeaction = function(action, only, hdlr)
local params = vim.lsp.util.make_range_params()
log(action, only)
if only then
params.context = { only = { only } }
end
local bufnr = vim.api.nvim_get_current_buf()

log(action, only, bufnr)
vim.lsp.buf_request_all(bufnr, 'textDocument/codeAction', params, function(result)
if not result or next(result) == nil then
log('nil result')
Expand Down
2 changes: 1 addition & 1 deletion lua/tests/fixtures/coverage/branch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package coverage

// import "fmt"

Expand Down
2 changes: 1 addition & 1 deletion lua/tests/fixtures/coverage/branch_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package coverage

import "testing"

Expand Down
3 changes: 0 additions & 3 deletions lua/tests/fixtures/coverage/go.mod

This file was deleted.

2 changes: 1 addition & 1 deletion lua/tests/fixtures/coverage/tag_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//+build integration,unit

package main
package coverage

func TestTag(t *testing.T) {
t.Log("TestTag")
Expand Down
3 changes: 0 additions & 3 deletions lua/tests/fixtures/fill/go.mod

This file was deleted.

3 changes: 0 additions & 3 deletions lua/tests/fixtures/fixplurals/go.mod

This file was deleted.

3 changes: 0 additions & 3 deletions lua/tests/fixtures/fmt/go.mod

This file was deleted.

3 changes: 3 additions & 0 deletions lua/tests/fixtures/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module fixtures

go 1.21.1
3 changes: 0 additions & 3 deletions lua/tests/fixtures/ts/go.mod

This file was deleted.

21 changes: 12 additions & 9 deletions lua/tests/go_commands_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,26 @@ describe('should run Go commands', function()
vim.fn.writefile(lines, name)
local cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd)
local bufn = vim.fn.bufnr('')
-- local bufn = vim.fn.bufnr('')
--
vim.cmd('GoFmt')

vim.cmd('GoRun')
vim.cmd('GoBuild')
path = cur_dir .. '/lua/tests/fixtures/'
local fname = 'coverage/branch_test.go' -- %:p:h ? %:p

path = cur_dir .. '/lua/tests/fixtures/coverage/branch_test.go' -- %:p:h ? %:p
-- local lines = vim.fn.readfile(path)
vim.cmd('cd ' .. path)

lines = vim.fn.readfile(path)
-- name = vim.fn.tempname() .. '.go'
-- vim.fn.writefile(lines, name)
local cmd = " silent exe 'e " .. fname .. "'"

name = vim.fn.tempname() .. '.go'
vim.fn.writefile(lines, name)
cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd)
bufn = vim.fn.bufnr('')
vim.cmd('GoRun')
vim.cmd('GoBuild')

vim.cmd('GoTest')
vim.cmd('GoTest -c')
vim.cmd('GoFmt')
end)
end)
42 changes: 21 additions & 21 deletions lua/tests/go_fillstruct_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,46 @@ local _ = require('plenary/busted')

local eq = assert.are.same
local cur_dir = vim.fn.expand('%:p:h')
-- local status = require("plenary.reload").reload_module("go.nvim")
-- status = require("plenary.reload").reload_module("nvim-treesitter")
-- local ulog = require('go.utils').log
describe('should run fillstruct', function()
vim.cmd([[packadd go.nvim]])

-- _GO_NVIM_CFG.fillstruct = "fillstruct"
it('should run fillstruct', function()
--

local name = vim.fn.tempname() .. '.go'
local path = cur_dir .. '/lua/tests/fixtures/fill/fill_struct_input.go' -- %:p:h ? %:p
local lines = vim.fn.readfile(path)
vim.fn.writefile(lines, name)
vim.o.ft = 'go'
local expected = vim.fn.join(
vim.fn.readfile(cur_dir .. '/lua/tests/fixtures/fill/fill_struct_golden.txt'),
'\n'
)
local cmd = " silent exe 'e " .. path .. "'"

local path = cur_dir .. '/lua/tests/fixtures/' -- %:p:h ? %:p
vim.cmd('cd ' .. path)
local fname = 'fill/fill_struct_input.go'

local cmd = " silent exe 'e " .. fname .. "'"
vim.cmd(cmd)
-- _GO_NVIM_CFG.log_path = '' -- log to console in github actions
vim.bo.filetype = 'go'
require('plenary.reload').reload_module('go.nvim')
require('go').setup({ verbose = true, lsp_cfg = true })

vim.cmd('sleep 1000m') -- allow gopls startup
vim.cmd('sleep 1500m') -- allow gopls startup
vim.fn.setpos('.', { 0, 20, 14, 0 })

vim.bo.filetype = 'go'

require('go.reftool').fillstruct()

vim.cmd('sleep 500m') -- allow cleanup
vim.wait(100, function()
local filled = vim.api.nvim_buf_get_lines(0, 0, 40, false)

-- local path = cur_dir .. "/lua/tests/fixtures/fill/fill_struct_input.go2" -- %:p:h ? %:p
-- vim.fn.writefile(filled, path)
local filled
for _ = 1, 6 do
require('go.utils').log('waiting for fill')
vim.wait(1000, function() return false end)

filled = vim.api.nvim_buf_get_lines(0, 0, 40, false)
filled = vim.fn.join(filled, '\n')
eq(expected, filled)
end)
require('go.utils').log(vim.inspect(filled))
if expected == filled then
break
end
end

eq(expected, filled)
end)
end)
53 changes: 37 additions & 16 deletions lua/tests/go_gopls_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@ local eq = assert.are.same
local cur_dir = vim.fn.expand('%:p:h')
local busted = require('plenary/busted')

local godir = cur_dir .. '/lua/tests/fixtures'
describe('should run gopls releated functions', function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
require('plenary.reload').reload_module('go.nvim')
local cmd = " silent exe 'e temp.go'"
vim.cmd(cmd)
require('go').setup({ goimport = 'gopls', lsp_cfg = true })

vim.cmd([[packadd go.nvim]])
it('should run import from file with gopls', function()
local path = cur_dir .. '/lua/tests/fixtures/fmt/goimports2.go' -- %:p:h ? %:p
require('plenary.reload').reload_module('go.nvim')
local cmd = " silent exe 'e temp.go'"
vim.cmd(cmd)
require('go').setup({ goimport = 'gopls', lsp_cfg = true })
local path = './fmt/goimports2.go' -- %:p:h ? %:p
local expected =
vim.fn.join(vim.fn.readfile(cur_dir .. '/lua/tests/fixtures/fmt/goimports2_golden.go'), '\n')

vim.cmd('%bdelete!')
local cmd = " silent exe 'e " .. path .. "'"
vim.cmd('cd ' .. godir)
cmd = " silent exe 'e " .. path .. "'"
vim.cmd(cmd)

vim.cmd([[cd %:p:h]])
_GO_NVIM_CFG.goimport = 'gopls'
vim.wait(2000, function()
return false
end)
local c = vim.lsp.get_active_clients()
eq(#c > 0, true)
require('go.format').goimport()
vim.wait(100, function()
return false
Expand All @@ -39,23 +44,39 @@ describe('should run gopls releated functions', function()
vim.cmd(cmd)
end)
it('should run import from file with gopls', function()
vim.cmd('%bdelete!')
local path = cur_dir .. '/lua/tests/fixtures/fmt/goimports3.go' -- %:p:h ? %:p
require('plenary.reload').reload_module('go.nvim')

require('go').setup({ goimport = 'gopls', verbose = true, log_path = '', lsp_cfg = true })
local cmd = " silent exe 'e temp.go'"
vim.cmd(cmd)
_GO_NVIM_CFG.log_path = '' -- enable log to console
local expected =
vim.fn.join(vim.fn.readfile(cur_dir .. '/lua/tests/fixtures/fmt/goimports3_golden.go'), '\n')
local cmd = " silent exe 'e " .. path .. "'"

vim.cmd('cd ' .. godir)
local path = './fmt/goimports3.go' -- %:p:h ? %:p
cmd = " silent exe 'e " .. path .. "'"
vim.cmd(cmd)

vim.cmd([[cd %:p:h]])
vim.wait(500, function()
vim.wait(2000, function()
return false
end)

_GO_NVIM_CFG.log_path = '' -- enable log to console
require('go.format').goimport()
vim.wait(200, function() end)
vim.cmd([[wa]])

print('workspaces:', vim.inspect(vim.lsp.buf.list_workspace_folders()))
local fmt = vim.fn.join(vim.fn.readfile(path), '\n')
print(vim.inspect(fmt))
local fmt
for _ = 1, 6 do
require('go.utils').log('waiting for import')
vim.wait(1000, function() return false end)
vim.cmd([[wa]])
fmt = vim.fn.join(vim.fn.readfile(path), '\n')
require('go.utils').log(vim.inspect(fmt))
if expected == fmt then
break
end
end
eq(expected, fmt)
-- eq(1, 1) -- still not working
cmd = 'bd! ' .. path
Expand Down
Loading