Home > Software design >  vs code debuging go tests not passing flags
vs code debuging go tests not passing flags

Time:07-20

I`m trying to config a debugger on vs-code to some tests in go. I have to pass some flags to it, but it's not working well.

main.go

package main

import (
    "flag"
    "fmt"
)

func DoTheThing() {
    flag1Ptr := flag.Bool("flag1", false, "flag1 is a flag")
    flag.Parse()
    fmt.Println(*flag1Ptr)
    fmt.Println("Hello, world")
}

func main() {
    DoTheThing()
}

main_test.go

package main

import "testing"

func TestDoTheThing(t *testing.T) {
    DoTheThing()
}

launch.json

{
    "name": "Launch app",
    "type": "go",
    "request": "launch",
    "mode": "auto",
    "program": "${workspaceFolder}",
    "args": [
        "-flag1"
    ]
},
{
    "name": "Run Tests",
    "type": "go",
    "request": "launch",
    "mode": "test",
    "program": "${workspaceFolder}",
    "args": [
        "--", "-flag1"
    ]
}

if I run it with Launch app config it pass the value in the right way, but using the test one it do not set the parameter

output using Launch app config

true
Hello, world

output using Run Tests config

false
Hello, world

CodePudding user response:

package main

import (
    "flag"
    "fmt"
)

var flag1Ptr *bool

func init(){
    flag1Ptr = flag.Bool("flag1", false, "flag1 is a flag")
}

func DoTheThing() {
    flag.Parse()
    fmt.Println(*flag1Ptr)
    fmt.Println("Hello, world")
}

func main() {
    DoTheThing()
}

launch.json

{
    "name": "Launch app",
    "type": "go",
    "request": "launch",
    "mode": "auto",
    "program": "${workspaceFolder}",
    "args": [
        "-flag1"
    ]
},
{
    "name": "Run Tests",
    "type": "go",
    "request": "launch",
    "mode": "test",
    "program": "${workspaceFolder}",
    "args": ["-flag1"]
}
  • Related