Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/go/internal/test/genflags.go

Documentation: cmd/go/internal/test

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build ignore
     6  // +build ignore
     7  
     8  package main
     9  
    10  import (
    11  	"bytes"
    12  	"flag"
    13  	exec "internal/execabs"
    14  	"log"
    15  	"os"
    16  	"strings"
    17  	"testing"
    18  	"text/template"
    19  )
    20  
    21  func main() {
    22  	if err := regenerate(); err != nil {
    23  		log.Fatal(err)
    24  	}
    25  }
    26  
    27  func regenerate() error {
    28  	t := template.Must(template.New("fileTemplate").Parse(fileTemplate))
    29  	buf := bytes.NewBuffer(nil)
    30  	if err := t.Execute(buf, testFlags()); err != nil {
    31  		return err
    32  	}
    33  
    34  	f, err := os.Create("flagdefs.go")
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	cmd := exec.Command("gofmt")
    40  	cmd.Stdin = buf
    41  	cmd.Stdout = f
    42  	cmd.Stderr = os.Stderr
    43  	cmdErr := cmd.Run()
    44  
    45  	if err := f.Close(); err != nil {
    46  		return err
    47  	}
    48  	if cmdErr != nil {
    49  		os.Remove(f.Name())
    50  		return cmdErr
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func testFlags() []string {
    57  	testing.Init()
    58  
    59  	var names []string
    60  	flag.VisitAll(func(f *flag.Flag) {
    61  		if !strings.HasPrefix(f.Name, "test.") {
    62  			return
    63  		}
    64  		name := strings.TrimPrefix(f.Name, "test.")
    65  
    66  		switch name {
    67  		case "testlogfile", "paniconexit0":
    68  			// These flags are only for use by cmd/go.
    69  		default:
    70  			names = append(names, name)
    71  		}
    72  	})
    73  
    74  	return names
    75  }
    76  
    77  const fileTemplate = `// Copyright 2019 The Go Authors. All rights reserved.
    78  // Use of this source code is governed by a BSD-style
    79  // license that can be found in the LICENSE file.
    80  
    81  // Code generated by genflags.go — DO NOT EDIT.
    82  
    83  package test
    84  
    85  // passFlagToTest contains the flags that should be forwarded to
    86  // the test binary with the prefix "test.".
    87  var passFlagToTest = map[string]bool {
    88  {{- range .}}
    89  	"{{.}}": true,
    90  {{- end }}
    91  }
    92  `
    93  

View as plain text