Source file src/internal/copyright/copyright_test.go

     1  // Copyright 2024 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 copyright
     6  
     7  import (
     8  	"bytes"
     9  	"internal/testenv"
    10  	"io"
    11  	"io/fs"
    12  	"os"
    13  	"path/filepath"
    14  	"testing"
    15  )
    16  
    17  var copyright = []byte("Copyright")
    18  
    19  var permitted = [][]byte{
    20  	[]byte("// Code generated by "),
    21  	[]byte("// Code generated from "),
    22  	[]byte("// Created by cgo -cdefs"),
    23  	[]byte("// DO NOT EDIT\n// generated by:"),
    24  	[]byte("// Empty assembly file"),
    25  	[]byte("// Generated using cgo"),
    26  	[]byte("// Original source:\n//\thttp://www.zorinaq.com/papers/md5-amd64.html"), // public domain crypto/md5
    27  	[]byte("// created by cgo -cdefs"),
    28  	[]byte("// go run mkasm.go"),
    29  	[]byte("// mkerrors"),
    30  	[]byte("// mksys"),
    31  	[]byte("// run\n// Code generated by"), // cmd/compile/internal/test/constFold_test.go
    32  }
    33  
    34  func TestCopyright(t *testing.T) {
    35  	buf := make([]byte, 2048)
    36  	filepath.WalkDir(filepath.Join(testenv.GOROOT(t), "src"), func(path string, d fs.DirEntry, err error) error {
    37  		if d.IsDir() && (d.Name() == "testdata" || d.Name() == "vendor") {
    38  			return filepath.SkipDir
    39  		}
    40  		switch filepath.Ext(d.Name()) {
    41  		default:
    42  			return nil
    43  		case ".s", ".go":
    44  			// check
    45  		}
    46  
    47  		f, err := os.Open(path)
    48  		if err != nil {
    49  			t.Error(err)
    50  			return nil
    51  		}
    52  		defer f.Close()
    53  		n, err := f.Read(buf)
    54  		if err != nil && err != io.EOF {
    55  			t.Error(err)
    56  			return nil
    57  		}
    58  		b := buf[:n]
    59  		if bytes.Contains(b, copyright) {
    60  			return nil
    61  		}
    62  		for _, ok := range permitted {
    63  			if bytes.HasPrefix(b, ok) {
    64  				return nil
    65  			}
    66  		}
    67  		t.Errorf("%s: missing copyright notice", path)
    68  		return nil
    69  	})
    70  }
    71  

View as plain text