Source file src/simd/archsimd/_gen/cmd/specfill/main_test.go

     1  // Copyright 2026 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 main
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"os"
    11  	"path/filepath"
    12  	"strings"
    13  	"testing"
    14  
    15  	"simd/archsimd/_gen/gentools"
    16  	"simd/archsimd/_gen/specdoc"
    17  	"simd/archsimd/_gen/specgen"
    18  )
    19  
    20  // TestFill verifies that exported declarations in simd and archsimd match
    21  // internal/spec.
    22  func TestFill(t *testing.T) {
    23  	goroot := gentools.DefaultGOROOT()
    24  	specDir := specgen.MustFindSpecDir(goroot)
    25  	specFuncs, err := specgen.Load(specDir, nil)
    26  	if err != nil {
    27  		t.Fatalf("loading spec: %v", err)
    28  	}
    29  	specIdx := specgen.NewIndex(specFuncs)
    30  
    31  	dirs := []string{
    32  		filepath.Join(goroot, "src/simd"),
    33  		filepath.Join(goroot, "src/simd/archsimd"),
    34  	}
    35  
    36  	for _, dir := range dirs {
    37  		entries, err := os.ReadDir(dir)
    38  		if err != nil {
    39  			t.Fatalf("reading %s: %v", dir, err)
    40  		}
    41  		for _, e := range entries {
    42  			if e.IsDir() || !strings.HasSuffix(e.Name(), ".go") || strings.HasSuffix(e.Name(), "_test.go") {
    43  				continue
    44  			}
    45  			filePath := filepath.Join(dir, e.Name())
    46  			relPath, err := filepath.Rel(goroot, filePath)
    47  			if err != nil {
    48  				relPath = filePath
    49  			}
    50  			src, err := os.ReadFile(filePath)
    51  			if err != nil {
    52  				t.Fatalf("reading %s: %v", relPath, err)
    53  			}
    54  
    55  			isGen, err := isGenerated(src)
    56  			if err != nil {
    57  				t.Fatalf("%s: %v", relPath, err)
    58  			}
    59  
    60  			filled, err := specdoc.Fill(src, specIdx, specdoc.Options{
    61  				Filename: filepath.ToSlash(relPath),
    62  				// Since we're checking generator output files, we expect there
    63  				// to already be spec-owned doc comments. Don't report these as
    64  				// errors.
    65  				AllowDocRewrite: true,
    66  				// TODO: Docs aren't yet filled by spec in the generators, so
    67  				// leave them alone in the check. Once they are filled by the
    68  				// generators and in hand-written files, drop this flag.
    69  				NoFillDoc: !isGen,
    70  			})
    71  			var rep *specdoc.Report
    72  			if errors.As(err, &rep) {
    73  				t.Errorf("%s diverges from internal/spec:\n%s", relPath, rep)
    74  			} else if err != nil {
    75  				t.Errorf("checking %s: %v", relPath, err)
    76  			} else if !bytes.Equal(src, filled) {
    77  				diff := gentools.Diff(relPath, src, relPath, filled)
    78  				var fix string
    79  				switch {
    80  				case isGen:
    81  					// Just the regenerate.
    82  				case filepath.ToSlash(relPath) == "src/simd/simd_emulated.go":
    83  					// Currently the only file with spec-filled docs.
    84  					fix = "\tgo run ./cmd/specfill -w ../../simd_emulated.go\n"
    85  				default:
    86  					fix = "\tgo run ./cmd/specfill -no-fill-doc -w\n"
    87  				}
    88  				t.Errorf("%s diverges from internal/spec:\n%s\nTo fix:\n\tcd $GOROOT/src/simd/archsimd/_gen\n%s\tgo run . -w", relPath, diff, fix)
    89  			}
    90  		}
    91  	}
    92  }
    93  

View as plain text