Skip to content

testing: 'go test' broken in go1.13 #34443

@gbbr

Description

@gbbr

With go1.13 the go test command broke due to the fix for #21051.

Consider the following program made of two files:

  • file.go:
package main

import (
	"flag"
	"fmt"
)

var x string

func init() {
	flag.StringVar(&x, "x", "y", "sets a value for x")
	flag.Parse()
}

func getNumber() int { return 2 }

func main() {
	fmt.Println("Hello world")
}
  • file_test.go:
package main

import "testing"

func TestGetNumber(t *testing.T) {
	if getNumber() != 2 {
		t.Fatal("did not get 2")
	}
}

Building the test binary fails to attach the test flags, resulting in an error of the type:

flag provided but not defined: -test.v
Usage of /var/folders/02/23s7r95n6t1739kdh0zc3trr0000gn/T/go-build752851519/b001/go1.13-flagbug.test:
  -x string
    	sets a value for x (default "y")
FAIL	_/Users/gabriel.aszalos/g/go1.13-flagbug	0.005s
FAIL

Consider the output of the below command with go1.13:

$ go test -o ./file.test -c && ./file.test --help
Usage of ./file.test:
  -x string
    	sets a value for x (default "y")

versus go1.12, which display the entire list of flags.

The issue is present because at the time the main package parses the flags, test flags are not yet attached.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions