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

feat: docker support via custom command prefix #259

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

fredrikaverpil
Copy link
Owner

@fredrikaverpil fredrikaverpil commented Jan 12, 2025

Why this change?

The only doubt I have here is the added maintenance burden and that devcontainers is a thing: https://cadu.dev/running-neovim-on-devcontainers/

What was changed?

  • Add new options cmd_prefix, which adds ability to prefix go list, go test and gotestsum commands with e.g. docker exec <container>, enabling executing these commands in a container.
  • Add new option gotestsum_jsonfile so to specify a location which exists both in the container and on the local host running Neovim, in which the output JSON file is stored (by gotestsum) and read (by notest-golang/Neovim).

Notes

Example usage:

# enter root of project
cd neotest-golang

# start container and leave it idle
docker run -d --name golang-test -v $(pwd):$(pwd) -w $(pwd)/tests/go golang:1.23-bullseye tail -f /dev/null

# optionally, install gotestsum
docker exec golang-test go install gotest.tools/gotestsum@latest

# make sure tests can run (and also download any dependencies)
docker exec golang-test go test ./...

Either modify the neotest-golang settings or add a .lazy.lua in the project root (so to enable this behavior on a per-project basis):

return {
  {
    "nvim-neotest/neotest",
    lazy = true,
    ft = { "go" },
    dependencies = {
      {
        "fredrikaverpil/neotest-golang",
        dependencies = {
          "uga-rosa/utf8.nvim",
        },
        branch = "docker",
        -- for local development
        -- dir = "~/code/public/neotest-golang",
      },
    },

    opts = {
      -- Neotest log
      -- log_level = vim.log.levels.DEBUG,
      adapters = {
        require("neotest-golang")({
          -- Neotest-golang log
          -- log_level = vim.log.levels.TRACE,

          cmd_prefix = {
            "docker",
            "exec",
            "golang-test",
          },
          go_test_args = {
            "-v",
            "-count=1",
            "-race",
            "-coverprofile=" .. vim.fn.getcwd() .. "/coverage.out",
            -- "-p=1",
            "-parallel=1",
          },

          runner = "go",
          -- optionally, use gotestsum:
          -- runner = "gotestsum",
          -- gotestsum_args = { "--format=standard-verbose" },
          -- gotestsum_jsonfile = function()
          --   return vim.fs.joinpath(vim.fn.getcwd(), "tests/go/output.json")
          -- end,

          -- NOTE: there is a test in the test suite which requires sanitization.
          sanitize_output = true,
        }),
      },
    },
  },
}

@fredrikaverpil fredrikaverpil self-assigned this Jan 12, 2025
@fredrikaverpil fredrikaverpil force-pushed the docker branch 2 times, most recently from 4aa2822 to 7739261 Compare January 12, 2025 19:12
This adds support for e.g. prefixing the `go test` command with a docker
command, so to run the tests inside a container.
Add ability to tell gotestsum where to store its output JSON file.
Copy link
Contributor

github-actions bot commented Feb 3, 2025

This PR has been open for 20 days with no activity. Remove the stale label or add a comment or it will be closed in 10 days.

@github-actions github-actions bot added the Stale label Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant