~tomleb/repo-url.nvim

6cdff2429992ada16764e2b9ac657df5f07ea25b — Tom Lebreux 7 months ago 3f18888
Add tests
1 files changed, 47 insertions(+), 21 deletions(-)

M lua/repo-url/init.lua
M lua/repo-url/init.lua => lua/repo-url/init.lua +47 -21
@@ 169,17 169,6 @@ local function go_modcache_info(filename)
  }
end

--- @param uconfig? RepoUrl.Config
M.setup = function(uconfig)
  vim.validate { config = { uconfig, 'table', true } }
  config = vim.tbl_deep_extend('force', config, uconfig)

  vim.validate {
    preferred_remotes = { config.preferred_remotes, 'table', true },
    urls_by_hostname = { config.urls_by_hostname, 'table', true },
  }
end

--- @param command string[]
--- @return string
--- @return boolean -- Whether the call succeeded


@@ 468,49 457,49 @@ M.open_raw_url = function()
  open_url(url)
end

local get_gomod_repo_and_version = function()
  local line = vim.fn.getline '.'
local get_gomod_repo_and_version = function(mline)
  local line = mline or vim.fn.getline '.'
  local match, repo, version

  -- Matches
  -- 	github.com/kardianos/service => github.com/netbirdio/service v0.0.0-20230215170314-b923b89432b0
  -- 	k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230215170314-b923b89432b0
  match, _, repo, version = string.find(line, '^	[^ ]+ => ([^ ]+) ([^ ]+)')
  if match then
    return repo, version
  end

  -- Matches
  -- 	github.com/kardianos/service v1.1.1 => github.com/netbirdio/service v0.0.0-20230215170314-b923b89432b0
  -- 	k8s.io/apimachinery v1.1.1 => k8s.io/apimachinery v0.0.0-20230215170314-b923b89432b0
  match, _, repo, version = string.find(line, '^	[^ ]+ [^ ]+ => ([^ ]+) ([^ ]+)')
  if match then
    return repo, version
  end

  -- Matches
  -- replace github.com/kardianos/service => github.com/netbirdio/service v0.0.0-20230215170314-b923b89432b0
  -- replace k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230215170314-b923b89432b0
  match, _, repo, version = string.find(line, '^replace [^ ]+ => ([^ ]+) ([^ ]+)')
  if match then
    return repo, version
  end

  -- Matches
  -- replace github.com/kardianos/service v1.1.1 => github.com/netbirdio/service v0.0.0-20230215170314-b923b89432b0
  -- replace k8s.io/apimachinery v1.1.1 => k8s.io/apimachinery v0.0.0-20230215170314-b923b89432b0
  match, _, repo, version = string.find(line, '^replace [^ ]+ [^ ]+ => ([^ ]+) ([^ ]+)')
  if match then
    return repo, version
  end

  --  Matches
  --  	github.com/pion/transport/v3 v3.0.1
  -- 	github.com/davecgh/go-spew v1.1.1 // indirect
  --  	k8s.io/apimachinery v3.0.1
  -- 	k8s.io/apimachinery v1.1.1 // indirect
  match, _, repo, version = string.find(line, '^	([^ ]+) ([^ ]+)')
  if match then
    return repo, version
  end

  --  Matches
  --  require github.com/pion/transport/v3 v3.0.1
  --  require github.com/davecgh/go-spew v1.1.1 // indirect
  --  require k8s.io/apimachinery v3.0.1
  --  require k8s.io/apimachinery v1.1.1 // indirect
  match, _, repo, version = string.find(line, '^require ([^ ]+) ([^ ]+)')
  if match then
    return repo, version


@@ 615,4 604,41 @@ M.open_gosum_blob_url = function()
  open_url(url)
end

local get_gomod_repo_and_version_test = function()
  local repo, version

  repo, version = get_gomod_repo_and_version '	k8s.io/api v0.27.4'
  assert(repo == 'k8s.io/api')
  assert(version == 'v0.27.4')

  repo, version = get_gomod_repo_and_version 'require k8s.io/api v0.27.4'
  assert(repo == 'k8s.io/api')
  assert(version == 'v0.27.4')

  repo, version = get_gomod_repo_and_version '	k8s.io/api => k8s.io/api v0.27.4'
  assert(repo == 'k8s.io/api')
  assert(version == 'v0.27.4')

  repo, version = get_gomod_repo_and_version '	k8s.io/api v0.26.3 => k8s.io/api v0.27.4'
  assert(repo == 'k8s.io/api')
  assert(version == 'v0.27.4')
end

--- @param uconfig? RepoUrl.Config
M.setup = function(uconfig)
  local tempdir = vim.fn.tempname()
  fallback_gocache_dir = string.format('%s/%s', tempdir, 'go/mod-cache')
  vim.fn.mkdir(fallback_gocache_dir, 'p')

  -- XXX: Need to learn how to run unit test with neotest instead of here..
  get_gomod_repo_and_version_test()
  vim.validate { config = { uconfig, 'table', true } }
  config = vim.tbl_deep_extend('force', config, uconfig)

  vim.validate {
    preferred_remotes = { config.preferred_remotes, 'table', true },
    urls_by_hostname = { config.urls_by_hostname, 'table', true },
  }
end

return M