Source file src/internal/godebugs/godebugs_test.go

     1  // Copyright 2023 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  package godebugs_test
     6  
     7  import (
     8  	"internal/godebugs"
     9  	"internal/testenv"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"regexp"
    14  	"runtime"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  func TestAll(t *testing.T) {
    20  	testenv.MustHaveGoBuild(t)
    21  
    22  	data, err := os.ReadFile("../../../doc/godebug.md")
    23  	if err != nil {
    24  		if os.IsNotExist(err) && (testenv.Builder() == "" || runtime.GOOS != "linux") {
    25  			t.Skip(err)
    26  		}
    27  		t.Fatal(err)
    28  	}
    29  	doc := string(data)
    30  
    31  	incs := incNonDefaults(t)
    32  
    33  	last := ""
    34  	for _, info := range godebugs.All {
    35  		if info.Name <= last {
    36  			t.Errorf("All not sorted: %s then %s", last, info.Name)
    37  		}
    38  		last = info.Name
    39  
    40  		if info.Package == "" {
    41  			t.Errorf("Name=%s missing Package", info.Name)
    42  		}
    43  		if info.Changed != 0 && info.Old == "" {
    44  			t.Errorf("Name=%s has Changed, missing Old", info.Name)
    45  		}
    46  		if info.Old != "" && info.Changed == 0 {
    47  			t.Errorf("Name=%s has Old, missing Changed", info.Name)
    48  		}
    49  		if !strings.Contains(doc, "`"+info.Name+"`") &&
    50  			!strings.Contains(doc, "`"+info.Name+"=") {
    51  			t.Errorf("Name=%s not documented in doc/godebug.md", info.Name)
    52  		}
    53  		if !info.Opaque && !incs[info.Name] {
    54  			t.Errorf("Name=%s missing IncNonDefault calls; see 'go doc internal/godebug'", info.Name)
    55  		}
    56  	}
    57  }
    58  
    59  var incNonDefaultRE = regexp.MustCompile(`([\pL\p{Nd}_]+)\.IncNonDefault\(\)`)
    60  
    61  func incNonDefaults(t *testing.T) map[string]bool {
    62  	// Build list of all files importing internal/godebug.
    63  	// Tried a more sophisticated search in go list looking for
    64  	// imports containing "internal/godebug", but that turned
    65  	// up a bug in go list instead. #66218
    66  	out, err := exec.Command("go", "list", "-f={{.Dir}}", "std", "cmd").CombinedOutput()
    67  	if err != nil {
    68  		t.Fatalf("go list: %v\n%s", err, out)
    69  	}
    70  
    71  	seen := map[string]bool{}
    72  	for _, dir := range strings.Split(string(out), "\n") {
    73  		if dir == "" {
    74  			continue
    75  		}
    76  		files, err := os.ReadDir(dir)
    77  		if err != nil {
    78  			t.Fatal(err)
    79  		}
    80  		for _, file := range files {
    81  			name := file.Name()
    82  			if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
    83  				continue
    84  			}
    85  			data, err := os.ReadFile(filepath.Join(dir, name))
    86  			if err != nil {
    87  				t.Fatal(err)
    88  			}
    89  			for _, m := range incNonDefaultRE.FindAllSubmatch(data, -1) {
    90  				seen[string(m[1])] = true
    91  			}
    92  		}
    93  	}
    94  	return seen
    95  }
    96  

View as plain text