...

Source file src/google.golang.org/api/header_test.go

Documentation: google.golang.org/api

     1  // Copyright 2019 Google LLC.
     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 api
     6  
     7  import (
     8  	"bytes"
     9  	"os"
    10  	"path/filepath"
    11  	"regexp"
    12  	"testing"
    13  )
    14  
    15  // Files in this package use a BSD-style license.
    16  var sentinel = regexp.MustCompile(`(//|#) Copyright \d\d\d\d (Google LLC|The Go Authors)(\.)*( All rights reserved\.)*
    17  (//|#) Use of this source code is governed by a BSD-style
    18  (//|#) license that can be found in the LICENSE file.
    19  `)
    20  
    21  const prefix = "// Copyright"
    22  
    23  // A few files have to be skipped.
    24  var skip = map[string]bool{
    25  	"tools.go": true, // This file requires another comment above the license.
    26  	"internal/third_party/uritemplates/uritemplates.go":      true, // This file is licensed to an individual.
    27  	"internal/third_party/uritemplates/uritemplates_test.go": true, // This file is licensed to an individual.
    28  }
    29  
    30  // This test validates that all go files in the repo start with an appropriate license.
    31  func TestLicense(t *testing.T) {
    32  	err := filepath.Walk(".", func(path string, fi os.FileInfo, err error) error {
    33  		if skip[path] {
    34  			return nil
    35  		}
    36  
    37  		if err != nil {
    38  			return err
    39  		}
    40  
    41  		if filepath.Ext(path) != ".go" && filepath.Ext(path) != ".sh" {
    42  			return nil
    43  		}
    44  
    45  		src, err := os.ReadFile(path)
    46  		if err != nil {
    47  			return nil
    48  		}
    49  
    50  		// Verify that the license is matched.
    51  		if !sentinel.Match(src) {
    52  			t.Errorf("%v: license header not present", path)
    53  			return nil
    54  		}
    55  
    56  		// Also check it is at the top of .go files (but not .sh files, because they must have a shebang first).
    57  		if filepath.Ext(path) == ".go" && !bytes.HasPrefix(src, []byte(prefix)) {
    58  			t.Errorf("%v: license header not at the top", path)
    59  		}
    60  		return nil
    61  	})
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  }
    66  

View as plain text