Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

extension does not handle command-line args that need to be passed to tests #1534

Closed
dvelitchkov opened this issue Feb 23, 2018 · 5 comments
Closed

Comments

@dvelitchkov
Copy link

the go test supports passing command-line arguments to the test binary. To quote go test -help:

 In addition to the build flags, the flags handled by 'go test' itself are:
 -args
        Pass the remainder of the command line (everything after -args)
        to the test binary, uninterpreted and unchanged.
        Because this flag consumes the remainder of the command line,
        the package list (if present) must appear before this flag.

Sadly, the extension isn't aware of this rule, and adding any "-args" to the "go.testFlags" setting does not work, as the extension does not put such arguments last in the arguments list.

Steps to Reproduce:

  1. Write a test file:
import (
    "flag"
    "os"
    "testing"
)

var dbCnn string

func TestMain(m *testing.M) {
    flag.StringVar(&dbCnn, "dbcnn", "", "database connection")
    flag.Parse()
    os.Exit(m.Run())
}

func TestReports(t *testing.T) {
    if dbCnn == "" {
	t.Fatal("db connection not set")
    }
}
  1. Change Go settings:
"go.testFlags": [
        "-args",
        "-dbcnn",
        "postgres://foo:bar@baz/abc"
    ]
  1. Run test

Result:

Running tool: /usr/local/Cellar/go/1.9.3/libexec/bin/go test -args -dbcnn postgres://foo:bar@baz/abc -timeout 30s ***/server/db -run ^TestReports$

flag provided but not defined: -timeout

The error is because "-args" is not placed after all other args to go test.

I think the correct solution would be to define a new setting - perhaps "go.testArgs" - an array of arguments to the test binary.

I would love to work on this myself and submit a PR

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Feb 28, 2018

So instead of

go test -args -dbcnn postgres://foo:bar@baz/abc -timeout 30s ***/server/db -run ^TestReports$

we should actually be running

go test -timeout 30s ***/server/db -run ^TestReports$ -args -dbcnn postgres://foo:bar@baz/abc ?

If so, then we don't really need a new setting. We can look for -args in the testFlags and append it and everything that comes after it in the end isn't it?

PRs are always welcome. The place to make the change is https://github.com/Microsoft/vscode-go/blob/0.6.77/src/testUtils.ts#L127

@dvelitchkov
Copy link
Author

Sounds good, I'll try to get this in ASAP!

@rschmied
Copy link
Contributor

Hey... I do face the exact same issue when clicking on the "run test" link next to a function in the editor... I want to pass extra args for the test but those have to be appended to the values that are being passed as defaults...

my go.testflags look like this:

"go.testFlags": ["-args", "-logtostderr", "-stderrthreshold", "info", "-v", "4"],

e.g. this is what happens now:

Running tool: /usr/local/go/bin/go test -args -logtostderr -stderrthreshold info -v 4 -timeout 30s -run ^Test_getServerID$

and it should be like this:

Running tool: /usr/local/go/bin/go test -timeout 30s -run ^Test_getServerID$ -args -logtostderr -stderrthreshold info -v 4 

I think I could experiment with the code you pointed to above (testUtils.ts#L127) but I have absolutely no idea how I would even start to test those changes.

@ramya-rao-a
Copy link
Contributor

@rschmied See Building and Debugging the extension to get the local set up and to debug the extension.

Also, wait a few days till October starts to send your PR to win a t-shirt! For more details, see https://open.microsoft.com/2018/09/18/hacktoberfest-2018-microsoft/?WT.mc_id=hacktoberfest-twitter-beverst

@ramya-rao-a
Copy link
Contributor

The fix for this issue is now out in the latest update to the Go extension (0.6.92). Thanks @rschmied!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants