Home > database >  Run and Debug unit tests with flags
Run and Debug unit tests with flags

Time:09-13

I want to be able to run and debug unit tests within VS Code using the test explorer or code lens.
But in order to run my tests, I need to add this flag:

-ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn" 

Therefore, in my vscode settings.json file, I have added this json:

"go.testFlags": [        
    "-ldflags",
    "\"-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn\""
]

Now when I click the Run Test button in test explorer or in the code lens, VS Code generates this command:

/opt/homebrew/bin/go test -timeout 30s -run ^TestCreateNamespace$ github.com/SomePath/SomeRepo/internal/models/v2 -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"

but the test case fails with this error:

panic: proto: extension number 1042 is already registered on message google.protobuf.FileOptions
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict

And this is the exact error that I am expecting if I dont suply the -ldflags in my go test command. But the surprising thing is that when I copy the exact same vs code generated test command mentioned above and run that in my terminal then the test case passes.
Along with running the tests from Vs Code, I would also like to be able to debug them by setting breakpoints and stepping through code.

Dev Environment: I am on an arm64 apple M1 Mac if that matters.

UPDATE: After fiddling around with the go.testFlags values, I have found that:

  1. This configuration works for vs code run test functionality:
"go.testFlags": [        
    "-ldflags",
    "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"
]
  1. This configuration works for vs code debug test functionality:
"go.testFlags": [        
    "-ldflags",
    "'-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'"
]

(Notice the extra single quotes in debug configuration).

Now I need to find a single configuration that works for both run test as well as debug test functionalities, Or somehow specify 2 different configs for run test and debug test in settings.json file of vs code so that I can use both functionalities without making changes to the settings.json file every-time. (This might be a delve thing I suspect)

CodePudding user response:

Have you tried this?

"go.testFlags": [        
    "-ldflags",
    "-X",
    "google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"
]

When you create new tasks in VSCode you need to write each space-separated word/character as different parameters.

CodePudding user response:

I recommend using Launch Configuration instead of making changes in settings.json.

So you need to create a file at .vscode/launch.json as instructed here and add the following line to the file:

"buildFlags": "-ldflags='-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'",

It seems to work for both running and debugging the project.

  • Related